blob: e6032d23814cf93db4ba601481ce95074aa3bf5a [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
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000037using vixl::ExactAssemblyScope;
38using vixl::CodeBufferCheckScope;
39using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010040
41#ifdef __
42#error "ARM64 Codegen VIXL macro-assembler macro already defined."
43#endif
44
Alexandre Rames5319def2014-10-23 10:03:10 +010045namespace art {
46
Roland Levillain22ccc3a2015-11-24 13:10:05 +000047template<class MirrorType>
48class GcRoot;
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace arm64 {
51
Alexandre Ramesbe919d92016-08-23 18:33:36 +010052using helpers::ARM64EncodableConstantOrRegister;
53using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080054using helpers::CPURegisterFrom;
55using helpers::DRegisterFrom;
56using helpers::FPRegisterFrom;
57using helpers::HeapOperand;
58using helpers::HeapOperandFrom;
59using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::LocationFrom;
67using helpers::OperandFromMemOperand;
68using helpers::OutputCPURegister;
69using helpers::OutputFPRegister;
70using helpers::OutputRegister;
71using helpers::RegisterFrom;
72using helpers::StackOperandFrom;
73using helpers::VIXLRegCodeFromART;
74using helpers::WRegisterFrom;
75using helpers::XRegisterFrom;
76
Alexandre Rames5319def2014-10-23 10:03:10 +010077static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080079// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
80// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010082
Alexandre Rames5319def2014-10-23 10:03:10 +010083inline Condition ARM64Condition(IfCondition cond) {
84 switch (cond) {
85 case kCondEQ: return eq;
86 case kCondNE: return ne;
87 case kCondLT: return lt;
88 case kCondLE: return le;
89 case kCondGT: return gt;
90 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070091 case kCondB: return lo;
92 case kCondBE: return ls;
93 case kCondA: return hi;
94 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010095 }
Roland Levillain7f63c522015-07-13 15:54:55 +000096 LOG(FATAL) << "Unreachable";
97 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010098}
99
Vladimir Markod6e069b2016-01-18 11:11:01 +0000100inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
101 // The ARM64 condition codes can express all the necessary branches, see the
102 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
103 // There is no dex instruction or HIR that would need the missing conditions
104 // "equal or unordered" or "not equal".
105 switch (cond) {
106 case kCondEQ: return eq;
107 case kCondNE: return ne /* unordered */;
108 case kCondLT: return gt_bias ? cc : lt /* unordered */;
109 case kCondLE: return gt_bias ? ls : le /* unordered */;
110 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
111 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
112 default:
113 LOG(FATAL) << "UNREACHABLE";
114 UNREACHABLE();
115 }
116}
117
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000118Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000119 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
120 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
121 // but we use the exact registers for clarity.
122 if (return_type == Primitive::kPrimFloat) {
123 return LocationFrom(s0);
124 } else if (return_type == Primitive::kPrimDouble) {
125 return LocationFrom(d0);
126 } else if (return_type == Primitive::kPrimLong) {
127 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100128 } else if (return_type == Primitive::kPrimVoid) {
129 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000130 } else {
131 return LocationFrom(w0);
132 }
133}
134
Alexandre Rames5319def2014-10-23 10:03:10 +0100135Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000136 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100137}
138
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100139// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
140#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700141#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100142
Zheng Xuda403092015-04-24 17:35:39 +0800143// Calculate memory accessing operand for save/restore live registers.
144static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800146 int64_t spill_offset,
147 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100148 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
149 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
150 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800151 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800153 codegen->GetNumberOfFloatingPointRegisters()));
154
Vladimir Marko804b03f2016-09-14 16:26:36 +0100155 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
156 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800157
158 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
159 UseScratchRegisterScope temps(masm);
160
161 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100162 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
163 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800164 int64_t reg_size = kXRegSizeInBytes;
165 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
166 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100167 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800168 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
169 // If the offset does not fit in the instruction's immediate field, use an alternate register
170 // to compute the base address(float point registers spill base address).
171 Register new_base = temps.AcquireSameSizeAs(base);
172 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
173 base = new_base;
174 spill_offset = -core_spill_size;
175 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
176 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
177 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
178 }
179
180 if (is_save) {
181 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
182 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 } else {
184 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
185 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
186 }
187}
188
189void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800190 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100191 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
192 for (uint32_t i : LowToHighBits(core_spills)) {
193 // If the register holds an object, update the stack mask.
194 if (locations->RegisterContainsObject(i)) {
195 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800196 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100197 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
198 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
199 saved_core_stack_offsets_[i] = stack_offset;
200 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800201 }
202
Vladimir Marko804b03f2016-09-14 16:26:36 +0100203 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
204 for (uint32_t i : LowToHighBits(fp_spills)) {
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_fpu_stack_offsets_[i] = stack_offset;
208 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800209 }
210
Vladimir Marko804b03f2016-09-14 16:26:36 +0100211 SaveRestoreLiveRegistersHelper(codegen,
212 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800213 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
214}
215
216void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100217 SaveRestoreLiveRegistersHelper(codegen,
218 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800219 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
220}
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000224 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 if (instruction_->CanThrowIntoCatchBlock()) {
232 // Live registers will be restored in the catch block if caught.
233 SaveLiveRegisters(codegen, instruction_->GetLocations());
234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000235 // We're moving two locations to locations that could overlap, so we need a parallel
236 // move resolver.
237 InvokeRuntimeCallingConvention calling_convention;
238 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100239 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
240 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000241 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
242 ? kQuickThrowStringBounds
243 : kQuickThrowArrayBounds;
244 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
255};
256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
258 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000259 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000264 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 }
267
Alexandre Rames8158f282015-08-07 10:26:17 +0100268 bool IsFatal() const OVERRIDE { return true; }
269
Alexandre Rames9931f312015-06-19 14:47:01 +0100270 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
271
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
274};
275
276class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
277 public:
278 LoadClassSlowPathARM64(HLoadClass* cls,
279 HInstruction* at,
280 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000281 bool do_clinit,
282 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
283 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
284 : SlowPathCodeARM64(at),
285 cls_(cls),
286 dex_pc_(dex_pc),
287 do_clinit_(do_clinit),
288 bss_entry_temp_(bss_entry_temp),
289 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
291 }
292
293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000294 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000295 Location out = locations->Out();
296 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
298
Vladimir Markoea4c1262017-02-06 19:59:33 +0000299 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the page address of
300 // the entry which is in a scratch register. Make sure it's not used for saving/restoring
301 // registers. Exclude the scratch register also for non-Baker read barrier for simplicity.
302 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
303 bool is_load_class_bss_entry =
304 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
305 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
306 if (is_load_class_bss_entry) {
307 // This temp is a scratch register.
308 DCHECK(bss_entry_temp_.IsValid());
309 temps.Exclude(bss_entry_temp_);
310 }
311
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314
315 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 dex::TypeIndex type_index = cls_->GetTypeIndex();
317 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000318 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
319 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000320 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800321 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100322 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800323 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100324 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
327 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 if (out.IsValid()) {
329 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000330 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000333 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000334 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000336 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000337 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000338 if (call_saves_everything_except_r0_ip0) {
339 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
340 } else {
341 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
342 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
343 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
344 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000345 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000346 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000347 {
348 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
349 __ Bind(strp_label);
350 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000351 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000352 }
353 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 __ B(GetExitLabel());
355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
358
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 private:
360 // The class this slow path will load.
361 HLoadClass* const cls_;
362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 // The dex PC of `at_`.
364 const uint32_t dex_pc_;
365
366 // Whether to initialize the class.
367 const bool do_clinit_;
368
Vladimir Markoea4c1262017-02-06 19:59:33 +0000369 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
370 vixl::aarch64::Register bss_entry_temp_;
371 vixl::aarch64::Label* bss_entry_adrp_label_;
372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
374};
375
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
377 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100378 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
379 : SlowPathCodeARM64(instruction),
380 temp_(temp),
381 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382
383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 LocationSummary* locations = instruction_->GetLocations();
385 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
386 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
387
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100388 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
389 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
390 temps.Exclude(temp_);
391
Vladimir Markoaad75c62016-10-03 08:46:48 +0000392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000396 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
397 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000398 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
399 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
400 Primitive::Type type = instruction_->GetType();
401 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
402
403 RestoreLiveRegisters(codegen, locations);
404
405 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100407 if (!kUseReadBarrier || kUseBakerReadBarrier) {
408 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
409 } else {
410 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
411 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
412 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
413 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000414 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100415 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000416 {
417 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
418 __ Bind(strp_label);
419 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100420 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000421 }
422
423 __ B(GetExitLabel());
424 }
425
426 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
427
428 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100429 const Register temp_;
430 vixl::aarch64::Label* adrp_label_;
431
Vladimir Markoaad75c62016-10-03 08:46:48 +0000432 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
433};
434
Alexandre Rames5319def2014-10-23 10:03:10 +0100435class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100438
Alexandre Rames67555f72014-11-18 10:55:16 +0000439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
440 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100441 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000442 if (instruction_->CanThrowIntoCatchBlock()) {
443 // Live registers will be restored in the catch block if caught.
444 SaveLiveRegisters(codegen, instruction_->GetLocations());
445 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000446 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
447 instruction_,
448 instruction_->GetDexPc(),
449 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800450 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100451 }
452
Alexandre Rames8158f282015-08-07 10:26:17 +0100453 bool IsFatal() const OVERRIDE { return true; }
454
Alexandre Rames9931f312015-06-19 14:47:01 +0100455 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
456
Alexandre Rames5319def2014-10-23 10:03:10 +0100457 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100458 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
459};
460
461class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
462 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100463 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100468 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000469 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800470 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000471 if (successor_ == nullptr) {
472 __ B(GetReturnLabel());
473 } else {
474 __ B(arm64_codegen->GetLabelOf(successor_));
475 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Scott Wakeling97c72b72016-06-24 16:19:36 +0100478 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100479 DCHECK(successor_ == nullptr);
480 return &return_label_;
481 }
482
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100483 HBasicBlock* GetSuccessor() const {
484 return successor_;
485 }
486
Alexandre Rames9931f312015-06-19 14:47:01 +0100487 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
488
Alexandre Rames5319def2014-10-23 10:03:10 +0100489 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100490 // If not null, the block to branch to after the suspend check.
491 HBasicBlock* const successor_;
492
493 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100494 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100495
496 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
497};
498
Alexandre Rames67555f72014-11-18 10:55:16 +0000499class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
500 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000502 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000505 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800506
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 DCHECK(instruction_->IsCheckCast()
508 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
509 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100510 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000514 if (!is_fatal_) {
515 SaveLiveRegisters(codegen, locations);
516 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000517
518 // We're moving two locations to locations that could overlap, so we need a parallel
519 // move resolver.
520 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800521 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800522 LocationFrom(calling_convention.GetRegisterAt(0)),
523 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800524 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800525 LocationFrom(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000527 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000528 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800529 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000530 Primitive::Type ret_type = instruction_->GetType();
531 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
532 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
533 } else {
534 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800535 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
536 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 }
538
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000539 if (!is_fatal_) {
540 RestoreLiveRegisters(codegen, locations);
541 __ B(GetExitLabel());
542 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000543 }
544
Alexandre Rames9931f312015-06-19 14:47:01 +0100545 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100546 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100547
Alexandre Rames67555f72014-11-18 10:55:16 +0000548 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000550
Alexandre Rames67555f72014-11-18 10:55:16 +0000551 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
552};
553
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700554class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
555 public:
Aart Bik42249c32016-01-07 15:33:50 -0800556 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000557 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800560 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000562 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000563 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700564 }
565
Alexandre Rames9931f312015-06-19 14:47:01 +0100566 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
567
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700568 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700569 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
570};
571
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
573 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000574 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575
576 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
577 LocationSummary* locations = instruction_->GetLocations();
578 __ Bind(GetEntryLabel());
579 SaveLiveRegisters(codegen, locations);
580
581 InvokeRuntimeCallingConvention calling_convention;
582 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
583 parallel_move.AddMove(
584 locations->InAt(0),
585 LocationFrom(calling_convention.GetRegisterAt(0)),
586 Primitive::kPrimNot,
587 nullptr);
588 parallel_move.AddMove(
589 locations->InAt(1),
590 LocationFrom(calling_convention.GetRegisterAt(1)),
591 Primitive::kPrimInt,
592 nullptr);
593 parallel_move.AddMove(
594 locations->InAt(2),
595 LocationFrom(calling_convention.GetRegisterAt(2)),
596 Primitive::kPrimNot,
597 nullptr);
598 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
599
600 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000601 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100602 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
603 RestoreLiveRegisters(codegen, locations);
604 __ B(GetExitLabel());
605 }
606
607 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
608
609 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100610 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
611};
612
Zheng Xu3927c8b2015-11-18 17:46:25 +0800613void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
614 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000615 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800616
617 // We are about to use the assembler to place literals directly. Make sure we have enough
618 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000619 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
620 num_entries * sizeof(int32_t),
621 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800622
623 __ Bind(&table_start_);
624 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
625 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100626 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800627 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100628 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800629 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
630 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
631 Literal<int32_t> literal(jump_offset);
632 __ place(&literal);
633 }
634}
635
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100636// Slow path marking an object reference `ref` during a read
637// barrier. The field `obj.field` in the object `obj` holding this
638// reference does not get updated by this slow path after marking (see
639// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
640//
641// This means that after the execution of this slow path, `ref` will
642// always be up-to-date, but `obj.field` may not; i.e., after the
643// flip, `ref` will be a to-space reference, but `obj.field` will
644// probably still be a from-space reference (unless it gets updated by
645// another thread, or if another thread installed another object
646// reference (different from `ref`) in `obj.field`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000647//
648// If `entrypoint` is a valid location it is assumed to already be
649// holding the entrypoint. The case where the entrypoint is passed in
650// is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000651class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
652 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800653 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
654 Location ref,
655 Location entrypoint = Location::NoLocation())
656 : SlowPathCodeARM64(instruction),
657 ref_(ref),
658 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000659 DCHECK(kEmitCompilerReadBarrier);
660 }
661
662 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
663
664 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
665 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000666 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100667 DCHECK(ref_.IsRegister()) << ref_;
668 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000669 DCHECK(instruction_->IsInstanceFieldGet() ||
670 instruction_->IsStaticFieldGet() ||
671 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100672 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000673 instruction_->IsLoadClass() ||
674 instruction_->IsLoadString() ||
675 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100676 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100677 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
678 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000679 << "Unexpected instruction in read barrier marking slow path: "
680 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000681 // The read barrier instrumentation of object ArrayGet
682 // instructions does not support the HIntermediateAddress
683 // instruction.
684 DCHECK(!(instruction_->IsArrayGet() &&
685 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000686
687 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100688 // No need to save live registers; it's taken care of by the
689 // entrypoint. Also, there is no need to update the stack mask,
690 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000691 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100692 DCHECK_NE(ref_.reg(), LR);
693 DCHECK_NE(ref_.reg(), WSP);
694 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100695 // IP0 is used internally by the ReadBarrierMarkRegX entry point
696 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100697 DCHECK_NE(ref_.reg(), IP0);
698 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100699 // "Compact" slow path, saving two moves.
700 //
701 // Instead of using the standard runtime calling convention (input
702 // and output in W0):
703 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100704 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100705 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100706 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100707 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100708 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100709 // of a dedicated entrypoint:
710 //
711 // rX <- ReadBarrierMarkRegX(rX)
712 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800713 if (entrypoint_.IsValid()) {
714 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
715 __ Blr(XRegisterFrom(entrypoint_));
716 } else {
717 // Entrypoint is not already loaded, load from the thread.
718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
720 // This runtime call does not require a stack map.
721 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
722 }
Roland Levillain44015862016-01-22 11:47:17 +0000723 __ B(GetExitLabel());
724 }
725
726 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100727 // The location (register) of the marked object reference.
728 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000729
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800730 // The location of the entrypoint if it is already loaded.
731 const Location entrypoint_;
732
Roland Levillain44015862016-01-22 11:47:17 +0000733 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
734};
735
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100736// Slow path marking an object reference `ref` during a read barrier,
737// and if needed, atomically updating the field `obj.field` in the
738// object `obj` holding this reference after marking (contrary to
739// ReadBarrierMarkSlowPathARM64 above, which never tries to update
740// `obj.field`).
741//
742// This means that after the execution of this slow path, both `ref`
743// and `obj.field` will be up-to-date; i.e., after the flip, both will
744// hold the same to-space reference (unless another thread installed
745// another object reference (different from `ref`) in `obj.field`).
746class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
747 public:
748 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
749 Location ref,
750 Register obj,
751 Location field_offset,
752 Register temp)
753 : SlowPathCodeARM64(instruction),
754 ref_(ref),
755 obj_(obj),
756 field_offset_(field_offset),
757 temp_(temp) {
758 DCHECK(kEmitCompilerReadBarrier);
759 }
760
761 const char* GetDescription() const OVERRIDE {
762 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
763 }
764
765 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
766 LocationSummary* locations = instruction_->GetLocations();
767 Register ref_reg = WRegisterFrom(ref_);
768 DCHECK(locations->CanCall());
769 DCHECK(ref_.IsRegister()) << ref_;
770 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
771 // This slow path is only used by the UnsafeCASObject intrinsic.
772 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
773 << "Unexpected instruction in read barrier marking and field updating slow path: "
774 << instruction_->DebugName();
775 DCHECK(instruction_->GetLocations()->Intrinsified());
776 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
777 DCHECK(field_offset_.IsRegister()) << field_offset_;
778
779 __ Bind(GetEntryLabel());
780
781 // Save the old reference.
782 // Note that we cannot use IP to save the old reference, as IP is
783 // used internally by the ReadBarrierMarkRegX entry point, and we
784 // need the old reference after the call to that entry point.
785 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
786 __ Mov(temp_.W(), ref_reg);
787
788 // No need to save live registers; it's taken care of by the
789 // entrypoint. Also, there is no need to update the stack mask,
790 // as this runtime call will not trigger a garbage collection.
791 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
792 DCHECK_NE(ref_.reg(), LR);
793 DCHECK_NE(ref_.reg(), WSP);
794 DCHECK_NE(ref_.reg(), WZR);
795 // IP0 is used internally by the ReadBarrierMarkRegX entry point
796 // as a temporary, it cannot be the entry point's input/output.
797 DCHECK_NE(ref_.reg(), IP0);
798 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
799 // "Compact" slow path, saving two moves.
800 //
801 // Instead of using the standard runtime calling convention (input
802 // and output in W0):
803 //
804 // W0 <- ref
805 // W0 <- ReadBarrierMark(W0)
806 // ref <- W0
807 //
808 // we just use rX (the register containing `ref`) as input and output
809 // of a dedicated entrypoint:
810 //
811 // rX <- ReadBarrierMarkRegX(rX)
812 //
813 int32_t entry_point_offset =
814 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
815 // This runtime call does not require a stack map.
816 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
817
818 // If the new reference is different from the old reference,
819 // update the field in the holder (`*(obj_ + field_offset_)`).
820 //
821 // Note that this field could also hold a different object, if
822 // another thread had concurrently changed it. In that case, the
823 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
824 // (CAS) operation below would abort the CAS, leaving the field
825 // as-is.
826 vixl::aarch64::Label done;
827 __ Cmp(temp_.W(), ref_reg);
828 __ B(eq, &done);
829
830 // Update the the holder's field atomically. This may fail if
831 // mutator updates before us, but it's OK. This is achieved
832 // using a strong compare-and-set (CAS) operation with relaxed
833 // memory synchronization ordering, where the expected value is
834 // the old reference and the desired value is the new reference.
835
836 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
837 UseScratchRegisterScope temps(masm);
838
839 // Convenience aliases.
840 Register base = obj_.W();
841 Register offset = XRegisterFrom(field_offset_);
842 Register expected = temp_.W();
843 Register value = ref_reg;
844 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
845 Register tmp_value = temps.AcquireW(); // Value in memory.
846
847 __ Add(tmp_ptr, base.X(), Operand(offset));
848
849 if (kPoisonHeapReferences) {
850 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
851 if (value.Is(expected)) {
852 // Do not poison `value`, as it is the same register as
853 // `expected`, which has just been poisoned.
854 } else {
855 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
856 }
857 }
858
859 // do {
860 // tmp_value = [tmp_ptr] - expected;
861 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
862
Roland Levillain24a4d112016-10-26 13:10:46 +0100863 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100864 __ Bind(&loop_head);
865 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
866 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100867 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100868 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
869 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100870 __ B(&exit_loop);
871 __ Bind(&comparison_failed);
872 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100873 __ Bind(&exit_loop);
874
875 if (kPoisonHeapReferences) {
876 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
877 if (value.Is(expected)) {
878 // Do not unpoison `value`, as it is the same register as
879 // `expected`, which has just been unpoisoned.
880 } else {
881 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
882 }
883 }
884
885 __ Bind(&done);
886 __ B(GetExitLabel());
887 }
888
889 private:
890 // The location (register) of the marked object reference.
891 const Location ref_;
892 // The register containing the object holding the marked object reference field.
893 const Register obj_;
894 // The location of the offset of the marked reference field within `obj_`.
895 Location field_offset_;
896
897 const Register temp_;
898
899 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
900};
901
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000902// Slow path generating a read barrier for a heap reference.
903class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
904 public:
905 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
906 Location out,
907 Location ref,
908 Location obj,
909 uint32_t offset,
910 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000911 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000912 out_(out),
913 ref_(ref),
914 obj_(obj),
915 offset_(offset),
916 index_(index) {
917 DCHECK(kEmitCompilerReadBarrier);
918 // If `obj` is equal to `out` or `ref`, it means the initial object
919 // has been overwritten by (or after) the heap object reference load
920 // to be instrumented, e.g.:
921 //
922 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000923 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000924 //
925 // In that case, we have lost the information about the original
926 // object, and the emitted read barrier cannot work properly.
927 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
928 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
929 }
930
931 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
932 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
933 LocationSummary* locations = instruction_->GetLocations();
934 Primitive::Type type = Primitive::kPrimNot;
935 DCHECK(locations->CanCall());
936 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100937 DCHECK(instruction_->IsInstanceFieldGet() ||
938 instruction_->IsStaticFieldGet() ||
939 instruction_->IsArrayGet() ||
940 instruction_->IsInstanceOf() ||
941 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100942 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000943 << "Unexpected instruction in read barrier for heap reference slow path: "
944 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000945 // The read barrier instrumentation of object ArrayGet
946 // instructions does not support the HIntermediateAddress
947 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000948 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100949 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000950
951 __ Bind(GetEntryLabel());
952
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000953 SaveLiveRegisters(codegen, locations);
954
955 // We may have to change the index's value, but as `index_` is a
956 // constant member (like other "inputs" of this slow path),
957 // introduce a copy of it, `index`.
958 Location index = index_;
959 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100960 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000961 if (instruction_->IsArrayGet()) {
962 // Compute the actual memory offset and store it in `index`.
963 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
964 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
965 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
966 // We are about to change the value of `index_reg` (see the
967 // calls to vixl::MacroAssembler::Lsl and
968 // vixl::MacroAssembler::Mov below), but it has
969 // not been saved by the previous call to
970 // art::SlowPathCode::SaveLiveRegisters, as it is a
971 // callee-save register --
972 // art::SlowPathCode::SaveLiveRegisters does not consider
973 // callee-save registers, as it has been designed with the
974 // assumption that callee-save registers are supposed to be
975 // handled by the called function. So, as a callee-save
976 // register, `index_reg` _would_ eventually be saved onto
977 // the stack, but it would be too late: we would have
978 // changed its value earlier. Therefore, we manually save
979 // it here into another freely available register,
980 // `free_reg`, chosen of course among the caller-save
981 // registers (as a callee-save `free_reg` register would
982 // exhibit the same problem).
983 //
984 // Note we could have requested a temporary register from
985 // the register allocator instead; but we prefer not to, as
986 // this is a slow path, and we know we can find a
987 // caller-save register that is available.
988 Register free_reg = FindAvailableCallerSaveRegister(codegen);
989 __ Mov(free_reg.W(), index_reg);
990 index_reg = free_reg;
991 index = LocationFrom(index_reg);
992 } else {
993 // The initial register stored in `index_` has already been
994 // saved in the call to art::SlowPathCode::SaveLiveRegisters
995 // (as it is not a callee-save register), so we can freely
996 // use it.
997 }
998 // Shifting the index value contained in `index_reg` by the scale
999 // factor (2) cannot overflow in practice, as the runtime is
1000 // unable to allocate object arrays with a size larger than
1001 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1002 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1003 static_assert(
1004 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1005 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1006 __ Add(index_reg, index_reg, Operand(offset_));
1007 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001008 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1009 // intrinsics, `index_` is not shifted by a scale factor of 2
1010 // (as in the case of ArrayGet), as it is actually an offset
1011 // to an object field within an object.
1012 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001013 DCHECK(instruction_->GetLocations()->Intrinsified());
1014 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1015 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1016 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001018 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001019 }
1020 }
1021
1022 // We're moving two or three locations to locations that could
1023 // overlap, so we need a parallel move resolver.
1024 InvokeRuntimeCallingConvention calling_convention;
1025 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1026 parallel_move.AddMove(ref_,
1027 LocationFrom(calling_convention.GetRegisterAt(0)),
1028 type,
1029 nullptr);
1030 parallel_move.AddMove(obj_,
1031 LocationFrom(calling_convention.GetRegisterAt(1)),
1032 type,
1033 nullptr);
1034 if (index.IsValid()) {
1035 parallel_move.AddMove(index,
1036 LocationFrom(calling_convention.GetRegisterAt(2)),
1037 Primitive::kPrimInt,
1038 nullptr);
1039 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1040 } else {
1041 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1042 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1043 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001044 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001045 instruction_,
1046 instruction_->GetDexPc(),
1047 this);
1048 CheckEntrypointTypes<
1049 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1050 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1051
1052 RestoreLiveRegisters(codegen, locations);
1053
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001054 __ B(GetExitLabel());
1055 }
1056
1057 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1058
1059 private:
1060 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001061 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1062 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001063 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1064 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1065 return Register(VIXLRegCodeFromART(i), kXRegSize);
1066 }
1067 }
1068 // We shall never fail to find a free caller-save register, as
1069 // there are more than two core caller-save registers on ARM64
1070 // (meaning it is possible to find one which is different from
1071 // `ref` and `obj`).
1072 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1073 LOG(FATAL) << "Could not find a free register";
1074 UNREACHABLE();
1075 }
1076
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001077 const Location out_;
1078 const Location ref_;
1079 const Location obj_;
1080 const uint32_t offset_;
1081 // An additional location containing an index to an array.
1082 // Only used for HArrayGet and the UnsafeGetObject &
1083 // UnsafeGetObjectVolatile intrinsics.
1084 const Location index_;
1085
1086 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1087};
1088
1089// Slow path generating a read barrier for a GC root.
1090class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1091 public:
1092 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001093 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001094 DCHECK(kEmitCompilerReadBarrier);
1095 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001096
1097 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1098 LocationSummary* locations = instruction_->GetLocations();
1099 Primitive::Type type = Primitive::kPrimNot;
1100 DCHECK(locations->CanCall());
1101 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001102 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1103 << "Unexpected instruction in read barrier for GC root slow path: "
1104 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001105
1106 __ Bind(GetEntryLabel());
1107 SaveLiveRegisters(codegen, locations);
1108
1109 InvokeRuntimeCallingConvention calling_convention;
1110 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1111 // The argument of the ReadBarrierForRootSlow is not a managed
1112 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1113 // thus we need a 64-bit move here, and we cannot use
1114 //
1115 // arm64_codegen->MoveLocation(
1116 // LocationFrom(calling_convention.GetRegisterAt(0)),
1117 // root_,
1118 // type);
1119 //
1120 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1121 // reference type (`Primitive::kPrimNot`).
1122 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001123 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001124 instruction_,
1125 instruction_->GetDexPc(),
1126 this);
1127 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1128 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1129
1130 RestoreLiveRegisters(codegen, locations);
1131 __ B(GetExitLabel());
1132 }
1133
1134 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1135
1136 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001137 const Location out_;
1138 const Location root_;
1139
1140 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1141};
1142
Alexandre Rames5319def2014-10-23 10:03:10 +01001143#undef __
1144
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001145Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001146 Location next_location;
1147 if (type == Primitive::kPrimVoid) {
1148 LOG(FATAL) << "Unreachable type " << type;
1149 }
1150
Alexandre Rames542361f2015-01-29 16:57:31 +00001151 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001152 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1153 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001154 } else if (!Primitive::IsFloatingPointType(type) &&
1155 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001156 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1157 } else {
1158 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001159 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1160 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001161 }
1162
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001163 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001164 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001165 return next_location;
1166}
1167
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001168Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001169 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001170}
1171
Serban Constantinescu579885a2015-02-22 20:51:33 +00001172CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1173 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001174 const CompilerOptions& compiler_options,
1175 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001176 : CodeGenerator(graph,
1177 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001178 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001179 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001180 callee_saved_core_registers.GetList(),
1181 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001182 compiler_options,
1183 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001184 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001185 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001186 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001187 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001188 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001189 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001190 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001191 uint32_literals_(std::less<uint32_t>(),
1192 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001193 uint64_literals_(std::less<uint64_t>(),
1194 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001195 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1196 boot_image_string_patches_(StringReferenceValueComparator(),
1197 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1198 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001199 boot_image_type_patches_(TypeReferenceValueComparator(),
1200 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1201 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001202 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001203 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001204 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1205 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001206 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1207 jit_class_patches_(TypeReferenceValueComparator(),
1208 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001209 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001210 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001211}
Alexandre Rames5319def2014-10-23 10:03:10 +01001212
Alexandre Rames67555f72014-11-18 10:55:16 +00001213#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001214
Zheng Xu3927c8b2015-11-18 17:46:25 +08001215void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001216 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001217 jump_table->EmitTable(this);
1218 }
1219}
1220
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001221void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001222 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001223 // Ensure we emit the literal pool.
1224 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001225
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001226 CodeGenerator::Finalize(allocator);
1227}
1228
Zheng Xuad4450e2015-04-17 18:48:56 +08001229void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1230 // Note: There are 6 kinds of moves:
1231 // 1. constant -> GPR/FPR (non-cycle)
1232 // 2. constant -> stack (non-cycle)
1233 // 3. GPR/FPR -> GPR/FPR
1234 // 4. GPR/FPR -> stack
1235 // 5. stack -> GPR/FPR
1236 // 6. stack -> stack (non-cycle)
1237 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1238 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1239 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1240 // dependency.
1241 vixl_temps_.Open(GetVIXLAssembler());
1242}
1243
1244void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1245 vixl_temps_.Close();
1246}
1247
1248Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1249 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1250 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1251 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1252 Location scratch = GetScratchLocation(kind);
1253 if (!scratch.Equals(Location::NoLocation())) {
1254 return scratch;
1255 }
1256 // Allocate from VIXL temp registers.
1257 if (kind == Location::kRegister) {
1258 scratch = LocationFrom(vixl_temps_.AcquireX());
1259 } else {
1260 DCHECK(kind == Location::kFpuRegister);
1261 scratch = LocationFrom(vixl_temps_.AcquireD());
1262 }
1263 AddScratchLocation(scratch);
1264 return scratch;
1265}
1266
1267void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1268 if (loc.IsRegister()) {
1269 vixl_temps_.Release(XRegisterFrom(loc));
1270 } else {
1271 DCHECK(loc.IsFpuRegister());
1272 vixl_temps_.Release(DRegisterFrom(loc));
1273 }
1274 RemoveScratchLocation(loc);
1275}
1276
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001278 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280}
1281
Alexandre Rames5319def2014-10-23 10:03:10 +01001282void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001283 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001284 __ Bind(&frame_entry_label_);
1285
Serban Constantinescu02164b32014-11-13 14:05:07 +00001286 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1287 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001288 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001289 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001290 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001291 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001292 {
1293 // Ensure that between load and RecordPcInfo there are no pools emitted.
1294 ExactAssemblyScope eas(GetVIXLAssembler(),
1295 kInstructionSize,
1296 CodeBufferCheckScope::kExactSize);
1297 __ ldr(wzr, MemOperand(temp, 0));
1298 RecordPcInfo(nullptr, 0);
1299 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001300 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001301
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001302 if (!HasEmptyFrame()) {
1303 int frame_size = GetFrameSize();
1304 // Stack layout:
1305 // sp[frame_size - 8] : lr.
1306 // ... : other preserved core registers.
1307 // ... : other preserved fp registers.
1308 // ... : reserved frame space.
1309 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001310
1311 // Save the current method if we need it. Note that we do not
1312 // do this in HCurrentMethod, as the instruction might have been removed
1313 // in the SSA graph.
1314 if (RequiresCurrentMethod()) {
1315 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001316 } else {
1317 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001318 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001319 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001320 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1321 frame_size - GetCoreSpillSize());
1322 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1323 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001324
1325 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1326 // Initialize should_deoptimize flag to 0.
1327 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1328 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1329 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001330 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001331}
1332
1333void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001334 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001335 if (!HasEmptyFrame()) {
1336 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001337 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1338 frame_size - FrameEntrySpillSize());
1339 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1340 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001341 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001342 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001343 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001344 __ Ret();
1345 GetAssembler()->cfi().RestoreState();
1346 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001347}
1348
Scott Wakeling97c72b72016-06-24 16:19:36 +01001349CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001350 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 return CPURegList(CPURegister::kRegister, kXRegSize,
1352 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001353}
1354
Scott Wakeling97c72b72016-06-24 16:19:36 +01001355CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001356 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1357 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001358 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1359 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001360}
1361
Alexandre Rames5319def2014-10-23 10:03:10 +01001362void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1363 __ Bind(GetLabelOf(block));
1364}
1365
Calin Juravle175dc732015-08-25 15:42:32 +01001366void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1367 DCHECK(location.IsRegister());
1368 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1369}
1370
Calin Juravlee460d1d2015-09-29 04:52:17 +01001371void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1372 if (location.IsRegister()) {
1373 locations->AddTemp(location);
1374 } else {
1375 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1376 }
1377}
1378
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001379void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001380 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001382 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001383 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001384 if (value_can_be_null) {
1385 __ Cbz(value, &done);
1386 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001387 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001388 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001389 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001390 if (value_can_be_null) {
1391 __ Bind(&done);
1392 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001393}
1394
David Brazdil58282f42016-01-14 12:45:10 +00001395void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001396 // Blocked core registers:
1397 // lr : Runtime reserved.
1398 // tr : Runtime reserved.
1399 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1400 // ip1 : VIXL core temp.
1401 // ip0 : VIXL core temp.
1402 //
1403 // Blocked fp registers:
1404 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001405 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1406 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001407 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001408 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001409 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001410
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001411 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001412 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001413 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001414 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001415
David Brazdil58282f42016-01-14 12:45:10 +00001416 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001417 // Stubs do not save callee-save floating point registers. If the graph
1418 // is debuggable, we need to deal with these registers differently. For
1419 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001420 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1421 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001422 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001423 }
1424 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001425}
1426
Alexandre Rames3e69f162014-12-10 10:36:50 +00001427size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1428 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1429 __ Str(reg, MemOperand(sp, stack_index));
1430 return kArm64WordSize;
1431}
1432
1433size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1434 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1435 __ Ldr(reg, MemOperand(sp, stack_index));
1436 return kArm64WordSize;
1437}
1438
1439size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1440 FPRegister reg = FPRegister(reg_id, kDRegSize);
1441 __ Str(reg, MemOperand(sp, stack_index));
1442 return kArm64WordSize;
1443}
1444
1445size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1446 FPRegister reg = FPRegister(reg_id, kDRegSize);
1447 __ Ldr(reg, MemOperand(sp, stack_index));
1448 return kArm64WordSize;
1449}
1450
Alexandre Rames5319def2014-10-23 10:03:10 +01001451void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001452 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001453}
1454
1455void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001456 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001457}
1458
Alexandre Rames67555f72014-11-18 10:55:16 +00001459void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001460 if (constant->IsIntConstant()) {
1461 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1462 } else if (constant->IsLongConstant()) {
1463 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1464 } else if (constant->IsNullConstant()) {
1465 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001466 } else if (constant->IsFloatConstant()) {
1467 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1468 } else {
1469 DCHECK(constant->IsDoubleConstant());
1470 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1471 }
1472}
1473
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474
1475static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1476 DCHECK(constant.IsConstant());
1477 HConstant* cst = constant.GetConstant();
1478 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001479 // Null is mapped to a core W register, which we associate with kPrimInt.
1480 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001481 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1482 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1483 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1484}
1485
Roland Levillain558dea12017-01-27 19:40:44 +00001486// Allocate a scratch register from the VIXL pool, querying first into
1487// the floating-point register pool, and then the the core register
1488// pool. This is essentially a reimplementation of
1489// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1490// using a different allocation strategy.
1491static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1492 vixl::aarch64::UseScratchRegisterScope* temps,
1493 int size_in_bits) {
1494 return masm->GetScratchFPRegisterList()->IsEmpty()
1495 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1496 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1497}
1498
Calin Juravlee460d1d2015-09-29 04:52:17 +01001499void CodeGeneratorARM64::MoveLocation(Location destination,
1500 Location source,
1501 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001502 if (source.Equals(destination)) {
1503 return;
1504 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505
1506 // A valid move can always be inferred from the destination and source
1507 // locations. When moving from and to a register, the argument type can be
1508 // used to generate 32bit instead of 64bit moves. In debug mode we also
1509 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001510 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511
1512 if (destination.IsRegister() || destination.IsFpuRegister()) {
1513 if (unspecified_type) {
1514 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1515 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001516 (src_cst != nullptr && (src_cst->IsIntConstant()
1517 || src_cst->IsFloatConstant()
1518 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001519 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001520 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001521 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522 // If the source is a double stack slot or a 64bit constant, a 64bit
1523 // type is appropriate. Else the source is a register, and since the
1524 // type has not been specified, we chose a 64bit type to force a 64bit
1525 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001527 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001529 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1530 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1531 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1533 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1534 __ Ldr(dst, StackOperandFrom(source));
1535 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001538 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001540 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001541 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001542 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001543 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1544 ? Primitive::kPrimLong
1545 : Primitive::kPrimInt;
1546 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1547 }
1548 } else {
1549 DCHECK(source.IsFpuRegister());
1550 if (destination.IsRegister()) {
1551 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1552 ? Primitive::kPrimDouble
1553 : Primitive::kPrimFloat;
1554 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1555 } else {
1556 DCHECK(destination.IsFpuRegister());
1557 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 }
1559 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001560 } else { // The destination is not a register. It must be a stack slot.
1561 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1562 if (source.IsRegister() || source.IsFpuRegister()) {
1563 if (unspecified_type) {
1564 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001565 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001566 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001567 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 }
1569 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001570 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1571 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1572 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001573 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001574 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1575 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001576 UseScratchRegisterScope temps(GetVIXLAssembler());
1577 HConstant* src_cst = source.GetConstant();
1578 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001579 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001580 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1581 ? Register(xzr)
1582 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001584 if (src_cst->IsIntConstant()) {
1585 temp = temps.AcquireW();
1586 } else if (src_cst->IsLongConstant()) {
1587 temp = temps.AcquireX();
1588 } else if (src_cst->IsFloatConstant()) {
1589 temp = temps.AcquireS();
1590 } else {
1591 DCHECK(src_cst->IsDoubleConstant());
1592 temp = temps.AcquireD();
1593 }
1594 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001595 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001597 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001598 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001599 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001600 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001601 // Use any scratch register (a core or a floating-point one)
1602 // from VIXL scratch register pools as a temporary.
1603 //
1604 // We used to only use the FP scratch register pool, but in some
1605 // rare cases the only register from this pool (D31) would
1606 // already be used (e.g. within a ParallelMove instruction, when
1607 // a move is blocked by a another move requiring a scratch FP
1608 // register, which would reserve D31). To prevent this issue, we
1609 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001610 //
1611 // Also, we start by asking for a FP scratch register first, as the
1612 // demand of scratch core registers is higher. This is why we
1613 // use AcquireFPOrCoreCPURegisterOfSize instead of
1614 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1615 // allocates core scratch registers first.
1616 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1617 GetVIXLAssembler(),
1618 &temps,
1619 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001620 __ Ldr(temp, StackOperandFrom(source));
1621 __ Str(temp, StackOperandFrom(destination));
1622 }
1623 }
1624}
1625
1626void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001627 CPURegister dst,
1628 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 switch (type) {
1630 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001631 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001632 break;
1633 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001634 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001635 break;
1636 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001637 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001638 break;
1639 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001640 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001641 break;
1642 case Primitive::kPrimInt:
1643 case Primitive::kPrimNot:
1644 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001645 case Primitive::kPrimFloat:
1646 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001647 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001648 __ Ldr(dst, src);
1649 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001650 case Primitive::kPrimVoid:
1651 LOG(FATAL) << "Unreachable type " << type;
1652 }
1653}
1654
Calin Juravle77520bc2015-01-12 18:45:46 +00001655void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001657 const MemOperand& src,
1658 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001659 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001660 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001662 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001663
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001664 DCHECK(!src.IsPreIndex());
1665 DCHECK(!src.IsPostIndex());
1666
1667 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001668 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001669 {
1670 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1671 MemOperand base = MemOperand(temp_base);
1672 switch (type) {
1673 case Primitive::kPrimBoolean:
1674 {
1675 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1676 __ ldarb(Register(dst), base);
1677 if (needs_null_check) {
1678 MaybeRecordImplicitNullCheck(instruction);
1679 }
1680 }
1681 break;
1682 case Primitive::kPrimByte:
1683 {
1684 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1685 __ ldarb(Register(dst), base);
1686 if (needs_null_check) {
1687 MaybeRecordImplicitNullCheck(instruction);
1688 }
1689 }
1690 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1691 break;
1692 case Primitive::kPrimChar:
1693 {
1694 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1695 __ ldarh(Register(dst), base);
1696 if (needs_null_check) {
1697 MaybeRecordImplicitNullCheck(instruction);
1698 }
1699 }
1700 break;
1701 case Primitive::kPrimShort:
1702 {
1703 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1704 __ ldarh(Register(dst), base);
1705 if (needs_null_check) {
1706 MaybeRecordImplicitNullCheck(instruction);
1707 }
1708 }
1709 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1710 break;
1711 case Primitive::kPrimInt:
1712 case Primitive::kPrimNot:
1713 case Primitive::kPrimLong:
1714 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1715 {
1716 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1717 __ ldar(Register(dst), base);
1718 if (needs_null_check) {
1719 MaybeRecordImplicitNullCheck(instruction);
1720 }
1721 }
1722 break;
1723 case Primitive::kPrimFloat:
1724 case Primitive::kPrimDouble: {
1725 DCHECK(dst.IsFPRegister());
1726 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001727
Artem Serov914d7a82017-02-07 14:33:49 +00001728 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1729 {
1730 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1731 __ ldar(temp, base);
1732 if (needs_null_check) {
1733 MaybeRecordImplicitNullCheck(instruction);
1734 }
1735 }
1736 __ Fmov(FPRegister(dst), temp);
1737 break;
Roland Levillain44015862016-01-22 11:47:17 +00001738 }
Artem Serov914d7a82017-02-07 14:33:49 +00001739 case Primitive::kPrimVoid:
1740 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001741 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001742 }
1743}
1744
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001745void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 CPURegister src,
1747 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001748 switch (type) {
1749 case Primitive::kPrimBoolean:
1750 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001751 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001752 break;
1753 case Primitive::kPrimChar:
1754 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001755 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001756 break;
1757 case Primitive::kPrimInt:
1758 case Primitive::kPrimNot:
1759 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001760 case Primitive::kPrimFloat:
1761 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001762 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001763 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001764 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001765 case Primitive::kPrimVoid:
1766 LOG(FATAL) << "Unreachable type " << type;
1767 }
1768}
1769
Artem Serov914d7a82017-02-07 14:33:49 +00001770void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
1771 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001773 const MemOperand& dst,
1774 bool needs_null_check) {
1775 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001776 UseScratchRegisterScope temps(GetVIXLAssembler());
1777 Register temp_base = temps.AcquireX();
1778
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001779 DCHECK(!dst.IsPreIndex());
1780 DCHECK(!dst.IsPostIndex());
1781
1782 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001783 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001784 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001785 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001786 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 switch (type) {
1788 case Primitive::kPrimBoolean:
1789 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00001790 {
1791 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1792 __ stlrb(Register(src), base);
1793 if (needs_null_check) {
1794 MaybeRecordImplicitNullCheck(instruction);
1795 }
1796 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001797 break;
1798 case Primitive::kPrimChar:
1799 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00001800 {
1801 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1802 __ stlrh(Register(src), base);
1803 if (needs_null_check) {
1804 MaybeRecordImplicitNullCheck(instruction);
1805 }
1806 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001807 break;
1808 case Primitive::kPrimInt:
1809 case Primitive::kPrimNot:
1810 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001811 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001812 {
1813 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1814 __ stlr(Register(src), base);
1815 if (needs_null_check) {
1816 MaybeRecordImplicitNullCheck(instruction);
1817 }
1818 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001819 break;
1820 case Primitive::kPrimFloat:
1821 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001822 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001823 Register temp_src;
1824 if (src.IsZero()) {
1825 // The zero register is used to avoid synthesizing zero constants.
1826 temp_src = Register(src);
1827 } else {
1828 DCHECK(src.IsFPRegister());
1829 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1830 __ Fmov(temp_src, FPRegister(src));
1831 }
Artem Serov914d7a82017-02-07 14:33:49 +00001832 {
1833 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1834 __ stlr(temp_src, base);
1835 if (needs_null_check) {
1836 MaybeRecordImplicitNullCheck(instruction);
1837 }
1838 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001839 break;
1840 }
1841 case Primitive::kPrimVoid:
1842 LOG(FATAL) << "Unreachable type " << type;
1843 }
1844}
1845
Calin Juravle175dc732015-08-25 15:42:32 +01001846void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1847 HInstruction* instruction,
1848 uint32_t dex_pc,
1849 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001850 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001851
1852 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
1853 {
1854 // Ensure the pc position is recorded immediately after the `blr` instruction.
1855 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1856 __ blr(lr);
1857 if (EntrypointRequiresStackMap(entrypoint)) {
1858 RecordPcInfo(instruction, dex_pc, slow_path);
1859 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001860 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001861}
1862
Roland Levillaindec8f632016-07-22 17:10:06 +01001863void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1864 HInstruction* instruction,
1865 SlowPathCode* slow_path) {
1866 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001867 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1868 __ Blr(lr);
1869}
1870
Alexandre Rames67555f72014-11-18 10:55:16 +00001871void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001872 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 UseScratchRegisterScope temps(GetVIXLAssembler());
1874 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001875 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1876
Serban Constantinescu02164b32014-11-13 14:05:07 +00001877 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001878 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1879 __ Add(temp, class_reg, status_offset);
1880 __ Ldar(temp, HeapOperand(temp));
1881 __ Cmp(temp, mirror::Class::kStatusInitialized);
1882 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001883 __ Bind(slow_path->GetExitLabel());
1884}
Alexandre Rames5319def2014-10-23 10:03:10 +01001885
Roland Levillain44015862016-01-22 11:47:17 +00001886void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001887 BarrierType type = BarrierAll;
1888
1889 switch (kind) {
1890 case MemBarrierKind::kAnyAny:
1891 case MemBarrierKind::kAnyStore: {
1892 type = BarrierAll;
1893 break;
1894 }
1895 case MemBarrierKind::kLoadAny: {
1896 type = BarrierReads;
1897 break;
1898 }
1899 case MemBarrierKind::kStoreStore: {
1900 type = BarrierWrites;
1901 break;
1902 }
1903 default:
1904 LOG(FATAL) << "Unexpected memory barrier " << kind;
1905 }
1906 __ Dmb(InnerShareable, type);
1907}
1908
Serban Constantinescu02164b32014-11-13 14:05:07 +00001909void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1910 HBasicBlock* successor) {
1911 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001912 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1913 if (slow_path == nullptr) {
1914 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1915 instruction->SetSlowPath(slow_path);
1916 codegen_->AddSlowPath(slow_path);
1917 if (successor != nullptr) {
1918 DCHECK(successor->IsLoopHeader());
1919 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1920 }
1921 } else {
1922 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1923 }
1924
Serban Constantinescu02164b32014-11-13 14:05:07 +00001925 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1926 Register temp = temps.AcquireW();
1927
Andreas Gampe542451c2016-07-26 09:02:02 -07001928 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001929 if (successor == nullptr) {
1930 __ Cbnz(temp, slow_path->GetEntryLabel());
1931 __ Bind(slow_path->GetReturnLabel());
1932 } else {
1933 __ Cbz(temp, codegen_->GetLabelOf(successor));
1934 __ B(slow_path->GetEntryLabel());
1935 // slow_path will return to GetLabelOf(successor).
1936 }
1937}
1938
Alexandre Rames5319def2014-10-23 10:03:10 +01001939InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1940 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001941 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001942 assembler_(codegen->GetAssembler()),
1943 codegen_(codegen) {}
1944
1945#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001946 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001947
1948#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1949
1950enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001951 // Using a base helps identify when we hit such breakpoints.
1952 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001953#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1954 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1955#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1956};
1957
1958#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001960 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1961 } \
1962 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1964 locations->SetOut(Location::Any()); \
1965 }
1966 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1967#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1968
1969#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001970#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001971
Alexandre Rames67555f72014-11-18 10:55:16 +00001972void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001973 DCHECK_EQ(instr->InputCount(), 2U);
1974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1975 Primitive::Type type = instr->GetResultType();
1976 switch (type) {
1977 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001978 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001979 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001980 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001982 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001983
1984 case Primitive::kPrimFloat:
1985 case Primitive::kPrimDouble:
1986 locations->SetInAt(0, Location::RequiresFpuRegister());
1987 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001988 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001989 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001990
Alexandre Rames5319def2014-10-23 10:03:10 +01001991 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001992 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 }
1994}
1995
Alexandre Rames09a99962015-04-15 11:47:56 +01001996void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001997 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1998
1999 bool object_field_get_with_read_barrier =
2000 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002001 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002002 new (GetGraph()->GetArena()) LocationSummary(instruction,
2003 object_field_get_with_read_barrier ?
2004 LocationSummary::kCallOnSlowPath :
2005 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002006 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002007 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002008 // We need a temporary register for the read barrier marking slow
2009 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2010 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002011 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002012 locations->SetInAt(0, Location::RequiresRegister());
2013 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2014 locations->SetOut(Location::RequiresFpuRegister());
2015 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002016 // The output overlaps for an object field get when read barriers
2017 // are enabled: we do not want the load to overwrite the object's
2018 // location, as we need it to emit the read barrier.
2019 locations->SetOut(
2020 Location::RequiresRegister(),
2021 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002022 }
2023}
2024
2025void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2026 const FieldInfo& field_info) {
2027 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002028 LocationSummary* locations = instruction->GetLocations();
2029 Location base_loc = locations->InAt(0);
2030 Location out = locations->Out();
2031 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002032 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002033 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002034
Roland Levillain44015862016-01-22 11:47:17 +00002035 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2036 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002037 // /* HeapReference<Object> */ out = *(base + offset)
2038 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002039 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002040 // Note that potential implicit null checks are handled in this
2041 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2042 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2043 instruction,
2044 out,
2045 base,
2046 offset,
2047 temp,
2048 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002049 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002050 } else {
2051 // General case.
2052 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002053 // Note that a potential implicit null check is handled in this
2054 // CodeGeneratorARM64::LoadAcquire call.
2055 // NB: LoadAcquire will record the pc info if needed.
2056 codegen_->LoadAcquire(
2057 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002058 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002059 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2060 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002061 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002062 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002063 }
Roland Levillain44015862016-01-22 11:47:17 +00002064 if (field_type == Primitive::kPrimNot) {
2065 // If read barriers are enabled, emit read barriers other than
2066 // Baker's using a slow path (and also unpoison the loaded
2067 // reference, if heap poisoning is enabled).
2068 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2069 }
Roland Levillain4d027112015-07-01 15:41:14 +01002070 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002071}
2072
2073void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2074 LocationSummary* locations =
2075 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2076 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002077 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2078 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2079 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002080 locations->SetInAt(1, Location::RequiresFpuRegister());
2081 } else {
2082 locations->SetInAt(1, Location::RequiresRegister());
2083 }
2084}
2085
2086void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002087 const FieldInfo& field_info,
2088 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002089 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2090
2091 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002092 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002093 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002094 Offset offset = field_info.GetFieldOffset();
2095 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002096
Roland Levillain4d027112015-07-01 15:41:14 +01002097 {
2098 // We use a block to end the scratch scope before the write barrier, thus
2099 // freeing the temporary registers so they can be used in `MarkGCCard`.
2100 UseScratchRegisterScope temps(GetVIXLAssembler());
2101
2102 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2103 DCHECK(value.IsW());
2104 Register temp = temps.AcquireW();
2105 __ Mov(temp, value.W());
2106 GetAssembler()->PoisonHeapReference(temp.W());
2107 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002108 }
Roland Levillain4d027112015-07-01 15:41:14 +01002109
2110 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002111 codegen_->StoreRelease(
2112 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002113 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002114 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2115 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002116 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2117 codegen_->MaybeRecordImplicitNullCheck(instruction);
2118 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002119 }
2120
2121 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002122 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002123 }
2124}
2125
Alexandre Rames67555f72014-11-18 10:55:16 +00002126void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002127 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002128
2129 switch (type) {
2130 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002131 case Primitive::kPrimLong: {
2132 Register dst = OutputRegister(instr);
2133 Register lhs = InputRegisterAt(instr, 0);
2134 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002135 if (instr->IsAdd()) {
2136 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002137 } else if (instr->IsAnd()) {
2138 __ And(dst, lhs, rhs);
2139 } else if (instr->IsOr()) {
2140 __ Orr(dst, lhs, rhs);
2141 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002142 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002143 } else if (instr->IsRor()) {
2144 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002145 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002146 __ Ror(dst, lhs, shift);
2147 } else {
2148 // Ensure shift distance is in the same size register as the result. If
2149 // we are rotating a long and the shift comes in a w register originally,
2150 // we don't need to sxtw for use as an x since the shift distances are
2151 // all & reg_bits - 1.
2152 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2153 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002154 } else {
2155 DCHECK(instr->IsXor());
2156 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002157 }
2158 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002159 }
2160 case Primitive::kPrimFloat:
2161 case Primitive::kPrimDouble: {
2162 FPRegister dst = OutputFPRegister(instr);
2163 FPRegister lhs = InputFPRegisterAt(instr, 0);
2164 FPRegister rhs = InputFPRegisterAt(instr, 1);
2165 if (instr->IsAdd()) {
2166 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002168 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002169 } else {
2170 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002171 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002175 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002176 }
2177}
2178
Serban Constantinescu02164b32014-11-13 14:05:07 +00002179void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2180 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2181
2182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2183 Primitive::Type type = instr->GetResultType();
2184 switch (type) {
2185 case Primitive::kPrimInt:
2186 case Primitive::kPrimLong: {
2187 locations->SetInAt(0, Location::RequiresRegister());
2188 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2189 locations->SetOut(Location::RequiresRegister());
2190 break;
2191 }
2192 default:
2193 LOG(FATAL) << "Unexpected shift type " << type;
2194 }
2195}
2196
2197void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2198 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2199
2200 Primitive::Type type = instr->GetType();
2201 switch (type) {
2202 case Primitive::kPrimInt:
2203 case Primitive::kPrimLong: {
2204 Register dst = OutputRegister(instr);
2205 Register lhs = InputRegisterAt(instr, 0);
2206 Operand rhs = InputOperandAt(instr, 1);
2207 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002208 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002209 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002210 if (instr->IsShl()) {
2211 __ Lsl(dst, lhs, shift_value);
2212 } else if (instr->IsShr()) {
2213 __ Asr(dst, lhs, shift_value);
2214 } else {
2215 __ Lsr(dst, lhs, shift_value);
2216 }
2217 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002218 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002219
2220 if (instr->IsShl()) {
2221 __ Lsl(dst, lhs, rhs_reg);
2222 } else if (instr->IsShr()) {
2223 __ Asr(dst, lhs, rhs_reg);
2224 } else {
2225 __ Lsr(dst, lhs, rhs_reg);
2226 }
2227 }
2228 break;
2229 }
2230 default:
2231 LOG(FATAL) << "Unexpected shift operation type " << type;
2232 }
2233}
2234
Alexandre Rames5319def2014-10-23 10:03:10 +01002235void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002236 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002237}
2238
2239void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002240 HandleBinaryOp(instruction);
2241}
2242
2243void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2244 HandleBinaryOp(instruction);
2245}
2246
2247void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2248 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002249}
2250
Artem Serov7fc63502016-02-09 17:15:29 +00002251void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002252 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2254 locations->SetInAt(0, Location::RequiresRegister());
2255 // There is no immediate variant of negated bitwise instructions in AArch64.
2256 locations->SetInAt(1, Location::RequiresRegister());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258}
2259
Artem Serov7fc63502016-02-09 17:15:29 +00002260void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002261 Register dst = OutputRegister(instr);
2262 Register lhs = InputRegisterAt(instr, 0);
2263 Register rhs = InputRegisterAt(instr, 1);
2264
2265 switch (instr->GetOpKind()) {
2266 case HInstruction::kAnd:
2267 __ Bic(dst, lhs, rhs);
2268 break;
2269 case HInstruction::kOr:
2270 __ Orn(dst, lhs, rhs);
2271 break;
2272 case HInstruction::kXor:
2273 __ Eon(dst, lhs, rhs);
2274 break;
2275 default:
2276 LOG(FATAL) << "Unreachable";
2277 }
2278}
2279
Alexandre Rames8626b742015-11-25 16:28:08 +00002280void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2281 HArm64DataProcWithShifterOp* instruction) {
2282 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2283 instruction->GetType() == Primitive::kPrimLong);
2284 LocationSummary* locations =
2285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2286 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2287 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2288 } else {
2289 locations->SetInAt(0, Location::RequiresRegister());
2290 }
2291 locations->SetInAt(1, Location::RequiresRegister());
2292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2293}
2294
2295void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2296 HArm64DataProcWithShifterOp* instruction) {
2297 Primitive::Type type = instruction->GetType();
2298 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2299 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2300 Register out = OutputRegister(instruction);
2301 Register left;
2302 if (kind != HInstruction::kNeg) {
2303 left = InputRegisterAt(instruction, 0);
2304 }
2305 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2306 // shifter operand operation, the IR generating `right_reg` (input to the type
2307 // conversion) can have a different type from the current instruction's type,
2308 // so we manually indicate the type.
2309 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002310 int64_t shift_amount = instruction->GetShiftAmount() &
2311 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002312
2313 Operand right_operand(0);
2314
2315 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2316 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2317 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2318 } else {
2319 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2320 }
2321
2322 // Logical binary operations do not support extension operations in the
2323 // operand. Note that VIXL would still manage if it was passed by generating
2324 // the extension as a separate instruction.
2325 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2326 DCHECK(!right_operand.IsExtendedRegister() ||
2327 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2328 kind != HInstruction::kNeg));
2329 switch (kind) {
2330 case HInstruction::kAdd:
2331 __ Add(out, left, right_operand);
2332 break;
2333 case HInstruction::kAnd:
2334 __ And(out, left, right_operand);
2335 break;
2336 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002337 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002338 __ Neg(out, right_operand);
2339 break;
2340 case HInstruction::kOr:
2341 __ Orr(out, left, right_operand);
2342 break;
2343 case HInstruction::kSub:
2344 __ Sub(out, left, right_operand);
2345 break;
2346 case HInstruction::kXor:
2347 __ Eor(out, left, right_operand);
2348 break;
2349 default:
2350 LOG(FATAL) << "Unexpected operation kind: " << kind;
2351 UNREACHABLE();
2352 }
2353}
2354
Artem Serov328429f2016-07-06 16:23:04 +01002355void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002356 LocationSummary* locations =
2357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2358 locations->SetInAt(0, Location::RequiresRegister());
2359 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2360 locations->SetOut(Location::RequiresRegister());
2361}
2362
Roland Levillain19c54192016-11-04 13:44:09 +00002363void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002364 __ Add(OutputRegister(instruction),
2365 InputRegisterAt(instruction, 0),
2366 Operand(InputOperandAt(instruction, 1)));
2367}
2368
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002369void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002372 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2373 if (instr->GetOpKind() == HInstruction::kSub &&
2374 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002375 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002376 // Don't allocate register for Mneg instruction.
2377 } else {
2378 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2379 Location::RequiresRegister());
2380 }
2381 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2382 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2384}
2385
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002386void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002387 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002388 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2389 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002390
2391 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2392 // This fixup should be carried out for all multiply-accumulate instructions:
2393 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2394 if (instr->GetType() == Primitive::kPrimLong &&
2395 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2396 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002397 vixl::aarch64::Instruction* prev =
2398 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002399 if (prev->IsLoadOrStore()) {
2400 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002401 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002402 __ nop();
2403 }
2404 }
2405
2406 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002407 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002408 __ Madd(res, mul_left, mul_right, accumulator);
2409 } else {
2410 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002411 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002412 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002413 __ Mneg(res, mul_left, mul_right);
2414 } else {
2415 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2416 __ Msub(res, mul_left, mul_right, accumulator);
2417 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002418 }
2419}
2420
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002421void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002422 bool object_array_get_with_read_barrier =
2423 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002424 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002425 new (GetGraph()->GetArena()) LocationSummary(instruction,
2426 object_array_get_with_read_barrier ?
2427 LocationSummary::kCallOnSlowPath :
2428 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002429 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002430 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002431 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002432 locations->SetInAt(0, Location::RequiresRegister());
2433 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002434 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2435 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2436 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002437 // The output overlaps in the case of an object array get with
2438 // read barriers enabled: we do not want the move to overwrite the
2439 // array's location, as we need it to emit the read barrier.
2440 locations->SetOut(
2441 Location::RequiresRegister(),
2442 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002443 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002444}
2445
2446void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002447 Primitive::Type type = instruction->GetType();
2448 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002449 LocationSummary* locations = instruction->GetLocations();
2450 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002451 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002452 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002453 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2454 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002455 MacroAssembler* masm = GetVIXLAssembler();
2456 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002457
Roland Levillain19c54192016-11-04 13:44:09 +00002458 // The read barrier instrumentation of object ArrayGet instructions
2459 // does not support the HIntermediateAddress instruction.
2460 DCHECK(!((type == Primitive::kPrimNot) &&
2461 instruction->GetArray()->IsIntermediateAddress() &&
2462 kEmitCompilerReadBarrier));
2463
Roland Levillain44015862016-01-22 11:47:17 +00002464 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2465 // Object ArrayGet with Baker's read barrier case.
2466 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002467 // Note that a potential implicit null check is handled in the
2468 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2469 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2470 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002471 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002472 // General case.
2473 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002474 Register length;
2475 if (maybe_compressed_char_at) {
2476 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2477 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002478 {
2479 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2480 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2481
2482 if (instruction->GetArray()->IsIntermediateAddress()) {
2483 DCHECK_LT(count_offset, offset);
2484 int64_t adjusted_offset =
2485 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2486 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2487 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2488 } else {
2489 __ Ldr(length, HeapOperand(obj, count_offset));
2490 }
2491 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002492 }
jessicahandojo05765752016-09-09 19:01:32 -07002493 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002494 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002495 if (maybe_compressed_char_at) {
2496 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002497 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2498 "Expecting 0=compressed, 1=uncompressed");
2499 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002500 __ Ldrb(Register(OutputCPURegister(instruction)),
2501 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2502 __ B(&done);
2503 __ Bind(&uncompressed_load);
2504 __ Ldrh(Register(OutputCPURegister(instruction)),
2505 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2506 __ Bind(&done);
2507 } else {
2508 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2509 source = HeapOperand(obj, offset);
2510 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002511 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002512 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002513 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002514 // We do not need to compute the intermediate address from the array: the
2515 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002516 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002517 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002518 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002519 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2520 }
2521 temp = obj;
2522 } else {
2523 __ Add(temp, obj, offset);
2524 }
jessicahandojo05765752016-09-09 19:01:32 -07002525 if (maybe_compressed_char_at) {
2526 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002527 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2528 "Expecting 0=compressed, 1=uncompressed");
2529 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002530 __ Ldrb(Register(OutputCPURegister(instruction)),
2531 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2532 __ B(&done);
2533 __ Bind(&uncompressed_load);
2534 __ Ldrh(Register(OutputCPURegister(instruction)),
2535 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2536 __ Bind(&done);
2537 } else {
2538 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2539 }
Roland Levillain44015862016-01-22 11:47:17 +00002540 }
jessicahandojo05765752016-09-09 19:01:32 -07002541 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002542 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2543 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002544 codegen_->Load(type, OutputCPURegister(instruction), source);
2545 codegen_->MaybeRecordImplicitNullCheck(instruction);
2546 }
Roland Levillain44015862016-01-22 11:47:17 +00002547
2548 if (type == Primitive::kPrimNot) {
2549 static_assert(
2550 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2551 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2552 Location obj_loc = locations->InAt(0);
2553 if (index.IsConstant()) {
2554 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2555 } else {
2556 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2557 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002558 }
Roland Levillain4d027112015-07-01 15:41:14 +01002559 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002560}
2561
Alexandre Rames5319def2014-10-23 10:03:10 +01002562void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2564 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002566}
2567
2568void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002569 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002570 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002571 {
2572 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2573 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2574 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2575 codegen_->MaybeRecordImplicitNullCheck(instruction);
2576 }
jessicahandojo05765752016-09-09 19:01:32 -07002577 // Mask out compression flag from String's array length.
2578 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002579 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002580 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002581}
2582
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002583void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002584 Primitive::Type value_type = instruction->GetComponentType();
2585
2586 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2588 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002589 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002590 LocationSummary::kCallOnSlowPath :
2591 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002592 locations->SetInAt(0, Location::RequiresRegister());
2593 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002594 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2595 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2596 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002597 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002598 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002599 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002600 }
2601}
2602
2603void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2604 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002605 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002606 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002607 bool needs_write_barrier =
2608 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002609
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002610 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002611 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002612 CPURegister source = value;
2613 Location index = locations->InAt(1);
2614 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2615 MemOperand destination = HeapOperand(array);
2616 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002617
2618 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002619 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002620 if (index.IsConstant()) {
2621 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2622 destination = HeapOperand(array, offset);
2623 } else {
2624 UseScratchRegisterScope temps(masm);
2625 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002626 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002627 // We do not need to compute the intermediate address from the array: the
2628 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002629 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002630 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002631 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002632 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2633 }
2634 temp = array;
2635 } else {
2636 __ Add(temp, array, offset);
2637 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002638 destination = HeapOperand(temp,
2639 XRegisterFrom(index),
2640 LSL,
2641 Primitive::ComponentSizeShift(value_type));
2642 }
Artem Serov914d7a82017-02-07 14:33:49 +00002643 {
2644 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2645 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2646 codegen_->Store(value_type, value, destination);
2647 codegen_->MaybeRecordImplicitNullCheck(instruction);
2648 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002649 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002650 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002651 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002653 {
2654 // We use a block to end the scratch scope before the write barrier, thus
2655 // freeing the temporary registers so they can be used in `MarkGCCard`.
2656 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002657 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002658 if (index.IsConstant()) {
2659 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002660 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002661 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002662 destination = HeapOperand(temp,
2663 XRegisterFrom(index),
2664 LSL,
2665 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002666 }
2667
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002668 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2669 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2670 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2671
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002672 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002673 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2674 codegen_->AddSlowPath(slow_path);
2675 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002676 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 __ Cbnz(Register(value), &non_zero);
2678 if (!index.IsConstant()) {
2679 __ Add(temp, array, offset);
2680 }
Artem Serov914d7a82017-02-07 14:33:49 +00002681 {
2682 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2683 // emitted.
2684 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2685 __ Str(wzr, destination);
2686 codegen_->MaybeRecordImplicitNullCheck(instruction);
2687 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002688 __ B(&done);
2689 __ Bind(&non_zero);
2690 }
2691
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002692 // Note that when Baker read barriers are enabled, the type
2693 // checks are performed without read barriers. This is fine,
2694 // even in the case where a class object is in the from-space
2695 // after the flip, as a comparison involving such a type would
2696 // not produce a false positive; it may of course produce a
2697 // false negative, in which case we would take the ArraySet
2698 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002699
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002700 Register temp2 = temps.AcquireSameSizeAs(array);
2701 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002702 {
2703 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2704 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2705 __ Ldr(temp, HeapOperand(array, class_offset));
2706 codegen_->MaybeRecordImplicitNullCheck(instruction);
2707 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002708 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002709
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002710 // /* HeapReference<Class> */ temp = temp->component_type_
2711 __ Ldr(temp, HeapOperand(temp, component_offset));
2712 // /* HeapReference<Class> */ temp2 = value->klass_
2713 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2714 // If heap poisoning is enabled, no need to unpoison `temp`
2715 // nor `temp2`, as we are comparing two poisoned references.
2716 __ Cmp(temp, temp2);
2717 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002718
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002719 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2720 vixl::aarch64::Label do_put;
2721 __ B(eq, &do_put);
2722 // If heap poisoning is enabled, the `temp` reference has
2723 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002724 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2725
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002726 // /* HeapReference<Class> */ temp = temp->super_class_
2727 __ Ldr(temp, HeapOperand(temp, super_offset));
2728 // If heap poisoning is enabled, no need to unpoison
2729 // `temp`, as we are comparing against null below.
2730 __ Cbnz(temp, slow_path->GetEntryLabel());
2731 __ Bind(&do_put);
2732 } else {
2733 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002734 }
2735 }
2736
2737 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002738 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002739 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002740 __ Mov(temp2, value.W());
2741 GetAssembler()->PoisonHeapReference(temp2);
2742 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002743 }
2744
2745 if (!index.IsConstant()) {
2746 __ Add(temp, array, offset);
2747 }
Artem Serov914d7a82017-02-07 14:33:49 +00002748 {
2749 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2750 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2751 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002752
Artem Serov914d7a82017-02-07 14:33:49 +00002753 if (!may_need_runtime_call_for_type_check) {
2754 codegen_->MaybeRecordImplicitNullCheck(instruction);
2755 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002756 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002757 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002758
2759 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2760
2761 if (done.IsLinked()) {
2762 __ Bind(&done);
2763 }
2764
2765 if (slow_path != nullptr) {
2766 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002767 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002768 }
2769}
2770
Alexandre Rames67555f72014-11-18 10:55:16 +00002771void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002772 RegisterSet caller_saves = RegisterSet::Empty();
2773 InvokeRuntimeCallingConvention calling_convention;
2774 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2775 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2776 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002777 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002778 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002779}
2780
2781void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002782 BoundsCheckSlowPathARM64* slow_path =
2783 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002784 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002785 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2786 __ B(slow_path->GetEntryLabel(), hs);
2787}
2788
Alexandre Rames67555f72014-11-18 10:55:16 +00002789void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2790 LocationSummary* locations =
2791 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2792 locations->SetInAt(0, Location::RequiresRegister());
2793 if (check->HasUses()) {
2794 locations->SetOut(Location::SameAsFirstInput());
2795 }
2796}
2797
2798void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2799 // We assume the class is not null.
2800 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2801 check->GetLoadClass(), check, check->GetDexPc(), true);
2802 codegen_->AddSlowPath(slow_path);
2803 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2804}
2805
Roland Levillain1a653882016-03-18 18:05:57 +00002806static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2807 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2808 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2809}
2810
2811void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2812 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2813 Location rhs_loc = instruction->GetLocations()->InAt(1);
2814 if (rhs_loc.IsConstant()) {
2815 // 0.0 is the only immediate that can be encoded directly in
2816 // an FCMP instruction.
2817 //
2818 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2819 // specify that in a floating-point comparison, positive zero
2820 // and negative zero are considered equal, so we can use the
2821 // literal 0.0 for both cases here.
2822 //
2823 // Note however that some methods (Float.equal, Float.compare,
2824 // Float.compareTo, Double.equal, Double.compare,
2825 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2826 // StrictMath.min) consider 0.0 to be (strictly) greater than
2827 // -0.0. So if we ever translate calls to these methods into a
2828 // HCompare instruction, we must handle the -0.0 case with
2829 // care here.
2830 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2831 __ Fcmp(lhs_reg, 0.0);
2832 } else {
2833 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2834 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002835}
2836
Serban Constantinescu02164b32014-11-13 14:05:07 +00002837void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002838 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002839 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2840 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002841 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002842 case Primitive::kPrimBoolean:
2843 case Primitive::kPrimByte:
2844 case Primitive::kPrimShort:
2845 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002846 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002847 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002848 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002849 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2851 break;
2852 }
2853 case Primitive::kPrimFloat:
2854 case Primitive::kPrimDouble: {
2855 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002856 locations->SetInAt(1,
2857 IsFloatingPointZeroConstant(compare->InputAt(1))
2858 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2859 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002860 locations->SetOut(Location::RequiresRegister());
2861 break;
2862 }
2863 default:
2864 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2865 }
2866}
2867
2868void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2869 Primitive::Type in_type = compare->InputAt(0)->GetType();
2870
2871 // 0 if: left == right
2872 // 1 if: left > right
2873 // -1 if: left < right
2874 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002875 case Primitive::kPrimBoolean:
2876 case Primitive::kPrimByte:
2877 case Primitive::kPrimShort:
2878 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002879 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002880 case Primitive::kPrimLong: {
2881 Register result = OutputRegister(compare);
2882 Register left = InputRegisterAt(compare, 0);
2883 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002884 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002885 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2886 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002887 break;
2888 }
2889 case Primitive::kPrimFloat:
2890 case Primitive::kPrimDouble: {
2891 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002892 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002893 __ Cset(result, ne);
2894 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002895 break;
2896 }
2897 default:
2898 LOG(FATAL) << "Unimplemented compare type " << in_type;
2899 }
2900}
2901
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002902void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002904
2905 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2906 locations->SetInAt(0, Location::RequiresFpuRegister());
2907 locations->SetInAt(1,
2908 IsFloatingPointZeroConstant(instruction->InputAt(1))
2909 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2910 : Location::RequiresFpuRegister());
2911 } else {
2912 // Integer cases.
2913 locations->SetInAt(0, Location::RequiresRegister());
2914 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2915 }
2916
David Brazdilb3e773e2016-01-26 11:28:37 +00002917 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002919 }
2920}
2921
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002922void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002923 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002924 return;
2925 }
2926
2927 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002928 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002929 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002930
Roland Levillain7f63c522015-07-13 15:54:55 +00002931 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002932 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002933 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002934 } else {
2935 // Integer cases.
2936 Register lhs = InputRegisterAt(instruction, 0);
2937 Operand rhs = InputOperandAt(instruction, 1);
2938 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002939 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002940 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002941}
2942
2943#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2944 M(Equal) \
2945 M(NotEqual) \
2946 M(LessThan) \
2947 M(LessThanOrEqual) \
2948 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002949 M(GreaterThanOrEqual) \
2950 M(Below) \
2951 M(BelowOrEqual) \
2952 M(Above) \
2953 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002954#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002955void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2956void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002957FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002958#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002959#undef FOR_EACH_CONDITION_INSTRUCTION
2960
Zheng Xuc6667102015-05-15 16:08:45 +08002961void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2962 DCHECK(instruction->IsDiv() || instruction->IsRem());
2963
2964 LocationSummary* locations = instruction->GetLocations();
2965 Location second = locations->InAt(1);
2966 DCHECK(second.IsConstant());
2967
2968 Register out = OutputRegister(instruction);
2969 Register dividend = InputRegisterAt(instruction, 0);
2970 int64_t imm = Int64FromConstant(second.GetConstant());
2971 DCHECK(imm == 1 || imm == -1);
2972
2973 if (instruction->IsRem()) {
2974 __ Mov(out, 0);
2975 } else {
2976 if (imm == 1) {
2977 __ Mov(out, dividend);
2978 } else {
2979 __ Neg(out, dividend);
2980 }
2981 }
2982}
2983
2984void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2985 DCHECK(instruction->IsDiv() || instruction->IsRem());
2986
2987 LocationSummary* locations = instruction->GetLocations();
2988 Location second = locations->InAt(1);
2989 DCHECK(second.IsConstant());
2990
2991 Register out = OutputRegister(instruction);
2992 Register dividend = InputRegisterAt(instruction, 0);
2993 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002994 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002995 int ctz_imm = CTZ(abs_imm);
2996
2997 UseScratchRegisterScope temps(GetVIXLAssembler());
2998 Register temp = temps.AcquireSameSizeAs(out);
2999
3000 if (instruction->IsDiv()) {
3001 __ Add(temp, dividend, abs_imm - 1);
3002 __ Cmp(dividend, 0);
3003 __ Csel(out, temp, dividend, lt);
3004 if (imm > 0) {
3005 __ Asr(out, out, ctz_imm);
3006 } else {
3007 __ Neg(out, Operand(out, ASR, ctz_imm));
3008 }
3009 } else {
3010 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3011 __ Asr(temp, dividend, bits - 1);
3012 __ Lsr(temp, temp, bits - ctz_imm);
3013 __ Add(out, dividend, temp);
3014 __ And(out, out, abs_imm - 1);
3015 __ Sub(out, out, temp);
3016 }
3017}
3018
3019void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3020 DCHECK(instruction->IsDiv() || instruction->IsRem());
3021
3022 LocationSummary* locations = instruction->GetLocations();
3023 Location second = locations->InAt(1);
3024 DCHECK(second.IsConstant());
3025
3026 Register out = OutputRegister(instruction);
3027 Register dividend = InputRegisterAt(instruction, 0);
3028 int64_t imm = Int64FromConstant(second.GetConstant());
3029
3030 Primitive::Type type = instruction->GetResultType();
3031 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3032
3033 int64_t magic;
3034 int shift;
3035 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3036
3037 UseScratchRegisterScope temps(GetVIXLAssembler());
3038 Register temp = temps.AcquireSameSizeAs(out);
3039
3040 // temp = get_high(dividend * magic)
3041 __ Mov(temp, magic);
3042 if (type == Primitive::kPrimLong) {
3043 __ Smulh(temp, dividend, temp);
3044 } else {
3045 __ Smull(temp.X(), dividend, temp);
3046 __ Lsr(temp.X(), temp.X(), 32);
3047 }
3048
3049 if (imm > 0 && magic < 0) {
3050 __ Add(temp, temp, dividend);
3051 } else if (imm < 0 && magic > 0) {
3052 __ Sub(temp, temp, dividend);
3053 }
3054
3055 if (shift != 0) {
3056 __ Asr(temp, temp, shift);
3057 }
3058
3059 if (instruction->IsDiv()) {
3060 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3061 } else {
3062 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3063 // TODO: Strength reduction for msub.
3064 Register temp_imm = temps.AcquireSameSizeAs(out);
3065 __ Mov(temp_imm, imm);
3066 __ Msub(out, temp, temp_imm, dividend);
3067 }
3068}
3069
3070void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3071 DCHECK(instruction->IsDiv() || instruction->IsRem());
3072 Primitive::Type type = instruction->GetResultType();
3073 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3074
3075 LocationSummary* locations = instruction->GetLocations();
3076 Register out = OutputRegister(instruction);
3077 Location second = locations->InAt(1);
3078
3079 if (second.IsConstant()) {
3080 int64_t imm = Int64FromConstant(second.GetConstant());
3081
3082 if (imm == 0) {
3083 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3084 } else if (imm == 1 || imm == -1) {
3085 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003086 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003087 DivRemByPowerOfTwo(instruction);
3088 } else {
3089 DCHECK(imm <= -2 || imm >= 2);
3090 GenerateDivRemWithAnyConstant(instruction);
3091 }
3092 } else {
3093 Register dividend = InputRegisterAt(instruction, 0);
3094 Register divisor = InputRegisterAt(instruction, 1);
3095 if (instruction->IsDiv()) {
3096 __ Sdiv(out, dividend, divisor);
3097 } else {
3098 UseScratchRegisterScope temps(GetVIXLAssembler());
3099 Register temp = temps.AcquireSameSizeAs(out);
3100 __ Sdiv(temp, dividend, divisor);
3101 __ Msub(out, temp, divisor, dividend);
3102 }
3103 }
3104}
3105
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003106void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3107 LocationSummary* locations =
3108 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3109 switch (div->GetResultType()) {
3110 case Primitive::kPrimInt:
3111 case Primitive::kPrimLong:
3112 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003113 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3115 break;
3116
3117 case Primitive::kPrimFloat:
3118 case Primitive::kPrimDouble:
3119 locations->SetInAt(0, Location::RequiresFpuRegister());
3120 locations->SetInAt(1, Location::RequiresFpuRegister());
3121 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3122 break;
3123
3124 default:
3125 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3126 }
3127}
3128
3129void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3130 Primitive::Type type = div->GetResultType();
3131 switch (type) {
3132 case Primitive::kPrimInt:
3133 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003134 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003135 break;
3136
3137 case Primitive::kPrimFloat:
3138 case Primitive::kPrimDouble:
3139 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3140 break;
3141
3142 default:
3143 LOG(FATAL) << "Unexpected div type " << type;
3144 }
3145}
3146
Alexandre Rames67555f72014-11-18 10:55:16 +00003147void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003148 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003149 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003150}
3151
3152void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3153 SlowPathCodeARM64* slow_path =
3154 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3155 codegen_->AddSlowPath(slow_path);
3156 Location value = instruction->GetLocations()->InAt(0);
3157
Alexandre Rames3e69f162014-12-10 10:36:50 +00003158 Primitive::Type type = instruction->GetType();
3159
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003160 if (!Primitive::IsIntegralType(type)) {
3161 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003162 return;
3163 }
3164
Alexandre Rames67555f72014-11-18 10:55:16 +00003165 if (value.IsConstant()) {
3166 int64_t divisor = Int64ConstantFrom(value);
3167 if (divisor == 0) {
3168 __ B(slow_path->GetEntryLabel());
3169 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003170 // A division by a non-null constant is valid. We don't need to perform
3171 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003172 }
3173 } else {
3174 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3175 }
3176}
3177
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003178void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3179 LocationSummary* locations =
3180 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3181 locations->SetOut(Location::ConstantLocation(constant));
3182}
3183
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003184void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3185 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003186 // Will be generated at use site.
3187}
3188
Alexandre Rames5319def2014-10-23 10:03:10 +01003189void LocationsBuilderARM64::VisitExit(HExit* exit) {
3190 exit->SetLocations(nullptr);
3191}
3192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003193void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003194}
3195
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003196void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3197 LocationSummary* locations =
3198 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3199 locations->SetOut(Location::ConstantLocation(constant));
3200}
3201
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003202void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003203 // Will be generated at use site.
3204}
3205
David Brazdilfc6a86a2015-06-26 10:33:45 +00003206void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003207 DCHECK(!successor->IsExitBlock());
3208 HBasicBlock* block = got->GetBlock();
3209 HInstruction* previous = got->GetPrevious();
3210 HLoopInformation* info = block->GetLoopInformation();
3211
David Brazdil46e2a392015-03-16 17:31:52 +00003212 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003213 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3214 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3215 return;
3216 }
3217 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3218 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3219 }
3220 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003221 __ B(codegen_->GetLabelOf(successor));
3222 }
3223}
3224
David Brazdilfc6a86a2015-06-26 10:33:45 +00003225void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3226 got->SetLocations(nullptr);
3227}
3228
3229void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3230 HandleGoto(got, got->GetSuccessor());
3231}
3232
3233void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3234 try_boundary->SetLocations(nullptr);
3235}
3236
3237void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3238 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3239 if (!successor->IsExitBlock()) {
3240 HandleGoto(try_boundary, successor);
3241 }
3242}
3243
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003244void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003245 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003246 vixl::aarch64::Label* true_target,
3247 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003248 // FP branching requires both targets to be explicit. If either of the targets
3249 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003250 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003251 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003252
David Brazdil0debae72015-11-12 18:37:00 +00003253 if (true_target == nullptr && false_target == nullptr) {
3254 // Nothing to do. The code always falls through.
3255 return;
3256 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003257 // Constant condition, statically compared against "true" (integer value 1).
3258 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003259 if (true_target != nullptr) {
3260 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003261 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003262 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003263 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003264 if (false_target != nullptr) {
3265 __ B(false_target);
3266 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003267 }
David Brazdil0debae72015-11-12 18:37:00 +00003268 return;
3269 }
3270
3271 // The following code generates these patterns:
3272 // (1) true_target == nullptr && false_target != nullptr
3273 // - opposite condition true => branch to false_target
3274 // (2) true_target != nullptr && false_target == nullptr
3275 // - condition true => branch to true_target
3276 // (3) true_target != nullptr && false_target != nullptr
3277 // - condition true => branch to true_target
3278 // - branch to false_target
3279 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003280 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003281 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003282 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003283 if (true_target == nullptr) {
3284 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3285 } else {
3286 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3287 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003288 } else {
3289 // The condition instruction has not been materialized, use its inputs as
3290 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003291 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003292
David Brazdil0debae72015-11-12 18:37:00 +00003293 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003294 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003295 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003296 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003297 IfCondition opposite_condition = condition->GetOppositeCondition();
3298 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003299 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003300 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003301 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003302 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003303 // Integer cases.
3304 Register lhs = InputRegisterAt(condition, 0);
3305 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003306
3307 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003308 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003309 if (true_target == nullptr) {
3310 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3311 non_fallthrough_target = false_target;
3312 } else {
3313 arm64_cond = ARM64Condition(condition->GetCondition());
3314 non_fallthrough_target = true_target;
3315 }
3316
Aart Bik086d27e2016-01-20 17:02:00 -08003317 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003318 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003319 switch (arm64_cond) {
3320 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003321 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003322 break;
3323 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003324 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003325 break;
3326 case lt:
3327 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003328 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003329 break;
3330 case ge:
3331 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003332 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003333 break;
3334 default:
3335 // Without the `static_cast` the compiler throws an error for
3336 // `-Werror=sign-promo`.
3337 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3338 }
3339 } else {
3340 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003341 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003342 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003343 }
3344 }
David Brazdil0debae72015-11-12 18:37:00 +00003345
3346 // If neither branch falls through (case 3), the conditional branch to `true_target`
3347 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3348 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003349 __ B(false_target);
3350 }
David Brazdil0debae72015-11-12 18:37:00 +00003351
3352 if (fallthrough_target.IsLinked()) {
3353 __ Bind(&fallthrough_target);
3354 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003355}
3356
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003357void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3358 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003359 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003360 locations->SetInAt(0, Location::RequiresRegister());
3361 }
3362}
3363
3364void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003365 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3366 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003367 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3368 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3369 true_target = nullptr;
3370 }
3371 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3372 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3373 false_target = nullptr;
3374 }
David Brazdil0debae72015-11-12 18:37:00 +00003375 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003376}
3377
3378void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3379 LocationSummary* locations = new (GetGraph()->GetArena())
3380 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003381 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003382 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003383 locations->SetInAt(0, Location::RequiresRegister());
3384 }
3385}
3386
3387void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003388 SlowPathCodeARM64* slow_path =
3389 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003390 GenerateTestAndBranch(deoptimize,
3391 /* condition_input_index */ 0,
3392 slow_path->GetEntryLabel(),
3393 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003394}
3395
Mingyao Yang063fc772016-08-02 11:02:54 -07003396void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3397 LocationSummary* locations = new (GetGraph()->GetArena())
3398 LocationSummary(flag, LocationSummary::kNoCall);
3399 locations->SetOut(Location::RequiresRegister());
3400}
3401
3402void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3403 __ Ldr(OutputRegister(flag),
3404 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3405}
3406
David Brazdilc0b601b2016-02-08 14:20:45 +00003407static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3408 return condition->IsCondition() &&
3409 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3410}
3411
Alexandre Rames880f1192016-06-13 16:04:50 +01003412static inline Condition GetConditionForSelect(HCondition* condition) {
3413 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003414 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3415 : ARM64Condition(cond);
3416}
3417
David Brazdil74eb1b22015-12-14 11:44:01 +00003418void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003420 if (Primitive::IsFloatingPointType(select->GetType())) {
3421 locations->SetInAt(0, Location::RequiresFpuRegister());
3422 locations->SetInAt(1, Location::RequiresFpuRegister());
3423 locations->SetOut(Location::RequiresFpuRegister());
3424 } else {
3425 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3426 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3427 bool is_true_value_constant = cst_true_value != nullptr;
3428 bool is_false_value_constant = cst_false_value != nullptr;
3429 // Ask VIXL whether we should synthesize constants in registers.
3430 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3431 Operand true_op = is_true_value_constant ?
3432 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3433 Operand false_op = is_false_value_constant ?
3434 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3435 bool true_value_in_register = false;
3436 bool false_value_in_register = false;
3437 MacroAssembler::GetCselSynthesisInformation(
3438 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3439 true_value_in_register |= !is_true_value_constant;
3440 false_value_in_register |= !is_false_value_constant;
3441
3442 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3443 : Location::ConstantLocation(cst_true_value));
3444 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3445 : Location::ConstantLocation(cst_false_value));
3446 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003447 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003448
David Brazdil74eb1b22015-12-14 11:44:01 +00003449 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3450 locations->SetInAt(2, Location::RequiresRegister());
3451 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003452}
3453
3454void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003455 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003456 Condition csel_cond;
3457
3458 if (IsBooleanValueOrMaterializedCondition(cond)) {
3459 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003460 // Use the condition flags set by the previous instruction.
3461 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003462 } else {
3463 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003464 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003465 }
3466 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003467 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003468 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003469 } else {
3470 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003471 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003472 }
3473
Alexandre Rames880f1192016-06-13 16:04:50 +01003474 if (Primitive::IsFloatingPointType(select->GetType())) {
3475 __ Fcsel(OutputFPRegister(select),
3476 InputFPRegisterAt(select, 1),
3477 InputFPRegisterAt(select, 0),
3478 csel_cond);
3479 } else {
3480 __ Csel(OutputRegister(select),
3481 InputOperandAt(select, 1),
3482 InputOperandAt(select, 0),
3483 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003484 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003485}
3486
David Srbecky0cf44932015-12-09 14:09:59 +00003487void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3488 new (GetGraph()->GetArena()) LocationSummary(info);
3489}
3490
David Srbeckyd28f4a02016-03-14 17:14:24 +00003491void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3492 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003493}
3494
3495void CodeGeneratorARM64::GenerateNop() {
3496 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003497}
3498
Alexandre Rames5319def2014-10-23 10:03:10 +01003499void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003500 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003501}
3502
3503void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003504 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003505}
3506
3507void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003508 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003509}
3510
3511void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003512 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003513}
3514
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003515// Temp is used for read barrier.
3516static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3517 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003518 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003519 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3520 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3521 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3522 return 1;
3523 }
3524 return 0;
3525}
3526
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003527// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003528// interface pointer, one for loading the current interface.
3529// The other checks have one temp for loading the object's class.
3530static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3531 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3532 return 3;
3533 }
3534 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003535}
3536
Alexandre Rames67555f72014-11-18 10:55:16 +00003537void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003538 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003539 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003540 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003542 case TypeCheckKind::kExactCheck:
3543 case TypeCheckKind::kAbstractClassCheck:
3544 case TypeCheckKind::kClassHierarchyCheck:
3545 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003546 call_kind =
3547 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003548 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003549 break;
3550 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 case TypeCheckKind::kUnresolvedCheck:
3552 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003553 call_kind = LocationSummary::kCallOnSlowPath;
3554 break;
3555 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003556
Alexandre Rames67555f72014-11-18 10:55:16 +00003557 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003558 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003559 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003560 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003561 locations->SetInAt(0, Location::RequiresRegister());
3562 locations->SetInAt(1, Location::RequiresRegister());
3563 // The "out" register is used as a temporary, so it overlaps with the inputs.
3564 // Note that TypeCheckSlowPathARM64 uses this register too.
3565 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003566 // Add temps if necessary for read barriers.
3567 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003568}
3569
3570void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003571 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003572 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003573 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003574 Register obj = InputRegisterAt(instruction, 0);
3575 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003576 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003577 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003578 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3579 DCHECK_LE(num_temps, 1u);
3580 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003581 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3582 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3583 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3584 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003585
Scott Wakeling97c72b72016-06-24 16:19:36 +01003586 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003587 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003588
3589 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003590 // Avoid null check if we know `obj` is not null.
3591 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003592 __ Cbz(obj, &zero);
3593 }
3594
Roland Levillain44015862016-01-22 11:47:17 +00003595 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003596 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003597 // /* HeapReference<Class> */ out = obj->klass_
3598 GenerateReferenceLoadTwoRegisters(instruction,
3599 out_loc,
3600 obj_loc,
3601 class_offset,
3602 maybe_temp_loc,
3603 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003604 __ Cmp(out, cls);
3605 __ Cset(out, eq);
3606 if (zero.IsLinked()) {
3607 __ B(&done);
3608 }
3609 break;
3610 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003611
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003612 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003613 // /* HeapReference<Class> */ out = obj->klass_
3614 GenerateReferenceLoadTwoRegisters(instruction,
3615 out_loc,
3616 obj_loc,
3617 class_offset,
3618 maybe_temp_loc,
3619 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003620 // If the class is abstract, we eagerly fetch the super class of the
3621 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003622 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003623 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003624 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003625 GenerateReferenceLoadOneRegister(instruction,
3626 out_loc,
3627 super_offset,
3628 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003629 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003630 // If `out` is null, we use it for the result, and jump to `done`.
3631 __ Cbz(out, &done);
3632 __ Cmp(out, cls);
3633 __ B(ne, &loop);
3634 __ Mov(out, 1);
3635 if (zero.IsLinked()) {
3636 __ B(&done);
3637 }
3638 break;
3639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003641 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003642 // /* HeapReference<Class> */ out = obj->klass_
3643 GenerateReferenceLoadTwoRegisters(instruction,
3644 out_loc,
3645 obj_loc,
3646 class_offset,
3647 maybe_temp_loc,
3648 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003649 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003650 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003651 __ Bind(&loop);
3652 __ Cmp(out, cls);
3653 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003655 GenerateReferenceLoadOneRegister(instruction,
3656 out_loc,
3657 super_offset,
3658 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003659 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003660 __ Cbnz(out, &loop);
3661 // If `out` is null, we use it for the result, and jump to `done`.
3662 __ B(&done);
3663 __ Bind(&success);
3664 __ Mov(out, 1);
3665 if (zero.IsLinked()) {
3666 __ B(&done);
3667 }
3668 break;
3669 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003671 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003672 // /* HeapReference<Class> */ out = obj->klass_
3673 GenerateReferenceLoadTwoRegisters(instruction,
3674 out_loc,
3675 obj_loc,
3676 class_offset,
3677 maybe_temp_loc,
3678 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003679 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003680 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003681 __ Cmp(out, cls);
3682 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003684 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003685 GenerateReferenceLoadOneRegister(instruction,
3686 out_loc,
3687 component_offset,
3688 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003689 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003690 // If `out` is null, we use it for the result, and jump to `done`.
3691 __ Cbz(out, &done);
3692 __ Ldrh(out, HeapOperand(out, primitive_offset));
3693 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3694 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003695 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 __ Mov(out, 1);
3697 __ B(&done);
3698 break;
3699 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003701 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003702 // No read barrier since the slow path will retry upon failure.
3703 // /* HeapReference<Class> */ out = obj->klass_
3704 GenerateReferenceLoadTwoRegisters(instruction,
3705 out_loc,
3706 obj_loc,
3707 class_offset,
3708 maybe_temp_loc,
3709 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003710 __ Cmp(out, cls);
3711 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003712 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3713 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003714 codegen_->AddSlowPath(slow_path);
3715 __ B(ne, slow_path->GetEntryLabel());
3716 __ Mov(out, 1);
3717 if (zero.IsLinked()) {
3718 __ B(&done);
3719 }
3720 break;
3721 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003722
Calin Juravle98893e12015-10-02 21:05:03 +01003723 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003724 case TypeCheckKind::kInterfaceCheck: {
3725 // Note that we indeed only call on slow path, but we always go
3726 // into the slow path for the unresolved and interface check
3727 // cases.
3728 //
3729 // We cannot directly call the InstanceofNonTrivial runtime
3730 // entry point without resorting to a type checking slow path
3731 // here (i.e. by calling InvokeRuntime directly), as it would
3732 // require to assign fixed registers for the inputs of this
3733 // HInstanceOf instruction (following the runtime calling
3734 // convention), which might be cluttered by the potential first
3735 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003736 //
3737 // TODO: Introduce a new runtime entry point taking the object
3738 // to test (instead of its class) as argument, and let it deal
3739 // with the read barrier issues. This will let us refactor this
3740 // case of the `switch` code as it was previously (with a direct
3741 // call to the runtime not using a type checking slow path).
3742 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003743 DCHECK(locations->OnlyCallsOnSlowPath());
3744 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3745 /* is_fatal */ false);
3746 codegen_->AddSlowPath(slow_path);
3747 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003748 if (zero.IsLinked()) {
3749 __ B(&done);
3750 }
3751 break;
3752 }
3753 }
3754
3755 if (zero.IsLinked()) {
3756 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003757 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003758 }
3759
3760 if (done.IsLinked()) {
3761 __ Bind(&done);
3762 }
3763
3764 if (slow_path != nullptr) {
3765 __ Bind(slow_path->GetExitLabel());
3766 }
3767}
3768
3769void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3770 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3771 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3772
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003773 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3774 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003775 case TypeCheckKind::kExactCheck:
3776 case TypeCheckKind::kAbstractClassCheck:
3777 case TypeCheckKind::kClassHierarchyCheck:
3778 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3780 LocationSummary::kCallOnSlowPath :
3781 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003782 break;
3783 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003784 case TypeCheckKind::kUnresolvedCheck:
3785 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003786 call_kind = LocationSummary::kCallOnSlowPath;
3787 break;
3788 }
3789
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003790 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3791 locations->SetInAt(0, Location::RequiresRegister());
3792 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003793 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3794 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003795}
3796
3797void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003798 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003799 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003800 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003801 Register obj = InputRegisterAt(instruction, 0);
3802 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003803 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3804 DCHECK_GE(num_temps, 1u);
3805 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003806 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003807 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3808 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003809 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003810 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3811 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3812 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3813 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3814 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3815 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3816 const uint32_t object_array_data_offset =
3817 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003818
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003819 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003820 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3821 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3822 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003823 if (!kEmitCompilerReadBarrier) {
3824 is_type_check_slow_path_fatal =
3825 (type_check_kind == TypeCheckKind::kExactCheck ||
3826 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3827 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3828 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3829 !instruction->CanThrowIntoCatchBlock();
3830 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003831 SlowPathCodeARM64* type_check_slow_path =
3832 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3833 is_type_check_slow_path_fatal);
3834 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835
Scott Wakeling97c72b72016-06-24 16:19:36 +01003836 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003837 // Avoid null check if we know obj is not null.
3838 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003839 __ Cbz(obj, &done);
3840 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003841
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003842 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 case TypeCheckKind::kExactCheck:
3844 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003845 // /* HeapReference<Class> */ temp = obj->klass_
3846 GenerateReferenceLoadTwoRegisters(instruction,
3847 temp_loc,
3848 obj_loc,
3849 class_offset,
3850 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003851 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003852
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003853 __ Cmp(temp, cls);
3854 // Jump to slow path for throwing the exception or doing a
3855 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 break;
3858 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003860 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003861 // /* HeapReference<Class> */ temp = obj->klass_
3862 GenerateReferenceLoadTwoRegisters(instruction,
3863 temp_loc,
3864 obj_loc,
3865 class_offset,
3866 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003867 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003868
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003869 // If the class is abstract, we eagerly fetch the super class of the
3870 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003871 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003872 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003874 GenerateReferenceLoadOneRegister(instruction,
3875 temp_loc,
3876 super_offset,
3877 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003878 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003880 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3881 // exception.
3882 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3883 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 __ Cmp(temp, cls);
3885 __ B(ne, &loop);
3886 break;
3887 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003890 // /* HeapReference<Class> */ temp = obj->klass_
3891 GenerateReferenceLoadTwoRegisters(instruction,
3892 temp_loc,
3893 obj_loc,
3894 class_offset,
3895 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003896 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003897
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003898 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003899 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 __ Bind(&loop);
3901 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003902 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003904 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003905 GenerateReferenceLoadOneRegister(instruction,
3906 temp_loc,
3907 super_offset,
3908 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003909 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910
3911 // If the class reference currently in `temp` is not null, jump
3912 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003913 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 break;
3917 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003920 // /* HeapReference<Class> */ temp = obj->klass_
3921 GenerateReferenceLoadTwoRegisters(instruction,
3922 temp_loc,
3923 obj_loc,
3924 class_offset,
3925 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003926 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003927
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003928 // Do an exact check.
3929 __ Cmp(temp, cls);
3930 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931
3932 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003933 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003934 GenerateReferenceLoadOneRegister(instruction,
3935 temp_loc,
3936 component_offset,
3937 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003938 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003940 // If the component type is null, jump to the slow path to throw the exception.
3941 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3942 // Otherwise, the object is indeed an array. Further check that this component type is not a
3943 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003944 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3945 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003946 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003947 break;
3948 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003949
Calin Juravle98893e12015-10-02 21:05:03 +01003950 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003951 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003952 //
3953 // We cannot directly call the CheckCast runtime entry point
3954 // without resorting to a type checking slow path here (i.e. by
3955 // calling InvokeRuntime directly), as it would require to
3956 // assign fixed registers for the inputs of this HInstanceOf
3957 // instruction (following the runtime calling convention), which
3958 // might be cluttered by the potential first read barrier
3959 // emission at the beginning of this method.
3960 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003962 case TypeCheckKind::kInterfaceCheck: {
3963 // /* HeapReference<Class> */ temp = obj->klass_
3964 GenerateReferenceLoadTwoRegisters(instruction,
3965 temp_loc,
3966 obj_loc,
3967 class_offset,
3968 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003969 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003970
3971 // /* HeapReference<Class> */ temp = temp->iftable_
3972 GenerateReferenceLoadTwoRegisters(instruction,
3973 temp_loc,
3974 temp_loc,
3975 iftable_offset,
3976 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003977 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003978 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003979 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003980 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003981 vixl::aarch64::Label start_loop;
3982 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003983 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003984 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3985 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003986 // Go to next interface.
3987 __ Add(temp, temp, 2 * kHeapReferenceSize);
3988 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003989 // Compare the classes and continue the loop if they do not match.
3990 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3991 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003992 break;
3993 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003994 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003995 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003998}
3999
Alexandre Rames5319def2014-10-23 10:03:10 +01004000void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4002 locations->SetOut(Location::ConstantLocation(constant));
4003}
4004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004005void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004006 // Will be generated at use site.
4007}
4008
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004009void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4011 locations->SetOut(Location::ConstantLocation(constant));
4012}
4013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004014void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004015 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004016}
4017
Calin Juravle175dc732015-08-25 15:42:32 +01004018void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4019 // The trampoline uses the same calling convention as dex calling conventions,
4020 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4021 // the method_idx.
4022 HandleInvoke(invoke);
4023}
4024
4025void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4026 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4027}
4028
Alexandre Rames5319def2014-10-23 10:03:10 +01004029void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004030 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004031 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004032}
4033
Alexandre Rames67555f72014-11-18 10:55:16 +00004034void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4035 HandleInvoke(invoke);
4036}
4037
4038void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4039 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004040 LocationSummary* locations = invoke->GetLocations();
4041 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004042 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004043 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004044 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004045
4046 // The register ip1 is required to be used for the hidden argument in
4047 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004048 MacroAssembler* masm = GetVIXLAssembler();
4049 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004050 scratch_scope.Exclude(ip1);
4051 __ Mov(ip1, invoke->GetDexMethodIndex());
4052
Artem Serov914d7a82017-02-07 14:33:49 +00004053 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004054 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004055 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004056 {
4057 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4058 // /* HeapReference<Class> */ temp = temp->klass_
4059 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4060 codegen_->MaybeRecordImplicitNullCheck(invoke);
4061 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004062 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004063 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004065 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004066 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004067 }
Artem Serov914d7a82017-02-07 14:33:49 +00004068
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004069 // Instead of simply (possibly) unpoisoning `temp` here, we should
4070 // emit a read barrier for the previous class reference load.
4071 // However this is not required in practice, as this is an
4072 // intermediate/temporary reference and because the current
4073 // concurrent copying collector keeps the from-space memory
4074 // intact/accessible until the end of the marking phase (the
4075 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004076 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004077 __ Ldr(temp,
4078 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4079 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004080 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004081 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004082 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004083 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004084 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004085
4086 {
4087 // Ensure the pc position is recorded immediately after the `blr` instruction.
4088 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4089
4090 // lr();
4091 __ blr(lr);
4092 DCHECK(!codegen_->IsLeafMethod());
4093 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4094 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004095}
4096
4097void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004098 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
4099 if (intrinsic.TryDispatch(invoke)) {
4100 return;
4101 }
4102
Alexandre Rames67555f72014-11-18 10:55:16 +00004103 HandleInvoke(invoke);
4104}
4105
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004106void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004107 // Explicit clinit checks triggered by static invokes must have been pruned by
4108 // art::PrepareForRegisterAllocation.
4109 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004110
Andreas Gampe878d58c2015-01-15 23:24:00 -08004111 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
4112 if (intrinsic.TryDispatch(invoke)) {
4113 return;
4114 }
4115
Alexandre Rames67555f72014-11-18 10:55:16 +00004116 HandleInvoke(invoke);
4117}
4118
Andreas Gampe878d58c2015-01-15 23:24:00 -08004119static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4120 if (invoke->GetLocations()->Intrinsified()) {
4121 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4122 intrinsic.Dispatch(invoke);
4123 return true;
4124 }
4125 return false;
4126}
4127
Vladimir Markodc151b22015-10-15 18:02:30 +01004128HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4129 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004130 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004131 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004132 return desired_dispatch_info;
4133}
4134
TatWai Chongd8c052a2016-11-02 16:12:48 +08004135Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4136 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004137 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004138 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4139 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004140 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4141 uint32_t offset =
4142 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004143 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004144 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004145 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004146 }
Vladimir Marko58155012015-08-19 12:49:41 +00004147 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004148 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004149 break;
4150 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4151 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004152 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004153 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004154 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4155 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004156 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004157 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004158 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004159 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004160 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004161 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004162 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004163 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004164 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004165 }
Vladimir Marko58155012015-08-19 12:49:41 +00004166 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004167 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004168 Register reg = XRegisterFrom(temp);
4169 Register method_reg;
4170 if (current_method.IsRegister()) {
4171 method_reg = XRegisterFrom(current_method);
4172 } else {
4173 DCHECK(invoke->GetLocations()->Intrinsified());
4174 DCHECK(!current_method.IsValid());
4175 method_reg = reg;
4176 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4177 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004178
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004180 __ Ldr(reg.X(),
4181 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004182 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004183 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004184 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4185 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004186 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4187 break;
4188 }
4189 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004190 return callee_method;
4191}
4192
4193void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4194 // All registers are assumed to be correctly set up.
4195 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004196
4197 switch (invoke->GetCodePtrLocation()) {
4198 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4199 __ Bl(&frame_entry_label_);
4200 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004201 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4202 // LR = callee_method->entry_point_from_quick_compiled_code_;
4203 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004204 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004205 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004206 {
4207 // To ensure that the pc position is recorded immediately after the `blr` instruction
4208 // BLR must be the last instruction emitted in this function.
4209 // Recording the pc will occur right after returning from this function.
4210 ExactAssemblyScope eas(GetVIXLAssembler(),
4211 kInstructionSize,
4212 CodeBufferCheckScope::kExactSize);
4213 // lr()
4214 __ blr(lr);
4215 }
Vladimir Marko58155012015-08-19 12:49:41 +00004216 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004217 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004218
Andreas Gampe878d58c2015-01-15 23:24:00 -08004219 DCHECK(!IsLeafMethod());
4220}
4221
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004222void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004223 // Use the calling convention instead of the location of the receiver, as
4224 // intrinsics may have put the receiver in a different register. In the intrinsics
4225 // slow path, the arguments have been moved to the right place, so here we are
4226 // guaranteed that the receiver is the first register of the calling convention.
4227 InvokeDexCallingConvention calling_convention;
4228 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004229 Register temp = XRegisterFrom(temp_in);
4230 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4231 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4232 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004233 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004234
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004235 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004236
4237 {
4238 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4239 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4240 // /* HeapReference<Class> */ temp = receiver->klass_
4241 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4242 MaybeRecordImplicitNullCheck(invoke);
4243 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244 // Instead of simply (possibly) unpoisoning `temp` here, we should
4245 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246 // intermediate/temporary reference and because the current
4247 // concurrent copying collector keeps the from-space memory
4248 // intact/accessible until the end of the marking phase (the
4249 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004250 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4251 // temp = temp->GetMethodAt(method_offset);
4252 __ Ldr(temp, MemOperand(temp, method_offset));
4253 // lr = temp->GetEntryPoint();
4254 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004255 {
4256 // To ensure that the pc position is recorded immediately after the `blr` instruction
4257 // BLR should be the last instruction emitted in this function.
4258 // Recording the pc will occur right after returning from this function.
4259 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4260 // lr();
4261 __ blr(lr);
4262 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004263}
4264
Orion Hodsonac141392017-01-13 11:53:47 +00004265void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4266 HandleInvoke(invoke);
4267}
4268
4269void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4270 codegen_->GenerateInvokePolymorphicCall(invoke);
4271}
4272
Scott Wakeling97c72b72016-06-24 16:19:36 +01004273vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4274 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004275 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004276 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004277 return
4278 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004279}
4280
Scott Wakeling97c72b72016-06-24 16:19:36 +01004281vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4282 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004283 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004284 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004285 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004286}
4287
Vladimir Marko1998cd02017-01-13 13:02:58 +00004288vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4289 const DexFile& dex_file,
4290 dex::TypeIndex type_index,
4291 vixl::aarch64::Label* adrp_label) {
4292 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4293}
4294
Scott Wakeling97c72b72016-06-24 16:19:36 +01004295vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4296 const DexFile& dex_file,
4297 uint32_t element_offset,
4298 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004299 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4300}
4301
Scott Wakeling97c72b72016-06-24 16:19:36 +01004302vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4303 const DexFile& dex_file,
4304 uint32_t offset_or_index,
4305 vixl::aarch64::Label* adrp_label,
4306 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004307 // Add a patch entry and return the label.
4308 patches->emplace_back(dex_file, offset_or_index);
4309 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004310 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004311 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4312 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4313 return label;
4314}
4315
Scott Wakeling97c72b72016-06-24 16:19:36 +01004316vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004317 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004318 return boot_image_string_patches_.GetOrCreate(
4319 StringReference(&dex_file, string_index),
4320 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4321}
4322
Scott Wakeling97c72b72016-06-24 16:19:36 +01004323vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004324 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004325 return boot_image_type_patches_.GetOrCreate(
4326 TypeReference(&dex_file, type_index),
4327 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4328}
4329
Scott Wakeling97c72b72016-06-24 16:19:36 +01004330vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4331 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004332 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4333 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4334 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4335}
4336
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004337vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004338 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4339 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4340 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004341 return jit_string_patches_.GetOrCreate(
4342 StringReference(&dex_file, string_index),
4343 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4344}
4345
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004346vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004347 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4348 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4349 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004350 return jit_class_patches_.GetOrCreate(
4351 TypeReference(&dex_file, type_index),
4352 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4353}
4354
Vladimir Markoaad75c62016-10-03 08:46:48 +00004355void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4356 vixl::aarch64::Register reg) {
4357 DCHECK(reg.IsX());
4358 SingleEmissionCheckScope guard(GetVIXLAssembler());
4359 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004360 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004361}
4362
4363void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4364 vixl::aarch64::Register out,
4365 vixl::aarch64::Register base) {
4366 DCHECK(out.IsX());
4367 DCHECK(base.IsX());
4368 SingleEmissionCheckScope guard(GetVIXLAssembler());
4369 __ Bind(fixup_label);
4370 __ add(out, base, Operand(/* offset placeholder */ 0));
4371}
4372
4373void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4374 vixl::aarch64::Register out,
4375 vixl::aarch64::Register base) {
4376 DCHECK(base.IsX());
4377 SingleEmissionCheckScope guard(GetVIXLAssembler());
4378 __ Bind(fixup_label);
4379 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4380}
4381
4382template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4383inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4384 const ArenaDeque<PcRelativePatchInfo>& infos,
4385 ArenaVector<LinkerPatch>* linker_patches) {
4386 for (const PcRelativePatchInfo& info : infos) {
4387 linker_patches->push_back(Factory(info.label.GetLocation(),
4388 &info.target_dex_file,
4389 info.pc_insn_label->GetLocation(),
4390 info.offset_or_index));
4391 }
4392}
4393
Vladimir Marko58155012015-08-19 12:49:41 +00004394void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4395 DCHECK(linker_patches->empty());
4396 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004397 pc_relative_dex_cache_patches_.size() +
4398 boot_image_string_patches_.size() +
4399 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004400 boot_image_type_patches_.size() +
4401 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004402 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004403 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004404 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004405 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004406 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004407 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004408 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004409 info.offset_or_index));
4410 }
4411 for (const auto& entry : boot_image_string_patches_) {
4412 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004413 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4414 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004415 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004416 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004417 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004418 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004419 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004420 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4421 linker_patches);
4422 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004423 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4424 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004425 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4426 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004427 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004428 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4429 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004430 for (const auto& entry : boot_image_type_patches_) {
4431 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004432 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4433 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004434 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004435 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004436 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004437 for (const auto& entry : boot_image_address_patches_) {
4438 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004439 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4440 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004441 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004442 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004443}
4444
Scott Wakeling97c72b72016-06-24 16:19:36 +01004445vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004446 Uint32ToLiteralMap* map) {
4447 return map->GetOrCreate(
4448 value,
4449 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4450}
4451
Scott Wakeling97c72b72016-06-24 16:19:36 +01004452vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004453 return uint64_literals_.GetOrCreate(
4454 value,
4455 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004456}
4457
Scott Wakeling97c72b72016-06-24 16:19:36 +01004458vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004459 MethodReference target_method,
4460 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004461 return map->GetOrCreate(
4462 target_method,
4463 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004464}
4465
Andreas Gampe878d58c2015-01-15 23:24:00 -08004466void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004467 // Explicit clinit checks triggered by static invokes must have been pruned by
4468 // art::PrepareForRegisterAllocation.
4469 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004470
Andreas Gampe878d58c2015-01-15 23:24:00 -08004471 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4472 return;
4473 }
4474
Artem Serov914d7a82017-02-07 14:33:49 +00004475 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4476 // are no pools emitted.
4477 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004478 LocationSummary* locations = invoke->GetLocations();
4479 codegen_->GenerateStaticOrDirectCall(
4480 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004481 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004482}
4483
4484void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004485 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4486 return;
4487 }
4488
Artem Serov914d7a82017-02-07 14:33:49 +00004489 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4490 // are no pools emitted.
4491 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004492 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004493 DCHECK(!codegen_->IsLeafMethod());
4494 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4495}
4496
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004497HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4498 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004499 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004500 case HLoadClass::LoadKind::kInvalid:
4501 LOG(FATAL) << "UNREACHABLE";
4502 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004503 case HLoadClass::LoadKind::kReferrersClass:
4504 break;
4505 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4506 DCHECK(!GetCompilerOptions().GetCompilePic());
4507 break;
4508 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4509 DCHECK(GetCompilerOptions().GetCompilePic());
4510 break;
4511 case HLoadClass::LoadKind::kBootImageAddress:
4512 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004513 case HLoadClass::LoadKind::kBssEntry:
4514 DCHECK(!Runtime::Current()->UseJitCompilation());
4515 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004516 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004517 DCHECK(Runtime::Current()->UseJitCompilation());
4518 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004519 case HLoadClass::LoadKind::kDexCacheViaMethod:
4520 break;
4521 }
4522 return desired_class_load_kind;
4523}
4524
Alexandre Rames67555f72014-11-18 10:55:16 +00004525void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004526 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4527 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004528 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004529 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004530 cls,
4531 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004532 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004533 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004534 return;
4535 }
Vladimir Marko41559982017-01-06 14:04:23 +00004536 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004537
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004538 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4539 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004540 ? LocationSummary::kCallOnSlowPath
4541 : LocationSummary::kNoCall;
4542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004543 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004544 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004545 }
4546
Vladimir Marko41559982017-01-06 14:04:23 +00004547 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004548 locations->SetInAt(0, Location::RequiresRegister());
4549 }
4550 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004551 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4552 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4553 // Rely on the type resolution or initialization and marking to save everything we need.
4554 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4555 // to the custom calling convention) or by marking, so we shall use IP1.
4556 RegisterSet caller_saves = RegisterSet::Empty();
4557 InvokeRuntimeCallingConvention calling_convention;
4558 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4559 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4560 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4561 Primitive::kPrimNot).GetCode());
4562 locations->SetCustomSlowPathCallerSaves(caller_saves);
4563 } else {
4564 // For non-Baker read barrier we have a temp-clobbering call.
4565 }
4566 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004567}
4568
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004569// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4570// move.
4571void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004572 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4573 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4574 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004575 return;
4576 }
Vladimir Marko41559982017-01-06 14:04:23 +00004577 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004578
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004579 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004580 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004581 Register bss_entry_temp;
4582 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004583
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004584 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4585 ? kWithoutReadBarrier
4586 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004587 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004588 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004589 case HLoadClass::LoadKind::kReferrersClass: {
4590 DCHECK(!cls->CanCallRuntime());
4591 DCHECK(!cls->MustGenerateClinitCheck());
4592 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4593 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004594 GenerateGcRootFieldLoad(cls,
4595 out_loc,
4596 current_method,
4597 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004598 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004599 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004600 break;
4601 }
4602 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004603 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004604 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4605 cls->GetTypeIndex()));
4606 break;
4607 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004608 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004609 // Add ADRP with its PC-relative type patch.
4610 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004611 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004612 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004613 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004614 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004615 vixl::aarch64::Label* add_label =
4616 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004617 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004618 break;
4619 }
4620 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004621 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004622 uint32_t address = dchecked_integral_cast<uint32_t>(
4623 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4624 DCHECK_NE(address, 0u);
4625 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004626 break;
4627 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004628 case HLoadClass::LoadKind::kBssEntry: {
4629 // Add ADRP with its PC-relative Class .bss entry patch.
4630 const DexFile& dex_file = cls->GetDexFile();
4631 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004632 // We can go to slow path even with non-zero reference and in that case marking
4633 // can clobber IP0, so we need to use IP1 which shall be preserved.
4634 bss_entry_temp = ip1;
4635 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4636 temps.Exclude(bss_entry_temp);
4637 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4638 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004639 // Add LDR with its PC-relative Class patch.
4640 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004641 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004642 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4643 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004644 out_loc,
4645 bss_entry_temp,
4646 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004647 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004648 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004649 generate_null_check = true;
4650 break;
4651 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004652 case HLoadClass::LoadKind::kJitTableAddress: {
4653 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4654 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004655 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004656 GenerateGcRootFieldLoad(cls,
4657 out_loc,
4658 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004659 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004660 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004661 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004662 break;
4663 }
Vladimir Marko41559982017-01-06 14:04:23 +00004664 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004665 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004666 LOG(FATAL) << "UNREACHABLE";
4667 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004668 }
4669
Vladimir Markoea4c1262017-02-06 19:59:33 +00004670 bool do_clinit = cls->MustGenerateClinitCheck();
4671 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004672 DCHECK(cls->CanCallRuntime());
4673 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004674 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004675 codegen_->AddSlowPath(slow_path);
4676 if (generate_null_check) {
4677 __ Cbz(out, slow_path->GetEntryLabel());
4678 }
4679 if (cls->MustGenerateClinitCheck()) {
4680 GenerateClassInitializationCheck(slow_path, out);
4681 } else {
4682 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004683 }
4684 }
4685}
4686
David Brazdilcb1c0552015-08-04 16:22:25 +01004687static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004688 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004689}
4690
Alexandre Rames67555f72014-11-18 10:55:16 +00004691void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4692 LocationSummary* locations =
4693 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4694 locations->SetOut(Location::RequiresRegister());
4695}
4696
4697void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004698 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4699}
4700
4701void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4702 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4703}
4704
4705void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4706 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004707}
4708
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004709HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4710 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004711 switch (desired_string_load_kind) {
4712 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4713 DCHECK(!GetCompilerOptions().GetCompilePic());
4714 break;
4715 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4716 DCHECK(GetCompilerOptions().GetCompilePic());
4717 break;
4718 case HLoadString::LoadKind::kBootImageAddress:
4719 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004720 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004721 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004722 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004723 case HLoadString::LoadKind::kJitTableAddress:
4724 DCHECK(Runtime::Current()->UseJitCompilation());
4725 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004726 case HLoadString::LoadKind::kDexCacheViaMethod:
4727 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004728 }
4729 return desired_string_load_kind;
4730}
4731
Alexandre Rames67555f72014-11-18 10:55:16 +00004732void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004733 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004734 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004735 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004736 InvokeRuntimeCallingConvention calling_convention;
4737 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4738 } else {
4739 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004740 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4741 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004742 // Rely on the pResolveString and marking to save everything we need.
4743 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4744 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004745 RegisterSet caller_saves = RegisterSet::Empty();
4746 InvokeRuntimeCallingConvention calling_convention;
4747 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4748 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4749 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4750 Primitive::kPrimNot).GetCode());
4751 locations->SetCustomSlowPathCallerSaves(caller_saves);
4752 } else {
4753 // For non-Baker read barrier we have a temp-clobbering call.
4754 }
4755 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004756 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004757}
4758
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004759// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4760// move.
4761void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004762 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004763 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004764
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004765 switch (load->GetLoadKind()) {
4766 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004767 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4768 load->GetStringIndex()));
4769 return; // No dex cache slow path.
4770 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004771 // Add ADRP with its PC-relative String patch.
4772 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004773 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004774 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004775 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004776 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004777 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004778 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004779 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004780 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004781 return; // No dex cache slow path.
4782 }
4783 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004784 uint32_t address = dchecked_integral_cast<uint32_t>(
4785 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4786 DCHECK_NE(address, 0u);
4787 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004788 return; // No dex cache slow path.
4789 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004790 case HLoadString::LoadKind::kBssEntry: {
4791 // Add ADRP with its PC-relative String .bss entry patch.
4792 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004793 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004794 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004795 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
4796 // that's when we need the slow path. But let's not rely on such details and use IP1.
4797 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004798 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004799 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004800 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004801 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004802 // Add LDR with its PC-relative String patch.
4803 vixl::aarch64::Label* ldr_label =
4804 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004805 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004806 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004807 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004808 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004809 /* offset placeholder */ 0u,
4810 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004811 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004812 SlowPathCodeARM64* slow_path =
4813 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004814 codegen_->AddSlowPath(slow_path);
4815 __ Cbz(out.X(), slow_path->GetEntryLabel());
4816 __ Bind(slow_path->GetExitLabel());
4817 return;
4818 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004819 case HLoadString::LoadKind::kJitTableAddress: {
4820 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004821 load->GetStringIndex(),
4822 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004823 GenerateGcRootFieldLoad(load,
4824 out_loc,
4825 out.X(),
4826 /* offset */ 0,
4827 /* fixup_label */ nullptr,
4828 kCompilerReadBarrierOption);
4829 return;
4830 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004831 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004832 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004833 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004834
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004835 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004836 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004837 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004838 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004839 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4840 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004841}
4842
Alexandre Rames5319def2014-10-23 10:03:10 +01004843void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4845 locations->SetOut(Location::ConstantLocation(constant));
4846}
4847
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004848void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004849 // Will be generated at use site.
4850}
4851
Alexandre Rames67555f72014-11-18 10:55:16 +00004852void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4853 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004855 InvokeRuntimeCallingConvention calling_convention;
4856 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4857}
4858
4859void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004860 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004861 instruction,
4862 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004863 if (instruction->IsEnter()) {
4864 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4865 } else {
4866 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4867 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004868}
4869
Alexandre Rames42d641b2014-10-27 14:00:51 +00004870void LocationsBuilderARM64::VisitMul(HMul* mul) {
4871 LocationSummary* locations =
4872 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4873 switch (mul->GetResultType()) {
4874 case Primitive::kPrimInt:
4875 case Primitive::kPrimLong:
4876 locations->SetInAt(0, Location::RequiresRegister());
4877 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004879 break;
4880
4881 case Primitive::kPrimFloat:
4882 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004883 locations->SetInAt(0, Location::RequiresFpuRegister());
4884 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004886 break;
4887
4888 default:
4889 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4890 }
4891}
4892
4893void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4894 switch (mul->GetResultType()) {
4895 case Primitive::kPrimInt:
4896 case Primitive::kPrimLong:
4897 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4898 break;
4899
4900 case Primitive::kPrimFloat:
4901 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004902 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004903 break;
4904
4905 default:
4906 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4907 }
4908}
4909
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004910void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4911 LocationSummary* locations =
4912 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4913 switch (neg->GetResultType()) {
4914 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004915 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004916 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004918 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004919
4920 case Primitive::kPrimFloat:
4921 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004922 locations->SetInAt(0, Location::RequiresFpuRegister());
4923 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004924 break;
4925
4926 default:
4927 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4928 }
4929}
4930
4931void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4932 switch (neg->GetResultType()) {
4933 case Primitive::kPrimInt:
4934 case Primitive::kPrimLong:
4935 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4936 break;
4937
4938 case Primitive::kPrimFloat:
4939 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004940 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004941 break;
4942
4943 default:
4944 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4945 }
4946}
4947
4948void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4949 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004951 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004952 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004953 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4954 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004955}
4956
4957void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004958 // Note: if heap poisoning is enabled, the entry point takes cares
4959 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004960 QuickEntrypointEnum entrypoint =
4961 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4962 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004963 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004964}
4965
Alexandre Rames5319def2014-10-23 10:03:10 +01004966void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4967 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004969 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004970 if (instruction->IsStringAlloc()) {
4971 locations->AddTemp(LocationFrom(kArtMethodRegister));
4972 } else {
4973 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004974 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004975 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4976}
4977
4978void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004979 // Note: if heap poisoning is enabled, the entry point takes cares
4980 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004981 if (instruction->IsStringAlloc()) {
4982 // String is allocated through StringFactory. Call NewEmptyString entry point.
4983 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004984 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004985 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4986 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004987
4988 {
4989 // Ensure the pc position is recorded immediately after the `blr` instruction.
4990 ExactAssemblyScope eas(GetVIXLAssembler(),
4991 kInstructionSize,
4992 CodeBufferCheckScope::kExactSize);
4993 __ blr(lr);
4994 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4995 }
David Brazdil6de19382016-01-08 17:37:10 +00004996 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004997 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004998 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004999 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005000}
5001
5002void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005004 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005005 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005006}
5007
5008void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005009 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005010 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005011 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005012 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005013 break;
5014
5015 default:
5016 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5017 }
5018}
5019
David Brazdil66d126e2015-04-03 16:02:44 +01005020void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5021 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5022 locations->SetInAt(0, Location::RequiresRegister());
5023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5024}
5025
5026void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005027 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005028}
5029
Alexandre Rames5319def2014-10-23 10:03:10 +01005030void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005031 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5032 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005033}
5034
Calin Juravle2ae48182016-03-16 14:05:09 +00005035void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5036 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005037 return;
5038 }
Artem Serov914d7a82017-02-07 14:33:49 +00005039 {
5040 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5041 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5042 Location obj = instruction->GetLocations()->InAt(0);
5043 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5044 RecordPcInfo(instruction, instruction->GetDexPc());
5045 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005046}
5047
Calin Juravle2ae48182016-03-16 14:05:09 +00005048void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005049 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005050 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005051
5052 LocationSummary* locations = instruction->GetLocations();
5053 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005054
5055 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005056}
5057
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005058void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005059 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005060}
5061
Alexandre Rames67555f72014-11-18 10:55:16 +00005062void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5063 HandleBinaryOp(instruction);
5064}
5065
5066void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5067 HandleBinaryOp(instruction);
5068}
5069
Alexandre Rames3e69f162014-12-10 10:36:50 +00005070void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5071 LOG(FATAL) << "Unreachable";
5072}
5073
5074void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5075 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5076}
5077
Alexandre Rames5319def2014-10-23 10:03:10 +01005078void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5080 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5081 if (location.IsStackSlot()) {
5082 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5083 } else if (location.IsDoubleStackSlot()) {
5084 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5085 }
5086 locations->SetOut(location);
5087}
5088
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005089void InstructionCodeGeneratorARM64::VisitParameterValue(
5090 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005091 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005092}
5093
5094void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5095 LocationSummary* locations =
5096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005097 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005098}
5099
5100void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5101 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5102 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005103}
5104
5105void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005107 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005108 locations->SetInAt(i, Location::Any());
5109 }
5110 locations->SetOut(Location::Any());
5111}
5112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005113void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005114 LOG(FATAL) << "Unreachable";
5115}
5116
Serban Constantinescu02164b32014-11-13 14:05:07 +00005117void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005118 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005119 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005120 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5121 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5123
5124 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005125 case Primitive::kPrimInt:
5126 case Primitive::kPrimLong:
5127 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005128 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5130 break;
5131
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005132 case Primitive::kPrimFloat:
5133 case Primitive::kPrimDouble: {
5134 InvokeRuntimeCallingConvention calling_convention;
5135 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5136 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5137 locations->SetOut(calling_convention.GetReturnLocation(type));
5138
5139 break;
5140 }
5141
Serban Constantinescu02164b32014-11-13 14:05:07 +00005142 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005143 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005144 }
5145}
5146
5147void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5148 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005149
Serban Constantinescu02164b32014-11-13 14:05:07 +00005150 switch (type) {
5151 case Primitive::kPrimInt:
5152 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005153 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005154 break;
5155 }
5156
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005157 case Primitive::kPrimFloat:
5158 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005159 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5160 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005161 if (type == Primitive::kPrimFloat) {
5162 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5163 } else {
5164 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5165 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005166 break;
5167 }
5168
Serban Constantinescu02164b32014-11-13 14:05:07 +00005169 default:
5170 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005171 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005172 }
5173}
5174
Calin Juravle27df7582015-04-17 19:12:31 +01005175void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5176 memory_barrier->SetLocations(nullptr);
5177}
5178
5179void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005180 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005181}
5182
Alexandre Rames5319def2014-10-23 10:03:10 +01005183void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5185 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005186 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005187}
5188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005190 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005191}
5192
5193void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5194 instruction->SetLocations(nullptr);
5195}
5196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005197void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005198 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005199}
5200
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005201void LocationsBuilderARM64::VisitRor(HRor* ror) {
5202 HandleBinaryOp(ror);
5203}
5204
5205void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5206 HandleBinaryOp(ror);
5207}
5208
Serban Constantinescu02164b32014-11-13 14:05:07 +00005209void LocationsBuilderARM64::VisitShl(HShl* shl) {
5210 HandleShift(shl);
5211}
5212
5213void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5214 HandleShift(shl);
5215}
5216
5217void LocationsBuilderARM64::VisitShr(HShr* shr) {
5218 HandleShift(shr);
5219}
5220
5221void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5222 HandleShift(shr);
5223}
5224
Alexandre Rames5319def2014-10-23 10:03:10 +01005225void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005226 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005227}
5228
5229void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005230 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005231}
5232
Alexandre Rames67555f72014-11-18 10:55:16 +00005233void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005234 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005235}
5236
5237void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005238 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005239}
5240
5241void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005242 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005243}
5244
Alexandre Rames67555f72014-11-18 10:55:16 +00005245void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005246 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005247}
5248
Calin Juravlee460d1d2015-09-29 04:52:17 +01005249void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5250 HUnresolvedInstanceFieldGet* instruction) {
5251 FieldAccessCallingConventionARM64 calling_convention;
5252 codegen_->CreateUnresolvedFieldLocationSummary(
5253 instruction, instruction->GetFieldType(), calling_convention);
5254}
5255
5256void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5257 HUnresolvedInstanceFieldGet* instruction) {
5258 FieldAccessCallingConventionARM64 calling_convention;
5259 codegen_->GenerateUnresolvedFieldAccess(instruction,
5260 instruction->GetFieldType(),
5261 instruction->GetFieldIndex(),
5262 instruction->GetDexPc(),
5263 calling_convention);
5264}
5265
5266void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5267 HUnresolvedInstanceFieldSet* instruction) {
5268 FieldAccessCallingConventionARM64 calling_convention;
5269 codegen_->CreateUnresolvedFieldLocationSummary(
5270 instruction, instruction->GetFieldType(), calling_convention);
5271}
5272
5273void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5274 HUnresolvedInstanceFieldSet* instruction) {
5275 FieldAccessCallingConventionARM64 calling_convention;
5276 codegen_->GenerateUnresolvedFieldAccess(instruction,
5277 instruction->GetFieldType(),
5278 instruction->GetFieldIndex(),
5279 instruction->GetDexPc(),
5280 calling_convention);
5281}
5282
5283void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5284 HUnresolvedStaticFieldGet* instruction) {
5285 FieldAccessCallingConventionARM64 calling_convention;
5286 codegen_->CreateUnresolvedFieldLocationSummary(
5287 instruction, instruction->GetFieldType(), calling_convention);
5288}
5289
5290void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5291 HUnresolvedStaticFieldGet* instruction) {
5292 FieldAccessCallingConventionARM64 calling_convention;
5293 codegen_->GenerateUnresolvedFieldAccess(instruction,
5294 instruction->GetFieldType(),
5295 instruction->GetFieldIndex(),
5296 instruction->GetDexPc(),
5297 calling_convention);
5298}
5299
5300void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5301 HUnresolvedStaticFieldSet* instruction) {
5302 FieldAccessCallingConventionARM64 calling_convention;
5303 codegen_->CreateUnresolvedFieldLocationSummary(
5304 instruction, instruction->GetFieldType(), calling_convention);
5305}
5306
5307void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5308 HUnresolvedStaticFieldSet* instruction) {
5309 FieldAccessCallingConventionARM64 calling_convention;
5310 codegen_->GenerateUnresolvedFieldAccess(instruction,
5311 instruction->GetFieldType(),
5312 instruction->GetFieldIndex(),
5313 instruction->GetDexPc(),
5314 calling_convention);
5315}
5316
Alexandre Rames5319def2014-10-23 10:03:10 +01005317void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005318 LocationSummary* locations =
5319 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005320 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005321}
5322
5323void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005324 HBasicBlock* block = instruction->GetBlock();
5325 if (block->GetLoopInformation() != nullptr) {
5326 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5327 // The back edge will generate the suspend check.
5328 return;
5329 }
5330 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5331 // The goto will generate the suspend check.
5332 return;
5333 }
5334 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005335}
5336
Alexandre Rames67555f72014-11-18 10:55:16 +00005337void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5338 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005340 InvokeRuntimeCallingConvention calling_convention;
5341 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5342}
5343
5344void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005345 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005346 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005347}
5348
5349void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5350 LocationSummary* locations =
5351 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5352 Primitive::Type input_type = conversion->GetInputType();
5353 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005354 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005355 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5356 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5357 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5358 }
5359
Alexandre Rames542361f2015-01-29 16:57:31 +00005360 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005361 locations->SetInAt(0, Location::RequiresFpuRegister());
5362 } else {
5363 locations->SetInAt(0, Location::RequiresRegister());
5364 }
5365
Alexandre Rames542361f2015-01-29 16:57:31 +00005366 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5368 } else {
5369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5370 }
5371}
5372
5373void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5374 Primitive::Type result_type = conversion->GetResultType();
5375 Primitive::Type input_type = conversion->GetInputType();
5376
5377 DCHECK_NE(input_type, result_type);
5378
Alexandre Rames542361f2015-01-29 16:57:31 +00005379 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005380 int result_size = Primitive::ComponentSize(result_type);
5381 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005382 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005383 Register output = OutputRegister(conversion);
5384 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005385 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005386 // 'int' values are used directly as W registers, discarding the top
5387 // bits, so we don't need to sign-extend and can just perform a move.
5388 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5389 // top 32 bits of the target register. We theoretically could leave those
5390 // bits unchanged, but we would have to make sure that no code uses a
5391 // 32bit input value as a 64bit value assuming that the top 32 bits are
5392 // zero.
5393 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005394 } else if (result_type == Primitive::kPrimChar ||
5395 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5396 __ Ubfx(output,
5397 output.IsX() ? source.X() : source.W(),
5398 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005399 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005400 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005401 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005402 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005403 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005404 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005405 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5406 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005407 } else if (Primitive::IsFloatingPointType(result_type) &&
5408 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005409 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5410 } else {
5411 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5412 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005413 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005414}
Alexandre Rames67555f72014-11-18 10:55:16 +00005415
Serban Constantinescu02164b32014-11-13 14:05:07 +00005416void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5417 HandleShift(ushr);
5418}
5419
5420void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5421 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005422}
5423
5424void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5425 HandleBinaryOp(instruction);
5426}
5427
5428void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5429 HandleBinaryOp(instruction);
5430}
5431
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005432void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005433 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005434 LOG(FATAL) << "Unreachable";
5435}
5436
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005437void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005438 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005439 LOG(FATAL) << "Unreachable";
5440}
5441
Mark Mendellfe57faa2015-09-18 09:26:15 -04005442// Simple implementation of packed switch - generate cascaded compare/jumps.
5443void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5444 LocationSummary* locations =
5445 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5446 locations->SetInAt(0, Location::RequiresRegister());
5447}
5448
5449void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5450 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005451 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005452 Register value_reg = InputRegisterAt(switch_instr, 0);
5453 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5454
Zheng Xu3927c8b2015-11-18 17:46:25 +08005455 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005456 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005457 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5458 // make sure we don't emit it if the target may run out of range.
5459 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5460 // ranges and emit the tables only as required.
5461 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005462
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005463 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005464 // Current instruction id is an upper bound of the number of HIRs in the graph.
5465 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5466 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005467 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5468 Register temp = temps.AcquireW();
5469 __ Subs(temp, value_reg, Operand(lower_bound));
5470
Zheng Xu3927c8b2015-11-18 17:46:25 +08005471 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005472 // Jump to successors[0] if value == lower_bound.
5473 __ B(eq, codegen_->GetLabelOf(successors[0]));
5474 int32_t last_index = 0;
5475 for (; num_entries - last_index > 2; last_index += 2) {
5476 __ Subs(temp, temp, Operand(2));
5477 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5478 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5479 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5480 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5481 }
5482 if (num_entries - last_index == 2) {
5483 // The last missing case_value.
5484 __ Cmp(temp, Operand(1));
5485 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005486 }
5487
5488 // And the default for any other value.
5489 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5490 __ B(codegen_->GetLabelOf(default_block));
5491 }
5492 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005493 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005494
5495 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5496
5497 // Below instructions should use at most one blocked register. Since there are two blocked
5498 // registers, we are free to block one.
5499 Register temp_w = temps.AcquireW();
5500 Register index;
5501 // Remove the bias.
5502 if (lower_bound != 0) {
5503 index = temp_w;
5504 __ Sub(index, value_reg, Operand(lower_bound));
5505 } else {
5506 index = value_reg;
5507 }
5508
5509 // Jump to default block if index is out of the range.
5510 __ Cmp(index, Operand(num_entries));
5511 __ B(hs, codegen_->GetLabelOf(default_block));
5512
5513 // In current VIXL implementation, it won't require any blocked registers to encode the
5514 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5515 // register pressure.
5516 Register table_base = temps.AcquireX();
5517 // Load jump offset from the table.
5518 __ Adr(table_base, jump_table->GetTableStartLabel());
5519 Register jump_offset = temp_w;
5520 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5521
5522 // Jump to target block by branching to table_base(pc related) + offset.
5523 Register target_address = table_base;
5524 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5525 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005526 }
5527}
5528
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005529void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5530 HInstruction* instruction,
5531 Location out,
5532 uint32_t offset,
5533 Location maybe_temp,
5534 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005535 Primitive::Type type = Primitive::kPrimNot;
5536 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005537 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005538 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005539 Register temp_reg = RegisterFrom(maybe_temp, type);
5540 if (kUseBakerReadBarrier) {
5541 // Load with fast path based Baker's read barrier.
5542 // /* HeapReference<Object> */ out = *(out + offset)
5543 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5544 out,
5545 out_reg,
5546 offset,
5547 temp_reg,
5548 /* needs_null_check */ false,
5549 /* use_load_acquire */ false);
5550 } else {
5551 // Load with slow path based read barrier.
5552 // Save the value of `out` into `maybe_temp` before overwriting it
5553 // in the following move operation, as we will need it for the
5554 // read barrier below.
5555 __ Mov(temp_reg, out_reg);
5556 // /* HeapReference<Object> */ out = *(out + offset)
5557 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5558 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5559 }
5560 } else {
5561 // Plain load with no read barrier.
5562 // /* HeapReference<Object> */ out = *(out + offset)
5563 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5564 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5565 }
5566}
5567
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005568void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5569 HInstruction* instruction,
5570 Location out,
5571 Location obj,
5572 uint32_t offset,
5573 Location maybe_temp,
5574 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005575 Primitive::Type type = Primitive::kPrimNot;
5576 Register out_reg = RegisterFrom(out, type);
5577 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005578 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005579 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005580 if (kUseBakerReadBarrier) {
5581 // Load with fast path based Baker's read barrier.
5582 Register temp_reg = RegisterFrom(maybe_temp, type);
5583 // /* HeapReference<Object> */ out = *(obj + offset)
5584 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5585 out,
5586 obj_reg,
5587 offset,
5588 temp_reg,
5589 /* needs_null_check */ false,
5590 /* use_load_acquire */ false);
5591 } else {
5592 // Load with slow path based read barrier.
5593 // /* HeapReference<Object> */ out = *(obj + offset)
5594 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5595 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5596 }
5597 } else {
5598 // Plain load with no read barrier.
5599 // /* HeapReference<Object> */ out = *(obj + offset)
5600 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5601 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5602 }
5603}
5604
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005605void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5606 HInstruction* instruction,
5607 Location root,
5608 Register obj,
5609 uint32_t offset,
5610 vixl::aarch64::Label* fixup_label,
5611 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005612 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005613 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005614 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005615 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005616 if (kUseBakerReadBarrier) {
5617 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5618 // Baker's read barrier are used:
5619 //
5620 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005621 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5622 // if (temp != null) {
5623 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005624 // }
5625
5626 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 if (fixup_label == nullptr) {
5628 __ Ldr(root_reg, MemOperand(obj, offset));
5629 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005630 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005631 }
Roland Levillain44015862016-01-22 11:47:17 +00005632 static_assert(
5633 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5634 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5635 "have different sizes.");
5636 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5637 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5638 "have different sizes.");
5639
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005640 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005641
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005642 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5643 SlowPathCodeARM64* slow_path =
5644 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5645 root,
5646 LocationFrom(temp));
5647 codegen_->AddSlowPath(slow_path);
5648 const int32_t entry_point_offset =
5649 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5650 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5651 // Loading the entrypoint does not require a load acquire since it is only changed when
5652 // threads are suspended or running a checkpoint.
5653 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5654 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5655 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005656 __ Cbnz(temp, slow_path->GetEntryLabel());
5657 __ Bind(slow_path->GetExitLabel());
5658 } else {
5659 // GC root loaded through a slow path for read barriers other
5660 // than Baker's.
5661 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005662 if (fixup_label == nullptr) {
5663 __ Add(root_reg.X(), obj.X(), offset);
5664 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005665 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005666 }
Roland Levillain44015862016-01-22 11:47:17 +00005667 // /* mirror::Object* */ root = root->Read()
5668 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5669 }
5670 } else {
5671 // Plain GC root load with no read barrier.
5672 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 if (fixup_label == nullptr) {
5674 __ Ldr(root_reg, MemOperand(obj, offset));
5675 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005676 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005677 }
Roland Levillain44015862016-01-22 11:47:17 +00005678 // Note that GC roots are not affected by heap poisoning, thus we
5679 // do not have to unpoison `root_reg` here.
5680 }
5681}
5682
5683void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5684 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005685 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005686 uint32_t offset,
5687 Register temp,
5688 bool needs_null_check,
5689 bool use_load_acquire) {
5690 DCHECK(kEmitCompilerReadBarrier);
5691 DCHECK(kUseBakerReadBarrier);
5692
5693 // /* HeapReference<Object> */ ref = *(obj + offset)
5694 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005695 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005696 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5697 ref,
5698 obj,
5699 offset,
5700 no_index,
5701 no_scale_factor,
5702 temp,
5703 needs_null_check,
5704 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005705}
5706
5707void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5708 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005709 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005710 uint32_t data_offset,
5711 Location index,
5712 Register temp,
5713 bool needs_null_check) {
5714 DCHECK(kEmitCompilerReadBarrier);
5715 DCHECK(kUseBakerReadBarrier);
5716
5717 // Array cells are never volatile variables, therefore array loads
5718 // never use Load-Acquire instructions on ARM64.
5719 const bool use_load_acquire = false;
5720
Roland Levillainbfea3352016-06-23 13:48:47 +01005721 static_assert(
5722 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5723 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005724 // /* HeapReference<Object> */ ref =
5725 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005726 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5727 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5728 ref,
5729 obj,
5730 data_offset,
5731 index,
5732 scale_factor,
5733 temp,
5734 needs_null_check,
5735 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005736}
5737
5738void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5739 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005740 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005741 uint32_t offset,
5742 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005743 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005744 Register temp,
5745 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005746 bool use_load_acquire,
5747 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005748 DCHECK(kEmitCompilerReadBarrier);
5749 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005750 // If we are emitting an array load, we should not be using a
5751 // Load Acquire instruction. In other words:
5752 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5753 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005754
5755 MacroAssembler* masm = GetVIXLAssembler();
5756 UseScratchRegisterScope temps(masm);
5757
5758 // In slow path based read barriers, the read barrier call is
5759 // inserted after the original load. However, in fast path based
5760 // Baker's read barriers, we need to perform the load of
5761 // mirror::Object::monitor_ *before* the original reference load.
5762 // This load-load ordering is required by the read barrier.
5763 // The fast path/slow path (for Baker's algorithm) should look like:
5764 //
5765 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5766 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5767 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005768 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005769 // if (is_gray) {
5770 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5771 // }
5772 //
5773 // Note: the original implementation in ReadBarrier::Barrier is
5774 // slightly more complex as it performs additional checks that we do
5775 // not do here for performance reasons.
5776
5777 Primitive::Type type = Primitive::kPrimNot;
5778 Register ref_reg = RegisterFrom(ref, type);
5779 DCHECK(obj.IsW());
5780 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5781
Artem Serov914d7a82017-02-07 14:33:49 +00005782 {
5783 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5784 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5785 // /* int32_t */ monitor = obj->monitor_
5786 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5787 if (needs_null_check) {
5788 MaybeRecordImplicitNullCheck(instruction);
5789 }
Roland Levillain44015862016-01-22 11:47:17 +00005790 }
5791 // /* LockWord */ lock_word = LockWord(monitor)
5792 static_assert(sizeof(LockWord) == sizeof(int32_t),
5793 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005794
Vladimir Marko877a0332016-07-11 19:30:56 +01005795 // Introduce a dependency on the lock_word including rb_state,
5796 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005797 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005798 // `obj` is unchanged by this operation, but its value now depends
5799 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005800 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005801
5802 // The actual reference load.
5803 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005804 // Load types involving an "index": ArrayGet,
5805 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5806 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005807 if (use_load_acquire) {
5808 // UnsafeGetObjectVolatile intrinsic case.
5809 // Register `index` is not an index in an object array, but an
5810 // offset to an object reference field within object `obj`.
5811 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5812 DCHECK(instruction->GetLocations()->Intrinsified());
5813 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5814 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005815 DCHECK_EQ(offset, 0u);
5816 DCHECK_EQ(scale_factor, 0u);
5817 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005818 // /* HeapReference<Object> */ ref = *(obj + index)
5819 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5820 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005821 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005822 // ArrayGet and UnsafeGetObject intrinsics cases.
5823 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5824 if (index.IsConstant()) {
5825 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5826 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5827 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005828 Register temp3 = temps.AcquireW();
5829 __ Add(temp3, obj, offset);
5830 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5831 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005832 }
Roland Levillain44015862016-01-22 11:47:17 +00005833 }
Roland Levillain44015862016-01-22 11:47:17 +00005834 } else {
5835 // /* HeapReference<Object> */ ref = *(obj + offset)
5836 MemOperand field = HeapOperand(obj, offset);
5837 if (use_load_acquire) {
5838 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5839 } else {
5840 Load(type, ref_reg, field);
5841 }
5842 }
5843
5844 // Object* ref = ref_addr->AsMirrorPtr()
5845 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5846
Vladimir Marko953437b2016-08-24 08:30:46 +00005847 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005848 SlowPathCodeARM64* slow_path;
5849 if (always_update_field) {
5850 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5851 // address of the form `obj + field_offset`, where `obj` is a
5852 // register and `field_offset` is a register. Thus `offset` and
5853 // `scale_factor` above are expected to be null in this code path.
5854 DCHECK_EQ(offset, 0u);
5855 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5856 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5857 instruction, ref, obj, /* field_offset */ index, temp);
5858 } else {
5859 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5860 }
Roland Levillain44015862016-01-22 11:47:17 +00005861 AddSlowPath(slow_path);
5862
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005863 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005864 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005865 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005866 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5867 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005868 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005869 __ Bind(slow_path->GetExitLabel());
5870}
5871
5872void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5873 Location out,
5874 Location ref,
5875 Location obj,
5876 uint32_t offset,
5877 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005878 DCHECK(kEmitCompilerReadBarrier);
5879
Roland Levillain44015862016-01-22 11:47:17 +00005880 // Insert a slow path based read barrier *after* the reference load.
5881 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005882 // If heap poisoning is enabled, the unpoisoning of the loaded
5883 // reference will be carried out by the runtime within the slow
5884 // path.
5885 //
5886 // Note that `ref` currently does not get unpoisoned (when heap
5887 // poisoning is enabled), which is alright as the `ref` argument is
5888 // not used by the artReadBarrierSlow entry point.
5889 //
5890 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5891 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5892 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5893 AddSlowPath(slow_path);
5894
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005895 __ B(slow_path->GetEntryLabel());
5896 __ Bind(slow_path->GetExitLabel());
5897}
5898
Roland Levillain44015862016-01-22 11:47:17 +00005899void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5900 Location out,
5901 Location ref,
5902 Location obj,
5903 uint32_t offset,
5904 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005905 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005906 // Baker's read barriers shall be handled by the fast path
5907 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5908 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005909 // If heap poisoning is enabled, unpoisoning will be taken care of
5910 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005911 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005912 } else if (kPoisonHeapReferences) {
5913 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5914 }
5915}
5916
Roland Levillain44015862016-01-22 11:47:17 +00005917void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5918 Location out,
5919 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005920 DCHECK(kEmitCompilerReadBarrier);
5921
Roland Levillain44015862016-01-22 11:47:17 +00005922 // Insert a slow path based read barrier *after* the GC root load.
5923 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005924 // Note that GC roots are not affected by heap poisoning, so we do
5925 // not need to do anything special for this here.
5926 SlowPathCodeARM64* slow_path =
5927 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5928 AddSlowPath(slow_path);
5929
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005930 __ B(slow_path->GetEntryLabel());
5931 __ Bind(slow_path->GetExitLabel());
5932}
5933
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005934void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5935 LocationSummary* locations =
5936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5937 locations->SetInAt(0, Location::RequiresRegister());
5938 locations->SetOut(Location::RequiresRegister());
5939}
5940
5941void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5942 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005943 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005944 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005945 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005946 __ Ldr(XRegisterFrom(locations->Out()),
5947 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005948 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005949 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005950 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005951 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5952 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005953 __ Ldr(XRegisterFrom(locations->Out()),
5954 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005955 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005956}
5957
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005958static void PatchJitRootUse(uint8_t* code,
5959 const uint8_t* roots_data,
5960 vixl::aarch64::Literal<uint32_t>* literal,
5961 uint64_t index_in_table) {
5962 uint32_t literal_offset = literal->GetOffset();
5963 uintptr_t address =
5964 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5965 uint8_t* data = code + literal_offset;
5966 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5967}
5968
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005969void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5970 for (const auto& entry : jit_string_patches_) {
5971 const auto& it = jit_string_roots_.find(entry.first);
5972 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005973 PatchJitRootUse(code, roots_data, entry.second, it->second);
5974 }
5975 for (const auto& entry : jit_class_patches_) {
5976 const auto& it = jit_class_roots_.find(entry.first);
5977 DCHECK(it != jit_class_roots_.end());
5978 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005979 }
5980}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005981
Alexandre Rames67555f72014-11-18 10:55:16 +00005982#undef __
5983#undef QUICK_ENTRY_POINT
5984
Alexandre Rames5319def2014-10-23 10:03:10 +01005985} // namespace arm64
5986} // namespace art