blob: a3732efeb7521053833d55cf0d4b42d9af67d9f6 [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 Levillain54f869e2017-03-06 13:54:11 +0000636// Abstract base class for read barrier slow paths marking a reference
637// `ref`.
638//
639// Argument `entrypoint` must be a register location holding the read
640// barrier marking runtime entry point to be invoked.
641class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
642 protected:
643 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
644 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
645 DCHECK(kEmitCompilerReadBarrier);
646 }
647
648 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
649
650 // Generate assembly code calling the read barrier marking runtime
651 // entry point (ReadBarrierMarkRegX).
652 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
653 // No need to save live registers; it's taken care of by the
654 // entrypoint. Also, there is no need to update the stack mask,
655 // as this runtime call will not trigger a garbage collection.
656 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
657 DCHECK_NE(ref_.reg(), LR);
658 DCHECK_NE(ref_.reg(), WSP);
659 DCHECK_NE(ref_.reg(), WZR);
660 // IP0 is used internally by the ReadBarrierMarkRegX entry point
661 // as a temporary, it cannot be the entry point's input/output.
662 DCHECK_NE(ref_.reg(), IP0);
663 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
664 // "Compact" slow path, saving two moves.
665 //
666 // Instead of using the standard runtime calling convention (input
667 // and output in W0):
668 //
669 // W0 <- ref
670 // W0 <- ReadBarrierMark(W0)
671 // ref <- W0
672 //
673 // we just use rX (the register containing `ref`) as input and output
674 // of a dedicated entrypoint:
675 //
676 // rX <- ReadBarrierMarkRegX(rX)
677 //
678 if (entrypoint_.IsValid()) {
679 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
680 __ Blr(XRegisterFrom(entrypoint_));
681 } else {
682 // Entrypoint is not already loaded, load from the thread.
683 int32_t entry_point_offset =
684 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
685 // This runtime call does not require a stack map.
686 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
687 }
688 }
689
690 // The location (register) of the marked object reference.
691 const Location ref_;
692
693 // The location of the entrypoint if it is already loaded.
694 const Location entrypoint_;
695
696 private:
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
698};
699
Roland Levillain47b3ab22017-02-27 14:31:35 +0000700// Slow path marking an object reference `ref` during a read
701// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000702// reference does not get updated by this slow path after marking.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000703//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000704// This means that after the execution of this slow path, `ref` will
705// always be up-to-date, but `obj.field` may not; i.e., after the
706// flip, `ref` will be a to-space reference, but `obj.field` will
707// probably still be a from-space reference (unless it gets updated by
708// another thread, or if another thread installed another object
709// reference (different from `ref`) in `obj.field`).
710//
711// If `entrypoint` is a valid location it is assumed to already be
712// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000713// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000714class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000715 public:
716 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
717 Location ref,
718 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000719 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000720 DCHECK(kEmitCompilerReadBarrier);
721 }
722
Roland Levillain47b3ab22017-02-27 14:31:35 +0000723 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000724
Roland Levillain47b3ab22017-02-27 14:31:35 +0000725 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
726 LocationSummary* locations = instruction_->GetLocations();
727 DCHECK(locations->CanCall());
728 DCHECK(ref_.IsRegister()) << ref_;
729 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000730 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
731 << "Unexpected instruction in read barrier marking slow path: "
732 << instruction_->DebugName();
733
734 __ Bind(GetEntryLabel());
735 GenerateReadBarrierMarkRuntimeCall(codegen);
736 __ B(GetExitLabel());
737 }
738
739 private:
740 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
741};
742
743// Slow path loading `obj`'s lock word, loading a reference from
744// object `*(obj + offset + (index << scale_factor))` into `ref`, and
745// marking `ref` if `obj` is gray according to the lock word (Baker
746// read barrier). The field `obj.field` in the object `obj` holding
747// this reference does not get updated by this slow path after marking
748// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
749// below for that).
750//
751// This means that after the execution of this slow path, `ref` will
752// always be up-to-date, but `obj.field` may not; i.e., after the
753// flip, `ref` will be a to-space reference, but `obj.field` will
754// probably still be a from-space reference (unless it gets updated by
755// another thread, or if another thread installed another object
756// reference (different from `ref`) in `obj.field`).
757//
758// Argument `entrypoint` must be a register location holding the read
759// barrier marking runtime entry point to be invoked.
760class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
761 public:
762 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
763 Location ref,
764 Register obj,
765 uint32_t offset,
766 Location index,
767 size_t scale_factor,
768 bool needs_null_check,
769 bool use_load_acquire,
770 Register temp,
771 Location entrypoint)
772 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
773 obj_(obj),
774 offset_(offset),
775 index_(index),
776 scale_factor_(scale_factor),
777 needs_null_check_(needs_null_check),
778 use_load_acquire_(use_load_acquire),
779 temp_(temp) {
780 DCHECK(kEmitCompilerReadBarrier);
781 DCHECK(kUseBakerReadBarrier);
782 }
783
784 const char* GetDescription() const OVERRIDE {
785 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
786 }
787
788 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
789 LocationSummary* locations = instruction_->GetLocations();
790 DCHECK(locations->CanCall());
791 DCHECK(ref_.IsRegister()) << ref_;
792 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
793 DCHECK(obj_.IsW());
794 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000795 DCHECK(instruction_->IsInstanceFieldGet() ||
796 instruction_->IsStaticFieldGet() ||
797 instruction_->IsArrayGet() ||
798 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799 instruction_->IsInstanceOf() ||
800 instruction_->IsCheckCast() ||
801 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
802 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
803 << "Unexpected instruction in read barrier marking slow path: "
804 << instruction_->DebugName();
805 // The read barrier instrumentation of object ArrayGet
806 // instructions does not support the HIntermediateAddress
807 // instruction.
808 DCHECK(!(instruction_->IsArrayGet() &&
809 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
810
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 // Temporary register `temp_`, used to store the lock word, must
812 // not be IP0 nor IP1, as we may use them to emit the reference
813 // load (in the call to GenerateRawReferenceLoad below), and we
814 // need the lock word to still be in `temp_` after the reference
815 // load.
816 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
817 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
818
Roland Levillain47b3ab22017-02-27 14:31:35 +0000819 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000820
821 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
822 // inserted after the original load. However, in fast path based
823 // Baker's read barriers, we need to perform the load of
824 // mirror::Object::monitor_ *before* the original reference load.
825 // This load-load ordering is required by the read barrier.
826 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000827 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000828 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
829 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
830 // HeapReference<mirror::Object> ref = *src; // Original reference load.
831 // bool is_gray = (rb_state == ReadBarrier::GrayState());
832 // if (is_gray) {
833 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
834 // }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000835 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000836 // Note: the original implementation in ReadBarrier::Barrier is
837 // slightly more complex as it performs additional checks that we do
838 // not do here for performance reasons.
839
840 // /* int32_t */ monitor = obj->monitor_
841 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
842 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
843 if (needs_null_check_) {
844 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000845 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000846 // /* LockWord */ lock_word = LockWord(monitor)
847 static_assert(sizeof(LockWord) == sizeof(int32_t),
848 "art::LockWord and int32_t have different sizes.");
849
850 // Introduce a dependency on the lock_word including rb_state,
851 // to prevent load-load reordering, and without using
852 // a memory barrier (which would be more expensive).
853 // `obj` is unchanged by this operation, but its value now depends
854 // on `temp`.
855 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
856
857 // The actual reference load.
858 // A possible implicit null check has already been handled above.
859 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
860 arm64_codegen->GenerateRawReferenceLoad(instruction_,
861 ref_,
862 obj_,
863 offset_,
864 index_,
865 scale_factor_,
866 /* needs_null_check */ false,
867 use_load_acquire_);
868
869 // Mark the object `ref` when `obj` is gray.
870 //
871 // if (rb_state == ReadBarrier::GrayState())
872 // ref = ReadBarrier::Mark(ref);
873 //
874 // Given the numeric representation, it's enough to check the low bit of the rb_state.
875 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
876 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
877 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
878 GenerateReadBarrierMarkRuntimeCall(codegen);
879
Roland Levillain47b3ab22017-02-27 14:31:35 +0000880 __ B(GetExitLabel());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000881 }
882
Roland Levillain47b3ab22017-02-27 14:31:35 +0000883 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000884 // The register containing the object holding the marked object reference field.
885 Register obj_;
886 // The offset, index and scale factor to access the reference in `obj_`.
887 uint32_t offset_;
888 Location index_;
889 size_t scale_factor_;
890 // Is a null check required?
891 bool needs_null_check_;
892 // Should this reference load use Load-Acquire semantics?
893 bool use_load_acquire_;
894 // A temporary register used to hold the lock word of `obj_`.
895 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000896
Roland Levillain54f869e2017-03-06 13:54:11 +0000897 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000898};
899
Roland Levillain54f869e2017-03-06 13:54:11 +0000900// Slow path loading `obj`'s lock word, loading a reference from
901// object `*(obj + offset + (index << scale_factor))` into `ref`, and
902// marking `ref` if `obj` is gray according to the lock word (Baker
903// read barrier). If needed, this slow path also atomically updates
904// the field `obj.field` in the object `obj` holding this reference
905// after marking (contrary to
906// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
907// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100908//
909// This means that after the execution of this slow path, both `ref`
910// and `obj.field` will be up-to-date; i.e., after the flip, both will
911// hold the same to-space reference (unless another thread installed
912// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000913//
Roland Levillain54f869e2017-03-06 13:54:11 +0000914// Argument `entrypoint` must be a register location holding the read
915// barrier marking runtime entry point to be invoked.
916class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
917 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100918 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
920 Location ref,
921 Register obj,
922 uint32_t offset,
923 Location index,
924 size_t scale_factor,
925 bool needs_null_check,
926 bool use_load_acquire,
927 Register temp,
928 Location entrypoint)
929 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100930 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000931 offset_(offset),
932 index_(index),
933 scale_factor_(scale_factor),
934 needs_null_check_(needs_null_check),
935 use_load_acquire_(use_load_acquire),
936 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100937 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000938 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100939 }
940
941 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000942 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100943 }
944
945 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
946 LocationSummary* locations = instruction_->GetLocations();
947 Register ref_reg = WRegisterFrom(ref_);
948 DCHECK(locations->CanCall());
949 DCHECK(ref_.IsRegister()) << ref_;
950 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000951 DCHECK(obj_.IsW());
952 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
953
954 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100955 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
956 << "Unexpected instruction in read barrier marking and field updating slow path: "
957 << instruction_->DebugName();
958 DCHECK(instruction_->GetLocations()->Intrinsified());
959 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000960 DCHECK_EQ(offset_, 0u);
961 DCHECK_EQ(scale_factor_, 0u);
962 DCHECK_EQ(use_load_acquire_, false);
963 // The location of the offset of the marked reference field within `obj_`.
964 Location field_offset = index_;
965 DCHECK(field_offset.IsRegister()) << field_offset;
966
967 // Temporary register `temp_`, used to store the lock word, must
968 // not be IP0 nor IP1, as we may use them to emit the reference
969 // load (in the call to GenerateRawReferenceLoad below), and we
970 // need the lock word to still be in `temp_` after the reference
971 // load.
972 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
973 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100974
975 __ Bind(GetEntryLabel());
976
Roland Levillain54f869e2017-03-06 13:54:11 +0000977 // /* int32_t */ monitor = obj->monitor_
978 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
979 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
980 if (needs_null_check_) {
981 codegen->MaybeRecordImplicitNullCheck(instruction_);
982 }
983 // /* LockWord */ lock_word = LockWord(monitor)
984 static_assert(sizeof(LockWord) == sizeof(int32_t),
985 "art::LockWord and int32_t have different sizes.");
986
987 // Introduce a dependency on the lock_word including rb_state,
988 // to prevent load-load reordering, and without using
989 // a memory barrier (which would be more expensive).
990 // `obj` is unchanged by this operation, but its value now depends
991 // on `temp`.
992 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
993
994 // The actual reference load.
995 // A possible implicit null check has already been handled above.
996 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
997 arm64_codegen->GenerateRawReferenceLoad(instruction_,
998 ref_,
999 obj_,
1000 offset_,
1001 index_,
1002 scale_factor_,
1003 /* needs_null_check */ false,
1004 use_load_acquire_);
1005
1006 // Mark the object `ref` when `obj` is gray.
1007 //
1008 // if (rb_state == ReadBarrier::GrayState())
1009 // ref = ReadBarrier::Mark(ref);
1010 //
1011 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1012 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1013 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1014 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1015
1016 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017 // Note that we cannot use IP to save the old reference, as IP is
1018 // used internally by the ReadBarrierMarkRegX entry point, and we
1019 // need the old reference after the call to that entry point.
1020 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1021 __ Mov(temp_.W(), ref_reg);
1022
Roland Levillain54f869e2017-03-06 13:54:11 +00001023 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001024
1025 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001027 //
1028 // Note that this field could also hold a different object, if
1029 // another thread had concurrently changed it. In that case, the
1030 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1031 // (CAS) operation below would abort the CAS, leaving the field
1032 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001033 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001034 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001035
1036 // Update the the holder's field atomically. This may fail if
1037 // mutator updates before us, but it's OK. This is achieved
1038 // using a strong compare-and-set (CAS) operation with relaxed
1039 // memory synchronization ordering, where the expected value is
1040 // the old reference and the desired value is the new reference.
1041
1042 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1043 UseScratchRegisterScope temps(masm);
1044
1045 // Convenience aliases.
1046 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001047 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001048 Register expected = temp_.W();
1049 Register value = ref_reg;
1050 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1051 Register tmp_value = temps.AcquireW(); // Value in memory.
1052
1053 __ Add(tmp_ptr, base.X(), Operand(offset));
1054
1055 if (kPoisonHeapReferences) {
1056 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1057 if (value.Is(expected)) {
1058 // Do not poison `value`, as it is the same register as
1059 // `expected`, which has just been poisoned.
1060 } else {
1061 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1062 }
1063 }
1064
1065 // do {
1066 // tmp_value = [tmp_ptr] - expected;
1067 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1068
Roland Levillain24a4d112016-10-26 13:10:46 +01001069 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001070 __ Bind(&loop_head);
1071 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1072 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001073 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1075 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001076 __ B(&exit_loop);
1077 __ Bind(&comparison_failed);
1078 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 __ Bind(&exit_loop);
1080
1081 if (kPoisonHeapReferences) {
1082 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1083 if (value.Is(expected)) {
1084 // Do not unpoison `value`, as it is the same register as
1085 // `expected`, which has just been unpoisoned.
1086 } else {
1087 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1088 }
1089 }
1090
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001091 __ B(GetExitLabel());
1092 }
1093
1094 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095 // The register containing the object holding the marked object reference field.
1096 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001097 // The offset, index and scale factor to access the reference in `obj_`.
1098 uint32_t offset_;
1099 Location index_;
1100 size_t scale_factor_;
1101 // Is a null check required?
1102 bool needs_null_check_;
1103 // Should this reference load use Load-Acquire semantics?
1104 bool use_load_acquire_;
1105 // A temporary register used to hold the lock word of `obj_`; and
1106 // also to hold the original reference value, when the reference is
1107 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001108 const Register temp_;
1109
Roland Levillain54f869e2017-03-06 13:54:11 +00001110 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001111};
1112
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001113// Slow path generating a read barrier for a heap reference.
1114class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1115 public:
1116 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1117 Location out,
1118 Location ref,
1119 Location obj,
1120 uint32_t offset,
1121 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001122 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001123 out_(out),
1124 ref_(ref),
1125 obj_(obj),
1126 offset_(offset),
1127 index_(index) {
1128 DCHECK(kEmitCompilerReadBarrier);
1129 // If `obj` is equal to `out` or `ref`, it means the initial object
1130 // has been overwritten by (or after) the heap object reference load
1131 // to be instrumented, e.g.:
1132 //
1133 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001134 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001135 //
1136 // In that case, we have lost the information about the original
1137 // object, and the emitted read barrier cannot work properly.
1138 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1139 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1140 }
1141
1142 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1143 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1144 LocationSummary* locations = instruction_->GetLocations();
1145 Primitive::Type type = Primitive::kPrimNot;
1146 DCHECK(locations->CanCall());
1147 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001148 DCHECK(instruction_->IsInstanceFieldGet() ||
1149 instruction_->IsStaticFieldGet() ||
1150 instruction_->IsArrayGet() ||
1151 instruction_->IsInstanceOf() ||
1152 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +01001153 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +00001154 << "Unexpected instruction in read barrier for heap reference slow path: "
1155 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001156 // The read barrier instrumentation of object ArrayGet
1157 // instructions does not support the HIntermediateAddress
1158 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001159 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001160 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001161
1162 __ Bind(GetEntryLabel());
1163
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001164 SaveLiveRegisters(codegen, locations);
1165
1166 // We may have to change the index's value, but as `index_` is a
1167 // constant member (like other "inputs" of this slow path),
1168 // introduce a copy of it, `index`.
1169 Location index = index_;
1170 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001171 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001172 if (instruction_->IsArrayGet()) {
1173 // Compute the actual memory offset and store it in `index`.
1174 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1175 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1176 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1177 // We are about to change the value of `index_reg` (see the
1178 // calls to vixl::MacroAssembler::Lsl and
1179 // vixl::MacroAssembler::Mov below), but it has
1180 // not been saved by the previous call to
1181 // art::SlowPathCode::SaveLiveRegisters, as it is a
1182 // callee-save register --
1183 // art::SlowPathCode::SaveLiveRegisters does not consider
1184 // callee-save registers, as it has been designed with the
1185 // assumption that callee-save registers are supposed to be
1186 // handled by the called function. So, as a callee-save
1187 // register, `index_reg` _would_ eventually be saved onto
1188 // the stack, but it would be too late: we would have
1189 // changed its value earlier. Therefore, we manually save
1190 // it here into another freely available register,
1191 // `free_reg`, chosen of course among the caller-save
1192 // registers (as a callee-save `free_reg` register would
1193 // exhibit the same problem).
1194 //
1195 // Note we could have requested a temporary register from
1196 // the register allocator instead; but we prefer not to, as
1197 // this is a slow path, and we know we can find a
1198 // caller-save register that is available.
1199 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1200 __ Mov(free_reg.W(), index_reg);
1201 index_reg = free_reg;
1202 index = LocationFrom(index_reg);
1203 } else {
1204 // The initial register stored in `index_` has already been
1205 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1206 // (as it is not a callee-save register), so we can freely
1207 // use it.
1208 }
1209 // Shifting the index value contained in `index_reg` by the scale
1210 // factor (2) cannot overflow in practice, as the runtime is
1211 // unable to allocate object arrays with a size larger than
1212 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1213 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1214 static_assert(
1215 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1216 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1217 __ Add(index_reg, index_reg, Operand(offset_));
1218 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001219 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1220 // intrinsics, `index_` is not shifted by a scale factor of 2
1221 // (as in the case of ArrayGet), as it is actually an offset
1222 // to an object field within an object.
1223 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001224 DCHECK(instruction_->GetLocations()->Intrinsified());
1225 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1226 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1227 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001228 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001229 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001230 }
1231 }
1232
1233 // We're moving two or three locations to locations that could
1234 // overlap, so we need a parallel move resolver.
1235 InvokeRuntimeCallingConvention calling_convention;
1236 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1237 parallel_move.AddMove(ref_,
1238 LocationFrom(calling_convention.GetRegisterAt(0)),
1239 type,
1240 nullptr);
1241 parallel_move.AddMove(obj_,
1242 LocationFrom(calling_convention.GetRegisterAt(1)),
1243 type,
1244 nullptr);
1245 if (index.IsValid()) {
1246 parallel_move.AddMove(index,
1247 LocationFrom(calling_convention.GetRegisterAt(2)),
1248 Primitive::kPrimInt,
1249 nullptr);
1250 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1251 } else {
1252 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1253 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1254 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001255 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001256 instruction_,
1257 instruction_->GetDexPc(),
1258 this);
1259 CheckEntrypointTypes<
1260 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1261 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1262
1263 RestoreLiveRegisters(codegen, locations);
1264
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001265 __ B(GetExitLabel());
1266 }
1267
1268 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1269
1270 private:
1271 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001272 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1273 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001274 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1275 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1276 return Register(VIXLRegCodeFromART(i), kXRegSize);
1277 }
1278 }
1279 // We shall never fail to find a free caller-save register, as
1280 // there are more than two core caller-save registers on ARM64
1281 // (meaning it is possible to find one which is different from
1282 // `ref` and `obj`).
1283 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1284 LOG(FATAL) << "Could not find a free register";
1285 UNREACHABLE();
1286 }
1287
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001288 const Location out_;
1289 const Location ref_;
1290 const Location obj_;
1291 const uint32_t offset_;
1292 // An additional location containing an index to an array.
1293 // Only used for HArrayGet and the UnsafeGetObject &
1294 // UnsafeGetObjectVolatile intrinsics.
1295 const Location index_;
1296
1297 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1298};
1299
1300// Slow path generating a read barrier for a GC root.
1301class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1302 public:
1303 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001304 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001305 DCHECK(kEmitCompilerReadBarrier);
1306 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001307
1308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1309 LocationSummary* locations = instruction_->GetLocations();
1310 Primitive::Type type = Primitive::kPrimNot;
1311 DCHECK(locations->CanCall());
1312 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001313 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1314 << "Unexpected instruction in read barrier for GC root slow path: "
1315 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001316
1317 __ Bind(GetEntryLabel());
1318 SaveLiveRegisters(codegen, locations);
1319
1320 InvokeRuntimeCallingConvention calling_convention;
1321 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1322 // The argument of the ReadBarrierForRootSlow is not a managed
1323 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1324 // thus we need a 64-bit move here, and we cannot use
1325 //
1326 // arm64_codegen->MoveLocation(
1327 // LocationFrom(calling_convention.GetRegisterAt(0)),
1328 // root_,
1329 // type);
1330 //
1331 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1332 // reference type (`Primitive::kPrimNot`).
1333 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001334 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335 instruction_,
1336 instruction_->GetDexPc(),
1337 this);
1338 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1339 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1340
1341 RestoreLiveRegisters(codegen, locations);
1342 __ B(GetExitLabel());
1343 }
1344
1345 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1346
1347 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001348 const Location out_;
1349 const Location root_;
1350
1351 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1352};
1353
Alexandre Rames5319def2014-10-23 10:03:10 +01001354#undef __
1355
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001356Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 Location next_location;
1358 if (type == Primitive::kPrimVoid) {
1359 LOG(FATAL) << "Unreachable type " << type;
1360 }
1361
Alexandre Rames542361f2015-01-29 16:57:31 +00001362 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001363 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1364 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001365 } else if (!Primitive::IsFloatingPointType(type) &&
1366 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1368 } else {
1369 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001370 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1371 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001372 }
1373
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001374 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001375 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 return next_location;
1377}
1378
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001379Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001380 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001381}
1382
Serban Constantinescu579885a2015-02-22 20:51:33 +00001383CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1384 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001385 const CompilerOptions& compiler_options,
1386 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 : CodeGenerator(graph,
1388 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001389 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001390 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 callee_saved_core_registers.GetList(),
1392 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001393 compiler_options,
1394 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001395 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001396 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001397 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001398 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001399 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001400 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001401 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001402 uint32_literals_(std::less<uint32_t>(),
1403 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001404 uint64_literals_(std::less<uint64_t>(),
1405 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001406 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1407 boot_image_string_patches_(StringReferenceValueComparator(),
1408 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1409 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001410 boot_image_type_patches_(TypeReferenceValueComparator(),
1411 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1412 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001413 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001414 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001415 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1416 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001417 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1418 jit_class_patches_(TypeReferenceValueComparator(),
1419 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001420 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001421 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001422}
Alexandre Rames5319def2014-10-23 10:03:10 +01001423
Alexandre Rames67555f72014-11-18 10:55:16 +00001424#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001425
Zheng Xu3927c8b2015-11-18 17:46:25 +08001426void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001427 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001428 jump_table->EmitTable(this);
1429 }
1430}
1431
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001432void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001433 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001434 // Ensure we emit the literal pool.
1435 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001436
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001437 CodeGenerator::Finalize(allocator);
1438}
1439
Zheng Xuad4450e2015-04-17 18:48:56 +08001440void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1441 // Note: There are 6 kinds of moves:
1442 // 1. constant -> GPR/FPR (non-cycle)
1443 // 2. constant -> stack (non-cycle)
1444 // 3. GPR/FPR -> GPR/FPR
1445 // 4. GPR/FPR -> stack
1446 // 5. stack -> GPR/FPR
1447 // 6. stack -> stack (non-cycle)
1448 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1449 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1450 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1451 // dependency.
1452 vixl_temps_.Open(GetVIXLAssembler());
1453}
1454
1455void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1456 vixl_temps_.Close();
1457}
1458
1459Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1460 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1461 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1462 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1463 Location scratch = GetScratchLocation(kind);
1464 if (!scratch.Equals(Location::NoLocation())) {
1465 return scratch;
1466 }
1467 // Allocate from VIXL temp registers.
1468 if (kind == Location::kRegister) {
1469 scratch = LocationFrom(vixl_temps_.AcquireX());
1470 } else {
1471 DCHECK(kind == Location::kFpuRegister);
1472 scratch = LocationFrom(vixl_temps_.AcquireD());
1473 }
1474 AddScratchLocation(scratch);
1475 return scratch;
1476}
1477
1478void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1479 if (loc.IsRegister()) {
1480 vixl_temps_.Release(XRegisterFrom(loc));
1481 } else {
1482 DCHECK(loc.IsFpuRegister());
1483 vixl_temps_.Release(DRegisterFrom(loc));
1484 }
1485 RemoveScratchLocation(loc);
1486}
1487
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001489 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001490 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001491}
1492
Alexandre Rames5319def2014-10-23 10:03:10 +01001493void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001494 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001495 __ Bind(&frame_entry_label_);
1496
Serban Constantinescu02164b32014-11-13 14:05:07 +00001497 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1498 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001499 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001500 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001501 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001502 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001503 {
1504 // Ensure that between load and RecordPcInfo there are no pools emitted.
1505 ExactAssemblyScope eas(GetVIXLAssembler(),
1506 kInstructionSize,
1507 CodeBufferCheckScope::kExactSize);
1508 __ ldr(wzr, MemOperand(temp, 0));
1509 RecordPcInfo(nullptr, 0);
1510 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001511 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001512
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001513 if (!HasEmptyFrame()) {
1514 int frame_size = GetFrameSize();
1515 // Stack layout:
1516 // sp[frame_size - 8] : lr.
1517 // ... : other preserved core registers.
1518 // ... : other preserved fp registers.
1519 // ... : reserved frame space.
1520 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001521
1522 // Save the current method if we need it. Note that we do not
1523 // do this in HCurrentMethod, as the instruction might have been removed
1524 // in the SSA graph.
1525 if (RequiresCurrentMethod()) {
1526 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001527 } else {
1528 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001529 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001530 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001531 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1532 frame_size - GetCoreSpillSize());
1533 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1534 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001535
1536 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1537 // Initialize should_deoptimize flag to 0.
1538 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1539 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1540 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001541 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001542}
1543
1544void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001545 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001546 if (!HasEmptyFrame()) {
1547 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001548 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1549 frame_size - FrameEntrySpillSize());
1550 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1551 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001552 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001553 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001554 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001555 __ Ret();
1556 GetAssembler()->cfi().RestoreState();
1557 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001558}
1559
Scott Wakeling97c72b72016-06-24 16:19:36 +01001560CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001561 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001562 return CPURegList(CPURegister::kRegister, kXRegSize,
1563 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001564}
1565
Scott Wakeling97c72b72016-06-24 16:19:36 +01001566CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001567 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1568 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001569 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1570 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001571}
1572
Alexandre Rames5319def2014-10-23 10:03:10 +01001573void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1574 __ Bind(GetLabelOf(block));
1575}
1576
Calin Juravle175dc732015-08-25 15:42:32 +01001577void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1578 DCHECK(location.IsRegister());
1579 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1580}
1581
Calin Juravlee460d1d2015-09-29 04:52:17 +01001582void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1583 if (location.IsRegister()) {
1584 locations->AddTemp(location);
1585 } else {
1586 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1587 }
1588}
1589
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001590void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001592 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001593 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001594 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001595 if (value_can_be_null) {
1596 __ Cbz(value, &done);
1597 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001598 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001599 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001600 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001601 if (value_can_be_null) {
1602 __ Bind(&done);
1603 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001604}
1605
David Brazdil58282f42016-01-14 12:45:10 +00001606void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001607 // Blocked core registers:
1608 // lr : Runtime reserved.
1609 // tr : Runtime reserved.
1610 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1611 // ip1 : VIXL core temp.
1612 // ip0 : VIXL core temp.
1613 //
1614 // Blocked fp registers:
1615 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001616 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1617 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001618 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001619 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001621
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001622 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001623 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001624 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001625 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001626
David Brazdil58282f42016-01-14 12:45:10 +00001627 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001628 // Stubs do not save callee-save floating point registers. If the graph
1629 // is debuggable, we need to deal with these registers differently. For
1630 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001631 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1632 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001633 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001634 }
1635 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001636}
1637
Alexandre Rames3e69f162014-12-10 10:36:50 +00001638size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1639 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1640 __ Str(reg, MemOperand(sp, stack_index));
1641 return kArm64WordSize;
1642}
1643
1644size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1645 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1646 __ Ldr(reg, MemOperand(sp, stack_index));
1647 return kArm64WordSize;
1648}
1649
1650size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1651 FPRegister reg = FPRegister(reg_id, kDRegSize);
1652 __ Str(reg, MemOperand(sp, stack_index));
1653 return kArm64WordSize;
1654}
1655
1656size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1657 FPRegister reg = FPRegister(reg_id, kDRegSize);
1658 __ Ldr(reg, MemOperand(sp, stack_index));
1659 return kArm64WordSize;
1660}
1661
Alexandre Rames5319def2014-10-23 10:03:10 +01001662void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001663 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001664}
1665
1666void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001667 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001668}
1669
Alexandre Rames67555f72014-11-18 10:55:16 +00001670void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001671 if (constant->IsIntConstant()) {
1672 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1673 } else if (constant->IsLongConstant()) {
1674 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1675 } else if (constant->IsNullConstant()) {
1676 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001677 } else if (constant->IsFloatConstant()) {
1678 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1679 } else {
1680 DCHECK(constant->IsDoubleConstant());
1681 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1682 }
1683}
1684
Alexandre Rames3e69f162014-12-10 10:36:50 +00001685
1686static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1687 DCHECK(constant.IsConstant());
1688 HConstant* cst = constant.GetConstant();
1689 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001690 // Null is mapped to a core W register, which we associate with kPrimInt.
1691 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001692 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1693 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1694 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1695}
1696
Roland Levillain558dea12017-01-27 19:40:44 +00001697// Allocate a scratch register from the VIXL pool, querying first into
1698// the floating-point register pool, and then the the core register
1699// pool. This is essentially a reimplementation of
1700// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1701// using a different allocation strategy.
1702static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1703 vixl::aarch64::UseScratchRegisterScope* temps,
1704 int size_in_bits) {
1705 return masm->GetScratchFPRegisterList()->IsEmpty()
1706 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1707 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1708}
1709
Calin Juravlee460d1d2015-09-29 04:52:17 +01001710void CodeGeneratorARM64::MoveLocation(Location destination,
1711 Location source,
1712 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001713 if (source.Equals(destination)) {
1714 return;
1715 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001716
1717 // A valid move can always be inferred from the destination and source
1718 // locations. When moving from and to a register, the argument type can be
1719 // used to generate 32bit instead of 64bit moves. In debug mode we also
1720 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001721 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001722
1723 if (destination.IsRegister() || destination.IsFpuRegister()) {
1724 if (unspecified_type) {
1725 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1726 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001727 (src_cst != nullptr && (src_cst->IsIntConstant()
1728 || src_cst->IsFloatConstant()
1729 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001730 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001731 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001732 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001733 // If the source is a double stack slot or a 64bit constant, a 64bit
1734 // type is appropriate. Else the source is a register, and since the
1735 // type has not been specified, we chose a 64bit type to force a 64bit
1736 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001737 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001739 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001740 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1741 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1742 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001743 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1744 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1745 __ Ldr(dst, StackOperandFrom(source));
1746 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001747 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001749 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001750 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001751 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001752 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001753 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001754 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1755 ? Primitive::kPrimLong
1756 : Primitive::kPrimInt;
1757 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1758 }
1759 } else {
1760 DCHECK(source.IsFpuRegister());
1761 if (destination.IsRegister()) {
1762 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1763 ? Primitive::kPrimDouble
1764 : Primitive::kPrimFloat;
1765 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1766 } else {
1767 DCHECK(destination.IsFpuRegister());
1768 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001769 }
1770 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771 } else { // The destination is not a register. It must be a stack slot.
1772 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1773 if (source.IsRegister() || source.IsFpuRegister()) {
1774 if (unspecified_type) {
1775 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001776 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001778 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 }
1780 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001781 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1782 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1783 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001784 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001785 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1786 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001787 UseScratchRegisterScope temps(GetVIXLAssembler());
1788 HConstant* src_cst = source.GetConstant();
1789 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001790 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001791 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1792 ? Register(xzr)
1793 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001795 if (src_cst->IsIntConstant()) {
1796 temp = temps.AcquireW();
1797 } else if (src_cst->IsLongConstant()) {
1798 temp = temps.AcquireX();
1799 } else if (src_cst->IsFloatConstant()) {
1800 temp = temps.AcquireS();
1801 } else {
1802 DCHECK(src_cst->IsDoubleConstant());
1803 temp = temps.AcquireD();
1804 }
1805 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001807 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001808 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001809 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001810 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001811 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001812 // Use any scratch register (a core or a floating-point one)
1813 // from VIXL scratch register pools as a temporary.
1814 //
1815 // We used to only use the FP scratch register pool, but in some
1816 // rare cases the only register from this pool (D31) would
1817 // already be used (e.g. within a ParallelMove instruction, when
1818 // a move is blocked by a another move requiring a scratch FP
1819 // register, which would reserve D31). To prevent this issue, we
1820 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001821 //
1822 // Also, we start by asking for a FP scratch register first, as the
1823 // demand of scratch core registers is higher. This is why we
1824 // use AcquireFPOrCoreCPURegisterOfSize instead of
1825 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1826 // allocates core scratch registers first.
1827 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1828 GetVIXLAssembler(),
1829 &temps,
1830 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001831 __ Ldr(temp, StackOperandFrom(source));
1832 __ Str(temp, StackOperandFrom(destination));
1833 }
1834 }
1835}
1836
1837void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001838 CPURegister dst,
1839 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001840 switch (type) {
1841 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001842 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001843 break;
1844 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001845 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001846 break;
1847 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001848 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001849 break;
1850 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001852 break;
1853 case Primitive::kPrimInt:
1854 case Primitive::kPrimNot:
1855 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001856 case Primitive::kPrimFloat:
1857 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001858 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001859 __ Ldr(dst, src);
1860 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001861 case Primitive::kPrimVoid:
1862 LOG(FATAL) << "Unreachable type " << type;
1863 }
1864}
1865
Calin Juravle77520bc2015-01-12 18:45:46 +00001866void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001867 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001868 const MemOperand& src,
1869 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001870 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001871 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001872 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001873 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001874
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001875 DCHECK(!src.IsPreIndex());
1876 DCHECK(!src.IsPostIndex());
1877
1878 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001879 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001880 {
1881 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1882 MemOperand base = MemOperand(temp_base);
1883 switch (type) {
1884 case Primitive::kPrimBoolean:
1885 {
1886 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1887 __ ldarb(Register(dst), base);
1888 if (needs_null_check) {
1889 MaybeRecordImplicitNullCheck(instruction);
1890 }
1891 }
1892 break;
1893 case Primitive::kPrimByte:
1894 {
1895 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1896 __ ldarb(Register(dst), base);
1897 if (needs_null_check) {
1898 MaybeRecordImplicitNullCheck(instruction);
1899 }
1900 }
1901 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1902 break;
1903 case Primitive::kPrimChar:
1904 {
1905 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1906 __ ldarh(Register(dst), base);
1907 if (needs_null_check) {
1908 MaybeRecordImplicitNullCheck(instruction);
1909 }
1910 }
1911 break;
1912 case Primitive::kPrimShort:
1913 {
1914 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1915 __ ldarh(Register(dst), base);
1916 if (needs_null_check) {
1917 MaybeRecordImplicitNullCheck(instruction);
1918 }
1919 }
1920 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1921 break;
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimNot:
1924 case Primitive::kPrimLong:
1925 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1926 {
1927 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1928 __ ldar(Register(dst), base);
1929 if (needs_null_check) {
1930 MaybeRecordImplicitNullCheck(instruction);
1931 }
1932 }
1933 break;
1934 case Primitive::kPrimFloat:
1935 case Primitive::kPrimDouble: {
1936 DCHECK(dst.IsFPRegister());
1937 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001938
Artem Serov914d7a82017-02-07 14:33:49 +00001939 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1940 {
1941 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1942 __ ldar(temp, base);
1943 if (needs_null_check) {
1944 MaybeRecordImplicitNullCheck(instruction);
1945 }
1946 }
1947 __ Fmov(FPRegister(dst), temp);
1948 break;
Roland Levillain44015862016-01-22 11:47:17 +00001949 }
Artem Serov914d7a82017-02-07 14:33:49 +00001950 case Primitive::kPrimVoid:
1951 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001952 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001953 }
1954}
1955
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001956void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001957 CPURegister src,
1958 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001959 switch (type) {
1960 case Primitive::kPrimBoolean:
1961 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001962 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001963 break;
1964 case Primitive::kPrimChar:
1965 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001966 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001967 break;
1968 case Primitive::kPrimInt:
1969 case Primitive::kPrimNot:
1970 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001971 case Primitive::kPrimFloat:
1972 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001973 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001974 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001975 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001976 case Primitive::kPrimVoid:
1977 LOG(FATAL) << "Unreachable type " << type;
1978 }
1979}
1980
Artem Serov914d7a82017-02-07 14:33:49 +00001981void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
1982 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001983 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001984 const MemOperand& dst,
1985 bool needs_null_check) {
1986 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987 UseScratchRegisterScope temps(GetVIXLAssembler());
1988 Register temp_base = temps.AcquireX();
1989
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001990 DCHECK(!dst.IsPreIndex());
1991 DCHECK(!dst.IsPostIndex());
1992
1993 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001994 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001995 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001996 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001997 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001998 switch (type) {
1999 case Primitive::kPrimBoolean:
2000 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002001 {
2002 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2003 __ stlrb(Register(src), base);
2004 if (needs_null_check) {
2005 MaybeRecordImplicitNullCheck(instruction);
2006 }
2007 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 break;
2009 case Primitive::kPrimChar:
2010 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002011 {
2012 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2013 __ stlrh(Register(src), base);
2014 if (needs_null_check) {
2015 MaybeRecordImplicitNullCheck(instruction);
2016 }
2017 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018 break;
2019 case Primitive::kPrimInt:
2020 case Primitive::kPrimNot:
2021 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002022 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002023 {
2024 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2025 __ stlr(Register(src), base);
2026 if (needs_null_check) {
2027 MaybeRecordImplicitNullCheck(instruction);
2028 }
2029 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 break;
2031 case Primitive::kPrimFloat:
2032 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002033 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002034 Register temp_src;
2035 if (src.IsZero()) {
2036 // The zero register is used to avoid synthesizing zero constants.
2037 temp_src = Register(src);
2038 } else {
2039 DCHECK(src.IsFPRegister());
2040 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2041 __ Fmov(temp_src, FPRegister(src));
2042 }
Artem Serov914d7a82017-02-07 14:33:49 +00002043 {
2044 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2045 __ stlr(temp_src, base);
2046 if (needs_null_check) {
2047 MaybeRecordImplicitNullCheck(instruction);
2048 }
2049 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002050 break;
2051 }
2052 case Primitive::kPrimVoid:
2053 LOG(FATAL) << "Unreachable type " << type;
2054 }
2055}
2056
Calin Juravle175dc732015-08-25 15:42:32 +01002057void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2058 HInstruction* instruction,
2059 uint32_t dex_pc,
2060 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002061 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002062
2063 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2064 {
2065 // Ensure the pc position is recorded immediately after the `blr` instruction.
2066 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2067 __ blr(lr);
2068 if (EntrypointRequiresStackMap(entrypoint)) {
2069 RecordPcInfo(instruction, dex_pc, slow_path);
2070 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002071 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002072}
2073
Roland Levillaindec8f632016-07-22 17:10:06 +01002074void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2075 HInstruction* instruction,
2076 SlowPathCode* slow_path) {
2077 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002078 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2079 __ Blr(lr);
2080}
2081
Alexandre Rames67555f72014-11-18 10:55:16 +00002082void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002083 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002084 UseScratchRegisterScope temps(GetVIXLAssembler());
2085 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002086 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2087
Serban Constantinescu02164b32014-11-13 14:05:07 +00002088 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002089 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2090 __ Add(temp, class_reg, status_offset);
2091 __ Ldar(temp, HeapOperand(temp));
2092 __ Cmp(temp, mirror::Class::kStatusInitialized);
2093 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002094 __ Bind(slow_path->GetExitLabel());
2095}
Alexandre Rames5319def2014-10-23 10:03:10 +01002096
Roland Levillain44015862016-01-22 11:47:17 +00002097void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002098 BarrierType type = BarrierAll;
2099
2100 switch (kind) {
2101 case MemBarrierKind::kAnyAny:
2102 case MemBarrierKind::kAnyStore: {
2103 type = BarrierAll;
2104 break;
2105 }
2106 case MemBarrierKind::kLoadAny: {
2107 type = BarrierReads;
2108 break;
2109 }
2110 case MemBarrierKind::kStoreStore: {
2111 type = BarrierWrites;
2112 break;
2113 }
2114 default:
2115 LOG(FATAL) << "Unexpected memory barrier " << kind;
2116 }
2117 __ Dmb(InnerShareable, type);
2118}
2119
Serban Constantinescu02164b32014-11-13 14:05:07 +00002120void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2121 HBasicBlock* successor) {
2122 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002123 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2124 if (slow_path == nullptr) {
2125 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2126 instruction->SetSlowPath(slow_path);
2127 codegen_->AddSlowPath(slow_path);
2128 if (successor != nullptr) {
2129 DCHECK(successor->IsLoopHeader());
2130 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2131 }
2132 } else {
2133 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2134 }
2135
Serban Constantinescu02164b32014-11-13 14:05:07 +00002136 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2137 Register temp = temps.AcquireW();
2138
Andreas Gampe542451c2016-07-26 09:02:02 -07002139 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002140 if (successor == nullptr) {
2141 __ Cbnz(temp, slow_path->GetEntryLabel());
2142 __ Bind(slow_path->GetReturnLabel());
2143 } else {
2144 __ Cbz(temp, codegen_->GetLabelOf(successor));
2145 __ B(slow_path->GetEntryLabel());
2146 // slow_path will return to GetLabelOf(successor).
2147 }
2148}
2149
Alexandre Rames5319def2014-10-23 10:03:10 +01002150InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2151 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002152 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 assembler_(codegen->GetAssembler()),
2154 codegen_(codegen) {}
2155
2156#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002157 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002158
2159#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2160
2161enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002162 // Using a base helps identify when we hit such breakpoints.
2163 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002164#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2165 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2166#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2167};
2168
2169#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002170 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002171 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2172 } \
2173 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2175 locations->SetOut(Location::Any()); \
2176 }
2177 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2178#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2179
2180#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002181#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002182
Alexandre Rames67555f72014-11-18 10:55:16 +00002183void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002184 DCHECK_EQ(instr->InputCount(), 2U);
2185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2186 Primitive::Type type = instr->GetResultType();
2187 switch (type) {
2188 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002189 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002190 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002191 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002192 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002193 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002194
2195 case Primitive::kPrimFloat:
2196 case Primitive::kPrimDouble:
2197 locations->SetInAt(0, Location::RequiresFpuRegister());
2198 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002199 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002200 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002201
Alexandre Rames5319def2014-10-23 10:03:10 +01002202 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002203 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002204 }
2205}
2206
Alexandre Rames09a99962015-04-15 11:47:56 +01002207void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002208 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2209
2210 bool object_field_get_with_read_barrier =
2211 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002212 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002213 new (GetGraph()->GetArena()) LocationSummary(instruction,
2214 object_field_get_with_read_barrier ?
2215 LocationSummary::kCallOnSlowPath :
2216 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002217 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002218 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002219 // We need a temporary register for the read barrier marking slow
2220 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2221 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002222 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002223 locations->SetInAt(0, Location::RequiresRegister());
2224 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2225 locations->SetOut(Location::RequiresFpuRegister());
2226 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002227 // The output overlaps for an object field get when read barriers
2228 // are enabled: we do not want the load to overwrite the object's
2229 // location, as we need it to emit the read barrier.
2230 locations->SetOut(
2231 Location::RequiresRegister(),
2232 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002233 }
2234}
2235
2236void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2237 const FieldInfo& field_info) {
2238 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002239 LocationSummary* locations = instruction->GetLocations();
2240 Location base_loc = locations->InAt(0);
2241 Location out = locations->Out();
2242 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002243 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002244 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002245
Roland Levillain44015862016-01-22 11:47:17 +00002246 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2247 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002248 // /* HeapReference<Object> */ out = *(base + offset)
2249 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002250 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002251 // Note that potential implicit null checks are handled in this
2252 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2253 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2254 instruction,
2255 out,
2256 base,
2257 offset,
2258 temp,
2259 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002260 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002261 } else {
2262 // General case.
2263 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002264 // Note that a potential implicit null check is handled in this
2265 // CodeGeneratorARM64::LoadAcquire call.
2266 // NB: LoadAcquire will record the pc info if needed.
2267 codegen_->LoadAcquire(
2268 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002270 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2271 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002272 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002274 }
Roland Levillain44015862016-01-22 11:47:17 +00002275 if (field_type == Primitive::kPrimNot) {
2276 // If read barriers are enabled, emit read barriers other than
2277 // Baker's using a slow path (and also unpoison the loaded
2278 // reference, if heap poisoning is enabled).
2279 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2280 }
Roland Levillain4d027112015-07-01 15:41:14 +01002281 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002282}
2283
2284void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2285 LocationSummary* locations =
2286 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2287 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002288 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2289 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2290 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002291 locations->SetInAt(1, Location::RequiresFpuRegister());
2292 } else {
2293 locations->SetInAt(1, Location::RequiresRegister());
2294 }
2295}
2296
2297void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002298 const FieldInfo& field_info,
2299 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002300 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2301
2302 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002303 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002304 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002305 Offset offset = field_info.GetFieldOffset();
2306 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002307
Roland Levillain4d027112015-07-01 15:41:14 +01002308 {
2309 // We use a block to end the scratch scope before the write barrier, thus
2310 // freeing the temporary registers so they can be used in `MarkGCCard`.
2311 UseScratchRegisterScope temps(GetVIXLAssembler());
2312
2313 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2314 DCHECK(value.IsW());
2315 Register temp = temps.AcquireW();
2316 __ Mov(temp, value.W());
2317 GetAssembler()->PoisonHeapReference(temp.W());
2318 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002319 }
Roland Levillain4d027112015-07-01 15:41:14 +01002320
2321 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002322 codegen_->StoreRelease(
2323 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002324 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002325 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2326 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002327 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2328 codegen_->MaybeRecordImplicitNullCheck(instruction);
2329 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002330 }
2331
2332 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002333 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002334 }
2335}
2336
Alexandre Rames67555f72014-11-18 10:55:16 +00002337void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002338 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002339
2340 switch (type) {
2341 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002342 case Primitive::kPrimLong: {
2343 Register dst = OutputRegister(instr);
2344 Register lhs = InputRegisterAt(instr, 0);
2345 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002346 if (instr->IsAdd()) {
2347 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 } else if (instr->IsAnd()) {
2349 __ And(dst, lhs, rhs);
2350 } else if (instr->IsOr()) {
2351 __ Orr(dst, lhs, rhs);
2352 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002353 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002354 } else if (instr->IsRor()) {
2355 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002356 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002357 __ Ror(dst, lhs, shift);
2358 } else {
2359 // Ensure shift distance is in the same size register as the result. If
2360 // we are rotating a long and the shift comes in a w register originally,
2361 // we don't need to sxtw for use as an x since the shift distances are
2362 // all & reg_bits - 1.
2363 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2364 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002365 } else {
2366 DCHECK(instr->IsXor());
2367 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002368 }
2369 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002370 }
2371 case Primitive::kPrimFloat:
2372 case Primitive::kPrimDouble: {
2373 FPRegister dst = OutputFPRegister(instr);
2374 FPRegister lhs = InputFPRegisterAt(instr, 0);
2375 FPRegister rhs = InputFPRegisterAt(instr, 1);
2376 if (instr->IsAdd()) {
2377 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002378 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002379 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002380 } else {
2381 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002382 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002383 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002384 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002385 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002386 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002387 }
2388}
2389
Serban Constantinescu02164b32014-11-13 14:05:07 +00002390void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2391 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2392
2393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2394 Primitive::Type type = instr->GetResultType();
2395 switch (type) {
2396 case Primitive::kPrimInt:
2397 case Primitive::kPrimLong: {
2398 locations->SetInAt(0, Location::RequiresRegister());
2399 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2400 locations->SetOut(Location::RequiresRegister());
2401 break;
2402 }
2403 default:
2404 LOG(FATAL) << "Unexpected shift type " << type;
2405 }
2406}
2407
2408void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2409 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2410
2411 Primitive::Type type = instr->GetType();
2412 switch (type) {
2413 case Primitive::kPrimInt:
2414 case Primitive::kPrimLong: {
2415 Register dst = OutputRegister(instr);
2416 Register lhs = InputRegisterAt(instr, 0);
2417 Operand rhs = InputOperandAt(instr, 1);
2418 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002419 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002420 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002421 if (instr->IsShl()) {
2422 __ Lsl(dst, lhs, shift_value);
2423 } else if (instr->IsShr()) {
2424 __ Asr(dst, lhs, shift_value);
2425 } else {
2426 __ Lsr(dst, lhs, shift_value);
2427 }
2428 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002429 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430
2431 if (instr->IsShl()) {
2432 __ Lsl(dst, lhs, rhs_reg);
2433 } else if (instr->IsShr()) {
2434 __ Asr(dst, lhs, rhs_reg);
2435 } else {
2436 __ Lsr(dst, lhs, rhs_reg);
2437 }
2438 }
2439 break;
2440 }
2441 default:
2442 LOG(FATAL) << "Unexpected shift operation type " << type;
2443 }
2444}
2445
Alexandre Rames5319def2014-10-23 10:03:10 +01002446void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002447 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002448}
2449
2450void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002451 HandleBinaryOp(instruction);
2452}
2453
2454void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2455 HandleBinaryOp(instruction);
2456}
2457
2458void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2459 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002460}
2461
Artem Serov7fc63502016-02-09 17:15:29 +00002462void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002463 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2464 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2465 locations->SetInAt(0, Location::RequiresRegister());
2466 // There is no immediate variant of negated bitwise instructions in AArch64.
2467 locations->SetInAt(1, Location::RequiresRegister());
2468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2469}
2470
Artem Serov7fc63502016-02-09 17:15:29 +00002471void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002472 Register dst = OutputRegister(instr);
2473 Register lhs = InputRegisterAt(instr, 0);
2474 Register rhs = InputRegisterAt(instr, 1);
2475
2476 switch (instr->GetOpKind()) {
2477 case HInstruction::kAnd:
2478 __ Bic(dst, lhs, rhs);
2479 break;
2480 case HInstruction::kOr:
2481 __ Orn(dst, lhs, rhs);
2482 break;
2483 case HInstruction::kXor:
2484 __ Eon(dst, lhs, rhs);
2485 break;
2486 default:
2487 LOG(FATAL) << "Unreachable";
2488 }
2489}
2490
Anton Kirilov74234da2017-01-13 14:42:47 +00002491void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2492 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002493 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2494 instruction->GetType() == Primitive::kPrimLong);
2495 LocationSummary* locations =
2496 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2497 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2498 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2499 } else {
2500 locations->SetInAt(0, Location::RequiresRegister());
2501 }
2502 locations->SetInAt(1, Location::RequiresRegister());
2503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2504}
2505
Anton Kirilov74234da2017-01-13 14:42:47 +00002506void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2507 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002508 Primitive::Type type = instruction->GetType();
2509 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2510 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2511 Register out = OutputRegister(instruction);
2512 Register left;
2513 if (kind != HInstruction::kNeg) {
2514 left = InputRegisterAt(instruction, 0);
2515 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002516 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002517 // shifter operand operation, the IR generating `right_reg` (input to the type
2518 // conversion) can have a different type from the current instruction's type,
2519 // so we manually indicate the type.
2520 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002521 Operand right_operand(0);
2522
Anton Kirilov74234da2017-01-13 14:42:47 +00002523 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2524 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002525 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2526 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002527 right_operand = Operand(right_reg,
2528 helpers::ShiftFromOpKind(op_kind),
2529 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002530 }
2531
2532 // Logical binary operations do not support extension operations in the
2533 // operand. Note that VIXL would still manage if it was passed by generating
2534 // the extension as a separate instruction.
2535 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2536 DCHECK(!right_operand.IsExtendedRegister() ||
2537 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2538 kind != HInstruction::kNeg));
2539 switch (kind) {
2540 case HInstruction::kAdd:
2541 __ Add(out, left, right_operand);
2542 break;
2543 case HInstruction::kAnd:
2544 __ And(out, left, right_operand);
2545 break;
2546 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002547 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002548 __ Neg(out, right_operand);
2549 break;
2550 case HInstruction::kOr:
2551 __ Orr(out, left, right_operand);
2552 break;
2553 case HInstruction::kSub:
2554 __ Sub(out, left, right_operand);
2555 break;
2556 case HInstruction::kXor:
2557 __ Eor(out, left, right_operand);
2558 break;
2559 default:
2560 LOG(FATAL) << "Unexpected operation kind: " << kind;
2561 UNREACHABLE();
2562 }
2563}
2564
Artem Serov328429f2016-07-06 16:23:04 +01002565void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002566 LocationSummary* locations =
2567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2570 locations->SetOut(Location::RequiresRegister());
2571}
2572
Roland Levillain19c54192016-11-04 13:44:09 +00002573void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002574 __ Add(OutputRegister(instruction),
2575 InputRegisterAt(instruction, 0),
2576 Operand(InputOperandAt(instruction, 1)));
2577}
2578
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002579void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002582 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2583 if (instr->GetOpKind() == HInstruction::kSub &&
2584 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002585 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002586 // Don't allocate register for Mneg instruction.
2587 } else {
2588 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2589 Location::RequiresRegister());
2590 }
2591 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2592 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2594}
2595
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002596void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002597 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002598 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2599 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002600
2601 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2602 // This fixup should be carried out for all multiply-accumulate instructions:
2603 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2604 if (instr->GetType() == Primitive::kPrimLong &&
2605 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2606 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002607 vixl::aarch64::Instruction* prev =
2608 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002609 if (prev->IsLoadOrStore()) {
2610 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002611 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002612 __ nop();
2613 }
2614 }
2615
2616 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002617 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002618 __ Madd(res, mul_left, mul_right, accumulator);
2619 } else {
2620 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002621 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002622 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002623 __ Mneg(res, mul_left, mul_right);
2624 } else {
2625 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2626 __ Msub(res, mul_left, mul_right, accumulator);
2627 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002628 }
2629}
2630
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002631void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002632 bool object_array_get_with_read_barrier =
2633 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002634 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002635 new (GetGraph()->GetArena()) LocationSummary(instruction,
2636 object_array_get_with_read_barrier ?
2637 LocationSummary::kCallOnSlowPath :
2638 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002639 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002640 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002641 // We need a temporary register for the read barrier marking slow
2642 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
2643 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002644 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002647 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2648 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2649 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002650 // The output overlaps in the case of an object array get with
2651 // read barriers enabled: we do not want the move to overwrite the
2652 // array's location, as we need it to emit the read barrier.
2653 locations->SetOut(
2654 Location::RequiresRegister(),
2655 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002656 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002657}
2658
2659void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002660 Primitive::Type type = instruction->GetType();
2661 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002662 LocationSummary* locations = instruction->GetLocations();
2663 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002664 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002665 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002666 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2667 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002668 MacroAssembler* masm = GetVIXLAssembler();
2669 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002670
Roland Levillain19c54192016-11-04 13:44:09 +00002671 // The read barrier instrumentation of object ArrayGet instructions
2672 // does not support the HIntermediateAddress instruction.
2673 DCHECK(!((type == Primitive::kPrimNot) &&
2674 instruction->GetArray()->IsIntermediateAddress() &&
2675 kEmitCompilerReadBarrier));
2676
Roland Levillain44015862016-01-22 11:47:17 +00002677 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2678 // Object ArrayGet with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +00002679 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002680 // Note that a potential implicit null check is handled in the
2681 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2682 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2683 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002684 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002685 // General case.
2686 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002687 Register length;
2688 if (maybe_compressed_char_at) {
2689 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2690 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002691 {
2692 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2693 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2694
2695 if (instruction->GetArray()->IsIntermediateAddress()) {
2696 DCHECK_LT(count_offset, offset);
2697 int64_t adjusted_offset =
2698 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2699 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2700 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2701 } else {
2702 __ Ldr(length, HeapOperand(obj, count_offset));
2703 }
2704 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002705 }
jessicahandojo05765752016-09-09 19:01:32 -07002706 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002707 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002708 if (maybe_compressed_char_at) {
2709 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002710 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2711 "Expecting 0=compressed, 1=uncompressed");
2712 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002713 __ Ldrb(Register(OutputCPURegister(instruction)),
2714 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2715 __ B(&done);
2716 __ Bind(&uncompressed_load);
2717 __ Ldrh(Register(OutputCPURegister(instruction)),
2718 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2719 __ Bind(&done);
2720 } else {
2721 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2722 source = HeapOperand(obj, offset);
2723 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002724 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002725 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002726 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002727 // We do not need to compute the intermediate address from the array: the
2728 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002729 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002730 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002731 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002732 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2733 }
2734 temp = obj;
2735 } else {
2736 __ Add(temp, obj, offset);
2737 }
jessicahandojo05765752016-09-09 19:01:32 -07002738 if (maybe_compressed_char_at) {
2739 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002740 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2741 "Expecting 0=compressed, 1=uncompressed");
2742 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002743 __ Ldrb(Register(OutputCPURegister(instruction)),
2744 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2745 __ B(&done);
2746 __ Bind(&uncompressed_load);
2747 __ Ldrh(Register(OutputCPURegister(instruction)),
2748 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2749 __ Bind(&done);
2750 } else {
2751 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2752 }
Roland Levillain44015862016-01-22 11:47:17 +00002753 }
jessicahandojo05765752016-09-09 19:01:32 -07002754 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002755 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2756 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002757 codegen_->Load(type, OutputCPURegister(instruction), source);
2758 codegen_->MaybeRecordImplicitNullCheck(instruction);
2759 }
Roland Levillain44015862016-01-22 11:47:17 +00002760
2761 if (type == Primitive::kPrimNot) {
2762 static_assert(
2763 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2764 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2765 Location obj_loc = locations->InAt(0);
2766 if (index.IsConstant()) {
2767 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2768 } else {
2769 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2770 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002771 }
Roland Levillain4d027112015-07-01 15:41:14 +01002772 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002773}
2774
Alexandre Rames5319def2014-10-23 10:03:10 +01002775void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2777 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002779}
2780
2781void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002782 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002783 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002784 {
2785 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2786 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2787 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2788 codegen_->MaybeRecordImplicitNullCheck(instruction);
2789 }
jessicahandojo05765752016-09-09 19:01:32 -07002790 // Mask out compression flag from String's array length.
2791 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002792 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002793 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002794}
2795
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002796void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002797 Primitive::Type value_type = instruction->GetComponentType();
2798
2799 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2801 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002802 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002803 LocationSummary::kCallOnSlowPath :
2804 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002805 locations->SetInAt(0, Location::RequiresRegister());
2806 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002807 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2808 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2809 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002810 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002811 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002812 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813 }
2814}
2815
2816void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2817 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002819 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002820 bool needs_write_barrier =
2821 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002822
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002823 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002824 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002825 CPURegister source = value;
2826 Location index = locations->InAt(1);
2827 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2828 MemOperand destination = HeapOperand(array);
2829 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002830
2831 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002832 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002833 if (index.IsConstant()) {
2834 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2835 destination = HeapOperand(array, offset);
2836 } else {
2837 UseScratchRegisterScope temps(masm);
2838 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002839 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002840 // We do not need to compute the intermediate address from the array: the
2841 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002842 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002843 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002844 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002845 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2846 }
2847 temp = array;
2848 } else {
2849 __ Add(temp, array, offset);
2850 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002851 destination = HeapOperand(temp,
2852 XRegisterFrom(index),
2853 LSL,
2854 Primitive::ComponentSizeShift(value_type));
2855 }
Artem Serov914d7a82017-02-07 14:33:49 +00002856 {
2857 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2858 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2859 codegen_->Store(value_type, value, destination);
2860 codegen_->MaybeRecordImplicitNullCheck(instruction);
2861 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002862 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002863 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002864 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002865 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002866 {
2867 // We use a block to end the scratch scope before the write barrier, thus
2868 // freeing the temporary registers so they can be used in `MarkGCCard`.
2869 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002870 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002871 if (index.IsConstant()) {
2872 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002873 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002874 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002875 destination = HeapOperand(temp,
2876 XRegisterFrom(index),
2877 LSL,
2878 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002879 }
2880
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002881 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2882 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2883 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2884
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002885 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002886 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2887 codegen_->AddSlowPath(slow_path);
2888 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002889 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002890 __ Cbnz(Register(value), &non_zero);
2891 if (!index.IsConstant()) {
2892 __ Add(temp, array, offset);
2893 }
Artem Serov914d7a82017-02-07 14:33:49 +00002894 {
2895 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2896 // emitted.
2897 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2898 __ Str(wzr, destination);
2899 codegen_->MaybeRecordImplicitNullCheck(instruction);
2900 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002901 __ B(&done);
2902 __ Bind(&non_zero);
2903 }
2904
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002905 // Note that when Baker read barriers are enabled, the type
2906 // checks are performed without read barriers. This is fine,
2907 // even in the case where a class object is in the from-space
2908 // after the flip, as a comparison involving such a type would
2909 // not produce a false positive; it may of course produce a
2910 // false negative, in which case we would take the ArraySet
2911 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002912
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002913 Register temp2 = temps.AcquireSameSizeAs(array);
2914 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002915 {
2916 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2917 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2918 __ Ldr(temp, HeapOperand(array, class_offset));
2919 codegen_->MaybeRecordImplicitNullCheck(instruction);
2920 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002921 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002922
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002923 // /* HeapReference<Class> */ temp = temp->component_type_
2924 __ Ldr(temp, HeapOperand(temp, component_offset));
2925 // /* HeapReference<Class> */ temp2 = value->klass_
2926 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2927 // If heap poisoning is enabled, no need to unpoison `temp`
2928 // nor `temp2`, as we are comparing two poisoned references.
2929 __ Cmp(temp, temp2);
2930 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002931
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002932 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2933 vixl::aarch64::Label do_put;
2934 __ B(eq, &do_put);
2935 // If heap poisoning is enabled, the `temp` reference has
2936 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002937 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2938
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002939 // /* HeapReference<Class> */ temp = temp->super_class_
2940 __ Ldr(temp, HeapOperand(temp, super_offset));
2941 // If heap poisoning is enabled, no need to unpoison
2942 // `temp`, as we are comparing against null below.
2943 __ Cbnz(temp, slow_path->GetEntryLabel());
2944 __ Bind(&do_put);
2945 } else {
2946 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002947 }
2948 }
2949
2950 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002951 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002952 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002953 __ Mov(temp2, value.W());
2954 GetAssembler()->PoisonHeapReference(temp2);
2955 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002956 }
2957
2958 if (!index.IsConstant()) {
2959 __ Add(temp, array, offset);
2960 }
Artem Serov914d7a82017-02-07 14:33:49 +00002961 {
2962 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2963 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2964 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965
Artem Serov914d7a82017-02-07 14:33:49 +00002966 if (!may_need_runtime_call_for_type_check) {
2967 codegen_->MaybeRecordImplicitNullCheck(instruction);
2968 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002969 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002970 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002971
2972 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2973
2974 if (done.IsLinked()) {
2975 __ Bind(&done);
2976 }
2977
2978 if (slow_path != nullptr) {
2979 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002980 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002981 }
2982}
2983
Alexandre Rames67555f72014-11-18 10:55:16 +00002984void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002985 RegisterSet caller_saves = RegisterSet::Empty();
2986 InvokeRuntimeCallingConvention calling_convention;
2987 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2988 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2989 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002990 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002991 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002992}
2993
2994void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002995 BoundsCheckSlowPathARM64* slow_path =
2996 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002997 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002998 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2999 __ B(slow_path->GetEntryLabel(), hs);
3000}
3001
Alexandre Rames67555f72014-11-18 10:55:16 +00003002void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3003 LocationSummary* locations =
3004 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3005 locations->SetInAt(0, Location::RequiresRegister());
3006 if (check->HasUses()) {
3007 locations->SetOut(Location::SameAsFirstInput());
3008 }
3009}
3010
3011void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3012 // We assume the class is not null.
3013 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3014 check->GetLoadClass(), check, check->GetDexPc(), true);
3015 codegen_->AddSlowPath(slow_path);
3016 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3017}
3018
Roland Levillain1a653882016-03-18 18:05:57 +00003019static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3020 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3021 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3022}
3023
3024void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3025 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3026 Location rhs_loc = instruction->GetLocations()->InAt(1);
3027 if (rhs_loc.IsConstant()) {
3028 // 0.0 is the only immediate that can be encoded directly in
3029 // an FCMP instruction.
3030 //
3031 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3032 // specify that in a floating-point comparison, positive zero
3033 // and negative zero are considered equal, so we can use the
3034 // literal 0.0 for both cases here.
3035 //
3036 // Note however that some methods (Float.equal, Float.compare,
3037 // Float.compareTo, Double.equal, Double.compare,
3038 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3039 // StrictMath.min) consider 0.0 to be (strictly) greater than
3040 // -0.0. So if we ever translate calls to these methods into a
3041 // HCompare instruction, we must handle the -0.0 case with
3042 // care here.
3043 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3044 __ Fcmp(lhs_reg, 0.0);
3045 } else {
3046 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3047 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003048}
3049
Serban Constantinescu02164b32014-11-13 14:05:07 +00003050void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003051 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003052 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3053 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003054 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003055 case Primitive::kPrimBoolean:
3056 case Primitive::kPrimByte:
3057 case Primitive::kPrimShort:
3058 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003059 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003060 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003062 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3064 break;
3065 }
3066 case Primitive::kPrimFloat:
3067 case Primitive::kPrimDouble: {
3068 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003069 locations->SetInAt(1,
3070 IsFloatingPointZeroConstant(compare->InputAt(1))
3071 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3072 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003073 locations->SetOut(Location::RequiresRegister());
3074 break;
3075 }
3076 default:
3077 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3078 }
3079}
3080
3081void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3082 Primitive::Type in_type = compare->InputAt(0)->GetType();
3083
3084 // 0 if: left == right
3085 // 1 if: left > right
3086 // -1 if: left < right
3087 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003088 case Primitive::kPrimBoolean:
3089 case Primitive::kPrimByte:
3090 case Primitive::kPrimShort:
3091 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003092 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003093 case Primitive::kPrimLong: {
3094 Register result = OutputRegister(compare);
3095 Register left = InputRegisterAt(compare, 0);
3096 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003097 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003098 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3099 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003100 break;
3101 }
3102 case Primitive::kPrimFloat:
3103 case Primitive::kPrimDouble: {
3104 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003105 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003106 __ Cset(result, ne);
3107 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003108 break;
3109 }
3110 default:
3111 LOG(FATAL) << "Unimplemented compare type " << in_type;
3112 }
3113}
3114
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003115void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003117
3118 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3119 locations->SetInAt(0, Location::RequiresFpuRegister());
3120 locations->SetInAt(1,
3121 IsFloatingPointZeroConstant(instruction->InputAt(1))
3122 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3123 : Location::RequiresFpuRegister());
3124 } else {
3125 // Integer cases.
3126 locations->SetInAt(0, Location::RequiresRegister());
3127 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3128 }
3129
David Brazdilb3e773e2016-01-26 11:28:37 +00003130 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003132 }
3133}
3134
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003135void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003136 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003137 return;
3138 }
3139
3140 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003141 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003142 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003143
Roland Levillain7f63c522015-07-13 15:54:55 +00003144 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003145 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003146 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003147 } else {
3148 // Integer cases.
3149 Register lhs = InputRegisterAt(instruction, 0);
3150 Operand rhs = InputOperandAt(instruction, 1);
3151 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003152 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003153 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003154}
3155
3156#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3157 M(Equal) \
3158 M(NotEqual) \
3159 M(LessThan) \
3160 M(LessThanOrEqual) \
3161 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003162 M(GreaterThanOrEqual) \
3163 M(Below) \
3164 M(BelowOrEqual) \
3165 M(Above) \
3166 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003167#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003168void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3169void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003170FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003171#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003172#undef FOR_EACH_CONDITION_INSTRUCTION
3173
Zheng Xuc6667102015-05-15 16:08:45 +08003174void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3175 DCHECK(instruction->IsDiv() || instruction->IsRem());
3176
3177 LocationSummary* locations = instruction->GetLocations();
3178 Location second = locations->InAt(1);
3179 DCHECK(second.IsConstant());
3180
3181 Register out = OutputRegister(instruction);
3182 Register dividend = InputRegisterAt(instruction, 0);
3183 int64_t imm = Int64FromConstant(second.GetConstant());
3184 DCHECK(imm == 1 || imm == -1);
3185
3186 if (instruction->IsRem()) {
3187 __ Mov(out, 0);
3188 } else {
3189 if (imm == 1) {
3190 __ Mov(out, dividend);
3191 } else {
3192 __ Neg(out, dividend);
3193 }
3194 }
3195}
3196
3197void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3198 DCHECK(instruction->IsDiv() || instruction->IsRem());
3199
3200 LocationSummary* locations = instruction->GetLocations();
3201 Location second = locations->InAt(1);
3202 DCHECK(second.IsConstant());
3203
3204 Register out = OutputRegister(instruction);
3205 Register dividend = InputRegisterAt(instruction, 0);
3206 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003207 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003208 int ctz_imm = CTZ(abs_imm);
3209
3210 UseScratchRegisterScope temps(GetVIXLAssembler());
3211 Register temp = temps.AcquireSameSizeAs(out);
3212
3213 if (instruction->IsDiv()) {
3214 __ Add(temp, dividend, abs_imm - 1);
3215 __ Cmp(dividend, 0);
3216 __ Csel(out, temp, dividend, lt);
3217 if (imm > 0) {
3218 __ Asr(out, out, ctz_imm);
3219 } else {
3220 __ Neg(out, Operand(out, ASR, ctz_imm));
3221 }
3222 } else {
3223 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3224 __ Asr(temp, dividend, bits - 1);
3225 __ Lsr(temp, temp, bits - ctz_imm);
3226 __ Add(out, dividend, temp);
3227 __ And(out, out, abs_imm - 1);
3228 __ Sub(out, out, temp);
3229 }
3230}
3231
3232void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3233 DCHECK(instruction->IsDiv() || instruction->IsRem());
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 Location second = locations->InAt(1);
3237 DCHECK(second.IsConstant());
3238
3239 Register out = OutputRegister(instruction);
3240 Register dividend = InputRegisterAt(instruction, 0);
3241 int64_t imm = Int64FromConstant(second.GetConstant());
3242
3243 Primitive::Type type = instruction->GetResultType();
3244 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3245
3246 int64_t magic;
3247 int shift;
3248 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3249
3250 UseScratchRegisterScope temps(GetVIXLAssembler());
3251 Register temp = temps.AcquireSameSizeAs(out);
3252
3253 // temp = get_high(dividend * magic)
3254 __ Mov(temp, magic);
3255 if (type == Primitive::kPrimLong) {
3256 __ Smulh(temp, dividend, temp);
3257 } else {
3258 __ Smull(temp.X(), dividend, temp);
3259 __ Lsr(temp.X(), temp.X(), 32);
3260 }
3261
3262 if (imm > 0 && magic < 0) {
3263 __ Add(temp, temp, dividend);
3264 } else if (imm < 0 && magic > 0) {
3265 __ Sub(temp, temp, dividend);
3266 }
3267
3268 if (shift != 0) {
3269 __ Asr(temp, temp, shift);
3270 }
3271
3272 if (instruction->IsDiv()) {
3273 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3274 } else {
3275 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3276 // TODO: Strength reduction for msub.
3277 Register temp_imm = temps.AcquireSameSizeAs(out);
3278 __ Mov(temp_imm, imm);
3279 __ Msub(out, temp, temp_imm, dividend);
3280 }
3281}
3282
3283void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3284 DCHECK(instruction->IsDiv() || instruction->IsRem());
3285 Primitive::Type type = instruction->GetResultType();
3286 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3287
3288 LocationSummary* locations = instruction->GetLocations();
3289 Register out = OutputRegister(instruction);
3290 Location second = locations->InAt(1);
3291
3292 if (second.IsConstant()) {
3293 int64_t imm = Int64FromConstant(second.GetConstant());
3294
3295 if (imm == 0) {
3296 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3297 } else if (imm == 1 || imm == -1) {
3298 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003299 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003300 DivRemByPowerOfTwo(instruction);
3301 } else {
3302 DCHECK(imm <= -2 || imm >= 2);
3303 GenerateDivRemWithAnyConstant(instruction);
3304 }
3305 } else {
3306 Register dividend = InputRegisterAt(instruction, 0);
3307 Register divisor = InputRegisterAt(instruction, 1);
3308 if (instruction->IsDiv()) {
3309 __ Sdiv(out, dividend, divisor);
3310 } else {
3311 UseScratchRegisterScope temps(GetVIXLAssembler());
3312 Register temp = temps.AcquireSameSizeAs(out);
3313 __ Sdiv(temp, dividend, divisor);
3314 __ Msub(out, temp, divisor, dividend);
3315 }
3316 }
3317}
3318
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003319void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3320 LocationSummary* locations =
3321 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3322 switch (div->GetResultType()) {
3323 case Primitive::kPrimInt:
3324 case Primitive::kPrimLong:
3325 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003326 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3328 break;
3329
3330 case Primitive::kPrimFloat:
3331 case Primitive::kPrimDouble:
3332 locations->SetInAt(0, Location::RequiresFpuRegister());
3333 locations->SetInAt(1, Location::RequiresFpuRegister());
3334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3335 break;
3336
3337 default:
3338 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3339 }
3340}
3341
3342void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3343 Primitive::Type type = div->GetResultType();
3344 switch (type) {
3345 case Primitive::kPrimInt:
3346 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003347 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003348 break;
3349
3350 case Primitive::kPrimFloat:
3351 case Primitive::kPrimDouble:
3352 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3353 break;
3354
3355 default:
3356 LOG(FATAL) << "Unexpected div type " << type;
3357 }
3358}
3359
Alexandre Rames67555f72014-11-18 10:55:16 +00003360void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003361 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003362 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003363}
3364
3365void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3366 SlowPathCodeARM64* slow_path =
3367 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3368 codegen_->AddSlowPath(slow_path);
3369 Location value = instruction->GetLocations()->InAt(0);
3370
Alexandre Rames3e69f162014-12-10 10:36:50 +00003371 Primitive::Type type = instruction->GetType();
3372
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003373 if (!Primitive::IsIntegralType(type)) {
3374 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003375 return;
3376 }
3377
Alexandre Rames67555f72014-11-18 10:55:16 +00003378 if (value.IsConstant()) {
3379 int64_t divisor = Int64ConstantFrom(value);
3380 if (divisor == 0) {
3381 __ B(slow_path->GetEntryLabel());
3382 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003383 // A division by a non-null constant is valid. We don't need to perform
3384 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003385 }
3386 } else {
3387 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3388 }
3389}
3390
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003391void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3394 locations->SetOut(Location::ConstantLocation(constant));
3395}
3396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003397void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3398 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003399 // Will be generated at use site.
3400}
3401
Alexandre Rames5319def2014-10-23 10:03:10 +01003402void LocationsBuilderARM64::VisitExit(HExit* exit) {
3403 exit->SetLocations(nullptr);
3404}
3405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003406void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003407}
3408
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003409void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3410 LocationSummary* locations =
3411 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3412 locations->SetOut(Location::ConstantLocation(constant));
3413}
3414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003415void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003416 // Will be generated at use site.
3417}
3418
David Brazdilfc6a86a2015-06-26 10:33:45 +00003419void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003420 DCHECK(!successor->IsExitBlock());
3421 HBasicBlock* block = got->GetBlock();
3422 HInstruction* previous = got->GetPrevious();
3423 HLoopInformation* info = block->GetLoopInformation();
3424
David Brazdil46e2a392015-03-16 17:31:52 +00003425 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003426 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3427 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3428 return;
3429 }
3430 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3431 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3432 }
3433 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003434 __ B(codegen_->GetLabelOf(successor));
3435 }
3436}
3437
David Brazdilfc6a86a2015-06-26 10:33:45 +00003438void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3439 got->SetLocations(nullptr);
3440}
3441
3442void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3443 HandleGoto(got, got->GetSuccessor());
3444}
3445
3446void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3447 try_boundary->SetLocations(nullptr);
3448}
3449
3450void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3451 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3452 if (!successor->IsExitBlock()) {
3453 HandleGoto(try_boundary, successor);
3454 }
3455}
3456
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003457void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003458 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003459 vixl::aarch64::Label* true_target,
3460 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003461 // FP branching requires both targets to be explicit. If either of the targets
3462 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003463 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003464 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003465
David Brazdil0debae72015-11-12 18:37:00 +00003466 if (true_target == nullptr && false_target == nullptr) {
3467 // Nothing to do. The code always falls through.
3468 return;
3469 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003470 // Constant condition, statically compared against "true" (integer value 1).
3471 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003472 if (true_target != nullptr) {
3473 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003474 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003475 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003476 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003477 if (false_target != nullptr) {
3478 __ B(false_target);
3479 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003480 }
David Brazdil0debae72015-11-12 18:37:00 +00003481 return;
3482 }
3483
3484 // The following code generates these patterns:
3485 // (1) true_target == nullptr && false_target != nullptr
3486 // - opposite condition true => branch to false_target
3487 // (2) true_target != nullptr && false_target == nullptr
3488 // - condition true => branch to true_target
3489 // (3) true_target != nullptr && false_target != nullptr
3490 // - condition true => branch to true_target
3491 // - branch to false_target
3492 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003493 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003494 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003495 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003496 if (true_target == nullptr) {
3497 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3498 } else {
3499 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3500 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003501 } else {
3502 // The condition instruction has not been materialized, use its inputs as
3503 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003504 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003505
David Brazdil0debae72015-11-12 18:37:00 +00003506 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003507 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003508 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003509 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003510 IfCondition opposite_condition = condition->GetOppositeCondition();
3511 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003512 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003513 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003514 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003515 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003516 // Integer cases.
3517 Register lhs = InputRegisterAt(condition, 0);
3518 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003519
3520 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003521 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003522 if (true_target == nullptr) {
3523 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3524 non_fallthrough_target = false_target;
3525 } else {
3526 arm64_cond = ARM64Condition(condition->GetCondition());
3527 non_fallthrough_target = true_target;
3528 }
3529
Aart Bik086d27e2016-01-20 17:02:00 -08003530 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003531 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003532 switch (arm64_cond) {
3533 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003534 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003535 break;
3536 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003537 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003538 break;
3539 case lt:
3540 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003541 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003542 break;
3543 case ge:
3544 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003545 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003546 break;
3547 default:
3548 // Without the `static_cast` the compiler throws an error for
3549 // `-Werror=sign-promo`.
3550 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3551 }
3552 } else {
3553 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003554 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003555 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003556 }
3557 }
David Brazdil0debae72015-11-12 18:37:00 +00003558
3559 // If neither branch falls through (case 3), the conditional branch to `true_target`
3560 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3561 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003562 __ B(false_target);
3563 }
David Brazdil0debae72015-11-12 18:37:00 +00003564
3565 if (fallthrough_target.IsLinked()) {
3566 __ Bind(&fallthrough_target);
3567 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003568}
3569
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003570void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003573 locations->SetInAt(0, Location::RequiresRegister());
3574 }
3575}
3576
3577void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003578 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3579 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003580 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3581 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3582 true_target = nullptr;
3583 }
3584 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3585 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3586 false_target = nullptr;
3587 }
David Brazdil0debae72015-11-12 18:37:00 +00003588 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003589}
3590
3591void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3592 LocationSummary* locations = new (GetGraph()->GetArena())
3593 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003594 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003595 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003596 locations->SetInAt(0, Location::RequiresRegister());
3597 }
3598}
3599
3600void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003601 SlowPathCodeARM64* slow_path =
3602 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003603 GenerateTestAndBranch(deoptimize,
3604 /* condition_input_index */ 0,
3605 slow_path->GetEntryLabel(),
3606 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003607}
3608
Mingyao Yang063fc772016-08-02 11:02:54 -07003609void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3610 LocationSummary* locations = new (GetGraph()->GetArena())
3611 LocationSummary(flag, LocationSummary::kNoCall);
3612 locations->SetOut(Location::RequiresRegister());
3613}
3614
3615void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3616 __ Ldr(OutputRegister(flag),
3617 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3618}
3619
David Brazdilc0b601b2016-02-08 14:20:45 +00003620static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3621 return condition->IsCondition() &&
3622 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3623}
3624
Alexandre Rames880f1192016-06-13 16:04:50 +01003625static inline Condition GetConditionForSelect(HCondition* condition) {
3626 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003627 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3628 : ARM64Condition(cond);
3629}
3630
David Brazdil74eb1b22015-12-14 11:44:01 +00003631void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3632 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003633 if (Primitive::IsFloatingPointType(select->GetType())) {
3634 locations->SetInAt(0, Location::RequiresFpuRegister());
3635 locations->SetInAt(1, Location::RequiresFpuRegister());
3636 locations->SetOut(Location::RequiresFpuRegister());
3637 } else {
3638 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3639 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3640 bool is_true_value_constant = cst_true_value != nullptr;
3641 bool is_false_value_constant = cst_false_value != nullptr;
3642 // Ask VIXL whether we should synthesize constants in registers.
3643 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3644 Operand true_op = is_true_value_constant ?
3645 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3646 Operand false_op = is_false_value_constant ?
3647 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3648 bool true_value_in_register = false;
3649 bool false_value_in_register = false;
3650 MacroAssembler::GetCselSynthesisInformation(
3651 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3652 true_value_in_register |= !is_true_value_constant;
3653 false_value_in_register |= !is_false_value_constant;
3654
3655 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3656 : Location::ConstantLocation(cst_true_value));
3657 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3658 : Location::ConstantLocation(cst_false_value));
3659 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003660 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003661
David Brazdil74eb1b22015-12-14 11:44:01 +00003662 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3663 locations->SetInAt(2, Location::RequiresRegister());
3664 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003665}
3666
3667void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003668 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003669 Condition csel_cond;
3670
3671 if (IsBooleanValueOrMaterializedCondition(cond)) {
3672 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003673 // Use the condition flags set by the previous instruction.
3674 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003675 } else {
3676 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003677 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003678 }
3679 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003680 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003681 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003682 } else {
3683 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003684 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003685 }
3686
Alexandre Rames880f1192016-06-13 16:04:50 +01003687 if (Primitive::IsFloatingPointType(select->GetType())) {
3688 __ Fcsel(OutputFPRegister(select),
3689 InputFPRegisterAt(select, 1),
3690 InputFPRegisterAt(select, 0),
3691 csel_cond);
3692 } else {
3693 __ Csel(OutputRegister(select),
3694 InputOperandAt(select, 1),
3695 InputOperandAt(select, 0),
3696 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003697 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003698}
3699
David Srbecky0cf44932015-12-09 14:09:59 +00003700void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3701 new (GetGraph()->GetArena()) LocationSummary(info);
3702}
3703
David Srbeckyd28f4a02016-03-14 17:14:24 +00003704void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3705 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003706}
3707
3708void CodeGeneratorARM64::GenerateNop() {
3709 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003710}
3711
Alexandre Rames5319def2014-10-23 10:03:10 +01003712void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003713 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003714}
3715
3716void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003717 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003718}
3719
3720void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003721 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003722}
3723
3724void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003725 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003726}
3727
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003728// Temp is used for read barrier.
3729static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3730 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003731 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003732 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3733 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3734 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3735 return 1;
3736 }
3737 return 0;
3738}
3739
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003740// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003741// interface pointer, one for loading the current interface.
3742// The other checks have one temp for loading the object's class.
3743static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3744 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3745 return 3;
3746 }
3747 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003748}
3749
Alexandre Rames67555f72014-11-18 10:55:16 +00003750void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003751 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003752 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003753 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003755 case TypeCheckKind::kExactCheck:
3756 case TypeCheckKind::kAbstractClassCheck:
3757 case TypeCheckKind::kClassHierarchyCheck:
3758 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003759 call_kind =
3760 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003761 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003762 break;
3763 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764 case TypeCheckKind::kUnresolvedCheck:
3765 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 call_kind = LocationSummary::kCallOnSlowPath;
3767 break;
3768 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003769
Alexandre Rames67555f72014-11-18 10:55:16 +00003770 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003771 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003772 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003773 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003774 locations->SetInAt(0, Location::RequiresRegister());
3775 locations->SetInAt(1, Location::RequiresRegister());
3776 // The "out" register is used as a temporary, so it overlaps with the inputs.
3777 // Note that TypeCheckSlowPathARM64 uses this register too.
3778 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003779 // Add temps if necessary for read barriers.
3780 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003781}
3782
3783void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003784 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003785 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003786 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003787 Register obj = InputRegisterAt(instruction, 0);
3788 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003789 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003790 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003791 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3792 DCHECK_LE(num_temps, 1u);
3793 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3795 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3796 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3797 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003798
Scott Wakeling97c72b72016-06-24 16:19:36 +01003799 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003800 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003801
3802 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003803 // Avoid null check if we know `obj` is not null.
3804 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003805 __ Cbz(obj, &zero);
3806 }
3807
Roland Levillain44015862016-01-22 11:47:17 +00003808 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003809 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003810 // /* HeapReference<Class> */ out = obj->klass_
3811 GenerateReferenceLoadTwoRegisters(instruction,
3812 out_loc,
3813 obj_loc,
3814 class_offset,
3815 maybe_temp_loc,
3816 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003817 __ Cmp(out, cls);
3818 __ Cset(out, eq);
3819 if (zero.IsLinked()) {
3820 __ B(&done);
3821 }
3822 break;
3823 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003825 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003826 // /* HeapReference<Class> */ out = obj->klass_
3827 GenerateReferenceLoadTwoRegisters(instruction,
3828 out_loc,
3829 obj_loc,
3830 class_offset,
3831 maybe_temp_loc,
3832 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003833 // If the class is abstract, we eagerly fetch the super class of the
3834 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003835 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003836 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003837 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003838 GenerateReferenceLoadOneRegister(instruction,
3839 out_loc,
3840 super_offset,
3841 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003842 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 // If `out` is null, we use it for the result, and jump to `done`.
3844 __ Cbz(out, &done);
3845 __ Cmp(out, cls);
3846 __ B(ne, &loop);
3847 __ Mov(out, 1);
3848 if (zero.IsLinked()) {
3849 __ B(&done);
3850 }
3851 break;
3852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003854 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003855 // /* HeapReference<Class> */ out = obj->klass_
3856 GenerateReferenceLoadTwoRegisters(instruction,
3857 out_loc,
3858 obj_loc,
3859 class_offset,
3860 maybe_temp_loc,
3861 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003862 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003863 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003864 __ Bind(&loop);
3865 __ Cmp(out, cls);
3866 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003867 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003868 GenerateReferenceLoadOneRegister(instruction,
3869 out_loc,
3870 super_offset,
3871 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003872 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003873 __ Cbnz(out, &loop);
3874 // If `out` is null, we use it for the result, and jump to `done`.
3875 __ B(&done);
3876 __ Bind(&success);
3877 __ Mov(out, 1);
3878 if (zero.IsLinked()) {
3879 __ B(&done);
3880 }
3881 break;
3882 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003885 // /* HeapReference<Class> */ out = obj->klass_
3886 GenerateReferenceLoadTwoRegisters(instruction,
3887 out_loc,
3888 obj_loc,
3889 class_offset,
3890 maybe_temp_loc,
3891 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003892 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003893 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003894 __ Cmp(out, cls);
3895 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003897 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003898 GenerateReferenceLoadOneRegister(instruction,
3899 out_loc,
3900 component_offset,
3901 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003902 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003903 // If `out` is null, we use it for the result, and jump to `done`.
3904 __ Cbz(out, &done);
3905 __ Ldrh(out, HeapOperand(out, primitive_offset));
3906 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3907 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003908 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 __ Mov(out, 1);
3910 __ B(&done);
3911 break;
3912 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003914 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003915 // No read barrier since the slow path will retry upon failure.
3916 // /* HeapReference<Class> */ out = obj->klass_
3917 GenerateReferenceLoadTwoRegisters(instruction,
3918 out_loc,
3919 obj_loc,
3920 class_offset,
3921 maybe_temp_loc,
3922 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 __ Cmp(out, cls);
3924 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003925 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3926 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 codegen_->AddSlowPath(slow_path);
3928 __ B(ne, slow_path->GetEntryLabel());
3929 __ Mov(out, 1);
3930 if (zero.IsLinked()) {
3931 __ B(&done);
3932 }
3933 break;
3934 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003935
Calin Juravle98893e12015-10-02 21:05:03 +01003936 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 case TypeCheckKind::kInterfaceCheck: {
3938 // Note that we indeed only call on slow path, but we always go
3939 // into the slow path for the unresolved and interface check
3940 // cases.
3941 //
3942 // We cannot directly call the InstanceofNonTrivial runtime
3943 // entry point without resorting to a type checking slow path
3944 // here (i.e. by calling InvokeRuntime directly), as it would
3945 // require to assign fixed registers for the inputs of this
3946 // HInstanceOf instruction (following the runtime calling
3947 // convention), which might be cluttered by the potential first
3948 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003949 //
3950 // TODO: Introduce a new runtime entry point taking the object
3951 // to test (instead of its class) as argument, and let it deal
3952 // with the read barrier issues. This will let us refactor this
3953 // case of the `switch` code as it was previously (with a direct
3954 // call to the runtime not using a type checking slow path).
3955 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003956 DCHECK(locations->OnlyCallsOnSlowPath());
3957 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3958 /* is_fatal */ false);
3959 codegen_->AddSlowPath(slow_path);
3960 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 if (zero.IsLinked()) {
3962 __ B(&done);
3963 }
3964 break;
3965 }
3966 }
3967
3968 if (zero.IsLinked()) {
3969 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003970 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 }
3972
3973 if (done.IsLinked()) {
3974 __ Bind(&done);
3975 }
3976
3977 if (slow_path != nullptr) {
3978 __ Bind(slow_path->GetExitLabel());
3979 }
3980}
3981
3982void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3983 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3984 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3985
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003986 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3987 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 case TypeCheckKind::kExactCheck:
3989 case TypeCheckKind::kAbstractClassCheck:
3990 case TypeCheckKind::kClassHierarchyCheck:
3991 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3993 LocationSummary::kCallOnSlowPath :
3994 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 break;
3996 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 case TypeCheckKind::kUnresolvedCheck:
3998 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 call_kind = LocationSummary::kCallOnSlowPath;
4000 break;
4001 }
4002
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4004 locations->SetInAt(0, Location::RequiresRegister());
4005 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004006 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4007 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008}
4009
4010void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004011 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 Register obj = InputRegisterAt(instruction, 0);
4015 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004016 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4017 DCHECK_GE(num_temps, 1u);
4018 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004020 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4021 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004022 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004023 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4024 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4025 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4026 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4027 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4028 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4029 const uint32_t object_array_data_offset =
4030 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004032 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004033 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4034 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4035 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004036 if (!kEmitCompilerReadBarrier) {
4037 is_type_check_slow_path_fatal =
4038 (type_check_kind == TypeCheckKind::kExactCheck ||
4039 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4040 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4041 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4042 !instruction->CanThrowIntoCatchBlock();
4043 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004044 SlowPathCodeARM64* type_check_slow_path =
4045 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4046 is_type_check_slow_path_fatal);
4047 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004048
Scott Wakeling97c72b72016-06-24 16:19:36 +01004049 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004050 // Avoid null check if we know obj is not null.
4051 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004052 __ Cbz(obj, &done);
4053 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004054
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004055 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004056 case TypeCheckKind::kExactCheck:
4057 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004058 // /* HeapReference<Class> */ temp = obj->klass_
4059 GenerateReferenceLoadTwoRegisters(instruction,
4060 temp_loc,
4061 obj_loc,
4062 class_offset,
4063 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004064 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004065
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 __ Cmp(temp, cls);
4067 // Jump to slow path for throwing the exception or doing a
4068 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004069 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004070 break;
4071 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004072
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004074 // /* HeapReference<Class> */ temp = obj->klass_
4075 GenerateReferenceLoadTwoRegisters(instruction,
4076 temp_loc,
4077 obj_loc,
4078 class_offset,
4079 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004080 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004081
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004082 // If the class is abstract, we eagerly fetch the super class of the
4083 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004084 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004085 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004087 GenerateReferenceLoadOneRegister(instruction,
4088 temp_loc,
4089 super_offset,
4090 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004091 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004093 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4094 // exception.
4095 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4096 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004097 __ Cmp(temp, cls);
4098 __ B(ne, &loop);
4099 break;
4100 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004101
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004102 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004103 // /* HeapReference<Class> */ temp = obj->klass_
4104 GenerateReferenceLoadTwoRegisters(instruction,
4105 temp_loc,
4106 obj_loc,
4107 class_offset,
4108 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004109 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004110
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004111 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004112 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 __ Bind(&loop);
4114 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004115 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004116
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004118 GenerateReferenceLoadOneRegister(instruction,
4119 temp_loc,
4120 super_offset,
4121 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004122 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004123
4124 // If the class reference currently in `temp` is not null, jump
4125 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004126 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004128 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 break;
4130 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004132 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004133 // /* HeapReference<Class> */ temp = obj->klass_
4134 GenerateReferenceLoadTwoRegisters(instruction,
4135 temp_loc,
4136 obj_loc,
4137 class_offset,
4138 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004139 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004140
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004141 // Do an exact check.
4142 __ Cmp(temp, cls);
4143 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004144
4145 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004147 GenerateReferenceLoadOneRegister(instruction,
4148 temp_loc,
4149 component_offset,
4150 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004151 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004152
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004153 // If the component type is null, jump to the slow path to throw the exception.
4154 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4155 // Otherwise, the object is indeed an array. Further check that this component type is not a
4156 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004157 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4158 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004159 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004160 break;
4161 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004162
Calin Juravle98893e12015-10-02 21:05:03 +01004163 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004164 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 //
4166 // We cannot directly call the CheckCast runtime entry point
4167 // without resorting to a type checking slow path here (i.e. by
4168 // calling InvokeRuntime directly), as it would require to
4169 // assign fixed registers for the inputs of this HInstanceOf
4170 // instruction (following the runtime calling convention), which
4171 // might be cluttered by the potential first read barrier
4172 // emission at the beginning of this method.
4173 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004174 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004175 case TypeCheckKind::kInterfaceCheck: {
4176 // /* HeapReference<Class> */ temp = obj->klass_
4177 GenerateReferenceLoadTwoRegisters(instruction,
4178 temp_loc,
4179 obj_loc,
4180 class_offset,
4181 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004182 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183
4184 // /* HeapReference<Class> */ temp = temp->iftable_
4185 GenerateReferenceLoadTwoRegisters(instruction,
4186 temp_loc,
4187 temp_loc,
4188 iftable_offset,
4189 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004190 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004191 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004192 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004193 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004194 vixl::aarch64::Label start_loop;
4195 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004196 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004197 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4198 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199 // Go to next interface.
4200 __ Add(temp, temp, 2 * kHeapReferenceSize);
4201 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004202 // Compare the classes and continue the loop if they do not match.
4203 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4204 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205 break;
4206 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004208 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004210 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004211}
4212
Alexandre Rames5319def2014-10-23 10:03:10 +01004213void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4215 locations->SetOut(Location::ConstantLocation(constant));
4216}
4217
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004218void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004219 // Will be generated at use site.
4220}
4221
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004222void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4224 locations->SetOut(Location::ConstantLocation(constant));
4225}
4226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004227void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004228 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004229}
4230
Calin Juravle175dc732015-08-25 15:42:32 +01004231void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4232 // The trampoline uses the same calling convention as dex calling conventions,
4233 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4234 // the method_idx.
4235 HandleInvoke(invoke);
4236}
4237
4238void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4239 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4240}
4241
Alexandre Rames5319def2014-10-23 10:03:10 +01004242void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004243 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004244 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004245}
4246
Alexandre Rames67555f72014-11-18 10:55:16 +00004247void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4248 HandleInvoke(invoke);
4249}
4250
4251void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4252 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004253 LocationSummary* locations = invoke->GetLocations();
4254 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004256 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004257 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004258
4259 // The register ip1 is required to be used for the hidden argument in
4260 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004261 MacroAssembler* masm = GetVIXLAssembler();
4262 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004263 scratch_scope.Exclude(ip1);
4264 __ Mov(ip1, invoke->GetDexMethodIndex());
4265
Artem Serov914d7a82017-02-07 14:33:49 +00004266 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004267 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004268 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004269 {
4270 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4271 // /* HeapReference<Class> */ temp = temp->klass_
4272 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4273 codegen_->MaybeRecordImplicitNullCheck(invoke);
4274 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004275 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004276 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004278 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004279 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004280 }
Artem Serov914d7a82017-02-07 14:33:49 +00004281
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282 // Instead of simply (possibly) unpoisoning `temp` here, we should
4283 // emit a read barrier for the previous class reference load.
4284 // However this is not required in practice, as this is an
4285 // intermediate/temporary reference and because the current
4286 // concurrent copying collector keeps the from-space memory
4287 // intact/accessible until the end of the marking phase (the
4288 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004289 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004290 __ Ldr(temp,
4291 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4292 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004293 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004294 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004295 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004296 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004297 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004298
4299 {
4300 // Ensure the pc position is recorded immediately after the `blr` instruction.
4301 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4302
4303 // lr();
4304 __ blr(lr);
4305 DCHECK(!codegen_->IsLeafMethod());
4306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4307 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004308}
4309
4310void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004311 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004312 if (intrinsic.TryDispatch(invoke)) {
4313 return;
4314 }
4315
Alexandre Rames67555f72014-11-18 10:55:16 +00004316 HandleInvoke(invoke);
4317}
4318
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004319void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004320 // Explicit clinit checks triggered by static invokes must have been pruned by
4321 // art::PrepareForRegisterAllocation.
4322 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004323
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004324 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004325 if (intrinsic.TryDispatch(invoke)) {
4326 return;
4327 }
4328
Alexandre Rames67555f72014-11-18 10:55:16 +00004329 HandleInvoke(invoke);
4330}
4331
Andreas Gampe878d58c2015-01-15 23:24:00 -08004332static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4333 if (invoke->GetLocations()->Intrinsified()) {
4334 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4335 intrinsic.Dispatch(invoke);
4336 return true;
4337 }
4338 return false;
4339}
4340
Vladimir Markodc151b22015-10-15 18:02:30 +01004341HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4342 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004343 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004344 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004345 return desired_dispatch_info;
4346}
4347
TatWai Chongd8c052a2016-11-02 16:12:48 +08004348Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4349 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004350 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004351 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4352 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004353 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4354 uint32_t offset =
4355 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004356 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004357 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004358 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004359 }
Vladimir Marko58155012015-08-19 12:49:41 +00004360 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004361 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004362 break;
4363 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4364 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004365 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004366 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004367 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4368 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004369 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004370 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004371 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004372 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004373 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004374 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004375 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004376 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004377 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004378 }
Vladimir Marko58155012015-08-19 12:49:41 +00004379 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004380 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004381 Register reg = XRegisterFrom(temp);
4382 Register method_reg;
4383 if (current_method.IsRegister()) {
4384 method_reg = XRegisterFrom(current_method);
4385 } else {
4386 DCHECK(invoke->GetLocations()->Intrinsified());
4387 DCHECK(!current_method.IsValid());
4388 method_reg = reg;
4389 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4390 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004391
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004392 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004393 __ Ldr(reg.X(),
4394 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004395 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004396 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004397 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4398 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004399 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4400 break;
4401 }
4402 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004403 return callee_method;
4404}
4405
4406void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4407 // All registers are assumed to be correctly set up.
4408 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004409
4410 switch (invoke->GetCodePtrLocation()) {
4411 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4412 __ Bl(&frame_entry_label_);
4413 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004414 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4415 // LR = callee_method->entry_point_from_quick_compiled_code_;
4416 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004417 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004418 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004419 {
4420 // To ensure that the pc position is recorded immediately after the `blr` instruction
4421 // BLR must be the last instruction emitted in this function.
4422 // Recording the pc will occur right after returning from this function.
4423 ExactAssemblyScope eas(GetVIXLAssembler(),
4424 kInstructionSize,
4425 CodeBufferCheckScope::kExactSize);
4426 // lr()
4427 __ blr(lr);
4428 }
Vladimir Marko58155012015-08-19 12:49:41 +00004429 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004430 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004431
Andreas Gampe878d58c2015-01-15 23:24:00 -08004432 DCHECK(!IsLeafMethod());
4433}
4434
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004435void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004436 // Use the calling convention instead of the location of the receiver, as
4437 // intrinsics may have put the receiver in a different register. In the intrinsics
4438 // slow path, the arguments have been moved to the right place, so here we are
4439 // guaranteed that the receiver is the first register of the calling convention.
4440 InvokeDexCallingConvention calling_convention;
4441 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004442 Register temp = XRegisterFrom(temp_in);
4443 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4444 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4445 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004446 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004447
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004448 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004449
4450 {
4451 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4452 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4453 // /* HeapReference<Class> */ temp = receiver->klass_
4454 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4455 MaybeRecordImplicitNullCheck(invoke);
4456 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004457 // Instead of simply (possibly) unpoisoning `temp` here, we should
4458 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004459 // intermediate/temporary reference and because the current
4460 // concurrent copying collector keeps the from-space memory
4461 // intact/accessible until the end of the marking phase (the
4462 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004463 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4464 // temp = temp->GetMethodAt(method_offset);
4465 __ Ldr(temp, MemOperand(temp, method_offset));
4466 // lr = temp->GetEntryPoint();
4467 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004468 {
4469 // To ensure that the pc position is recorded immediately after the `blr` instruction
4470 // BLR should be the last instruction emitted in this function.
4471 // Recording the pc will occur right after returning from this function.
4472 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4473 // lr();
4474 __ blr(lr);
4475 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004476}
4477
Orion Hodsonac141392017-01-13 11:53:47 +00004478void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4479 HandleInvoke(invoke);
4480}
4481
4482void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4483 codegen_->GenerateInvokePolymorphicCall(invoke);
4484}
4485
Scott Wakeling97c72b72016-06-24 16:19:36 +01004486vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4487 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004488 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004489 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004490 return
4491 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004492}
4493
Scott Wakeling97c72b72016-06-24 16:19:36 +01004494vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4495 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004496 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004497 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004498 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004499}
4500
Vladimir Marko1998cd02017-01-13 13:02:58 +00004501vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4502 const DexFile& dex_file,
4503 dex::TypeIndex type_index,
4504 vixl::aarch64::Label* adrp_label) {
4505 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4506}
4507
Scott Wakeling97c72b72016-06-24 16:19:36 +01004508vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4509 const DexFile& dex_file,
4510 uint32_t element_offset,
4511 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4513}
4514
Scott Wakeling97c72b72016-06-24 16:19:36 +01004515vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4516 const DexFile& dex_file,
4517 uint32_t offset_or_index,
4518 vixl::aarch64::Label* adrp_label,
4519 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520 // Add a patch entry and return the label.
4521 patches->emplace_back(dex_file, offset_or_index);
4522 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004523 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4525 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4526 return label;
4527}
4528
Scott Wakeling97c72b72016-06-24 16:19:36 +01004529vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004530 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004531 return boot_image_string_patches_.GetOrCreate(
4532 StringReference(&dex_file, string_index),
4533 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4534}
4535
Scott Wakeling97c72b72016-06-24 16:19:36 +01004536vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004537 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004538 return boot_image_type_patches_.GetOrCreate(
4539 TypeReference(&dex_file, type_index),
4540 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4541}
4542
Scott Wakeling97c72b72016-06-24 16:19:36 +01004543vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4544 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004545 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4546 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4547 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4548}
4549
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004550vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004551 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4552 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4553 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004554 return jit_string_patches_.GetOrCreate(
4555 StringReference(&dex_file, string_index),
4556 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4557}
4558
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004559vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004560 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4561 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4562 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004563 return jit_class_patches_.GetOrCreate(
4564 TypeReference(&dex_file, type_index),
4565 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4566}
4567
Vladimir Markoaad75c62016-10-03 08:46:48 +00004568void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4569 vixl::aarch64::Register reg) {
4570 DCHECK(reg.IsX());
4571 SingleEmissionCheckScope guard(GetVIXLAssembler());
4572 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004573 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004574}
4575
4576void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4577 vixl::aarch64::Register out,
4578 vixl::aarch64::Register base) {
4579 DCHECK(out.IsX());
4580 DCHECK(base.IsX());
4581 SingleEmissionCheckScope guard(GetVIXLAssembler());
4582 __ Bind(fixup_label);
4583 __ add(out, base, Operand(/* offset placeholder */ 0));
4584}
4585
4586void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4587 vixl::aarch64::Register out,
4588 vixl::aarch64::Register base) {
4589 DCHECK(base.IsX());
4590 SingleEmissionCheckScope guard(GetVIXLAssembler());
4591 __ Bind(fixup_label);
4592 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4593}
4594
4595template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4596inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4597 const ArenaDeque<PcRelativePatchInfo>& infos,
4598 ArenaVector<LinkerPatch>* linker_patches) {
4599 for (const PcRelativePatchInfo& info : infos) {
4600 linker_patches->push_back(Factory(info.label.GetLocation(),
4601 &info.target_dex_file,
4602 info.pc_insn_label->GetLocation(),
4603 info.offset_or_index));
4604 }
4605}
4606
Vladimir Marko58155012015-08-19 12:49:41 +00004607void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4608 DCHECK(linker_patches->empty());
4609 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004610 pc_relative_dex_cache_patches_.size() +
4611 boot_image_string_patches_.size() +
4612 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004613 boot_image_type_patches_.size() +
4614 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004615 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004616 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004617 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004618 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004619 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004620 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004621 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004622 info.offset_or_index));
4623 }
4624 for (const auto& entry : boot_image_string_patches_) {
4625 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004626 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4627 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004628 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004629 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004630 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004631 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004632 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004633 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4634 linker_patches);
4635 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004636 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4637 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004638 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4639 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004641 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4642 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004643 for (const auto& entry : boot_image_type_patches_) {
4644 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004645 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4646 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004647 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004648 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004649 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004650 for (const auto& entry : boot_image_address_patches_) {
4651 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004652 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4653 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004654 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004655 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004656}
4657
Scott Wakeling97c72b72016-06-24 16:19:36 +01004658vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004659 Uint32ToLiteralMap* map) {
4660 return map->GetOrCreate(
4661 value,
4662 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4663}
4664
Scott Wakeling97c72b72016-06-24 16:19:36 +01004665vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 return uint64_literals_.GetOrCreate(
4667 value,
4668 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004669}
4670
Scott Wakeling97c72b72016-06-24 16:19:36 +01004671vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004672 MethodReference target_method,
4673 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004674 return map->GetOrCreate(
4675 target_method,
4676 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004677}
4678
Andreas Gampe878d58c2015-01-15 23:24:00 -08004679void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004680 // Explicit clinit checks triggered by static invokes must have been pruned by
4681 // art::PrepareForRegisterAllocation.
4682 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004683
Andreas Gampe878d58c2015-01-15 23:24:00 -08004684 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4685 return;
4686 }
4687
Artem Serov914d7a82017-02-07 14:33:49 +00004688 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4689 // are no pools emitted.
4690 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004691 LocationSummary* locations = invoke->GetLocations();
4692 codegen_->GenerateStaticOrDirectCall(
4693 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004694 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004695}
4696
4697void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004698 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4699 return;
4700 }
4701
Artem Serov914d7a82017-02-07 14:33:49 +00004702 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4703 // are no pools emitted.
4704 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004705 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004706 DCHECK(!codegen_->IsLeafMethod());
4707 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4708}
4709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004710HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4711 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004712 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004713 case HLoadClass::LoadKind::kInvalid:
4714 LOG(FATAL) << "UNREACHABLE";
4715 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004716 case HLoadClass::LoadKind::kReferrersClass:
4717 break;
4718 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4719 DCHECK(!GetCompilerOptions().GetCompilePic());
4720 break;
4721 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4722 DCHECK(GetCompilerOptions().GetCompilePic());
4723 break;
4724 case HLoadClass::LoadKind::kBootImageAddress:
4725 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004726 case HLoadClass::LoadKind::kBssEntry:
4727 DCHECK(!Runtime::Current()->UseJitCompilation());
4728 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004729 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004730 DCHECK(Runtime::Current()->UseJitCompilation());
4731 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004732 case HLoadClass::LoadKind::kDexCacheViaMethod:
4733 break;
4734 }
4735 return desired_class_load_kind;
4736}
4737
Alexandre Rames67555f72014-11-18 10:55:16 +00004738void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004739 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4740 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004741 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004742 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004743 cls,
4744 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004745 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004746 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004747 return;
4748 }
Vladimir Marko41559982017-01-06 14:04:23 +00004749 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004750
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004751 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4752 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004753 ? LocationSummary::kCallOnSlowPath
4754 : LocationSummary::kNoCall;
4755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004756 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004757 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004758 }
4759
Vladimir Marko41559982017-01-06 14:04:23 +00004760 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004761 locations->SetInAt(0, Location::RequiresRegister());
4762 }
4763 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004764 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4765 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4766 // Rely on the type resolution or initialization and marking to save everything we need.
4767 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4768 // to the custom calling convention) or by marking, so we shall use IP1.
4769 RegisterSet caller_saves = RegisterSet::Empty();
4770 InvokeRuntimeCallingConvention calling_convention;
4771 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4772 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4773 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4774 Primitive::kPrimNot).GetCode());
4775 locations->SetCustomSlowPathCallerSaves(caller_saves);
4776 } else {
4777 // For non-Baker read barrier we have a temp-clobbering call.
4778 }
4779 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004780}
4781
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004782// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4783// move.
4784void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004785 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4786 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4787 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004788 return;
4789 }
Vladimir Marko41559982017-01-06 14:04:23 +00004790 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004791
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004792 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004793 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004794 Register bss_entry_temp;
4795 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004796
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004797 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4798 ? kWithoutReadBarrier
4799 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004800 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004801 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004802 case HLoadClass::LoadKind::kReferrersClass: {
4803 DCHECK(!cls->CanCallRuntime());
4804 DCHECK(!cls->MustGenerateClinitCheck());
4805 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4806 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004807 GenerateGcRootFieldLoad(cls,
4808 out_loc,
4809 current_method,
4810 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004811 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004812 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004813 break;
4814 }
4815 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004816 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004817 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4818 cls->GetTypeIndex()));
4819 break;
4820 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004821 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004822 // Add ADRP with its PC-relative type patch.
4823 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004824 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004825 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004826 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004828 vixl::aarch64::Label* add_label =
4829 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004830 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004831 break;
4832 }
4833 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004834 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004835 uint32_t address = dchecked_integral_cast<uint32_t>(
4836 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4837 DCHECK_NE(address, 0u);
4838 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839 break;
4840 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004841 case HLoadClass::LoadKind::kBssEntry: {
4842 // Add ADRP with its PC-relative Class .bss entry patch.
4843 const DexFile& dex_file = cls->GetDexFile();
4844 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004845 // We can go to slow path even with non-zero reference and in that case marking
4846 // can clobber IP0, so we need to use IP1 which shall be preserved.
4847 bss_entry_temp = ip1;
4848 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4849 temps.Exclude(bss_entry_temp);
4850 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4851 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004852 // Add LDR with its PC-relative Class patch.
4853 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004854 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004855 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4856 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004857 out_loc,
4858 bss_entry_temp,
4859 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004860 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004861 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004862 generate_null_check = true;
4863 break;
4864 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004865 case HLoadClass::LoadKind::kJitTableAddress: {
4866 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4867 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004868 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004869 GenerateGcRootFieldLoad(cls,
4870 out_loc,
4871 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004872 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004873 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004874 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004875 break;
4876 }
Vladimir Marko41559982017-01-06 14:04:23 +00004877 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004878 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004879 LOG(FATAL) << "UNREACHABLE";
4880 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004881 }
4882
Vladimir Markoea4c1262017-02-06 19:59:33 +00004883 bool do_clinit = cls->MustGenerateClinitCheck();
4884 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004885 DCHECK(cls->CanCallRuntime());
4886 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004887 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004888 codegen_->AddSlowPath(slow_path);
4889 if (generate_null_check) {
4890 __ Cbz(out, slow_path->GetEntryLabel());
4891 }
4892 if (cls->MustGenerateClinitCheck()) {
4893 GenerateClassInitializationCheck(slow_path, out);
4894 } else {
4895 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004896 }
4897 }
4898}
4899
David Brazdilcb1c0552015-08-04 16:22:25 +01004900static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004901 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004902}
4903
Alexandre Rames67555f72014-11-18 10:55:16 +00004904void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4905 LocationSummary* locations =
4906 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4907 locations->SetOut(Location::RequiresRegister());
4908}
4909
4910void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004911 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4912}
4913
4914void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4915 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4916}
4917
4918void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4919 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004920}
4921
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004922HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4923 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004924 switch (desired_string_load_kind) {
4925 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4926 DCHECK(!GetCompilerOptions().GetCompilePic());
4927 break;
4928 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4929 DCHECK(GetCompilerOptions().GetCompilePic());
4930 break;
4931 case HLoadString::LoadKind::kBootImageAddress:
4932 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004933 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004934 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004935 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004936 case HLoadString::LoadKind::kJitTableAddress:
4937 DCHECK(Runtime::Current()->UseJitCompilation());
4938 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004939 case HLoadString::LoadKind::kDexCacheViaMethod:
4940 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004941 }
4942 return desired_string_load_kind;
4943}
4944
Alexandre Rames67555f72014-11-18 10:55:16 +00004945void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004946 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004947 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004948 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004949 InvokeRuntimeCallingConvention calling_convention;
4950 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4951 } else {
4952 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004953 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4954 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004955 // Rely on the pResolveString and marking to save everything we need.
4956 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4957 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004958 RegisterSet caller_saves = RegisterSet::Empty();
4959 InvokeRuntimeCallingConvention calling_convention;
4960 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4961 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4962 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4963 Primitive::kPrimNot).GetCode());
4964 locations->SetCustomSlowPathCallerSaves(caller_saves);
4965 } else {
4966 // For non-Baker read barrier we have a temp-clobbering call.
4967 }
4968 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004969 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004970}
4971
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004972// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4973// move.
4974void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004975 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004976 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004977
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004978 switch (load->GetLoadKind()) {
4979 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004980 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4981 load->GetStringIndex()));
4982 return; // No dex cache slow path.
4983 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004984 // Add ADRP with its PC-relative String patch.
4985 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004986 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004987 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004988 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004989 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004990 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004991 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004992 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004993 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004994 return; // No dex cache slow path.
4995 }
4996 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004997 uint32_t address = dchecked_integral_cast<uint32_t>(
4998 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4999 DCHECK_NE(address, 0u);
5000 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005001 return; // No dex cache slow path.
5002 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005003 case HLoadString::LoadKind::kBssEntry: {
5004 // Add ADRP with its PC-relative String .bss entry patch.
5005 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005006 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005007 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005008 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
5009 // that's when we need the slow path. But let's not rely on such details and use IP1.
5010 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005011 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005012 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005013 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005014 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005015 // Add LDR with its PC-relative String patch.
5016 vixl::aarch64::Label* ldr_label =
5017 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005018 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005019 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005020 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005021 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005022 /* offset placeholder */ 0u,
5023 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005024 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005025 SlowPathCodeARM64* slow_path =
5026 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005027 codegen_->AddSlowPath(slow_path);
5028 __ Cbz(out.X(), slow_path->GetEntryLabel());
5029 __ Bind(slow_path->GetExitLabel());
5030 return;
5031 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005032 case HLoadString::LoadKind::kJitTableAddress: {
5033 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005034 load->GetStringIndex(),
5035 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005036 GenerateGcRootFieldLoad(load,
5037 out_loc,
5038 out.X(),
5039 /* offset */ 0,
5040 /* fixup_label */ nullptr,
5041 kCompilerReadBarrierOption);
5042 return;
5043 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005044 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005045 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005046 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005047
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005048 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005049 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005050 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005051 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005052 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5053 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005054}
5055
Alexandre Rames5319def2014-10-23 10:03:10 +01005056void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5058 locations->SetOut(Location::ConstantLocation(constant));
5059}
5060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005061void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005062 // Will be generated at use site.
5063}
5064
Alexandre Rames67555f72014-11-18 10:55:16 +00005065void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5066 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005068 InvokeRuntimeCallingConvention calling_convention;
5069 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5070}
5071
5072void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005073 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005074 instruction,
5075 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005076 if (instruction->IsEnter()) {
5077 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5078 } else {
5079 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5080 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005081}
5082
Alexandre Rames42d641b2014-10-27 14:00:51 +00005083void LocationsBuilderARM64::VisitMul(HMul* mul) {
5084 LocationSummary* locations =
5085 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5086 switch (mul->GetResultType()) {
5087 case Primitive::kPrimInt:
5088 case Primitive::kPrimLong:
5089 locations->SetInAt(0, Location::RequiresRegister());
5090 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005092 break;
5093
5094 case Primitive::kPrimFloat:
5095 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005096 locations->SetInAt(0, Location::RequiresFpuRegister());
5097 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005098 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005099 break;
5100
5101 default:
5102 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5103 }
5104}
5105
5106void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5107 switch (mul->GetResultType()) {
5108 case Primitive::kPrimInt:
5109 case Primitive::kPrimLong:
5110 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5111 break;
5112
5113 case Primitive::kPrimFloat:
5114 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005115 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005116 break;
5117
5118 default:
5119 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5120 }
5121}
5122
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005123void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5124 LocationSummary* locations =
5125 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5126 switch (neg->GetResultType()) {
5127 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005128 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005129 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005131 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005132
5133 case Primitive::kPrimFloat:
5134 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005135 locations->SetInAt(0, Location::RequiresFpuRegister());
5136 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005137 break;
5138
5139 default:
5140 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5141 }
5142}
5143
5144void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5145 switch (neg->GetResultType()) {
5146 case Primitive::kPrimInt:
5147 case Primitive::kPrimLong:
5148 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5149 break;
5150
5151 case Primitive::kPrimFloat:
5152 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005153 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005154 break;
5155
5156 default:
5157 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5158 }
5159}
5160
5161void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5162 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005164 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005165 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005166 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5167 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005168}
5169
5170void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005171 // Note: if heap poisoning is enabled, the entry point takes cares
5172 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005173 QuickEntrypointEnum entrypoint =
5174 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5175 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005176 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005177}
5178
Alexandre Rames5319def2014-10-23 10:03:10 +01005179void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5180 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005182 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005183 if (instruction->IsStringAlloc()) {
5184 locations->AddTemp(LocationFrom(kArtMethodRegister));
5185 } else {
5186 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005187 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005188 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5189}
5190
5191void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005192 // Note: if heap poisoning is enabled, the entry point takes cares
5193 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005194 if (instruction->IsStringAlloc()) {
5195 // String is allocated through StringFactory. Call NewEmptyString entry point.
5196 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005197 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005198 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5199 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005200
5201 {
5202 // Ensure the pc position is recorded immediately after the `blr` instruction.
5203 ExactAssemblyScope eas(GetVIXLAssembler(),
5204 kInstructionSize,
5205 CodeBufferCheckScope::kExactSize);
5206 __ blr(lr);
5207 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5208 }
David Brazdil6de19382016-01-08 17:37:10 +00005209 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005210 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005211 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005212 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005213}
5214
5215void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005217 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005218 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005219}
5220
5221void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005222 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005223 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005224 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005225 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005226 break;
5227
5228 default:
5229 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5230 }
5231}
5232
David Brazdil66d126e2015-04-03 16:02:44 +01005233void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5234 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5235 locations->SetInAt(0, Location::RequiresRegister());
5236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5237}
5238
5239void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005240 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005241}
5242
Alexandre Rames5319def2014-10-23 10:03:10 +01005243void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005244 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5245 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005246}
5247
Calin Juravle2ae48182016-03-16 14:05:09 +00005248void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5249 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005250 return;
5251 }
Artem Serov914d7a82017-02-07 14:33:49 +00005252 {
5253 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5254 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5255 Location obj = instruction->GetLocations()->InAt(0);
5256 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5257 RecordPcInfo(instruction, instruction->GetDexPc());
5258 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005259}
5260
Calin Juravle2ae48182016-03-16 14:05:09 +00005261void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005262 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005263 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005264
5265 LocationSummary* locations = instruction->GetLocations();
5266 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005267
5268 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005269}
5270
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005271void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005272 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005273}
5274
Alexandre Rames67555f72014-11-18 10:55:16 +00005275void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5276 HandleBinaryOp(instruction);
5277}
5278
5279void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5280 HandleBinaryOp(instruction);
5281}
5282
Alexandre Rames3e69f162014-12-10 10:36:50 +00005283void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5284 LOG(FATAL) << "Unreachable";
5285}
5286
5287void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5288 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5289}
5290
Alexandre Rames5319def2014-10-23 10:03:10 +01005291void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5293 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5294 if (location.IsStackSlot()) {
5295 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5296 } else if (location.IsDoubleStackSlot()) {
5297 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5298 }
5299 locations->SetOut(location);
5300}
5301
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005302void InstructionCodeGeneratorARM64::VisitParameterValue(
5303 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005304 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005305}
5306
5307void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5308 LocationSummary* locations =
5309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005310 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005311}
5312
5313void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5314 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5315 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005316}
5317
5318void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005320 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005321 locations->SetInAt(i, Location::Any());
5322 }
5323 locations->SetOut(Location::Any());
5324}
5325
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005326void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005327 LOG(FATAL) << "Unreachable";
5328}
5329
Serban Constantinescu02164b32014-11-13 14:05:07 +00005330void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005331 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005332 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005333 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5334 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5336
5337 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005338 case Primitive::kPrimInt:
5339 case Primitive::kPrimLong:
5340 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005341 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5343 break;
5344
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005345 case Primitive::kPrimFloat:
5346 case Primitive::kPrimDouble: {
5347 InvokeRuntimeCallingConvention calling_convention;
5348 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5349 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5350 locations->SetOut(calling_convention.GetReturnLocation(type));
5351
5352 break;
5353 }
5354
Serban Constantinescu02164b32014-11-13 14:05:07 +00005355 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005356 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005357 }
5358}
5359
5360void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5361 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005362
Serban Constantinescu02164b32014-11-13 14:05:07 +00005363 switch (type) {
5364 case Primitive::kPrimInt:
5365 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005366 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005367 break;
5368 }
5369
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005370 case Primitive::kPrimFloat:
5371 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005372 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5373 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005374 if (type == Primitive::kPrimFloat) {
5375 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5376 } else {
5377 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5378 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005379 break;
5380 }
5381
Serban Constantinescu02164b32014-11-13 14:05:07 +00005382 default:
5383 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005384 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005385 }
5386}
5387
Calin Juravle27df7582015-04-17 19:12:31 +01005388void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5389 memory_barrier->SetLocations(nullptr);
5390}
5391
5392void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005393 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005394}
5395
Alexandre Rames5319def2014-10-23 10:03:10 +01005396void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5398 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005399 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005400}
5401
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005402void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005403 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005404}
5405
5406void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5407 instruction->SetLocations(nullptr);
5408}
5409
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005410void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005411 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005412}
5413
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005414void LocationsBuilderARM64::VisitRor(HRor* ror) {
5415 HandleBinaryOp(ror);
5416}
5417
5418void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5419 HandleBinaryOp(ror);
5420}
5421
Serban Constantinescu02164b32014-11-13 14:05:07 +00005422void LocationsBuilderARM64::VisitShl(HShl* shl) {
5423 HandleShift(shl);
5424}
5425
5426void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5427 HandleShift(shl);
5428}
5429
5430void LocationsBuilderARM64::VisitShr(HShr* shr) {
5431 HandleShift(shr);
5432}
5433
5434void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5435 HandleShift(shr);
5436}
5437
Alexandre Rames5319def2014-10-23 10:03:10 +01005438void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005439 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005440}
5441
5442void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005443 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005444}
5445
Alexandre Rames67555f72014-11-18 10:55:16 +00005446void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005447 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005448}
5449
5450void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005451 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005452}
5453
5454void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005455 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005456}
5457
Alexandre Rames67555f72014-11-18 10:55:16 +00005458void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005460}
5461
Calin Juravlee460d1d2015-09-29 04:52:17 +01005462void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5463 HUnresolvedInstanceFieldGet* instruction) {
5464 FieldAccessCallingConventionARM64 calling_convention;
5465 codegen_->CreateUnresolvedFieldLocationSummary(
5466 instruction, instruction->GetFieldType(), calling_convention);
5467}
5468
5469void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5470 HUnresolvedInstanceFieldGet* instruction) {
5471 FieldAccessCallingConventionARM64 calling_convention;
5472 codegen_->GenerateUnresolvedFieldAccess(instruction,
5473 instruction->GetFieldType(),
5474 instruction->GetFieldIndex(),
5475 instruction->GetDexPc(),
5476 calling_convention);
5477}
5478
5479void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5480 HUnresolvedInstanceFieldSet* instruction) {
5481 FieldAccessCallingConventionARM64 calling_convention;
5482 codegen_->CreateUnresolvedFieldLocationSummary(
5483 instruction, instruction->GetFieldType(), calling_convention);
5484}
5485
5486void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5487 HUnresolvedInstanceFieldSet* instruction) {
5488 FieldAccessCallingConventionARM64 calling_convention;
5489 codegen_->GenerateUnresolvedFieldAccess(instruction,
5490 instruction->GetFieldType(),
5491 instruction->GetFieldIndex(),
5492 instruction->GetDexPc(),
5493 calling_convention);
5494}
5495
5496void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5497 HUnresolvedStaticFieldGet* instruction) {
5498 FieldAccessCallingConventionARM64 calling_convention;
5499 codegen_->CreateUnresolvedFieldLocationSummary(
5500 instruction, instruction->GetFieldType(), calling_convention);
5501}
5502
5503void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5504 HUnresolvedStaticFieldGet* instruction) {
5505 FieldAccessCallingConventionARM64 calling_convention;
5506 codegen_->GenerateUnresolvedFieldAccess(instruction,
5507 instruction->GetFieldType(),
5508 instruction->GetFieldIndex(),
5509 instruction->GetDexPc(),
5510 calling_convention);
5511}
5512
5513void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5514 HUnresolvedStaticFieldSet* instruction) {
5515 FieldAccessCallingConventionARM64 calling_convention;
5516 codegen_->CreateUnresolvedFieldLocationSummary(
5517 instruction, instruction->GetFieldType(), calling_convention);
5518}
5519
5520void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5521 HUnresolvedStaticFieldSet* instruction) {
5522 FieldAccessCallingConventionARM64 calling_convention;
5523 codegen_->GenerateUnresolvedFieldAccess(instruction,
5524 instruction->GetFieldType(),
5525 instruction->GetFieldIndex(),
5526 instruction->GetDexPc(),
5527 calling_convention);
5528}
5529
Alexandre Rames5319def2014-10-23 10:03:10 +01005530void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005531 LocationSummary* locations =
5532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005533 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005534}
5535
5536void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005537 HBasicBlock* block = instruction->GetBlock();
5538 if (block->GetLoopInformation() != nullptr) {
5539 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5540 // The back edge will generate the suspend check.
5541 return;
5542 }
5543 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5544 // The goto will generate the suspend check.
5545 return;
5546 }
5547 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005548}
5549
Alexandre Rames67555f72014-11-18 10:55:16 +00005550void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5551 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005553 InvokeRuntimeCallingConvention calling_convention;
5554 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5555}
5556
5557void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005558 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005559 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005560}
5561
5562void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5563 LocationSummary* locations =
5564 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5565 Primitive::Type input_type = conversion->GetInputType();
5566 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005567 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005568 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5569 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5570 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5571 }
5572
Alexandre Rames542361f2015-01-29 16:57:31 +00005573 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005574 locations->SetInAt(0, Location::RequiresFpuRegister());
5575 } else {
5576 locations->SetInAt(0, Location::RequiresRegister());
5577 }
5578
Alexandre Rames542361f2015-01-29 16:57:31 +00005579 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5581 } else {
5582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5583 }
5584}
5585
5586void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5587 Primitive::Type result_type = conversion->GetResultType();
5588 Primitive::Type input_type = conversion->GetInputType();
5589
5590 DCHECK_NE(input_type, result_type);
5591
Alexandre Rames542361f2015-01-29 16:57:31 +00005592 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005593 int result_size = Primitive::ComponentSize(result_type);
5594 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005595 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005596 Register output = OutputRegister(conversion);
5597 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005598 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005599 // 'int' values are used directly as W registers, discarding the top
5600 // bits, so we don't need to sign-extend and can just perform a move.
5601 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5602 // top 32 bits of the target register. We theoretically could leave those
5603 // bits unchanged, but we would have to make sure that no code uses a
5604 // 32bit input value as a 64bit value assuming that the top 32 bits are
5605 // zero.
5606 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005607 } else if (result_type == Primitive::kPrimChar ||
5608 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5609 __ Ubfx(output,
5610 output.IsX() ? source.X() : source.W(),
5611 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005612 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005613 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005614 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005615 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005616 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005617 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005618 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5619 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005620 } else if (Primitive::IsFloatingPointType(result_type) &&
5621 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005622 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5623 } else {
5624 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5625 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005626 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005627}
Alexandre Rames67555f72014-11-18 10:55:16 +00005628
Serban Constantinescu02164b32014-11-13 14:05:07 +00005629void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5630 HandleShift(ushr);
5631}
5632
5633void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5634 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005635}
5636
5637void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5638 HandleBinaryOp(instruction);
5639}
5640
5641void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5642 HandleBinaryOp(instruction);
5643}
5644
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005645void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005646 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005647 LOG(FATAL) << "Unreachable";
5648}
5649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005650void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005651 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005652 LOG(FATAL) << "Unreachable";
5653}
5654
Mark Mendellfe57faa2015-09-18 09:26:15 -04005655// Simple implementation of packed switch - generate cascaded compare/jumps.
5656void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5657 LocationSummary* locations =
5658 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5659 locations->SetInAt(0, Location::RequiresRegister());
5660}
5661
5662void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5663 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005664 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005665 Register value_reg = InputRegisterAt(switch_instr, 0);
5666 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5667
Zheng Xu3927c8b2015-11-18 17:46:25 +08005668 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005669 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005670 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5671 // make sure we don't emit it if the target may run out of range.
5672 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5673 // ranges and emit the tables only as required.
5674 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005675
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005676 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005677 // Current instruction id is an upper bound of the number of HIRs in the graph.
5678 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5679 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005680 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5681 Register temp = temps.AcquireW();
5682 __ Subs(temp, value_reg, Operand(lower_bound));
5683
Zheng Xu3927c8b2015-11-18 17:46:25 +08005684 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005685 // Jump to successors[0] if value == lower_bound.
5686 __ B(eq, codegen_->GetLabelOf(successors[0]));
5687 int32_t last_index = 0;
5688 for (; num_entries - last_index > 2; last_index += 2) {
5689 __ Subs(temp, temp, Operand(2));
5690 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5691 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5692 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5693 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5694 }
5695 if (num_entries - last_index == 2) {
5696 // The last missing case_value.
5697 __ Cmp(temp, Operand(1));
5698 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005699 }
5700
5701 // And the default for any other value.
5702 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5703 __ B(codegen_->GetLabelOf(default_block));
5704 }
5705 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005706 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005707
5708 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5709
5710 // Below instructions should use at most one blocked register. Since there are two blocked
5711 // registers, we are free to block one.
5712 Register temp_w = temps.AcquireW();
5713 Register index;
5714 // Remove the bias.
5715 if (lower_bound != 0) {
5716 index = temp_w;
5717 __ Sub(index, value_reg, Operand(lower_bound));
5718 } else {
5719 index = value_reg;
5720 }
5721
5722 // Jump to default block if index is out of the range.
5723 __ Cmp(index, Operand(num_entries));
5724 __ B(hs, codegen_->GetLabelOf(default_block));
5725
5726 // In current VIXL implementation, it won't require any blocked registers to encode the
5727 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5728 // register pressure.
5729 Register table_base = temps.AcquireX();
5730 // Load jump offset from the table.
5731 __ Adr(table_base, jump_table->GetTableStartLabel());
5732 Register jump_offset = temp_w;
5733 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5734
5735 // Jump to target block by branching to table_base(pc related) + offset.
5736 Register target_address = table_base;
5737 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5738 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005739 }
5740}
5741
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005742void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5743 HInstruction* instruction,
5744 Location out,
5745 uint32_t offset,
5746 Location maybe_temp,
5747 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005748 Primitive::Type type = Primitive::kPrimNot;
5749 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005750 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005751 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005752 Register temp_reg = RegisterFrom(maybe_temp, type);
5753 if (kUseBakerReadBarrier) {
5754 // Load with fast path based Baker's read barrier.
5755 // /* HeapReference<Object> */ out = *(out + offset)
5756 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5757 out,
5758 out_reg,
5759 offset,
5760 temp_reg,
5761 /* needs_null_check */ false,
5762 /* use_load_acquire */ false);
5763 } else {
5764 // Load with slow path based read barrier.
5765 // Save the value of `out` into `maybe_temp` before overwriting it
5766 // in the following move operation, as we will need it for the
5767 // read barrier below.
5768 __ Mov(temp_reg, out_reg);
5769 // /* HeapReference<Object> */ out = *(out + offset)
5770 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5771 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5772 }
5773 } else {
5774 // Plain load with no read barrier.
5775 // /* HeapReference<Object> */ out = *(out + offset)
5776 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5777 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5778 }
5779}
5780
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005781void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5782 HInstruction* instruction,
5783 Location out,
5784 Location obj,
5785 uint32_t offset,
5786 Location maybe_temp,
5787 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005788 Primitive::Type type = Primitive::kPrimNot;
5789 Register out_reg = RegisterFrom(out, type);
5790 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005791 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005792 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005793 if (kUseBakerReadBarrier) {
5794 // Load with fast path based Baker's read barrier.
5795 Register temp_reg = RegisterFrom(maybe_temp, type);
5796 // /* HeapReference<Object> */ out = *(obj + offset)
5797 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5798 out,
5799 obj_reg,
5800 offset,
5801 temp_reg,
5802 /* needs_null_check */ false,
5803 /* use_load_acquire */ false);
5804 } else {
5805 // Load with slow path based read barrier.
5806 // /* HeapReference<Object> */ out = *(obj + offset)
5807 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5808 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5809 }
5810 } else {
5811 // Plain load with no read barrier.
5812 // /* HeapReference<Object> */ out = *(obj + offset)
5813 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5814 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5815 }
5816}
5817
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005818void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5819 HInstruction* instruction,
5820 Location root,
5821 Register obj,
5822 uint32_t offset,
5823 vixl::aarch64::Label* fixup_label,
5824 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005825 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005826 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005827 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005828 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005829 if (kUseBakerReadBarrier) {
5830 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005831 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005832 //
Roland Levillainba650a42017-03-06 13:52:32 +00005833 // Note that we do not actually check the value of
5834 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5835 // root or not. Instead, we load into `temp` the read barrier
5836 // mark entry point corresponding to register `root`. If `temp`
5837 // is null, it means that `GetIsGcMarking()` is false, and vice
5838 // versa.
5839 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005840 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005841 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5842 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5843 // // Slow path.
5844 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005845 // }
5846
Roland Levillainba650a42017-03-06 13:52:32 +00005847 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5848 Register temp = lr;
5849 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5850 instruction, root, /* entrypoint */ LocationFrom(temp));
5851 codegen_->AddSlowPath(slow_path);
5852
5853 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5854 const int32_t entry_point_offset =
5855 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5856 // Loading the entrypoint does not require a load acquire since it is only changed when
5857 // threads are suspended or running a checkpoint.
5858 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5859
Roland Levillain44015862016-01-22 11:47:17 +00005860 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 if (fixup_label == nullptr) {
5862 __ Ldr(root_reg, MemOperand(obj, offset));
5863 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005864 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865 }
Roland Levillain44015862016-01-22 11:47:17 +00005866 static_assert(
5867 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5868 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5869 "have different sizes.");
5870 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5871 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5872 "have different sizes.");
5873
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005874 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5875 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005876 __ Cbnz(temp, slow_path->GetEntryLabel());
5877 __ Bind(slow_path->GetExitLabel());
5878 } else {
5879 // GC root loaded through a slow path for read barriers other
5880 // than Baker's.
5881 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 if (fixup_label == nullptr) {
5883 __ Add(root_reg.X(), obj.X(), offset);
5884 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005885 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886 }
Roland Levillain44015862016-01-22 11:47:17 +00005887 // /* mirror::Object* */ root = root->Read()
5888 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5889 }
5890 } else {
5891 // Plain GC root load with no read barrier.
5892 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 if (fixup_label == nullptr) {
5894 __ Ldr(root_reg, MemOperand(obj, offset));
5895 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005896 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897 }
Roland Levillain44015862016-01-22 11:47:17 +00005898 // Note that GC roots are not affected by heap poisoning, thus we
5899 // do not have to unpoison `root_reg` here.
5900 }
5901}
5902
5903void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5904 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005905 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005906 uint32_t offset,
5907 Register temp,
5908 bool needs_null_check,
5909 bool use_load_acquire) {
5910 DCHECK(kEmitCompilerReadBarrier);
5911 DCHECK(kUseBakerReadBarrier);
5912
5913 // /* HeapReference<Object> */ ref = *(obj + offset)
5914 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005915 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005916 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5917 ref,
5918 obj,
5919 offset,
5920 no_index,
5921 no_scale_factor,
5922 temp,
5923 needs_null_check,
5924 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005925}
5926
5927void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5928 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005929 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005930 uint32_t data_offset,
5931 Location index,
5932 Register temp,
5933 bool needs_null_check) {
5934 DCHECK(kEmitCompilerReadBarrier);
5935 DCHECK(kUseBakerReadBarrier);
5936
5937 // Array cells are never volatile variables, therefore array loads
5938 // never use Load-Acquire instructions on ARM64.
5939 const bool use_load_acquire = false;
5940
Roland Levillainbfea3352016-06-23 13:48:47 +01005941 static_assert(
5942 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5943 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005944 // /* HeapReference<Object> */ ref =
5945 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005946 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5947 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5948 ref,
5949 obj,
5950 data_offset,
5951 index,
5952 scale_factor,
5953 temp,
5954 needs_null_check,
5955 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005956}
5957
5958void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5959 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005960 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005961 uint32_t offset,
5962 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005963 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005964 Register temp,
5965 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005966 bool use_load_acquire,
5967 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005968 DCHECK(kEmitCompilerReadBarrier);
5969 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005970 // If we are emitting an array load, we should not be using a
5971 // Load Acquire instruction. In other words:
5972 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5973 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005974
Roland Levillain54f869e2017-03-06 13:54:11 +00005975 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
5976 // whether we need to enter the slow path to mark the reference.
5977 // Then, in the slow path, check the gray bit in the lock word of
5978 // the reference's holder (`obj`) to decide whether to mark `ref` or
5979 // not.
Roland Levillain44015862016-01-22 11:47:17 +00005980 //
Roland Levillainba650a42017-03-06 13:52:32 +00005981 // Note that we do not actually check the value of `GetIsGcMarking()`;
5982 // instead, we load into `temp2` the read barrier mark entry point
5983 // corresponding to register `ref`. If `temp2` is null, it means
5984 // that `GetIsGcMarking()` is false, and vice versa.
5985 //
5986 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005987 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5988 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00005989 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5990 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5991 // HeapReference<mirror::Object> ref = *src; // Original reference load.
5992 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5993 // if (is_gray) {
5994 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
5995 // }
5996 // } else {
5997 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00005998 // }
Roland Levillain44015862016-01-22 11:47:17 +00005999
Roland Levillainba650a42017-03-06 13:52:32 +00006000 // Slow path marking the object `ref` when the GC is marking. The
6001 // entrypoint will already be loaded in `temp2`.
6002 Register temp2 = lr;
6003 Location temp2_loc = LocationFrom(temp2);
6004 SlowPathCodeARM64* slow_path;
6005 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006006 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6007 // only supports address of the form `obj + field_offset`, where
6008 // `obj` is a register and `field_offset` is a register. Thus
6009 // `offset` and `scale_factor` above are expected to be null in
6010 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006011 DCHECK_EQ(offset, 0u);
6012 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006013 Location field_offset = index;
6014 slow_path =
6015 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6016 instruction,
6017 ref,
6018 obj,
6019 offset,
6020 /* index */ field_offset,
6021 scale_factor,
6022 needs_null_check,
6023 use_load_acquire,
6024 temp,
6025 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006026 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006027 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6028 instruction,
6029 ref,
6030 obj,
6031 offset,
6032 index,
6033 scale_factor,
6034 needs_null_check,
6035 use_load_acquire,
6036 temp,
6037 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006038 }
6039 AddSlowPath(slow_path);
6040
6041 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6042 const int32_t entry_point_offset =
6043 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6044 // Loading the entrypoint does not require a load acquire since it is only changed when
6045 // threads are suspended or running a checkpoint.
6046 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006047 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6048 // checking GetIsGcMarking.
6049 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006050 // Fast path: just load the reference.
6051 GenerateRawReferenceLoad(
6052 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006053 __ Bind(slow_path->GetExitLabel());
6054}
6055
6056void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6057 Location ref,
6058 Register obj,
6059 uint32_t offset,
6060 Location index,
6061 size_t scale_factor,
6062 bool needs_null_check,
6063 bool use_load_acquire) {
6064 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006065 Primitive::Type type = Primitive::kPrimNot;
6066 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006067
Roland Levillainba650a42017-03-06 13:52:32 +00006068 // If needed, vixl::EmissionCheckScope guards are used to ensure
6069 // that no pools are emitted between the load (macro) instruction
6070 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006071
Roland Levillain44015862016-01-22 11:47:17 +00006072 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006073 // Load types involving an "index": ArrayGet,
6074 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6075 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006076 if (use_load_acquire) {
6077 // UnsafeGetObjectVolatile intrinsic case.
6078 // Register `index` is not an index in an object array, but an
6079 // offset to an object reference field within object `obj`.
6080 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6081 DCHECK(instruction->GetLocations()->Intrinsified());
6082 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6083 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006084 DCHECK_EQ(offset, 0u);
6085 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006086 DCHECK_EQ(needs_null_check, false);
6087 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006088 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6089 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006090 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006091 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6092 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006093 if (index.IsConstant()) {
6094 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006095 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006096 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006097 if (needs_null_check) {
6098 MaybeRecordImplicitNullCheck(instruction);
6099 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006100 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006101 UseScratchRegisterScope temps(GetVIXLAssembler());
6102 Register temp = temps.AcquireW();
6103 __ Add(temp, obj, offset);
6104 {
6105 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6106 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6107 if (needs_null_check) {
6108 MaybeRecordImplicitNullCheck(instruction);
6109 }
6110 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006111 }
Roland Levillain44015862016-01-22 11:47:17 +00006112 }
Roland Levillain44015862016-01-22 11:47:17 +00006113 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006114 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006115 MemOperand field = HeapOperand(obj, offset);
6116 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006117 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6118 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006119 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006120 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006121 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006122 if (needs_null_check) {
6123 MaybeRecordImplicitNullCheck(instruction);
6124 }
Roland Levillain44015862016-01-22 11:47:17 +00006125 }
6126 }
6127
6128 // Object* ref = ref_addr->AsMirrorPtr()
6129 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006130}
6131
6132void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6133 Location out,
6134 Location ref,
6135 Location obj,
6136 uint32_t offset,
6137 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006138 DCHECK(kEmitCompilerReadBarrier);
6139
Roland Levillain44015862016-01-22 11:47:17 +00006140 // Insert a slow path based read barrier *after* the reference load.
6141 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006142 // If heap poisoning is enabled, the unpoisoning of the loaded
6143 // reference will be carried out by the runtime within the slow
6144 // path.
6145 //
6146 // Note that `ref` currently does not get unpoisoned (when heap
6147 // poisoning is enabled), which is alright as the `ref` argument is
6148 // not used by the artReadBarrierSlow entry point.
6149 //
6150 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6151 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6152 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6153 AddSlowPath(slow_path);
6154
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006155 __ B(slow_path->GetEntryLabel());
6156 __ Bind(slow_path->GetExitLabel());
6157}
6158
Roland Levillain44015862016-01-22 11:47:17 +00006159void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6160 Location out,
6161 Location ref,
6162 Location obj,
6163 uint32_t offset,
6164 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006165 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006166 // Baker's read barriers shall be handled by the fast path
6167 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6168 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006169 // If heap poisoning is enabled, unpoisoning will be taken care of
6170 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006171 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006172 } else if (kPoisonHeapReferences) {
6173 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6174 }
6175}
6176
Roland Levillain44015862016-01-22 11:47:17 +00006177void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6178 Location out,
6179 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006180 DCHECK(kEmitCompilerReadBarrier);
6181
Roland Levillain44015862016-01-22 11:47:17 +00006182 // Insert a slow path based read barrier *after* the GC root load.
6183 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006184 // Note that GC roots are not affected by heap poisoning, so we do
6185 // not need to do anything special for this here.
6186 SlowPathCodeARM64* slow_path =
6187 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6188 AddSlowPath(slow_path);
6189
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006190 __ B(slow_path->GetEntryLabel());
6191 __ Bind(slow_path->GetExitLabel());
6192}
6193
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006194void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6195 LocationSummary* locations =
6196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6197 locations->SetInAt(0, Location::RequiresRegister());
6198 locations->SetOut(Location::RequiresRegister());
6199}
6200
6201void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6202 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006204 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006205 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006206 __ Ldr(XRegisterFrom(locations->Out()),
6207 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006208 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006209 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006210 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006211 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6212 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006213 __ Ldr(XRegisterFrom(locations->Out()),
6214 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006215 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006216}
6217
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006218static void PatchJitRootUse(uint8_t* code,
6219 const uint8_t* roots_data,
6220 vixl::aarch64::Literal<uint32_t>* literal,
6221 uint64_t index_in_table) {
6222 uint32_t literal_offset = literal->GetOffset();
6223 uintptr_t address =
6224 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6225 uint8_t* data = code + literal_offset;
6226 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6227}
6228
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006229void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6230 for (const auto& entry : jit_string_patches_) {
6231 const auto& it = jit_string_roots_.find(entry.first);
6232 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006233 PatchJitRootUse(code, roots_data, entry.second, it->second);
6234 }
6235 for (const auto& entry : jit_class_patches_) {
6236 const auto& it = jit_class_roots_.find(entry.first);
6237 DCHECK(it != jit_class_roots_.end());
6238 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006239 }
6240}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006241
Alexandre Rames67555f72014-11-18 10:55:16 +00006242#undef __
6243#undef QUICK_ENTRY_POINT
6244
Alexandre Rames5319def2014-10-23 10:03:10 +01006245} // namespace arm64
6246} // namespace art