blob: 7b6c97cd2331c39e28185142ffe013e9ab7a0cb4 [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 Levillain27b1f9c2017-01-17 16:56:34 +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 Levillaina1aa3b12016-10-26 13:03:38 +0100700// Slow path marking an object reference `ref` during a read
701// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000702// reference does not get updated by this slow path after marking.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100703//
704// 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`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000710//
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 Levillain1372c9f2017-01-13 11:47:39 +0000713// is when the decision to mark is based on whether the GC is marking.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000714class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillain44015862016-01-22 11:47:17 +0000715 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800716 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
717 Location ref,
718 Location entrypoint = Location::NoLocation())
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000719 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000720 DCHECK(kEmitCompilerReadBarrier);
721 }
722
723 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
724
725 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
726 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000727 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100728 DCHECK(ref_.IsRegister()) << ref_;
729 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain27b1f9c2017-01-17 16:56:34 +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 Levillain44015862016-01-22 11:47:17 +0000795 DCHECK(instruction_->IsInstanceFieldGet() ||
796 instruction_->IsStaticFieldGet() ||
797 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100798 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000799 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100800 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100801 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
802 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000803 << "Unexpected instruction in read barrier marking slow path: "
804 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000805 // 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()));
Roland Levillain44015862016-01-22 11:47:17 +0000810
811 __ Bind(GetEntryLabel());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000812
813 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
814 // inserted after the original load. However, in fast path based
815 // Baker's read barriers, we need to perform the load of
816 // mirror::Object::monitor_ *before* the original reference load.
817 // This load-load ordering is required by the read barrier.
818 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillain02b75802016-07-13 11:54:35 +0100819 //
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000820 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
821 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
822 // HeapReference<mirror::Object> ref = *src; // Original reference load.
823 // bool is_gray = (rb_state == ReadBarrier::GrayState());
824 // if (is_gray) {
825 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
826 // }
Roland Levillain02b75802016-07-13 11:54:35 +0100827 //
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000828 // Note: the original implementation in ReadBarrier::Barrier is
829 // slightly more complex as it performs additional checks that we do
830 // not do here for performance reasons.
831
832 // /* int32_t */ monitor = obj->monitor_
833 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
834 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
835 if (needs_null_check_) {
836 codegen->MaybeRecordImplicitNullCheck(instruction_);
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800837 }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000838 // /* LockWord */ lock_word = LockWord(monitor)
839 static_assert(sizeof(LockWord) == sizeof(int32_t),
840 "art::LockWord and int32_t have different sizes.");
841
842 // Introduce a dependency on the lock_word including rb_state,
843 // to prevent load-load reordering, and without using
844 // a memory barrier (which would be more expensive).
845 // `obj` is unchanged by this operation, but its value now depends
846 // on `temp`.
847 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
848
849 // The actual reference load.
850 // A possible implicit null check has already been handled above.
851 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
852 arm64_codegen->GenerateRawReferenceLoad(instruction_,
853 ref_,
854 obj_,
855 offset_,
856 index_,
857 scale_factor_,
858 /* needs_null_check */ false,
859 use_load_acquire_);
860
861 // Mark the object `ref` when `obj` is gray.
862 //
863 // if (rb_state == ReadBarrier::GrayState())
864 // ref = ReadBarrier::Mark(ref);
865 //
866 // Given the numeric representation, it's enough to check the low bit of the rb_state.
867 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
868 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
869 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
870 GenerateReadBarrierMarkRuntimeCall(codegen);
871
Roland Levillain44015862016-01-22 11:47:17 +0000872 __ B(GetExitLabel());
873 }
874
875 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000876 // The register containing the object holding the marked object reference field.
877 Register obj_;
878 // The offset, index and scale factor to access the reference in `obj_`.
879 uint32_t offset_;
880 Location index_;
881 size_t scale_factor_;
882 // Is a null check required?
883 bool needs_null_check_;
884 // Should this reference load use Load-Acquire semantics?
885 bool use_load_acquire_;
886 // A temporary register used to hold the lock word of `obj_`.
887 Register temp_;
Roland Levillain44015862016-01-22 11:47:17 +0000888
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000889 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain44015862016-01-22 11:47:17 +0000890};
891
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000892// Slow path loading `obj`'s lock word, loading a reference from
893// object `*(obj + offset + (index << scale_factor))` into `ref`, and
894// marking `ref` if `obj` is gray according to the lock word (Baker
895// read barrier). If needed, this slow path also atomically updates
896// the field `obj.field` in the object `obj` holding this reference
897// after marking (contrary to
898// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
899// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100900//
901// This means that after the execution of this slow path, both `ref`
902// and `obj.field` will be up-to-date; i.e., after the flip, both will
903// hold the same to-space reference (unless another thread installed
904// another object reference (different from `ref`) in `obj.field`).
Roland Levillain1372c9f2017-01-13 11:47:39 +0000905//
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000906// Argument `entrypoint` must be a register location holding the read
907// barrier marking runtime entry point to be invoked.
908class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
909 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100910 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000911 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
912 Location ref,
913 Register obj,
914 uint32_t offset,
915 Location index,
916 size_t scale_factor,
917 bool needs_null_check,
918 bool use_load_acquire,
919 Register temp,
920 Location entrypoint)
921 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100922 obj_(obj),
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000923 offset_(offset),
924 index_(index),
925 scale_factor_(scale_factor),
926 needs_null_check_(needs_null_check),
927 use_load_acquire_(use_load_acquire),
928 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100929 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000930 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100931 }
932
933 const char* GetDescription() const OVERRIDE {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000934 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100935 }
936
937 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
938 LocationSummary* locations = instruction_->GetLocations();
939 Register ref_reg = WRegisterFrom(ref_);
940 DCHECK(locations->CanCall());
941 DCHECK(ref_.IsRegister()) << ref_;
942 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000943 DCHECK(obj_.IsW());
944 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
945
946 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100947 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
948 << "Unexpected instruction in read barrier marking and field updating slow path: "
949 << instruction_->DebugName();
950 DCHECK(instruction_->GetLocations()->Intrinsified());
951 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000952 DCHECK_EQ(offset_, 0u);
953 DCHECK_EQ(scale_factor_, 0u);
954 DCHECK_EQ(use_load_acquire_, false);
955 // The location of the offset of the marked reference field within `obj_`.
956 Location field_offset = index_;
957 DCHECK(field_offset.IsRegister()) << field_offset;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100958
959 __ Bind(GetEntryLabel());
960
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000961 // /* int32_t */ monitor = obj->monitor_
962 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
963 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
964 if (needs_null_check_) {
965 codegen->MaybeRecordImplicitNullCheck(instruction_);
966 }
967 // /* LockWord */ lock_word = LockWord(monitor)
968 static_assert(sizeof(LockWord) == sizeof(int32_t),
969 "art::LockWord and int32_t have different sizes.");
970
971 // Introduce a dependency on the lock_word including rb_state,
972 // to prevent load-load reordering, and without using
973 // a memory barrier (which would be more expensive).
974 // `obj` is unchanged by this operation, but its value now depends
975 // on `temp`.
976 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
977
978 // The actual reference load.
979 // A possible implicit null check has already been handled above.
980 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
981 arm64_codegen->GenerateRawReferenceLoad(instruction_,
982 ref_,
983 obj_,
984 offset_,
985 index_,
986 scale_factor_,
987 /* needs_null_check */ false,
988 use_load_acquire_);
989
990 // Mark the object `ref` when `obj` is gray.
991 //
992 // if (rb_state == ReadBarrier::GrayState())
993 // ref = ReadBarrier::Mark(ref);
994 //
995 // Given the numeric representation, it's enough to check the low bit of the rb_state.
996 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
997 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
998 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
999
1000 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001001 // Note that we cannot use IP to save the old reference, as IP is
1002 // used internally by the ReadBarrierMarkRegX entry point, and we
1003 // need the old reference after the call to that entry point.
1004 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1005 __ Mov(temp_.W(), ref_reg);
1006
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001007 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001008
1009 // If the new reference is different from the old reference,
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001010 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001011 //
1012 // Note that this field could also hold a different object, if
1013 // another thread had concurrently changed it. In that case, the
1014 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1015 // (CAS) operation below would abort the CAS, leaving the field
1016 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017 __ Cmp(temp_.W(), ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001018 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001019
1020 // Update the the holder's field atomically. This may fail if
1021 // mutator updates before us, but it's OK. This is achieved
1022 // using a strong compare-and-set (CAS) operation with relaxed
1023 // memory synchronization ordering, where the expected value is
1024 // the old reference and the desired value is the new reference.
1025
1026 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1027 UseScratchRegisterScope temps(masm);
1028
1029 // Convenience aliases.
1030 Register base = obj_.W();
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001031 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001032 Register expected = temp_.W();
1033 Register value = ref_reg;
1034 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1035 Register tmp_value = temps.AcquireW(); // Value in memory.
1036
1037 __ Add(tmp_ptr, base.X(), Operand(offset));
1038
1039 if (kPoisonHeapReferences) {
1040 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1041 if (value.Is(expected)) {
1042 // Do not poison `value`, as it is the same register as
1043 // `expected`, which has just been poisoned.
1044 } else {
1045 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1046 }
1047 }
1048
1049 // do {
1050 // tmp_value = [tmp_ptr] - expected;
1051 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1052
Roland Levillain24a4d112016-10-26 13:10:46 +01001053 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001054 __ Bind(&loop_head);
1055 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1056 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001057 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001058 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1059 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001060 __ B(&exit_loop);
1061 __ Bind(&comparison_failed);
1062 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063 __ Bind(&exit_loop);
1064
1065 if (kPoisonHeapReferences) {
1066 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1067 if (value.Is(expected)) {
1068 // Do not unpoison `value`, as it is the same register as
1069 // `expected`, which has just been unpoisoned.
1070 } else {
1071 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1072 }
1073 }
1074
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001075 __ B(GetExitLabel());
1076 }
1077
1078 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 // The register containing the object holding the marked object reference field.
1080 const Register obj_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001081 // The offset, index and scale factor to access the reference in `obj_`.
1082 uint32_t offset_;
1083 Location index_;
1084 size_t scale_factor_;
1085 // Is a null check required?
1086 bool needs_null_check_;
1087 // Should this reference load use Load-Acquire semantics?
1088 bool use_load_acquire_;
1089 // A temporary register used to hold the lock word of `obj_`; and
1090 // also to hold the original reference value, when the reference is
1091 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001092 const Register temp_;
1093
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001094 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095};
1096
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001097// Slow path generating a read barrier for a heap reference.
1098class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1099 public:
1100 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1101 Location out,
1102 Location ref,
1103 Location obj,
1104 uint32_t offset,
1105 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001106 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001107 out_(out),
1108 ref_(ref),
1109 obj_(obj),
1110 offset_(offset),
1111 index_(index) {
1112 DCHECK(kEmitCompilerReadBarrier);
1113 // If `obj` is equal to `out` or `ref`, it means the initial object
1114 // has been overwritten by (or after) the heap object reference load
1115 // to be instrumented, e.g.:
1116 //
1117 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001118 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001119 //
1120 // In that case, we have lost the information about the original
1121 // object, and the emitted read barrier cannot work properly.
1122 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1123 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1124 }
1125
1126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1127 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1128 LocationSummary* locations = instruction_->GetLocations();
1129 Primitive::Type type = Primitive::kPrimNot;
1130 DCHECK(locations->CanCall());
1131 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001132 DCHECK(instruction_->IsInstanceFieldGet() ||
1133 instruction_->IsStaticFieldGet() ||
1134 instruction_->IsArrayGet() ||
1135 instruction_->IsInstanceOf() ||
1136 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +01001137 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +00001138 << "Unexpected instruction in read barrier for heap reference slow path: "
1139 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001140 // The read barrier instrumentation of object ArrayGet
1141 // instructions does not support the HIntermediateAddress
1142 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001143 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001144 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001145
1146 __ Bind(GetEntryLabel());
1147
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001148 SaveLiveRegisters(codegen, locations);
1149
1150 // We may have to change the index's value, but as `index_` is a
1151 // constant member (like other "inputs" of this slow path),
1152 // introduce a copy of it, `index`.
1153 Location index = index_;
1154 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001155 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001156 if (instruction_->IsArrayGet()) {
1157 // Compute the actual memory offset and store it in `index`.
1158 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1159 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1160 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1161 // We are about to change the value of `index_reg` (see the
1162 // calls to vixl::MacroAssembler::Lsl and
1163 // vixl::MacroAssembler::Mov below), but it has
1164 // not been saved by the previous call to
1165 // art::SlowPathCode::SaveLiveRegisters, as it is a
1166 // callee-save register --
1167 // art::SlowPathCode::SaveLiveRegisters does not consider
1168 // callee-save registers, as it has been designed with the
1169 // assumption that callee-save registers are supposed to be
1170 // handled by the called function. So, as a callee-save
1171 // register, `index_reg` _would_ eventually be saved onto
1172 // the stack, but it would be too late: we would have
1173 // changed its value earlier. Therefore, we manually save
1174 // it here into another freely available register,
1175 // `free_reg`, chosen of course among the caller-save
1176 // registers (as a callee-save `free_reg` register would
1177 // exhibit the same problem).
1178 //
1179 // Note we could have requested a temporary register from
1180 // the register allocator instead; but we prefer not to, as
1181 // this is a slow path, and we know we can find a
1182 // caller-save register that is available.
1183 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1184 __ Mov(free_reg.W(), index_reg);
1185 index_reg = free_reg;
1186 index = LocationFrom(index_reg);
1187 } else {
1188 // The initial register stored in `index_` has already been
1189 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1190 // (as it is not a callee-save register), so we can freely
1191 // use it.
1192 }
1193 // Shifting the index value contained in `index_reg` by the scale
1194 // factor (2) cannot overflow in practice, as the runtime is
1195 // unable to allocate object arrays with a size larger than
1196 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1197 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1198 static_assert(
1199 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1200 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1201 __ Add(index_reg, index_reg, Operand(offset_));
1202 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001203 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1204 // intrinsics, `index_` is not shifted by a scale factor of 2
1205 // (as in the case of ArrayGet), as it is actually an offset
1206 // to an object field within an object.
1207 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001208 DCHECK(instruction_->GetLocations()->Intrinsified());
1209 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1210 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1211 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001212 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001213 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001214 }
1215 }
1216
1217 // We're moving two or three locations to locations that could
1218 // overlap, so we need a parallel move resolver.
1219 InvokeRuntimeCallingConvention calling_convention;
1220 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1221 parallel_move.AddMove(ref_,
1222 LocationFrom(calling_convention.GetRegisterAt(0)),
1223 type,
1224 nullptr);
1225 parallel_move.AddMove(obj_,
1226 LocationFrom(calling_convention.GetRegisterAt(1)),
1227 type,
1228 nullptr);
1229 if (index.IsValid()) {
1230 parallel_move.AddMove(index,
1231 LocationFrom(calling_convention.GetRegisterAt(2)),
1232 Primitive::kPrimInt,
1233 nullptr);
1234 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1235 } else {
1236 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1237 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1238 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001239 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001240 instruction_,
1241 instruction_->GetDexPc(),
1242 this);
1243 CheckEntrypointTypes<
1244 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1245 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1246
1247 RestoreLiveRegisters(codegen, locations);
1248
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001249 __ B(GetExitLabel());
1250 }
1251
1252 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1253
1254 private:
1255 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001256 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1257 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001258 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1259 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1260 return Register(VIXLRegCodeFromART(i), kXRegSize);
1261 }
1262 }
1263 // We shall never fail to find a free caller-save register, as
1264 // there are more than two core caller-save registers on ARM64
1265 // (meaning it is possible to find one which is different from
1266 // `ref` and `obj`).
1267 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1268 LOG(FATAL) << "Could not find a free register";
1269 UNREACHABLE();
1270 }
1271
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001272 const Location out_;
1273 const Location ref_;
1274 const Location obj_;
1275 const uint32_t offset_;
1276 // An additional location containing an index to an array.
1277 // Only used for HArrayGet and the UnsafeGetObject &
1278 // UnsafeGetObjectVolatile intrinsics.
1279 const Location index_;
1280
1281 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1282};
1283
1284// Slow path generating a read barrier for a GC root.
1285class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1286 public:
1287 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001288 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001289 DCHECK(kEmitCompilerReadBarrier);
1290 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001291
1292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1293 LocationSummary* locations = instruction_->GetLocations();
1294 Primitive::Type type = Primitive::kPrimNot;
1295 DCHECK(locations->CanCall());
1296 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001297 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1298 << "Unexpected instruction in read barrier for GC root slow path: "
1299 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001300
1301 __ Bind(GetEntryLabel());
1302 SaveLiveRegisters(codegen, locations);
1303
1304 InvokeRuntimeCallingConvention calling_convention;
1305 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1306 // The argument of the ReadBarrierForRootSlow is not a managed
1307 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1308 // thus we need a 64-bit move here, and we cannot use
1309 //
1310 // arm64_codegen->MoveLocation(
1311 // LocationFrom(calling_convention.GetRegisterAt(0)),
1312 // root_,
1313 // type);
1314 //
1315 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1316 // reference type (`Primitive::kPrimNot`).
1317 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001318 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001319 instruction_,
1320 instruction_->GetDexPc(),
1321 this);
1322 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1323 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1324
1325 RestoreLiveRegisters(codegen, locations);
1326 __ B(GetExitLabel());
1327 }
1328
1329 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1330
1331 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001332 const Location out_;
1333 const Location root_;
1334
1335 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1336};
1337
Alexandre Rames5319def2014-10-23 10:03:10 +01001338#undef __
1339
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001340Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001341 Location next_location;
1342 if (type == Primitive::kPrimVoid) {
1343 LOG(FATAL) << "Unreachable type " << type;
1344 }
1345
Alexandre Rames542361f2015-01-29 16:57:31 +00001346 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001347 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1348 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001349 } else if (!Primitive::IsFloatingPointType(type) &&
1350 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001351 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1352 } else {
1353 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001354 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1355 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 }
1357
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001359 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 return next_location;
1361}
1362
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001363Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001364 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001365}
1366
Serban Constantinescu579885a2015-02-22 20:51:33 +00001367CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1368 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001369 const CompilerOptions& compiler_options,
1370 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001371 : CodeGenerator(graph,
1372 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001373 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001374 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001375 callee_saved_core_registers.GetList(),
1376 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001377 compiler_options,
1378 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001379 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001380 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001382 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001383 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001384 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001385 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001386 uint32_literals_(std::less<uint32_t>(),
1387 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001388 uint64_literals_(std::less<uint64_t>(),
1389 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001390 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1391 boot_image_string_patches_(StringReferenceValueComparator(),
1392 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1393 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001394 boot_image_type_patches_(TypeReferenceValueComparator(),
1395 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1396 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001397 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001398 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001399 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1400 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001401 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1402 jit_class_patches_(TypeReferenceValueComparator(),
1403 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001404 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001405 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001406}
Alexandre Rames5319def2014-10-23 10:03:10 +01001407
Alexandre Rames67555f72014-11-18 10:55:16 +00001408#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001409
Zheng Xu3927c8b2015-11-18 17:46:25 +08001410void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001411 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001412 jump_table->EmitTable(this);
1413 }
1414}
1415
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001416void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001417 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001418 // Ensure we emit the literal pool.
1419 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001420
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001421 CodeGenerator::Finalize(allocator);
1422}
1423
Zheng Xuad4450e2015-04-17 18:48:56 +08001424void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1425 // Note: There are 6 kinds of moves:
1426 // 1. constant -> GPR/FPR (non-cycle)
1427 // 2. constant -> stack (non-cycle)
1428 // 3. GPR/FPR -> GPR/FPR
1429 // 4. GPR/FPR -> stack
1430 // 5. stack -> GPR/FPR
1431 // 6. stack -> stack (non-cycle)
1432 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1433 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1434 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1435 // dependency.
1436 vixl_temps_.Open(GetVIXLAssembler());
1437}
1438
1439void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1440 vixl_temps_.Close();
1441}
1442
1443Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1444 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1445 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1446 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1447 Location scratch = GetScratchLocation(kind);
1448 if (!scratch.Equals(Location::NoLocation())) {
1449 return scratch;
1450 }
1451 // Allocate from VIXL temp registers.
1452 if (kind == Location::kRegister) {
1453 scratch = LocationFrom(vixl_temps_.AcquireX());
1454 } else {
1455 DCHECK(kind == Location::kFpuRegister);
1456 scratch = LocationFrom(vixl_temps_.AcquireD());
1457 }
1458 AddScratchLocation(scratch);
1459 return scratch;
1460}
1461
1462void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1463 if (loc.IsRegister()) {
1464 vixl_temps_.Release(XRegisterFrom(loc));
1465 } else {
1466 DCHECK(loc.IsFpuRegister());
1467 vixl_temps_.Release(DRegisterFrom(loc));
1468 }
1469 RemoveScratchLocation(loc);
1470}
1471
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001473 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001474 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475}
1476
Alexandre Rames5319def2014-10-23 10:03:10 +01001477void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001478 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001479 __ Bind(&frame_entry_label_);
1480
Serban Constantinescu02164b32014-11-13 14:05:07 +00001481 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1482 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001483 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001484 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001485 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001486 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001487 {
1488 // Ensure that between load and RecordPcInfo there are no pools emitted.
1489 ExactAssemblyScope eas(GetVIXLAssembler(),
1490 kInstructionSize,
1491 CodeBufferCheckScope::kExactSize);
1492 __ ldr(wzr, MemOperand(temp, 0));
1493 RecordPcInfo(nullptr, 0);
1494 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001495 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001496
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001497 if (!HasEmptyFrame()) {
1498 int frame_size = GetFrameSize();
1499 // Stack layout:
1500 // sp[frame_size - 8] : lr.
1501 // ... : other preserved core registers.
1502 // ... : other preserved fp registers.
1503 // ... : reserved frame space.
1504 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001505
1506 // Save the current method if we need it. Note that we do not
1507 // do this in HCurrentMethod, as the instruction might have been removed
1508 // in the SSA graph.
1509 if (RequiresCurrentMethod()) {
1510 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001511 } else {
1512 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001513 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001514 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001515 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1516 frame_size - GetCoreSpillSize());
1517 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1518 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001519
1520 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1521 // Initialize should_deoptimize flag to 0.
1522 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1523 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1524 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001525 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001526}
1527
1528void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001529 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001530 if (!HasEmptyFrame()) {
1531 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001532 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1533 frame_size - FrameEntrySpillSize());
1534 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1535 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001536 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001537 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001538 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001539 __ Ret();
1540 GetAssembler()->cfi().RestoreState();
1541 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001542}
1543
Scott Wakeling97c72b72016-06-24 16:19:36 +01001544CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001545 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001546 return CPURegList(CPURegister::kRegister, kXRegSize,
1547 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001548}
1549
Scott Wakeling97c72b72016-06-24 16:19:36 +01001550CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001551 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1552 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001553 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1554 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001555}
1556
Alexandre Rames5319def2014-10-23 10:03:10 +01001557void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1558 __ Bind(GetLabelOf(block));
1559}
1560
Calin Juravle175dc732015-08-25 15:42:32 +01001561void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1562 DCHECK(location.IsRegister());
1563 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1564}
1565
Calin Juravlee460d1d2015-09-29 04:52:17 +01001566void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1567 if (location.IsRegister()) {
1568 locations->AddTemp(location);
1569 } else {
1570 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1571 }
1572}
1573
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001574void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001575 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001576 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001577 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001578 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001579 if (value_can_be_null) {
1580 __ Cbz(value, &done);
1581 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001582 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001583 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001584 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001585 if (value_can_be_null) {
1586 __ Bind(&done);
1587 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001588}
1589
David Brazdil58282f42016-01-14 12:45:10 +00001590void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001591 // Blocked core registers:
1592 // lr : Runtime reserved.
1593 // tr : Runtime reserved.
1594 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1595 // ip1 : VIXL core temp.
1596 // ip0 : VIXL core temp.
1597 //
1598 // Blocked fp registers:
1599 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1601 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001602 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001603 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001604 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001605
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001606 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001607 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001608 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001609 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001610
David Brazdil58282f42016-01-14 12:45:10 +00001611 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001612 // Stubs do not save callee-save floating point registers. If the graph
1613 // is debuggable, we need to deal with these registers differently. For
1614 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001615 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1616 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001617 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001618 }
1619 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001620}
1621
Alexandre Rames3e69f162014-12-10 10:36:50 +00001622size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1623 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1624 __ Str(reg, MemOperand(sp, stack_index));
1625 return kArm64WordSize;
1626}
1627
1628size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1629 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1630 __ Ldr(reg, MemOperand(sp, stack_index));
1631 return kArm64WordSize;
1632}
1633
1634size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1635 FPRegister reg = FPRegister(reg_id, kDRegSize);
1636 __ Str(reg, MemOperand(sp, stack_index));
1637 return kArm64WordSize;
1638}
1639
1640size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1641 FPRegister reg = FPRegister(reg_id, kDRegSize);
1642 __ Ldr(reg, MemOperand(sp, stack_index));
1643 return kArm64WordSize;
1644}
1645
Alexandre Rames5319def2014-10-23 10:03:10 +01001646void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001647 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001648}
1649
1650void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001651 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001652}
1653
Alexandre Rames67555f72014-11-18 10:55:16 +00001654void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001655 if (constant->IsIntConstant()) {
1656 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1657 } else if (constant->IsLongConstant()) {
1658 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1659 } else if (constant->IsNullConstant()) {
1660 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001661 } else if (constant->IsFloatConstant()) {
1662 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1663 } else {
1664 DCHECK(constant->IsDoubleConstant());
1665 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1666 }
1667}
1668
Alexandre Rames3e69f162014-12-10 10:36:50 +00001669
1670static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1671 DCHECK(constant.IsConstant());
1672 HConstant* cst = constant.GetConstant();
1673 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001674 // Null is mapped to a core W register, which we associate with kPrimInt.
1675 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001676 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1677 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1678 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1679}
1680
Roland Levillain558dea12017-01-27 19:40:44 +00001681// Allocate a scratch register from the VIXL pool, querying first into
1682// the floating-point register pool, and then the the core register
1683// pool. This is essentially a reimplementation of
1684// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1685// using a different allocation strategy.
1686static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1687 vixl::aarch64::UseScratchRegisterScope* temps,
1688 int size_in_bits) {
1689 return masm->GetScratchFPRegisterList()->IsEmpty()
1690 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1691 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1692}
1693
Calin Juravlee460d1d2015-09-29 04:52:17 +01001694void CodeGeneratorARM64::MoveLocation(Location destination,
1695 Location source,
1696 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001697 if (source.Equals(destination)) {
1698 return;
1699 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001700
1701 // A valid move can always be inferred from the destination and source
1702 // locations. When moving from and to a register, the argument type can be
1703 // used to generate 32bit instead of 64bit moves. In debug mode we also
1704 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001705 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001706
1707 if (destination.IsRegister() || destination.IsFpuRegister()) {
1708 if (unspecified_type) {
1709 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1710 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001711 (src_cst != nullptr && (src_cst->IsIntConstant()
1712 || src_cst->IsFloatConstant()
1713 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001714 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001715 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001716 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001717 // If the source is a double stack slot or a 64bit constant, a 64bit
1718 // type is appropriate. Else the source is a register, and since the
1719 // type has not been specified, we chose a 64bit type to force a 64bit
1720 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001721 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001723 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001724 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1725 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1726 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001727 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1728 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1729 __ Ldr(dst, StackOperandFrom(source));
1730 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001731 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001732 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001733 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001734 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001735 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001736 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001737 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001738 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1739 ? Primitive::kPrimLong
1740 : Primitive::kPrimInt;
1741 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1742 }
1743 } else {
1744 DCHECK(source.IsFpuRegister());
1745 if (destination.IsRegister()) {
1746 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1747 ? Primitive::kPrimDouble
1748 : Primitive::kPrimFloat;
1749 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1750 } else {
1751 DCHECK(destination.IsFpuRegister());
1752 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753 }
1754 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001755 } else { // The destination is not a register. It must be a stack slot.
1756 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1757 if (source.IsRegister() || source.IsFpuRegister()) {
1758 if (unspecified_type) {
1759 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001760 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001761 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001762 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001763 }
1764 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001765 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1766 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1767 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001768 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001769 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1770 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771 UseScratchRegisterScope temps(GetVIXLAssembler());
1772 HConstant* src_cst = source.GetConstant();
1773 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001774 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001775 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1776 ? Register(xzr)
1777 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001778 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001779 if (src_cst->IsIntConstant()) {
1780 temp = temps.AcquireW();
1781 } else if (src_cst->IsLongConstant()) {
1782 temp = temps.AcquireX();
1783 } else if (src_cst->IsFloatConstant()) {
1784 temp = temps.AcquireS();
1785 } else {
1786 DCHECK(src_cst->IsDoubleConstant());
1787 temp = temps.AcquireD();
1788 }
1789 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001790 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001791 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001792 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001793 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001795 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001796 // Use any scratch register (a core or a floating-point one)
1797 // from VIXL scratch register pools as a temporary.
1798 //
1799 // We used to only use the FP scratch register pool, but in some
1800 // rare cases the only register from this pool (D31) would
1801 // already be used (e.g. within a ParallelMove instruction, when
1802 // a move is blocked by a another move requiring a scratch FP
1803 // register, which would reserve D31). To prevent this issue, we
1804 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001805 //
1806 // Also, we start by asking for a FP scratch register first, as the
1807 // demand of scratch core registers is higher. This is why we
1808 // use AcquireFPOrCoreCPURegisterOfSize instead of
1809 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1810 // allocates core scratch registers first.
1811 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1812 GetVIXLAssembler(),
1813 &temps,
1814 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001815 __ Ldr(temp, StackOperandFrom(source));
1816 __ Str(temp, StackOperandFrom(destination));
1817 }
1818 }
1819}
1820
1821void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001822 CPURegister dst,
1823 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001824 switch (type) {
1825 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001826 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001827 break;
1828 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001829 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001830 break;
1831 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001832 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001833 break;
1834 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001835 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001836 break;
1837 case Primitive::kPrimInt:
1838 case Primitive::kPrimNot:
1839 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001840 case Primitive::kPrimFloat:
1841 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001842 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 __ Ldr(dst, src);
1844 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001845 case Primitive::kPrimVoid:
1846 LOG(FATAL) << "Unreachable type " << type;
1847 }
1848}
1849
Calin Juravle77520bc2015-01-12 18:45:46 +00001850void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001851 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001852 const MemOperand& src,
1853 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001854 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001855 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001856 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001857 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001858
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001859 DCHECK(!src.IsPreIndex());
1860 DCHECK(!src.IsPostIndex());
1861
1862 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001863 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001864 {
1865 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1866 MemOperand base = MemOperand(temp_base);
1867 switch (type) {
1868 case Primitive::kPrimBoolean:
1869 {
1870 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1871 __ ldarb(Register(dst), base);
1872 if (needs_null_check) {
1873 MaybeRecordImplicitNullCheck(instruction);
1874 }
1875 }
1876 break;
1877 case Primitive::kPrimByte:
1878 {
1879 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1880 __ ldarb(Register(dst), base);
1881 if (needs_null_check) {
1882 MaybeRecordImplicitNullCheck(instruction);
1883 }
1884 }
1885 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1886 break;
1887 case Primitive::kPrimChar:
1888 {
1889 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1890 __ ldarh(Register(dst), base);
1891 if (needs_null_check) {
1892 MaybeRecordImplicitNullCheck(instruction);
1893 }
1894 }
1895 break;
1896 case Primitive::kPrimShort:
1897 {
1898 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1899 __ ldarh(Register(dst), base);
1900 if (needs_null_check) {
1901 MaybeRecordImplicitNullCheck(instruction);
1902 }
1903 }
1904 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1905 break;
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimNot:
1908 case Primitive::kPrimLong:
1909 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1910 {
1911 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1912 __ ldar(Register(dst), base);
1913 if (needs_null_check) {
1914 MaybeRecordImplicitNullCheck(instruction);
1915 }
1916 }
1917 break;
1918 case Primitive::kPrimFloat:
1919 case Primitive::kPrimDouble: {
1920 DCHECK(dst.IsFPRegister());
1921 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001922
Artem Serov914d7a82017-02-07 14:33:49 +00001923 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1924 {
1925 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1926 __ ldar(temp, base);
1927 if (needs_null_check) {
1928 MaybeRecordImplicitNullCheck(instruction);
1929 }
1930 }
1931 __ Fmov(FPRegister(dst), temp);
1932 break;
Roland Levillain44015862016-01-22 11:47:17 +00001933 }
Artem Serov914d7a82017-02-07 14:33:49 +00001934 case Primitive::kPrimVoid:
1935 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001936 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001937 }
1938}
1939
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001940void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001941 CPURegister src,
1942 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001943 switch (type) {
1944 case Primitive::kPrimBoolean:
1945 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001946 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001947 break;
1948 case Primitive::kPrimChar:
1949 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001950 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001951 break;
1952 case Primitive::kPrimInt:
1953 case Primitive::kPrimNot:
1954 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001957 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001958 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001959 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001960 case Primitive::kPrimVoid:
1961 LOG(FATAL) << "Unreachable type " << type;
1962 }
1963}
1964
Artem Serov914d7a82017-02-07 14:33:49 +00001965void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
1966 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001967 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001968 const MemOperand& dst,
1969 bool needs_null_check) {
1970 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001971 UseScratchRegisterScope temps(GetVIXLAssembler());
1972 Register temp_base = temps.AcquireX();
1973
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001974 DCHECK(!dst.IsPreIndex());
1975 DCHECK(!dst.IsPostIndex());
1976
1977 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001978 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001979 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001980 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001981 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001982 switch (type) {
1983 case Primitive::kPrimBoolean:
1984 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00001985 {
1986 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1987 __ stlrb(Register(src), base);
1988 if (needs_null_check) {
1989 MaybeRecordImplicitNullCheck(instruction);
1990 }
1991 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001992 break;
1993 case Primitive::kPrimChar:
1994 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00001995 {
1996 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1997 __ stlrh(Register(src), base);
1998 if (needs_null_check) {
1999 MaybeRecordImplicitNullCheck(instruction);
2000 }
2001 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002002 break;
2003 case Primitive::kPrimInt:
2004 case Primitive::kPrimNot:
2005 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002006 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002007 {
2008 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2009 __ stlr(Register(src), base);
2010 if (needs_null_check) {
2011 MaybeRecordImplicitNullCheck(instruction);
2012 }
2013 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002014 break;
2015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002017 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002018 Register temp_src;
2019 if (src.IsZero()) {
2020 // The zero register is used to avoid synthesizing zero constants.
2021 temp_src = Register(src);
2022 } else {
2023 DCHECK(src.IsFPRegister());
2024 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2025 __ Fmov(temp_src, FPRegister(src));
2026 }
Artem Serov914d7a82017-02-07 14:33:49 +00002027 {
2028 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2029 __ stlr(temp_src, base);
2030 if (needs_null_check) {
2031 MaybeRecordImplicitNullCheck(instruction);
2032 }
2033 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 break;
2035 }
2036 case Primitive::kPrimVoid:
2037 LOG(FATAL) << "Unreachable type " << type;
2038 }
2039}
2040
Calin Juravle175dc732015-08-25 15:42:32 +01002041void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2042 HInstruction* instruction,
2043 uint32_t dex_pc,
2044 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002045 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002046
2047 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2048 {
2049 // Ensure the pc position is recorded immediately after the `blr` instruction.
2050 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2051 __ blr(lr);
2052 if (EntrypointRequiresStackMap(entrypoint)) {
2053 RecordPcInfo(instruction, dex_pc, slow_path);
2054 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002055 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002056}
2057
Roland Levillaindec8f632016-07-22 17:10:06 +01002058void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2059 HInstruction* instruction,
2060 SlowPathCode* slow_path) {
2061 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002062 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2063 __ Blr(lr);
2064}
2065
Alexandre Rames67555f72014-11-18 10:55:16 +00002066void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002067 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002068 UseScratchRegisterScope temps(GetVIXLAssembler());
2069 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002070 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2071
Serban Constantinescu02164b32014-11-13 14:05:07 +00002072 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002073 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2074 __ Add(temp, class_reg, status_offset);
2075 __ Ldar(temp, HeapOperand(temp));
2076 __ Cmp(temp, mirror::Class::kStatusInitialized);
2077 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002078 __ Bind(slow_path->GetExitLabel());
2079}
Alexandre Rames5319def2014-10-23 10:03:10 +01002080
Roland Levillain44015862016-01-22 11:47:17 +00002081void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002082 BarrierType type = BarrierAll;
2083
2084 switch (kind) {
2085 case MemBarrierKind::kAnyAny:
2086 case MemBarrierKind::kAnyStore: {
2087 type = BarrierAll;
2088 break;
2089 }
2090 case MemBarrierKind::kLoadAny: {
2091 type = BarrierReads;
2092 break;
2093 }
2094 case MemBarrierKind::kStoreStore: {
2095 type = BarrierWrites;
2096 break;
2097 }
2098 default:
2099 LOG(FATAL) << "Unexpected memory barrier " << kind;
2100 }
2101 __ Dmb(InnerShareable, type);
2102}
2103
Serban Constantinescu02164b32014-11-13 14:05:07 +00002104void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2105 HBasicBlock* successor) {
2106 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002107 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2108 if (slow_path == nullptr) {
2109 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2110 instruction->SetSlowPath(slow_path);
2111 codegen_->AddSlowPath(slow_path);
2112 if (successor != nullptr) {
2113 DCHECK(successor->IsLoopHeader());
2114 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2115 }
2116 } else {
2117 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2118 }
2119
Serban Constantinescu02164b32014-11-13 14:05:07 +00002120 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2121 Register temp = temps.AcquireW();
2122
Andreas Gampe542451c2016-07-26 09:02:02 -07002123 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002124 if (successor == nullptr) {
2125 __ Cbnz(temp, slow_path->GetEntryLabel());
2126 __ Bind(slow_path->GetReturnLabel());
2127 } else {
2128 __ Cbz(temp, codegen_->GetLabelOf(successor));
2129 __ B(slow_path->GetEntryLabel());
2130 // slow_path will return to GetLabelOf(successor).
2131 }
2132}
2133
Alexandre Rames5319def2014-10-23 10:03:10 +01002134InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2135 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002136 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002137 assembler_(codegen->GetAssembler()),
2138 codegen_(codegen) {}
2139
2140#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002141 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002142
2143#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2144
2145enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002146 // Using a base helps identify when we hit such breakpoints.
2147 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002148#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2149 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2150#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2151};
2152
2153#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002154 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002155 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2156 } \
2157 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2159 locations->SetOut(Location::Any()); \
2160 }
2161 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2162#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2163
2164#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002165#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002166
Alexandre Rames67555f72014-11-18 10:55:16 +00002167void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002168 DCHECK_EQ(instr->InputCount(), 2U);
2169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2170 Primitive::Type type = instr->GetResultType();
2171 switch (type) {
2172 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002175 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002177 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002178
2179 case Primitive::kPrimFloat:
2180 case Primitive::kPrimDouble:
2181 locations->SetInAt(0, Location::RequiresFpuRegister());
2182 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002183 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002184 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002185
Alexandre Rames5319def2014-10-23 10:03:10 +01002186 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002187 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002188 }
2189}
2190
Alexandre Rames09a99962015-04-15 11:47:56 +01002191void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002192 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2193
2194 bool object_field_get_with_read_barrier =
2195 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002196 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002197 new (GetGraph()->GetArena()) LocationSummary(instruction,
2198 object_field_get_with_read_barrier ?
2199 LocationSummary::kCallOnSlowPath :
2200 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002201 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002203 // We need a temporary register for the read barrier marking slow
2204 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2205 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002206 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002207 locations->SetInAt(0, Location::RequiresRegister());
2208 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2209 locations->SetOut(Location::RequiresFpuRegister());
2210 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002211 // The output overlaps for an object field get when read barriers
2212 // are enabled: we do not want the load to overwrite the object's
2213 // location, as we need it to emit the read barrier.
2214 locations->SetOut(
2215 Location::RequiresRegister(),
2216 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002217 }
2218}
2219
2220void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2221 const FieldInfo& field_info) {
2222 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002223 LocationSummary* locations = instruction->GetLocations();
2224 Location base_loc = locations->InAt(0);
2225 Location out = locations->Out();
2226 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002227 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002228 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002229
Roland Levillain44015862016-01-22 11:47:17 +00002230 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2231 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002232 // /* HeapReference<Object> */ out = *(base + offset)
2233 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002234 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002235 // Note that potential implicit null checks are handled in this
2236 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2237 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2238 instruction,
2239 out,
2240 base,
2241 offset,
2242 temp,
2243 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002244 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002245 } else {
2246 // General case.
2247 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002248 // Note that a potential implicit null check is handled in this
2249 // CodeGeneratorARM64::LoadAcquire call.
2250 // NB: LoadAcquire will record the pc info if needed.
2251 codegen_->LoadAcquire(
2252 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002253 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002254 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2255 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002256 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002257 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002258 }
Roland Levillain44015862016-01-22 11:47:17 +00002259 if (field_type == Primitive::kPrimNot) {
2260 // If read barriers are enabled, emit read barriers other than
2261 // Baker's using a slow path (and also unpoison the loaded
2262 // reference, if heap poisoning is enabled).
2263 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2264 }
Roland Levillain4d027112015-07-01 15:41:14 +01002265 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002266}
2267
2268void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2269 LocationSummary* locations =
2270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2271 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002272 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2273 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2274 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002275 locations->SetInAt(1, Location::RequiresFpuRegister());
2276 } else {
2277 locations->SetInAt(1, Location::RequiresRegister());
2278 }
2279}
2280
2281void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002282 const FieldInfo& field_info,
2283 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002284 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2285
2286 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002287 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002288 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002289 Offset offset = field_info.GetFieldOffset();
2290 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002291
Roland Levillain4d027112015-07-01 15:41:14 +01002292 {
2293 // We use a block to end the scratch scope before the write barrier, thus
2294 // freeing the temporary registers so they can be used in `MarkGCCard`.
2295 UseScratchRegisterScope temps(GetVIXLAssembler());
2296
2297 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2298 DCHECK(value.IsW());
2299 Register temp = temps.AcquireW();
2300 __ Mov(temp, value.W());
2301 GetAssembler()->PoisonHeapReference(temp.W());
2302 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002303 }
Roland Levillain4d027112015-07-01 15:41:14 +01002304
2305 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002306 codegen_->StoreRelease(
2307 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002308 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002309 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2310 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002311 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2312 codegen_->MaybeRecordImplicitNullCheck(instruction);
2313 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002314 }
2315
2316 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002317 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002318 }
2319}
2320
Alexandre Rames67555f72014-11-18 10:55:16 +00002321void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002322 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002323
2324 switch (type) {
2325 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002326 case Primitive::kPrimLong: {
2327 Register dst = OutputRegister(instr);
2328 Register lhs = InputRegisterAt(instr, 0);
2329 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002330 if (instr->IsAdd()) {
2331 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 } else if (instr->IsAnd()) {
2333 __ And(dst, lhs, rhs);
2334 } else if (instr->IsOr()) {
2335 __ Orr(dst, lhs, rhs);
2336 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002337 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002338 } else if (instr->IsRor()) {
2339 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002340 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002341 __ Ror(dst, lhs, shift);
2342 } else {
2343 // Ensure shift distance is in the same size register as the result. If
2344 // we are rotating a long and the shift comes in a w register originally,
2345 // we don't need to sxtw for use as an x since the shift distances are
2346 // all & reg_bits - 1.
2347 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2348 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002349 } else {
2350 DCHECK(instr->IsXor());
2351 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002352 }
2353 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002354 }
2355 case Primitive::kPrimFloat:
2356 case Primitive::kPrimDouble: {
2357 FPRegister dst = OutputFPRegister(instr);
2358 FPRegister lhs = InputFPRegisterAt(instr, 0);
2359 FPRegister rhs = InputFPRegisterAt(instr, 1);
2360 if (instr->IsAdd()) {
2361 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002362 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002363 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002364 } else {
2365 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002366 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002367 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002368 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002369 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002370 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002371 }
2372}
2373
Serban Constantinescu02164b32014-11-13 14:05:07 +00002374void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2375 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2376
2377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2378 Primitive::Type type = instr->GetResultType();
2379 switch (type) {
2380 case Primitive::kPrimInt:
2381 case Primitive::kPrimLong: {
2382 locations->SetInAt(0, Location::RequiresRegister());
2383 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2384 locations->SetOut(Location::RequiresRegister());
2385 break;
2386 }
2387 default:
2388 LOG(FATAL) << "Unexpected shift type " << type;
2389 }
2390}
2391
2392void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2393 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2394
2395 Primitive::Type type = instr->GetType();
2396 switch (type) {
2397 case Primitive::kPrimInt:
2398 case Primitive::kPrimLong: {
2399 Register dst = OutputRegister(instr);
2400 Register lhs = InputRegisterAt(instr, 0);
2401 Operand rhs = InputOperandAt(instr, 1);
2402 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002403 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002404 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002405 if (instr->IsShl()) {
2406 __ Lsl(dst, lhs, shift_value);
2407 } else if (instr->IsShr()) {
2408 __ Asr(dst, lhs, shift_value);
2409 } else {
2410 __ Lsr(dst, lhs, shift_value);
2411 }
2412 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002413 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002414
2415 if (instr->IsShl()) {
2416 __ Lsl(dst, lhs, rhs_reg);
2417 } else if (instr->IsShr()) {
2418 __ Asr(dst, lhs, rhs_reg);
2419 } else {
2420 __ Lsr(dst, lhs, rhs_reg);
2421 }
2422 }
2423 break;
2424 }
2425 default:
2426 LOG(FATAL) << "Unexpected shift operation type " << type;
2427 }
2428}
2429
Alexandre Rames5319def2014-10-23 10:03:10 +01002430void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002431 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002432}
2433
2434void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002435 HandleBinaryOp(instruction);
2436}
2437
2438void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2439 HandleBinaryOp(instruction);
2440}
2441
2442void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2443 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002444}
2445
Artem Serov7fc63502016-02-09 17:15:29 +00002446void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002447 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2448 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2449 locations->SetInAt(0, Location::RequiresRegister());
2450 // There is no immediate variant of negated bitwise instructions in AArch64.
2451 locations->SetInAt(1, Location::RequiresRegister());
2452 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2453}
2454
Artem Serov7fc63502016-02-09 17:15:29 +00002455void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002456 Register dst = OutputRegister(instr);
2457 Register lhs = InputRegisterAt(instr, 0);
2458 Register rhs = InputRegisterAt(instr, 1);
2459
2460 switch (instr->GetOpKind()) {
2461 case HInstruction::kAnd:
2462 __ Bic(dst, lhs, rhs);
2463 break;
2464 case HInstruction::kOr:
2465 __ Orn(dst, lhs, rhs);
2466 break;
2467 case HInstruction::kXor:
2468 __ Eon(dst, lhs, rhs);
2469 break;
2470 default:
2471 LOG(FATAL) << "Unreachable";
2472 }
2473}
2474
Anton Kirilov74234da2017-01-13 14:42:47 +00002475void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2476 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002477 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2478 instruction->GetType() == Primitive::kPrimLong);
2479 LocationSummary* locations =
2480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2481 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2482 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2483 } else {
2484 locations->SetInAt(0, Location::RequiresRegister());
2485 }
2486 locations->SetInAt(1, Location::RequiresRegister());
2487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2488}
2489
Anton Kirilov74234da2017-01-13 14:42:47 +00002490void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2491 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002492 Primitive::Type type = instruction->GetType();
2493 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2494 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2495 Register out = OutputRegister(instruction);
2496 Register left;
2497 if (kind != HInstruction::kNeg) {
2498 left = InputRegisterAt(instruction, 0);
2499 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002500 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002501 // shifter operand operation, the IR generating `right_reg` (input to the type
2502 // conversion) can have a different type from the current instruction's type,
2503 // so we manually indicate the type.
2504 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002505 Operand right_operand(0);
2506
Anton Kirilov74234da2017-01-13 14:42:47 +00002507 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2508 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002509 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2510 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002511 right_operand = Operand(right_reg,
2512 helpers::ShiftFromOpKind(op_kind),
2513 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002514 }
2515
2516 // Logical binary operations do not support extension operations in the
2517 // operand. Note that VIXL would still manage if it was passed by generating
2518 // the extension as a separate instruction.
2519 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2520 DCHECK(!right_operand.IsExtendedRegister() ||
2521 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2522 kind != HInstruction::kNeg));
2523 switch (kind) {
2524 case HInstruction::kAdd:
2525 __ Add(out, left, right_operand);
2526 break;
2527 case HInstruction::kAnd:
2528 __ And(out, left, right_operand);
2529 break;
2530 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002531 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002532 __ Neg(out, right_operand);
2533 break;
2534 case HInstruction::kOr:
2535 __ Orr(out, left, right_operand);
2536 break;
2537 case HInstruction::kSub:
2538 __ Sub(out, left, right_operand);
2539 break;
2540 case HInstruction::kXor:
2541 __ Eor(out, left, right_operand);
2542 break;
2543 default:
2544 LOG(FATAL) << "Unexpected operation kind: " << kind;
2545 UNREACHABLE();
2546 }
2547}
2548
Artem Serov328429f2016-07-06 16:23:04 +01002549void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2552 locations->SetInAt(0, Location::RequiresRegister());
2553 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2554 locations->SetOut(Location::RequiresRegister());
2555}
2556
Roland Levillain19c54192016-11-04 13:44:09 +00002557void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002558 __ Add(OutputRegister(instruction),
2559 InputRegisterAt(instruction, 0),
2560 Operand(InputOperandAt(instruction, 1)));
2561}
2562
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002563void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002564 LocationSummary* locations =
2565 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002566 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2567 if (instr->GetOpKind() == HInstruction::kSub &&
2568 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002569 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002570 // Don't allocate register for Mneg instruction.
2571 } else {
2572 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2573 Location::RequiresRegister());
2574 }
2575 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2576 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2578}
2579
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002580void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002581 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002582 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2583 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002584
2585 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2586 // This fixup should be carried out for all multiply-accumulate instructions:
2587 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2588 if (instr->GetType() == Primitive::kPrimLong &&
2589 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2590 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002591 vixl::aarch64::Instruction* prev =
2592 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002593 if (prev->IsLoadOrStore()) {
2594 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002595 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002596 __ nop();
2597 }
2598 }
2599
2600 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002601 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002602 __ Madd(res, mul_left, mul_right, accumulator);
2603 } else {
2604 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002605 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002606 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002607 __ Mneg(res, mul_left, mul_right);
2608 } else {
2609 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2610 __ Msub(res, mul_left, mul_right, accumulator);
2611 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002612 }
2613}
2614
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002615void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002616 bool object_array_get_with_read_barrier =
2617 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002618 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002619 new (GetGraph()->GetArena()) LocationSummary(instruction,
2620 object_array_get_with_read_barrier ?
2621 LocationSummary::kCallOnSlowPath :
2622 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002623 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002624 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002625 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002626 locations->SetInAt(0, Location::RequiresRegister());
2627 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002628 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2630 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002631 // The output overlaps in the case of an object array get with
2632 // read barriers enabled: we do not want the move to overwrite the
2633 // array's location, as we need it to emit the read barrier.
2634 locations->SetOut(
2635 Location::RequiresRegister(),
2636 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002637 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002638}
2639
2640void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002641 Primitive::Type type = instruction->GetType();
2642 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002643 LocationSummary* locations = instruction->GetLocations();
2644 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002645 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002646 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002647 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2648 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002649 MacroAssembler* masm = GetVIXLAssembler();
2650 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002651
Roland Levillain19c54192016-11-04 13:44:09 +00002652 // The read barrier instrumentation of object ArrayGet instructions
2653 // does not support the HIntermediateAddress instruction.
2654 DCHECK(!((type == Primitive::kPrimNot) &&
2655 instruction->GetArray()->IsIntermediateAddress() &&
2656 kEmitCompilerReadBarrier));
2657
Roland Levillain44015862016-01-22 11:47:17 +00002658 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2659 // Object ArrayGet with Baker's read barrier case.
2660 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002661 // Note that a potential implicit null check is handled in the
2662 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2663 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2664 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002665 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002666 // General case.
2667 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002668 Register length;
2669 if (maybe_compressed_char_at) {
2670 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2671 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002672 {
2673 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2674 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2675
2676 if (instruction->GetArray()->IsIntermediateAddress()) {
2677 DCHECK_LT(count_offset, offset);
2678 int64_t adjusted_offset =
2679 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2680 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2681 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2682 } else {
2683 __ Ldr(length, HeapOperand(obj, count_offset));
2684 }
2685 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002686 }
jessicahandojo05765752016-09-09 19:01:32 -07002687 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002688 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002689 if (maybe_compressed_char_at) {
2690 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002691 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2692 "Expecting 0=compressed, 1=uncompressed");
2693 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002694 __ Ldrb(Register(OutputCPURegister(instruction)),
2695 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2696 __ B(&done);
2697 __ Bind(&uncompressed_load);
2698 __ Ldrh(Register(OutputCPURegister(instruction)),
2699 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2700 __ Bind(&done);
2701 } else {
2702 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2703 source = HeapOperand(obj, offset);
2704 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002705 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002706 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002707 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002708 // We do not need to compute the intermediate address from the array: the
2709 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002710 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002711 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002712 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002713 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2714 }
2715 temp = obj;
2716 } else {
2717 __ Add(temp, obj, offset);
2718 }
jessicahandojo05765752016-09-09 19:01:32 -07002719 if (maybe_compressed_char_at) {
2720 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002721 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2722 "Expecting 0=compressed, 1=uncompressed");
2723 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002724 __ Ldrb(Register(OutputCPURegister(instruction)),
2725 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2726 __ B(&done);
2727 __ Bind(&uncompressed_load);
2728 __ Ldrh(Register(OutputCPURegister(instruction)),
2729 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2730 __ Bind(&done);
2731 } else {
2732 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2733 }
Roland Levillain44015862016-01-22 11:47:17 +00002734 }
jessicahandojo05765752016-09-09 19:01:32 -07002735 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002736 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2737 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002738 codegen_->Load(type, OutputCPURegister(instruction), source);
2739 codegen_->MaybeRecordImplicitNullCheck(instruction);
2740 }
Roland Levillain44015862016-01-22 11:47:17 +00002741
2742 if (type == Primitive::kPrimNot) {
2743 static_assert(
2744 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2745 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2746 Location obj_loc = locations->InAt(0);
2747 if (index.IsConstant()) {
2748 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2749 } else {
2750 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2751 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002752 }
Roland Levillain4d027112015-07-01 15:41:14 +01002753 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002754}
2755
Alexandre Rames5319def2014-10-23 10:03:10 +01002756void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2758 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002760}
2761
2762void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002763 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002764 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002765 {
2766 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2767 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2768 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2769 codegen_->MaybeRecordImplicitNullCheck(instruction);
2770 }
jessicahandojo05765752016-09-09 19:01:32 -07002771 // Mask out compression flag from String's array length.
2772 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002773 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002774 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002775}
2776
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002777void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002778 Primitive::Type value_type = instruction->GetComponentType();
2779
2780 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2782 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002783 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002784 LocationSummary::kCallOnSlowPath :
2785 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002786 locations->SetInAt(0, Location::RequiresRegister());
2787 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002788 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2789 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2790 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002791 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002792 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002793 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002794 }
2795}
2796
2797void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2798 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002799 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002800 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002801 bool needs_write_barrier =
2802 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002803
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002804 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002805 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002806 CPURegister source = value;
2807 Location index = locations->InAt(1);
2808 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2809 MemOperand destination = HeapOperand(array);
2810 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002811
2812 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002813 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002814 if (index.IsConstant()) {
2815 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2816 destination = HeapOperand(array, offset);
2817 } else {
2818 UseScratchRegisterScope temps(masm);
2819 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002820 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002821 // We do not need to compute the intermediate address from the array: the
2822 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002823 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002824 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002825 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002826 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2827 }
2828 temp = array;
2829 } else {
2830 __ Add(temp, array, offset);
2831 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002832 destination = HeapOperand(temp,
2833 XRegisterFrom(index),
2834 LSL,
2835 Primitive::ComponentSizeShift(value_type));
2836 }
Artem Serov914d7a82017-02-07 14:33:49 +00002837 {
2838 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2839 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2840 codegen_->Store(value_type, value, destination);
2841 codegen_->MaybeRecordImplicitNullCheck(instruction);
2842 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002843 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002844 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002845 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002846 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002847 {
2848 // We use a block to end the scratch scope before the write barrier, thus
2849 // freeing the temporary registers so they can be used in `MarkGCCard`.
2850 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002851 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002852 if (index.IsConstant()) {
2853 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002854 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002855 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002856 destination = HeapOperand(temp,
2857 XRegisterFrom(index),
2858 LSL,
2859 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002860 }
2861
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002862 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2863 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2864 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2865
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002866 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002867 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2868 codegen_->AddSlowPath(slow_path);
2869 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002870 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002871 __ Cbnz(Register(value), &non_zero);
2872 if (!index.IsConstant()) {
2873 __ Add(temp, array, offset);
2874 }
Artem Serov914d7a82017-02-07 14:33:49 +00002875 {
2876 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2877 // emitted.
2878 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2879 __ Str(wzr, destination);
2880 codegen_->MaybeRecordImplicitNullCheck(instruction);
2881 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002882 __ B(&done);
2883 __ Bind(&non_zero);
2884 }
2885
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002886 // Note that when Baker read barriers are enabled, the type
2887 // checks are performed without read barriers. This is fine,
2888 // even in the case where a class object is in the from-space
2889 // after the flip, as a comparison involving such a type would
2890 // not produce a false positive; it may of course produce a
2891 // false negative, in which case we would take the ArraySet
2892 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002893
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002894 Register temp2 = temps.AcquireSameSizeAs(array);
2895 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002896 {
2897 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2898 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2899 __ Ldr(temp, HeapOperand(array, class_offset));
2900 codegen_->MaybeRecordImplicitNullCheck(instruction);
2901 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002902 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002903
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002904 // /* HeapReference<Class> */ temp = temp->component_type_
2905 __ Ldr(temp, HeapOperand(temp, component_offset));
2906 // /* HeapReference<Class> */ temp2 = value->klass_
2907 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2908 // If heap poisoning is enabled, no need to unpoison `temp`
2909 // nor `temp2`, as we are comparing two poisoned references.
2910 __ Cmp(temp, temp2);
2911 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002912
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002913 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2914 vixl::aarch64::Label do_put;
2915 __ B(eq, &do_put);
2916 // If heap poisoning is enabled, the `temp` reference has
2917 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002918 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2919
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002920 // /* HeapReference<Class> */ temp = temp->super_class_
2921 __ Ldr(temp, HeapOperand(temp, super_offset));
2922 // If heap poisoning is enabled, no need to unpoison
2923 // `temp`, as we are comparing against null below.
2924 __ Cbnz(temp, slow_path->GetEntryLabel());
2925 __ Bind(&do_put);
2926 } else {
2927 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002928 }
2929 }
2930
2931 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002932 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002933 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002934 __ Mov(temp2, value.W());
2935 GetAssembler()->PoisonHeapReference(temp2);
2936 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002937 }
2938
2939 if (!index.IsConstant()) {
2940 __ Add(temp, array, offset);
2941 }
Artem Serov914d7a82017-02-07 14:33:49 +00002942 {
2943 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2944 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2945 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002946
Artem Serov914d7a82017-02-07 14:33:49 +00002947 if (!may_need_runtime_call_for_type_check) {
2948 codegen_->MaybeRecordImplicitNullCheck(instruction);
2949 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002950 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002951 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002952
2953 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2954
2955 if (done.IsLinked()) {
2956 __ Bind(&done);
2957 }
2958
2959 if (slow_path != nullptr) {
2960 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002961 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002962 }
2963}
2964
Alexandre Rames67555f72014-11-18 10:55:16 +00002965void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002966 RegisterSet caller_saves = RegisterSet::Empty();
2967 InvokeRuntimeCallingConvention calling_convention;
2968 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2969 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2970 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002971 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002972 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002973}
2974
2975void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002976 BoundsCheckSlowPathARM64* slow_path =
2977 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002978 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002979 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2980 __ B(slow_path->GetEntryLabel(), hs);
2981}
2982
Alexandre Rames67555f72014-11-18 10:55:16 +00002983void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2984 LocationSummary* locations =
2985 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2986 locations->SetInAt(0, Location::RequiresRegister());
2987 if (check->HasUses()) {
2988 locations->SetOut(Location::SameAsFirstInput());
2989 }
2990}
2991
2992void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2993 // We assume the class is not null.
2994 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2995 check->GetLoadClass(), check, check->GetDexPc(), true);
2996 codegen_->AddSlowPath(slow_path);
2997 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2998}
2999
Roland Levillain1a653882016-03-18 18:05:57 +00003000static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3001 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3002 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3003}
3004
3005void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3006 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3007 Location rhs_loc = instruction->GetLocations()->InAt(1);
3008 if (rhs_loc.IsConstant()) {
3009 // 0.0 is the only immediate that can be encoded directly in
3010 // an FCMP instruction.
3011 //
3012 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3013 // specify that in a floating-point comparison, positive zero
3014 // and negative zero are considered equal, so we can use the
3015 // literal 0.0 for both cases here.
3016 //
3017 // Note however that some methods (Float.equal, Float.compare,
3018 // Float.compareTo, Double.equal, Double.compare,
3019 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3020 // StrictMath.min) consider 0.0 to be (strictly) greater than
3021 // -0.0. So if we ever translate calls to these methods into a
3022 // HCompare instruction, we must handle the -0.0 case with
3023 // care here.
3024 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3025 __ Fcmp(lhs_reg, 0.0);
3026 } else {
3027 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3028 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003029}
3030
Serban Constantinescu02164b32014-11-13 14:05:07 +00003031void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003032 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003033 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3034 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003035 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003036 case Primitive::kPrimBoolean:
3037 case Primitive::kPrimByte:
3038 case Primitive::kPrimShort:
3039 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003040 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003041 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003042 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003043 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3045 break;
3046 }
3047 case Primitive::kPrimFloat:
3048 case Primitive::kPrimDouble: {
3049 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003050 locations->SetInAt(1,
3051 IsFloatingPointZeroConstant(compare->InputAt(1))
3052 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3053 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003054 locations->SetOut(Location::RequiresRegister());
3055 break;
3056 }
3057 default:
3058 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3059 }
3060}
3061
3062void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3063 Primitive::Type in_type = compare->InputAt(0)->GetType();
3064
3065 // 0 if: left == right
3066 // 1 if: left > right
3067 // -1 if: left < right
3068 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003069 case Primitive::kPrimBoolean:
3070 case Primitive::kPrimByte:
3071 case Primitive::kPrimShort:
3072 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003073 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003074 case Primitive::kPrimLong: {
3075 Register result = OutputRegister(compare);
3076 Register left = InputRegisterAt(compare, 0);
3077 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003078 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003079 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3080 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003081 break;
3082 }
3083 case Primitive::kPrimFloat:
3084 case Primitive::kPrimDouble: {
3085 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003086 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003087 __ Cset(result, ne);
3088 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003089 break;
3090 }
3091 default:
3092 LOG(FATAL) << "Unimplemented compare type " << in_type;
3093 }
3094}
3095
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003096void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003098
3099 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3100 locations->SetInAt(0, Location::RequiresFpuRegister());
3101 locations->SetInAt(1,
3102 IsFloatingPointZeroConstant(instruction->InputAt(1))
3103 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3104 : Location::RequiresFpuRegister());
3105 } else {
3106 // Integer cases.
3107 locations->SetInAt(0, Location::RequiresRegister());
3108 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3109 }
3110
David Brazdilb3e773e2016-01-26 11:28:37 +00003111 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003113 }
3114}
3115
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003116void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003117 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 return;
3119 }
3120
3121 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003122 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003123 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003124
Roland Levillain7f63c522015-07-13 15:54:55 +00003125 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003126 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003127 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003128 } else {
3129 // Integer cases.
3130 Register lhs = InputRegisterAt(instruction, 0);
3131 Operand rhs = InputOperandAt(instruction, 1);
3132 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003133 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003134 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003135}
3136
3137#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3138 M(Equal) \
3139 M(NotEqual) \
3140 M(LessThan) \
3141 M(LessThanOrEqual) \
3142 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003143 M(GreaterThanOrEqual) \
3144 M(Below) \
3145 M(BelowOrEqual) \
3146 M(Above) \
3147 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003148#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003149void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3150void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003151FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003152#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003153#undef FOR_EACH_CONDITION_INSTRUCTION
3154
Zheng Xuc6667102015-05-15 16:08:45 +08003155void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3156 DCHECK(instruction->IsDiv() || instruction->IsRem());
3157
3158 LocationSummary* locations = instruction->GetLocations();
3159 Location second = locations->InAt(1);
3160 DCHECK(second.IsConstant());
3161
3162 Register out = OutputRegister(instruction);
3163 Register dividend = InputRegisterAt(instruction, 0);
3164 int64_t imm = Int64FromConstant(second.GetConstant());
3165 DCHECK(imm == 1 || imm == -1);
3166
3167 if (instruction->IsRem()) {
3168 __ Mov(out, 0);
3169 } else {
3170 if (imm == 1) {
3171 __ Mov(out, dividend);
3172 } else {
3173 __ Neg(out, dividend);
3174 }
3175 }
3176}
3177
3178void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3179 DCHECK(instruction->IsDiv() || instruction->IsRem());
3180
3181 LocationSummary* locations = instruction->GetLocations();
3182 Location second = locations->InAt(1);
3183 DCHECK(second.IsConstant());
3184
3185 Register out = OutputRegister(instruction);
3186 Register dividend = InputRegisterAt(instruction, 0);
3187 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003188 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003189 int ctz_imm = CTZ(abs_imm);
3190
3191 UseScratchRegisterScope temps(GetVIXLAssembler());
3192 Register temp = temps.AcquireSameSizeAs(out);
3193
3194 if (instruction->IsDiv()) {
3195 __ Add(temp, dividend, abs_imm - 1);
3196 __ Cmp(dividend, 0);
3197 __ Csel(out, temp, dividend, lt);
3198 if (imm > 0) {
3199 __ Asr(out, out, ctz_imm);
3200 } else {
3201 __ Neg(out, Operand(out, ASR, ctz_imm));
3202 }
3203 } else {
3204 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3205 __ Asr(temp, dividend, bits - 1);
3206 __ Lsr(temp, temp, bits - ctz_imm);
3207 __ Add(out, dividend, temp);
3208 __ And(out, out, abs_imm - 1);
3209 __ Sub(out, out, temp);
3210 }
3211}
3212
3213void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3214 DCHECK(instruction->IsDiv() || instruction->IsRem());
3215
3216 LocationSummary* locations = instruction->GetLocations();
3217 Location second = locations->InAt(1);
3218 DCHECK(second.IsConstant());
3219
3220 Register out = OutputRegister(instruction);
3221 Register dividend = InputRegisterAt(instruction, 0);
3222 int64_t imm = Int64FromConstant(second.GetConstant());
3223
3224 Primitive::Type type = instruction->GetResultType();
3225 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3226
3227 int64_t magic;
3228 int shift;
3229 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3230
3231 UseScratchRegisterScope temps(GetVIXLAssembler());
3232 Register temp = temps.AcquireSameSizeAs(out);
3233
3234 // temp = get_high(dividend * magic)
3235 __ Mov(temp, magic);
3236 if (type == Primitive::kPrimLong) {
3237 __ Smulh(temp, dividend, temp);
3238 } else {
3239 __ Smull(temp.X(), dividend, temp);
3240 __ Lsr(temp.X(), temp.X(), 32);
3241 }
3242
3243 if (imm > 0 && magic < 0) {
3244 __ Add(temp, temp, dividend);
3245 } else if (imm < 0 && magic > 0) {
3246 __ Sub(temp, temp, dividend);
3247 }
3248
3249 if (shift != 0) {
3250 __ Asr(temp, temp, shift);
3251 }
3252
3253 if (instruction->IsDiv()) {
3254 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3255 } else {
3256 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3257 // TODO: Strength reduction for msub.
3258 Register temp_imm = temps.AcquireSameSizeAs(out);
3259 __ Mov(temp_imm, imm);
3260 __ Msub(out, temp, temp_imm, dividend);
3261 }
3262}
3263
3264void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3265 DCHECK(instruction->IsDiv() || instruction->IsRem());
3266 Primitive::Type type = instruction->GetResultType();
3267 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3268
3269 LocationSummary* locations = instruction->GetLocations();
3270 Register out = OutputRegister(instruction);
3271 Location second = locations->InAt(1);
3272
3273 if (second.IsConstant()) {
3274 int64_t imm = Int64FromConstant(second.GetConstant());
3275
3276 if (imm == 0) {
3277 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3278 } else if (imm == 1 || imm == -1) {
3279 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003280 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003281 DivRemByPowerOfTwo(instruction);
3282 } else {
3283 DCHECK(imm <= -2 || imm >= 2);
3284 GenerateDivRemWithAnyConstant(instruction);
3285 }
3286 } else {
3287 Register dividend = InputRegisterAt(instruction, 0);
3288 Register divisor = InputRegisterAt(instruction, 1);
3289 if (instruction->IsDiv()) {
3290 __ Sdiv(out, dividend, divisor);
3291 } else {
3292 UseScratchRegisterScope temps(GetVIXLAssembler());
3293 Register temp = temps.AcquireSameSizeAs(out);
3294 __ Sdiv(temp, dividend, divisor);
3295 __ Msub(out, temp, divisor, dividend);
3296 }
3297 }
3298}
3299
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003300void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3301 LocationSummary* locations =
3302 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3303 switch (div->GetResultType()) {
3304 case Primitive::kPrimInt:
3305 case Primitive::kPrimLong:
3306 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003307 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3309 break;
3310
3311 case Primitive::kPrimFloat:
3312 case Primitive::kPrimDouble:
3313 locations->SetInAt(0, Location::RequiresFpuRegister());
3314 locations->SetInAt(1, Location::RequiresFpuRegister());
3315 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3316 break;
3317
3318 default:
3319 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3320 }
3321}
3322
3323void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3324 Primitive::Type type = div->GetResultType();
3325 switch (type) {
3326 case Primitive::kPrimInt:
3327 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003328 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003329 break;
3330
3331 case Primitive::kPrimFloat:
3332 case Primitive::kPrimDouble:
3333 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3334 break;
3335
3336 default:
3337 LOG(FATAL) << "Unexpected div type " << type;
3338 }
3339}
3340
Alexandre Rames67555f72014-11-18 10:55:16 +00003341void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003342 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003343 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003344}
3345
3346void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3347 SlowPathCodeARM64* slow_path =
3348 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3349 codegen_->AddSlowPath(slow_path);
3350 Location value = instruction->GetLocations()->InAt(0);
3351
Alexandre Rames3e69f162014-12-10 10:36:50 +00003352 Primitive::Type type = instruction->GetType();
3353
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003354 if (!Primitive::IsIntegralType(type)) {
3355 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003356 return;
3357 }
3358
Alexandre Rames67555f72014-11-18 10:55:16 +00003359 if (value.IsConstant()) {
3360 int64_t divisor = Int64ConstantFrom(value);
3361 if (divisor == 0) {
3362 __ B(slow_path->GetEntryLabel());
3363 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003364 // A division by a non-null constant is valid. We don't need to perform
3365 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003366 }
3367 } else {
3368 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3369 }
3370}
3371
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003372void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3373 LocationSummary* locations =
3374 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3375 locations->SetOut(Location::ConstantLocation(constant));
3376}
3377
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003378void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3379 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003380 // Will be generated at use site.
3381}
3382
Alexandre Rames5319def2014-10-23 10:03:10 +01003383void LocationsBuilderARM64::VisitExit(HExit* exit) {
3384 exit->SetLocations(nullptr);
3385}
3386
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003387void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003388}
3389
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003390void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3391 LocationSummary* locations =
3392 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3393 locations->SetOut(Location::ConstantLocation(constant));
3394}
3395
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003396void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003397 // Will be generated at use site.
3398}
3399
David Brazdilfc6a86a2015-06-26 10:33:45 +00003400void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003401 DCHECK(!successor->IsExitBlock());
3402 HBasicBlock* block = got->GetBlock();
3403 HInstruction* previous = got->GetPrevious();
3404 HLoopInformation* info = block->GetLoopInformation();
3405
David Brazdil46e2a392015-03-16 17:31:52 +00003406 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003407 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3408 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3409 return;
3410 }
3411 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3412 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3413 }
3414 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003415 __ B(codegen_->GetLabelOf(successor));
3416 }
3417}
3418
David Brazdilfc6a86a2015-06-26 10:33:45 +00003419void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3420 got->SetLocations(nullptr);
3421}
3422
3423void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3424 HandleGoto(got, got->GetSuccessor());
3425}
3426
3427void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3428 try_boundary->SetLocations(nullptr);
3429}
3430
3431void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3432 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3433 if (!successor->IsExitBlock()) {
3434 HandleGoto(try_boundary, successor);
3435 }
3436}
3437
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003438void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003439 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003440 vixl::aarch64::Label* true_target,
3441 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003442 // FP branching requires both targets to be explicit. If either of the targets
3443 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003444 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003445 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003446
David Brazdil0debae72015-11-12 18:37:00 +00003447 if (true_target == nullptr && false_target == nullptr) {
3448 // Nothing to do. The code always falls through.
3449 return;
3450 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003451 // Constant condition, statically compared against "true" (integer value 1).
3452 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003453 if (true_target != nullptr) {
3454 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003455 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003456 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003457 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003458 if (false_target != nullptr) {
3459 __ B(false_target);
3460 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003461 }
David Brazdil0debae72015-11-12 18:37:00 +00003462 return;
3463 }
3464
3465 // The following code generates these patterns:
3466 // (1) true_target == nullptr && false_target != nullptr
3467 // - opposite condition true => branch to false_target
3468 // (2) true_target != nullptr && false_target == nullptr
3469 // - condition true => branch to true_target
3470 // (3) true_target != nullptr && false_target != nullptr
3471 // - condition true => branch to true_target
3472 // - branch to false_target
3473 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003474 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003475 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003476 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003477 if (true_target == nullptr) {
3478 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3479 } else {
3480 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3481 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003482 } else {
3483 // The condition instruction has not been materialized, use its inputs as
3484 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003485 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003486
David Brazdil0debae72015-11-12 18:37:00 +00003487 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003488 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003489 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003490 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003491 IfCondition opposite_condition = condition->GetOppositeCondition();
3492 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003493 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003494 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003495 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003496 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003497 // Integer cases.
3498 Register lhs = InputRegisterAt(condition, 0);
3499 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003500
3501 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003502 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003503 if (true_target == nullptr) {
3504 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3505 non_fallthrough_target = false_target;
3506 } else {
3507 arm64_cond = ARM64Condition(condition->GetCondition());
3508 non_fallthrough_target = true_target;
3509 }
3510
Aart Bik086d27e2016-01-20 17:02:00 -08003511 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003512 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003513 switch (arm64_cond) {
3514 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003515 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003516 break;
3517 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003518 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003519 break;
3520 case lt:
3521 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003522 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003523 break;
3524 case ge:
3525 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003526 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003527 break;
3528 default:
3529 // Without the `static_cast` the compiler throws an error for
3530 // `-Werror=sign-promo`.
3531 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3532 }
3533 } else {
3534 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003535 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003536 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003537 }
3538 }
David Brazdil0debae72015-11-12 18:37:00 +00003539
3540 // If neither branch falls through (case 3), the conditional branch to `true_target`
3541 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3542 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003543 __ B(false_target);
3544 }
David Brazdil0debae72015-11-12 18:37:00 +00003545
3546 if (fallthrough_target.IsLinked()) {
3547 __ Bind(&fallthrough_target);
3548 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003549}
3550
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003551void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003553 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003554 locations->SetInAt(0, Location::RequiresRegister());
3555 }
3556}
3557
3558void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003559 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3560 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003561 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3562 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3563 true_target = nullptr;
3564 }
3565 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3566 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3567 false_target = nullptr;
3568 }
David Brazdil0debae72015-11-12 18:37:00 +00003569 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003570}
3571
3572void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3573 LocationSummary* locations = new (GetGraph()->GetArena())
3574 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003575 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003576 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003577 locations->SetInAt(0, Location::RequiresRegister());
3578 }
3579}
3580
3581void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003582 SlowPathCodeARM64* slow_path =
3583 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003584 GenerateTestAndBranch(deoptimize,
3585 /* condition_input_index */ 0,
3586 slow_path->GetEntryLabel(),
3587 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003588}
3589
Mingyao Yang063fc772016-08-02 11:02:54 -07003590void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3591 LocationSummary* locations = new (GetGraph()->GetArena())
3592 LocationSummary(flag, LocationSummary::kNoCall);
3593 locations->SetOut(Location::RequiresRegister());
3594}
3595
3596void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3597 __ Ldr(OutputRegister(flag),
3598 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3599}
3600
David Brazdilc0b601b2016-02-08 14:20:45 +00003601static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3602 return condition->IsCondition() &&
3603 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3604}
3605
Alexandre Rames880f1192016-06-13 16:04:50 +01003606static inline Condition GetConditionForSelect(HCondition* condition) {
3607 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003608 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3609 : ARM64Condition(cond);
3610}
3611
David Brazdil74eb1b22015-12-14 11:44:01 +00003612void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003614 if (Primitive::IsFloatingPointType(select->GetType())) {
3615 locations->SetInAt(0, Location::RequiresFpuRegister());
3616 locations->SetInAt(1, Location::RequiresFpuRegister());
3617 locations->SetOut(Location::RequiresFpuRegister());
3618 } else {
3619 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3620 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3621 bool is_true_value_constant = cst_true_value != nullptr;
3622 bool is_false_value_constant = cst_false_value != nullptr;
3623 // Ask VIXL whether we should synthesize constants in registers.
3624 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3625 Operand true_op = is_true_value_constant ?
3626 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3627 Operand false_op = is_false_value_constant ?
3628 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3629 bool true_value_in_register = false;
3630 bool false_value_in_register = false;
3631 MacroAssembler::GetCselSynthesisInformation(
3632 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3633 true_value_in_register |= !is_true_value_constant;
3634 false_value_in_register |= !is_false_value_constant;
3635
3636 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3637 : Location::ConstantLocation(cst_true_value));
3638 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3639 : Location::ConstantLocation(cst_false_value));
3640 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003641 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003642
David Brazdil74eb1b22015-12-14 11:44:01 +00003643 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3644 locations->SetInAt(2, Location::RequiresRegister());
3645 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003646}
3647
3648void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003649 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003650 Condition csel_cond;
3651
3652 if (IsBooleanValueOrMaterializedCondition(cond)) {
3653 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003654 // Use the condition flags set by the previous instruction.
3655 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003656 } else {
3657 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003658 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003659 }
3660 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003661 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003662 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003663 } else {
3664 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003665 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003666 }
3667
Alexandre Rames880f1192016-06-13 16:04:50 +01003668 if (Primitive::IsFloatingPointType(select->GetType())) {
3669 __ Fcsel(OutputFPRegister(select),
3670 InputFPRegisterAt(select, 1),
3671 InputFPRegisterAt(select, 0),
3672 csel_cond);
3673 } else {
3674 __ Csel(OutputRegister(select),
3675 InputOperandAt(select, 1),
3676 InputOperandAt(select, 0),
3677 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003678 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003679}
3680
David Srbecky0cf44932015-12-09 14:09:59 +00003681void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3682 new (GetGraph()->GetArena()) LocationSummary(info);
3683}
3684
David Srbeckyd28f4a02016-03-14 17:14:24 +00003685void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3686 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003687}
3688
3689void CodeGeneratorARM64::GenerateNop() {
3690 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003691}
3692
Alexandre Rames5319def2014-10-23 10:03:10 +01003693void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003694 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003695}
3696
3697void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003698 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003699}
3700
3701void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003702 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003703}
3704
3705void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003706 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003707}
3708
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003709// Temp is used for read barrier.
3710static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3711 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003712 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003713 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3714 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3715 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3716 return 1;
3717 }
3718 return 0;
3719}
3720
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003721// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003722// interface pointer, one for loading the current interface.
3723// The other checks have one temp for loading the object's class.
3724static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3725 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3726 return 3;
3727 }
3728 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003729}
3730
Alexandre Rames67555f72014-11-18 10:55:16 +00003731void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003732 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003733 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003734 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003735 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003736 case TypeCheckKind::kExactCheck:
3737 case TypeCheckKind::kAbstractClassCheck:
3738 case TypeCheckKind::kClassHierarchyCheck:
3739 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740 call_kind =
3741 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003742 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 break;
3744 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745 case TypeCheckKind::kUnresolvedCheck:
3746 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003747 call_kind = LocationSummary::kCallOnSlowPath;
3748 break;
3749 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003750
Alexandre Rames67555f72014-11-18 10:55:16 +00003751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003752 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003753 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003754 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003755 locations->SetInAt(0, Location::RequiresRegister());
3756 locations->SetInAt(1, Location::RequiresRegister());
3757 // The "out" register is used as a temporary, so it overlaps with the inputs.
3758 // Note that TypeCheckSlowPathARM64 uses this register too.
3759 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003760 // Add temps if necessary for read barriers.
3761 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003762}
3763
3764void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003765 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003766 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003767 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003768 Register obj = InputRegisterAt(instruction, 0);
3769 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003770 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003771 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003772 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3773 DCHECK_LE(num_temps, 1u);
3774 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003775 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3776 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3777 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3778 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003779
Scott Wakeling97c72b72016-06-24 16:19:36 +01003780 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003781 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003782
3783 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003784 // Avoid null check if we know `obj` is not null.
3785 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003786 __ Cbz(obj, &zero);
3787 }
3788
Roland Levillain44015862016-01-22 11:47:17 +00003789 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003790 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003791 // /* HeapReference<Class> */ out = obj->klass_
3792 GenerateReferenceLoadTwoRegisters(instruction,
3793 out_loc,
3794 obj_loc,
3795 class_offset,
3796 maybe_temp_loc,
3797 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003798 __ Cmp(out, cls);
3799 __ Cset(out, eq);
3800 if (zero.IsLinked()) {
3801 __ B(&done);
3802 }
3803 break;
3804 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003805
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003806 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003807 // /* HeapReference<Class> */ out = obj->klass_
3808 GenerateReferenceLoadTwoRegisters(instruction,
3809 out_loc,
3810 obj_loc,
3811 class_offset,
3812 maybe_temp_loc,
3813 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003814 // If the class is abstract, we eagerly fetch the super class of the
3815 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003816 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003817 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003818 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003819 GenerateReferenceLoadOneRegister(instruction,
3820 out_loc,
3821 super_offset,
3822 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003823 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003824 // If `out` is null, we use it for the result, and jump to `done`.
3825 __ Cbz(out, &done);
3826 __ Cmp(out, cls);
3827 __ B(ne, &loop);
3828 __ Mov(out, 1);
3829 if (zero.IsLinked()) {
3830 __ B(&done);
3831 }
3832 break;
3833 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003834
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003836 // /* HeapReference<Class> */ out = obj->klass_
3837 GenerateReferenceLoadTwoRegisters(instruction,
3838 out_loc,
3839 obj_loc,
3840 class_offset,
3841 maybe_temp_loc,
3842 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003844 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003845 __ Bind(&loop);
3846 __ Cmp(out, cls);
3847 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003848 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003849 GenerateReferenceLoadOneRegister(instruction,
3850 out_loc,
3851 super_offset,
3852 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003853 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003854 __ Cbnz(out, &loop);
3855 // If `out` is null, we use it for the result, and jump to `done`.
3856 __ B(&done);
3857 __ Bind(&success);
3858 __ Mov(out, 1);
3859 if (zero.IsLinked()) {
3860 __ B(&done);
3861 }
3862 break;
3863 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003866 // /* HeapReference<Class> */ out = obj->klass_
3867 GenerateReferenceLoadTwoRegisters(instruction,
3868 out_loc,
3869 obj_loc,
3870 class_offset,
3871 maybe_temp_loc,
3872 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003873 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003874 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003875 __ Cmp(out, cls);
3876 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003877 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003878 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003879 GenerateReferenceLoadOneRegister(instruction,
3880 out_loc,
3881 component_offset,
3882 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003883 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 // If `out` is null, we use it for the result, and jump to `done`.
3885 __ Cbz(out, &done);
3886 __ Ldrh(out, HeapOperand(out, primitive_offset));
3887 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3888 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003889 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 __ Mov(out, 1);
3891 __ B(&done);
3892 break;
3893 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003896 // No read barrier since the slow path will retry upon failure.
3897 // /* HeapReference<Class> */ out = obj->klass_
3898 GenerateReferenceLoadTwoRegisters(instruction,
3899 out_loc,
3900 obj_loc,
3901 class_offset,
3902 maybe_temp_loc,
3903 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 __ Cmp(out, cls);
3905 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3907 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 codegen_->AddSlowPath(slow_path);
3909 __ B(ne, slow_path->GetEntryLabel());
3910 __ Mov(out, 1);
3911 if (zero.IsLinked()) {
3912 __ B(&done);
3913 }
3914 break;
3915 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916
Calin Juravle98893e12015-10-02 21:05:03 +01003917 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 case TypeCheckKind::kInterfaceCheck: {
3919 // Note that we indeed only call on slow path, but we always go
3920 // into the slow path for the unresolved and interface check
3921 // cases.
3922 //
3923 // We cannot directly call the InstanceofNonTrivial runtime
3924 // entry point without resorting to a type checking slow path
3925 // here (i.e. by calling InvokeRuntime directly), as it would
3926 // require to assign fixed registers for the inputs of this
3927 // HInstanceOf instruction (following the runtime calling
3928 // convention), which might be cluttered by the potential first
3929 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003930 //
3931 // TODO: Introduce a new runtime entry point taking the object
3932 // to test (instead of its class) as argument, and let it deal
3933 // with the read barrier issues. This will let us refactor this
3934 // case of the `switch` code as it was previously (with a direct
3935 // call to the runtime not using a type checking slow path).
3936 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 DCHECK(locations->OnlyCallsOnSlowPath());
3938 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3939 /* is_fatal */ false);
3940 codegen_->AddSlowPath(slow_path);
3941 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003942 if (zero.IsLinked()) {
3943 __ B(&done);
3944 }
3945 break;
3946 }
3947 }
3948
3949 if (zero.IsLinked()) {
3950 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003951 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003952 }
3953
3954 if (done.IsLinked()) {
3955 __ Bind(&done);
3956 }
3957
3958 if (slow_path != nullptr) {
3959 __ Bind(slow_path->GetExitLabel());
3960 }
3961}
3962
3963void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3964 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3965 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3966
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003967 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3968 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 case TypeCheckKind::kExactCheck:
3970 case TypeCheckKind::kAbstractClassCheck:
3971 case TypeCheckKind::kClassHierarchyCheck:
3972 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003973 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3974 LocationSummary::kCallOnSlowPath :
3975 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 break;
3977 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978 case TypeCheckKind::kUnresolvedCheck:
3979 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003980 call_kind = LocationSummary::kCallOnSlowPath;
3981 break;
3982 }
3983
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003984 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3985 locations->SetInAt(0, Location::RequiresRegister());
3986 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003987 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3988 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003989}
3990
3991void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003992 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003994 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 Register obj = InputRegisterAt(instruction, 0);
3996 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003997 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3998 DCHECK_GE(num_temps, 1u);
3999 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004000 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004001 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4002 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004004 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4005 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4006 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4007 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4008 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4009 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4010 const uint32_t object_array_data_offset =
4011 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004013 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004014 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4015 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4016 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004017 if (!kEmitCompilerReadBarrier) {
4018 is_type_check_slow_path_fatal =
4019 (type_check_kind == TypeCheckKind::kExactCheck ||
4020 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4021 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4022 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4023 !instruction->CanThrowIntoCatchBlock();
4024 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004025 SlowPathCodeARM64* type_check_slow_path =
4026 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4027 is_type_check_slow_path_fatal);
4028 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004029
Scott Wakeling97c72b72016-06-24 16:19:36 +01004030 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 // Avoid null check if we know obj is not null.
4032 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004033 __ Cbz(obj, &done);
4034 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004035
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004036 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 case TypeCheckKind::kExactCheck:
4038 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004039 // /* HeapReference<Class> */ temp = obj->klass_
4040 GenerateReferenceLoadTwoRegisters(instruction,
4041 temp_loc,
4042 obj_loc,
4043 class_offset,
4044 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004045 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004046
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004047 __ Cmp(temp, cls);
4048 // Jump to slow path for throwing the exception or doing a
4049 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004050 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004051 break;
4052 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004053
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004055 // /* HeapReference<Class> */ temp = obj->klass_
4056 GenerateReferenceLoadTwoRegisters(instruction,
4057 temp_loc,
4058 obj_loc,
4059 class_offset,
4060 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004061 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004062
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 // If the class is abstract, we eagerly fetch the super class of the
4064 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004065 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004068 GenerateReferenceLoadOneRegister(instruction,
4069 temp_loc,
4070 super_offset,
4071 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004072 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004074 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4075 // exception.
4076 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4077 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004078 __ Cmp(temp, cls);
4079 __ B(ne, &loop);
4080 break;
4081 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004082
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004083 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004084 // /* HeapReference<Class> */ temp = obj->klass_
4085 GenerateReferenceLoadTwoRegisters(instruction,
4086 temp_loc,
4087 obj_loc,
4088 class_offset,
4089 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004090 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004091
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004092 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004093 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004094 __ Bind(&loop);
4095 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004096 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004098 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004099 GenerateReferenceLoadOneRegister(instruction,
4100 temp_loc,
4101 super_offset,
4102 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004103 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004104
4105 // If the class reference currently in `temp` is not null, jump
4106 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004108 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004109 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004110 break;
4111 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004112
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004114 // /* HeapReference<Class> */ temp = obj->klass_
4115 GenerateReferenceLoadTwoRegisters(instruction,
4116 temp_loc,
4117 obj_loc,
4118 class_offset,
4119 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004120 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004121
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004122 // Do an exact check.
4123 __ Cmp(temp, cls);
4124 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004125
4126 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004128 GenerateReferenceLoadOneRegister(instruction,
4129 temp_loc,
4130 component_offset,
4131 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004132 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004134 // If the component type is null, jump to the slow path to throw the exception.
4135 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4136 // Otherwise, the object is indeed an array. Further check that this component type is not a
4137 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004138 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4139 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004140 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004141 break;
4142 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004143
Calin Juravle98893e12015-10-02 21:05:03 +01004144 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004145 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 //
4147 // We cannot directly call the CheckCast runtime entry point
4148 // without resorting to a type checking slow path here (i.e. by
4149 // calling InvokeRuntime directly), as it would require to
4150 // assign fixed registers for the inputs of this HInstanceOf
4151 // instruction (following the runtime calling convention), which
4152 // might be cluttered by the potential first read barrier
4153 // emission at the beginning of this method.
4154 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004155 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004156 case TypeCheckKind::kInterfaceCheck: {
4157 // /* HeapReference<Class> */ temp = obj->klass_
4158 GenerateReferenceLoadTwoRegisters(instruction,
4159 temp_loc,
4160 obj_loc,
4161 class_offset,
4162 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004163 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004164
4165 // /* HeapReference<Class> */ temp = temp->iftable_
4166 GenerateReferenceLoadTwoRegisters(instruction,
4167 temp_loc,
4168 temp_loc,
4169 iftable_offset,
4170 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004171 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004172 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004173 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004174 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004175 vixl::aarch64::Label start_loop;
4176 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004177 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004178 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4179 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004180 // Go to next interface.
4181 __ Add(temp, temp, 2 * kHeapReferenceSize);
4182 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004183 // Compare the classes and continue the loop if they do not match.
4184 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4185 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004186 break;
4187 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004188 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004189 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004190
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004191 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004192}
4193
Alexandre Rames5319def2014-10-23 10:03:10 +01004194void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4196 locations->SetOut(Location::ConstantLocation(constant));
4197}
4198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004199void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004200 // Will be generated at use site.
4201}
4202
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004203void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4204 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4205 locations->SetOut(Location::ConstantLocation(constant));
4206}
4207
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004208void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004209 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004210}
4211
Calin Juravle175dc732015-08-25 15:42:32 +01004212void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4213 // The trampoline uses the same calling convention as dex calling conventions,
4214 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4215 // the method_idx.
4216 HandleInvoke(invoke);
4217}
4218
4219void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4220 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4221}
4222
Alexandre Rames5319def2014-10-23 10:03:10 +01004223void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004224 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004225 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004226}
4227
Alexandre Rames67555f72014-11-18 10:55:16 +00004228void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4229 HandleInvoke(invoke);
4230}
4231
4232void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4233 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004234 LocationSummary* locations = invoke->GetLocations();
4235 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004237 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004238 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004239
4240 // The register ip1 is required to be used for the hidden argument in
4241 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004242 MacroAssembler* masm = GetVIXLAssembler();
4243 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004244 scratch_scope.Exclude(ip1);
4245 __ Mov(ip1, invoke->GetDexMethodIndex());
4246
Artem Serov914d7a82017-02-07 14:33:49 +00004247 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004248 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004249 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004250 {
4251 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4252 // /* HeapReference<Class> */ temp = temp->klass_
4253 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4254 codegen_->MaybeRecordImplicitNullCheck(invoke);
4255 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004256 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004257 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004259 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004260 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004261 }
Artem Serov914d7a82017-02-07 14:33:49 +00004262
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263 // Instead of simply (possibly) unpoisoning `temp` here, we should
4264 // emit a read barrier for the previous class reference load.
4265 // However this is not required in practice, as this is an
4266 // intermediate/temporary reference and because the current
4267 // concurrent copying collector keeps the from-space memory
4268 // intact/accessible until the end of the marking phase (the
4269 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004270 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004271 __ Ldr(temp,
4272 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4273 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004274 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004275 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004276 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004277 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004278 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004279
4280 {
4281 // Ensure the pc position is recorded immediately after the `blr` instruction.
4282 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4283
4284 // lr();
4285 __ blr(lr);
4286 DCHECK(!codegen_->IsLeafMethod());
4287 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4288 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004289}
4290
4291void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004292 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
4293 if (intrinsic.TryDispatch(invoke)) {
4294 return;
4295 }
4296
Alexandre Rames67555f72014-11-18 10:55:16 +00004297 HandleInvoke(invoke);
4298}
4299
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004300void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004301 // Explicit clinit checks triggered by static invokes must have been pruned by
4302 // art::PrepareForRegisterAllocation.
4303 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004304
Andreas Gampe878d58c2015-01-15 23:24:00 -08004305 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
4306 if (intrinsic.TryDispatch(invoke)) {
4307 return;
4308 }
4309
Alexandre Rames67555f72014-11-18 10:55:16 +00004310 HandleInvoke(invoke);
4311}
4312
Andreas Gampe878d58c2015-01-15 23:24:00 -08004313static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4314 if (invoke->GetLocations()->Intrinsified()) {
4315 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4316 intrinsic.Dispatch(invoke);
4317 return true;
4318 }
4319 return false;
4320}
4321
Vladimir Markodc151b22015-10-15 18:02:30 +01004322HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4323 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004324 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004325 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004326 return desired_dispatch_info;
4327}
4328
TatWai Chongd8c052a2016-11-02 16:12:48 +08004329Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4330 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004331 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004332 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4333 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004334 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4335 uint32_t offset =
4336 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004337 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004338 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004339 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004340 }
Vladimir Marko58155012015-08-19 12:49:41 +00004341 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004342 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004343 break;
4344 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4345 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004346 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004347 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004348 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4349 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004350 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004351 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004352 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004353 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004354 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004355 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004356 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004357 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004358 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004359 }
Vladimir Marko58155012015-08-19 12:49:41 +00004360 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004361 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004362 Register reg = XRegisterFrom(temp);
4363 Register method_reg;
4364 if (current_method.IsRegister()) {
4365 method_reg = XRegisterFrom(current_method);
4366 } else {
4367 DCHECK(invoke->GetLocations()->Intrinsified());
4368 DCHECK(!current_method.IsValid());
4369 method_reg = reg;
4370 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4371 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004372
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004373 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004374 __ Ldr(reg.X(),
4375 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004376 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004377 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004378 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4379 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004380 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4381 break;
4382 }
4383 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004384 return callee_method;
4385}
4386
4387void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4388 // All registers are assumed to be correctly set up.
4389 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004390
4391 switch (invoke->GetCodePtrLocation()) {
4392 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4393 __ Bl(&frame_entry_label_);
4394 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004395 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4396 // LR = callee_method->entry_point_from_quick_compiled_code_;
4397 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004398 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004399 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004400 {
4401 // To ensure that the pc position is recorded immediately after the `blr` instruction
4402 // BLR must be the last instruction emitted in this function.
4403 // Recording the pc will occur right after returning from this function.
4404 ExactAssemblyScope eas(GetVIXLAssembler(),
4405 kInstructionSize,
4406 CodeBufferCheckScope::kExactSize);
4407 // lr()
4408 __ blr(lr);
4409 }
Vladimir Marko58155012015-08-19 12:49:41 +00004410 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004411 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004412
Andreas Gampe878d58c2015-01-15 23:24:00 -08004413 DCHECK(!IsLeafMethod());
4414}
4415
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004416void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004417 // Use the calling convention instead of the location of the receiver, as
4418 // intrinsics may have put the receiver in a different register. In the intrinsics
4419 // slow path, the arguments have been moved to the right place, so here we are
4420 // guaranteed that the receiver is the first register of the calling convention.
4421 InvokeDexCallingConvention calling_convention;
4422 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004423 Register temp = XRegisterFrom(temp_in);
4424 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4425 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4426 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004427 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004429 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004430
4431 {
4432 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4433 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4434 // /* HeapReference<Class> */ temp = receiver->klass_
4435 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4436 MaybeRecordImplicitNullCheck(invoke);
4437 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004438 // Instead of simply (possibly) unpoisoning `temp` here, we should
4439 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004440 // intermediate/temporary reference and because the current
4441 // concurrent copying collector keeps the from-space memory
4442 // intact/accessible until the end of the marking phase (the
4443 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004444 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4445 // temp = temp->GetMethodAt(method_offset);
4446 __ Ldr(temp, MemOperand(temp, method_offset));
4447 // lr = temp->GetEntryPoint();
4448 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004449 {
4450 // To ensure that the pc position is recorded immediately after the `blr` instruction
4451 // BLR should be the last instruction emitted in this function.
4452 // Recording the pc will occur right after returning from this function.
4453 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4454 // lr();
4455 __ blr(lr);
4456 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004457}
4458
Orion Hodsonac141392017-01-13 11:53:47 +00004459void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4460 HandleInvoke(invoke);
4461}
4462
4463void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4464 codegen_->GenerateInvokePolymorphicCall(invoke);
4465}
4466
Scott Wakeling97c72b72016-06-24 16:19:36 +01004467vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4468 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004469 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004470 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004471 return
4472 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004473}
4474
Scott Wakeling97c72b72016-06-24 16:19:36 +01004475vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4476 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004477 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004478 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004479 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004480}
4481
Vladimir Marko1998cd02017-01-13 13:02:58 +00004482vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4483 const DexFile& dex_file,
4484 dex::TypeIndex type_index,
4485 vixl::aarch64::Label* adrp_label) {
4486 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4487}
4488
Scott Wakeling97c72b72016-06-24 16:19:36 +01004489vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4490 const DexFile& dex_file,
4491 uint32_t element_offset,
4492 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004493 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4494}
4495
Scott Wakeling97c72b72016-06-24 16:19:36 +01004496vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4497 const DexFile& dex_file,
4498 uint32_t offset_or_index,
4499 vixl::aarch64::Label* adrp_label,
4500 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004501 // Add a patch entry and return the label.
4502 patches->emplace_back(dex_file, offset_or_index);
4503 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004504 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004505 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4506 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4507 return label;
4508}
4509
Scott Wakeling97c72b72016-06-24 16:19:36 +01004510vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004511 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512 return boot_image_string_patches_.GetOrCreate(
4513 StringReference(&dex_file, string_index),
4514 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4515}
4516
Scott Wakeling97c72b72016-06-24 16:19:36 +01004517vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004518 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004519 return boot_image_type_patches_.GetOrCreate(
4520 TypeReference(&dex_file, type_index),
4521 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4522}
4523
Scott Wakeling97c72b72016-06-24 16:19:36 +01004524vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4525 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4527 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4528 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4529}
4530
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004531vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004532 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4533 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4534 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004535 return jit_string_patches_.GetOrCreate(
4536 StringReference(&dex_file, string_index),
4537 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4538}
4539
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004540vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004541 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4542 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4543 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004544 return jit_class_patches_.GetOrCreate(
4545 TypeReference(&dex_file, type_index),
4546 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4547}
4548
Vladimir Markoaad75c62016-10-03 08:46:48 +00004549void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4550 vixl::aarch64::Register reg) {
4551 DCHECK(reg.IsX());
4552 SingleEmissionCheckScope guard(GetVIXLAssembler());
4553 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004554 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004555}
4556
4557void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4558 vixl::aarch64::Register out,
4559 vixl::aarch64::Register base) {
4560 DCHECK(out.IsX());
4561 DCHECK(base.IsX());
4562 SingleEmissionCheckScope guard(GetVIXLAssembler());
4563 __ Bind(fixup_label);
4564 __ add(out, base, Operand(/* offset placeholder */ 0));
4565}
4566
4567void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4568 vixl::aarch64::Register out,
4569 vixl::aarch64::Register base) {
4570 DCHECK(base.IsX());
4571 SingleEmissionCheckScope guard(GetVIXLAssembler());
4572 __ Bind(fixup_label);
4573 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4574}
4575
4576template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4577inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4578 const ArenaDeque<PcRelativePatchInfo>& infos,
4579 ArenaVector<LinkerPatch>* linker_patches) {
4580 for (const PcRelativePatchInfo& info : infos) {
4581 linker_patches->push_back(Factory(info.label.GetLocation(),
4582 &info.target_dex_file,
4583 info.pc_insn_label->GetLocation(),
4584 info.offset_or_index));
4585 }
4586}
4587
Vladimir Marko58155012015-08-19 12:49:41 +00004588void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4589 DCHECK(linker_patches->empty());
4590 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004591 pc_relative_dex_cache_patches_.size() +
4592 boot_image_string_patches_.size() +
4593 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004594 boot_image_type_patches_.size() +
4595 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004596 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004597 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004598 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004599 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004600 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004601 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004602 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004603 info.offset_or_index));
4604 }
4605 for (const auto& entry : boot_image_string_patches_) {
4606 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004607 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4608 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004609 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004610 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004611 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004613 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004614 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4615 linker_patches);
4616 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004617 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4618 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004619 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4620 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004621 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004622 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4623 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004624 for (const auto& entry : boot_image_type_patches_) {
4625 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004626 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4627 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004628 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004629 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004630 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004631 for (const auto& entry : boot_image_address_patches_) {
4632 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004633 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4634 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004635 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004636 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004637}
4638
Scott Wakeling97c72b72016-06-24 16:19:36 +01004639vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 Uint32ToLiteralMap* map) {
4641 return map->GetOrCreate(
4642 value,
4643 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4644}
4645
Scott Wakeling97c72b72016-06-24 16:19:36 +01004646vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004647 return uint64_literals_.GetOrCreate(
4648 value,
4649 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004650}
4651
Scott Wakeling97c72b72016-06-24 16:19:36 +01004652vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004653 MethodReference target_method,
4654 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004655 return map->GetOrCreate(
4656 target_method,
4657 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004658}
4659
Andreas Gampe878d58c2015-01-15 23:24:00 -08004660void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004661 // Explicit clinit checks triggered by static invokes must have been pruned by
4662 // art::PrepareForRegisterAllocation.
4663 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004664
Andreas Gampe878d58c2015-01-15 23:24:00 -08004665 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4666 return;
4667 }
4668
Artem Serov914d7a82017-02-07 14:33:49 +00004669 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4670 // are no pools emitted.
4671 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004672 LocationSummary* locations = invoke->GetLocations();
4673 codegen_->GenerateStaticOrDirectCall(
4674 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004675 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004676}
4677
4678void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004679 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4680 return;
4681 }
4682
Artem Serov914d7a82017-02-07 14:33:49 +00004683 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4684 // are no pools emitted.
4685 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004686 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004687 DCHECK(!codegen_->IsLeafMethod());
4688 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4689}
4690
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004691HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4692 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004693 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004694 case HLoadClass::LoadKind::kInvalid:
4695 LOG(FATAL) << "UNREACHABLE";
4696 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004697 case HLoadClass::LoadKind::kReferrersClass:
4698 break;
4699 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4700 DCHECK(!GetCompilerOptions().GetCompilePic());
4701 break;
4702 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4703 DCHECK(GetCompilerOptions().GetCompilePic());
4704 break;
4705 case HLoadClass::LoadKind::kBootImageAddress:
4706 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004707 case HLoadClass::LoadKind::kBssEntry:
4708 DCHECK(!Runtime::Current()->UseJitCompilation());
4709 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004710 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004711 DCHECK(Runtime::Current()->UseJitCompilation());
4712 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004713 case HLoadClass::LoadKind::kDexCacheViaMethod:
4714 break;
4715 }
4716 return desired_class_load_kind;
4717}
4718
Alexandre Rames67555f72014-11-18 10:55:16 +00004719void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004720 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4721 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004722 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004723 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004724 cls,
4725 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004726 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004727 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004728 return;
4729 }
Vladimir Marko41559982017-01-06 14:04:23 +00004730 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004731
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004732 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4733 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004734 ? LocationSummary::kCallOnSlowPath
4735 : LocationSummary::kNoCall;
4736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004737 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004738 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004739 }
4740
Vladimir Marko41559982017-01-06 14:04:23 +00004741 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004742 locations->SetInAt(0, Location::RequiresRegister());
4743 }
4744 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004745 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4746 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4747 // Rely on the type resolution or initialization and marking to save everything we need.
4748 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4749 // to the custom calling convention) or by marking, so we shall use IP1.
4750 RegisterSet caller_saves = RegisterSet::Empty();
4751 InvokeRuntimeCallingConvention calling_convention;
4752 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4753 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4754 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4755 Primitive::kPrimNot).GetCode());
4756 locations->SetCustomSlowPathCallerSaves(caller_saves);
4757 } else {
4758 // For non-Baker read barrier we have a temp-clobbering call.
4759 }
4760 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004761}
4762
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004763// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4764// move.
4765void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004766 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4767 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4768 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004769 return;
4770 }
Vladimir Marko41559982017-01-06 14:04:23 +00004771 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004772
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004773 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004774 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004775 Register bss_entry_temp;
4776 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004777
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004778 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4779 ? kWithoutReadBarrier
4780 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004781 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004782 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004783 case HLoadClass::LoadKind::kReferrersClass: {
4784 DCHECK(!cls->CanCallRuntime());
4785 DCHECK(!cls->MustGenerateClinitCheck());
4786 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4787 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004788 GenerateGcRootFieldLoad(cls,
4789 out_loc,
4790 current_method,
4791 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004792 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004793 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004794 break;
4795 }
4796 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004797 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004798 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4799 cls->GetTypeIndex()));
4800 break;
4801 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004802 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004803 // Add ADRP with its PC-relative type patch.
4804 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004805 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004806 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004807 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004808 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004809 vixl::aarch64::Label* add_label =
4810 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004811 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004812 break;
4813 }
4814 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004815 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004816 uint32_t address = dchecked_integral_cast<uint32_t>(
4817 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4818 DCHECK_NE(address, 0u);
4819 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004820 break;
4821 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004822 case HLoadClass::LoadKind::kBssEntry: {
4823 // Add ADRP with its PC-relative Class .bss entry patch.
4824 const DexFile& dex_file = cls->GetDexFile();
4825 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004826 // We can go to slow path even with non-zero reference and in that case marking
4827 // can clobber IP0, so we need to use IP1 which shall be preserved.
4828 bss_entry_temp = ip1;
4829 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4830 temps.Exclude(bss_entry_temp);
4831 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4832 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004833 // Add LDR with its PC-relative Class patch.
4834 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004835 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004836 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4837 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004838 out_loc,
4839 bss_entry_temp,
4840 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004841 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004842 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004843 generate_null_check = true;
4844 break;
4845 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004846 case HLoadClass::LoadKind::kJitTableAddress: {
4847 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4848 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004849 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004850 GenerateGcRootFieldLoad(cls,
4851 out_loc,
4852 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004853 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004854 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004855 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004856 break;
4857 }
Vladimir Marko41559982017-01-06 14:04:23 +00004858 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004859 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004860 LOG(FATAL) << "UNREACHABLE";
4861 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004862 }
4863
Vladimir Markoea4c1262017-02-06 19:59:33 +00004864 bool do_clinit = cls->MustGenerateClinitCheck();
4865 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866 DCHECK(cls->CanCallRuntime());
4867 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004868 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 codegen_->AddSlowPath(slow_path);
4870 if (generate_null_check) {
4871 __ Cbz(out, slow_path->GetEntryLabel());
4872 }
4873 if (cls->MustGenerateClinitCheck()) {
4874 GenerateClassInitializationCheck(slow_path, out);
4875 } else {
4876 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004877 }
4878 }
4879}
4880
David Brazdilcb1c0552015-08-04 16:22:25 +01004881static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004882 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004883}
4884
Alexandre Rames67555f72014-11-18 10:55:16 +00004885void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4886 LocationSummary* locations =
4887 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4888 locations->SetOut(Location::RequiresRegister());
4889}
4890
4891void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004892 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4893}
4894
4895void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4896 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4897}
4898
4899void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4900 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004901}
4902
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004903HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4904 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004905 switch (desired_string_load_kind) {
4906 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4907 DCHECK(!GetCompilerOptions().GetCompilePic());
4908 break;
4909 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4910 DCHECK(GetCompilerOptions().GetCompilePic());
4911 break;
4912 case HLoadString::LoadKind::kBootImageAddress:
4913 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004914 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004915 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004916 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004917 case HLoadString::LoadKind::kJitTableAddress:
4918 DCHECK(Runtime::Current()->UseJitCompilation());
4919 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004920 case HLoadString::LoadKind::kDexCacheViaMethod:
4921 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004922 }
4923 return desired_string_load_kind;
4924}
4925
Alexandre Rames67555f72014-11-18 10:55:16 +00004926void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004927 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004928 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004929 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004930 InvokeRuntimeCallingConvention calling_convention;
4931 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4932 } else {
4933 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004934 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4935 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004936 // Rely on the pResolveString and marking to save everything we need.
4937 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4938 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004939 RegisterSet caller_saves = RegisterSet::Empty();
4940 InvokeRuntimeCallingConvention calling_convention;
4941 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4942 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4943 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4944 Primitive::kPrimNot).GetCode());
4945 locations->SetCustomSlowPathCallerSaves(caller_saves);
4946 } else {
4947 // For non-Baker read barrier we have a temp-clobbering call.
4948 }
4949 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004950 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004951}
4952
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004953// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4954// move.
4955void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004956 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004957 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004958
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004959 switch (load->GetLoadKind()) {
4960 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004961 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4962 load->GetStringIndex()));
4963 return; // No dex cache slow path.
4964 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004965 // Add ADRP with its PC-relative String patch.
4966 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004967 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004969 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004970 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004971 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004972 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004973 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004974 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004975 return; // No dex cache slow path.
4976 }
4977 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004978 uint32_t address = dchecked_integral_cast<uint32_t>(
4979 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4980 DCHECK_NE(address, 0u);
4981 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004982 return; // No dex cache slow path.
4983 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004984 case HLoadString::LoadKind::kBssEntry: {
4985 // Add ADRP with its PC-relative String .bss entry patch.
4986 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004987 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004988 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004989 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
4990 // that's when we need the slow path. But let's not rely on such details and use IP1.
4991 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004992 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004993 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004994 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004995 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004996 // Add LDR with its PC-relative String patch.
4997 vixl::aarch64::Label* ldr_label =
4998 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004999 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005000 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005001 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005002 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005003 /* offset placeholder */ 0u,
5004 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005005 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005006 SlowPathCodeARM64* slow_path =
5007 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005008 codegen_->AddSlowPath(slow_path);
5009 __ Cbz(out.X(), slow_path->GetEntryLabel());
5010 __ Bind(slow_path->GetExitLabel());
5011 return;
5012 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005013 case HLoadString::LoadKind::kJitTableAddress: {
5014 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005015 load->GetStringIndex(),
5016 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005017 GenerateGcRootFieldLoad(load,
5018 out_loc,
5019 out.X(),
5020 /* offset */ 0,
5021 /* fixup_label */ nullptr,
5022 kCompilerReadBarrierOption);
5023 return;
5024 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005025 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005026 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005027 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005028
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005029 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005030 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005031 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005032 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005033 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5034 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005035}
5036
Alexandre Rames5319def2014-10-23 10:03:10 +01005037void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5038 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5039 locations->SetOut(Location::ConstantLocation(constant));
5040}
5041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005042void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005043 // Will be generated at use site.
5044}
5045
Alexandre Rames67555f72014-11-18 10:55:16 +00005046void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5047 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005049 InvokeRuntimeCallingConvention calling_convention;
5050 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5051}
5052
5053void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005054 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005055 instruction,
5056 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005057 if (instruction->IsEnter()) {
5058 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5059 } else {
5060 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5061 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005062}
5063
Alexandre Rames42d641b2014-10-27 14:00:51 +00005064void LocationsBuilderARM64::VisitMul(HMul* mul) {
5065 LocationSummary* locations =
5066 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5067 switch (mul->GetResultType()) {
5068 case Primitive::kPrimInt:
5069 case Primitive::kPrimLong:
5070 locations->SetInAt(0, Location::RequiresRegister());
5071 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005073 break;
5074
5075 case Primitive::kPrimFloat:
5076 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005077 locations->SetInAt(0, Location::RequiresFpuRegister());
5078 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005079 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005080 break;
5081
5082 default:
5083 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5084 }
5085}
5086
5087void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5088 switch (mul->GetResultType()) {
5089 case Primitive::kPrimInt:
5090 case Primitive::kPrimLong:
5091 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5092 break;
5093
5094 case Primitive::kPrimFloat:
5095 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005096 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005097 break;
5098
5099 default:
5100 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5101 }
5102}
5103
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005104void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5105 LocationSummary* locations =
5106 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5107 switch (neg->GetResultType()) {
5108 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005109 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005110 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005112 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005113
5114 case Primitive::kPrimFloat:
5115 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005116 locations->SetInAt(0, Location::RequiresFpuRegister());
5117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005118 break;
5119
5120 default:
5121 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5122 }
5123}
5124
5125void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5126 switch (neg->GetResultType()) {
5127 case Primitive::kPrimInt:
5128 case Primitive::kPrimLong:
5129 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5130 break;
5131
5132 case Primitive::kPrimFloat:
5133 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005134 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005135 break;
5136
5137 default:
5138 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5139 }
5140}
5141
5142void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5143 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005145 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005146 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005147 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5148 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005149}
5150
5151void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005152 // Note: if heap poisoning is enabled, the entry point takes cares
5153 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005154 QuickEntrypointEnum entrypoint =
5155 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5156 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005157 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005158}
5159
Alexandre Rames5319def2014-10-23 10:03:10 +01005160void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5161 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005163 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005164 if (instruction->IsStringAlloc()) {
5165 locations->AddTemp(LocationFrom(kArtMethodRegister));
5166 } else {
5167 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005168 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005169 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5170}
5171
5172void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005173 // Note: if heap poisoning is enabled, the entry point takes cares
5174 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005175 if (instruction->IsStringAlloc()) {
5176 // String is allocated through StringFactory. Call NewEmptyString entry point.
5177 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005178 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005179 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5180 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005181
5182 {
5183 // Ensure the pc position is recorded immediately after the `blr` instruction.
5184 ExactAssemblyScope eas(GetVIXLAssembler(),
5185 kInstructionSize,
5186 CodeBufferCheckScope::kExactSize);
5187 __ blr(lr);
5188 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5189 }
David Brazdil6de19382016-01-08 17:37:10 +00005190 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005191 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005192 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005193 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005194}
5195
5196void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005198 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005200}
5201
5202void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005203 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005204 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005205 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005206 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005207 break;
5208
5209 default:
5210 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5211 }
5212}
5213
David Brazdil66d126e2015-04-03 16:02:44 +01005214void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5215 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5216 locations->SetInAt(0, Location::RequiresRegister());
5217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5218}
5219
5220void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005221 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005222}
5223
Alexandre Rames5319def2014-10-23 10:03:10 +01005224void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005225 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5226 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005227}
5228
Calin Juravle2ae48182016-03-16 14:05:09 +00005229void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5230 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005231 return;
5232 }
Artem Serov914d7a82017-02-07 14:33:49 +00005233 {
5234 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5235 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5236 Location obj = instruction->GetLocations()->InAt(0);
5237 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5238 RecordPcInfo(instruction, instruction->GetDexPc());
5239 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005240}
5241
Calin Juravle2ae48182016-03-16 14:05:09 +00005242void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005243 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005244 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005245
5246 LocationSummary* locations = instruction->GetLocations();
5247 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005248
5249 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005250}
5251
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005252void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005253 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005254}
5255
Alexandre Rames67555f72014-11-18 10:55:16 +00005256void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5257 HandleBinaryOp(instruction);
5258}
5259
5260void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5261 HandleBinaryOp(instruction);
5262}
5263
Alexandre Rames3e69f162014-12-10 10:36:50 +00005264void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5265 LOG(FATAL) << "Unreachable";
5266}
5267
5268void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5269 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5270}
5271
Alexandre Rames5319def2014-10-23 10:03:10 +01005272void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5274 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5275 if (location.IsStackSlot()) {
5276 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5277 } else if (location.IsDoubleStackSlot()) {
5278 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5279 }
5280 locations->SetOut(location);
5281}
5282
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005283void InstructionCodeGeneratorARM64::VisitParameterValue(
5284 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005285 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005286}
5287
5288void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5289 LocationSummary* locations =
5290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005291 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005292}
5293
5294void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5295 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5296 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005297}
5298
5299void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005301 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005302 locations->SetInAt(i, Location::Any());
5303 }
5304 locations->SetOut(Location::Any());
5305}
5306
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005307void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005308 LOG(FATAL) << "Unreachable";
5309}
5310
Serban Constantinescu02164b32014-11-13 14:05:07 +00005311void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005312 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005313 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005314 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5315 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5317
5318 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005319 case Primitive::kPrimInt:
5320 case Primitive::kPrimLong:
5321 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005322 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5324 break;
5325
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005326 case Primitive::kPrimFloat:
5327 case Primitive::kPrimDouble: {
5328 InvokeRuntimeCallingConvention calling_convention;
5329 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5330 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5331 locations->SetOut(calling_convention.GetReturnLocation(type));
5332
5333 break;
5334 }
5335
Serban Constantinescu02164b32014-11-13 14:05:07 +00005336 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005337 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005338 }
5339}
5340
5341void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5342 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005343
Serban Constantinescu02164b32014-11-13 14:05:07 +00005344 switch (type) {
5345 case Primitive::kPrimInt:
5346 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005347 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005348 break;
5349 }
5350
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005351 case Primitive::kPrimFloat:
5352 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005353 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5354 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005355 if (type == Primitive::kPrimFloat) {
5356 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5357 } else {
5358 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5359 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005360 break;
5361 }
5362
Serban Constantinescu02164b32014-11-13 14:05:07 +00005363 default:
5364 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005365 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005366 }
5367}
5368
Calin Juravle27df7582015-04-17 19:12:31 +01005369void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5370 memory_barrier->SetLocations(nullptr);
5371}
5372
5373void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005374 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005375}
5376
Alexandre Rames5319def2014-10-23 10:03:10 +01005377void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5379 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005380 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005381}
5382
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005383void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005384 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005385}
5386
5387void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5388 instruction->SetLocations(nullptr);
5389}
5390
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005391void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005392 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005393}
5394
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005395void LocationsBuilderARM64::VisitRor(HRor* ror) {
5396 HandleBinaryOp(ror);
5397}
5398
5399void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5400 HandleBinaryOp(ror);
5401}
5402
Serban Constantinescu02164b32014-11-13 14:05:07 +00005403void LocationsBuilderARM64::VisitShl(HShl* shl) {
5404 HandleShift(shl);
5405}
5406
5407void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5408 HandleShift(shl);
5409}
5410
5411void LocationsBuilderARM64::VisitShr(HShr* shr) {
5412 HandleShift(shr);
5413}
5414
5415void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5416 HandleShift(shr);
5417}
5418
Alexandre Rames5319def2014-10-23 10:03:10 +01005419void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005420 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005421}
5422
5423void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005424 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005425}
5426
Alexandre Rames67555f72014-11-18 10:55:16 +00005427void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005428 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005429}
5430
5431void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005432 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005433}
5434
5435void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005436 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005437}
5438
Alexandre Rames67555f72014-11-18 10:55:16 +00005439void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005440 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005441}
5442
Calin Juravlee460d1d2015-09-29 04:52:17 +01005443void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5444 HUnresolvedInstanceFieldGet* instruction) {
5445 FieldAccessCallingConventionARM64 calling_convention;
5446 codegen_->CreateUnresolvedFieldLocationSummary(
5447 instruction, instruction->GetFieldType(), calling_convention);
5448}
5449
5450void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5451 HUnresolvedInstanceFieldGet* instruction) {
5452 FieldAccessCallingConventionARM64 calling_convention;
5453 codegen_->GenerateUnresolvedFieldAccess(instruction,
5454 instruction->GetFieldType(),
5455 instruction->GetFieldIndex(),
5456 instruction->GetDexPc(),
5457 calling_convention);
5458}
5459
5460void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5461 HUnresolvedInstanceFieldSet* instruction) {
5462 FieldAccessCallingConventionARM64 calling_convention;
5463 codegen_->CreateUnresolvedFieldLocationSummary(
5464 instruction, instruction->GetFieldType(), calling_convention);
5465}
5466
5467void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5468 HUnresolvedInstanceFieldSet* instruction) {
5469 FieldAccessCallingConventionARM64 calling_convention;
5470 codegen_->GenerateUnresolvedFieldAccess(instruction,
5471 instruction->GetFieldType(),
5472 instruction->GetFieldIndex(),
5473 instruction->GetDexPc(),
5474 calling_convention);
5475}
5476
5477void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5478 HUnresolvedStaticFieldGet* instruction) {
5479 FieldAccessCallingConventionARM64 calling_convention;
5480 codegen_->CreateUnresolvedFieldLocationSummary(
5481 instruction, instruction->GetFieldType(), calling_convention);
5482}
5483
5484void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5485 HUnresolvedStaticFieldGet* instruction) {
5486 FieldAccessCallingConventionARM64 calling_convention;
5487 codegen_->GenerateUnresolvedFieldAccess(instruction,
5488 instruction->GetFieldType(),
5489 instruction->GetFieldIndex(),
5490 instruction->GetDexPc(),
5491 calling_convention);
5492}
5493
5494void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5495 HUnresolvedStaticFieldSet* instruction) {
5496 FieldAccessCallingConventionARM64 calling_convention;
5497 codegen_->CreateUnresolvedFieldLocationSummary(
5498 instruction, instruction->GetFieldType(), calling_convention);
5499}
5500
5501void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5502 HUnresolvedStaticFieldSet* instruction) {
5503 FieldAccessCallingConventionARM64 calling_convention;
5504 codegen_->GenerateUnresolvedFieldAccess(instruction,
5505 instruction->GetFieldType(),
5506 instruction->GetFieldIndex(),
5507 instruction->GetDexPc(),
5508 calling_convention);
5509}
5510
Alexandre Rames5319def2014-10-23 10:03:10 +01005511void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005512 LocationSummary* locations =
5513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005514 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
5517void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005518 HBasicBlock* block = instruction->GetBlock();
5519 if (block->GetLoopInformation() != nullptr) {
5520 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5521 // The back edge will generate the suspend check.
5522 return;
5523 }
5524 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5525 // The goto will generate the suspend check.
5526 return;
5527 }
5528 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005529}
5530
Alexandre Rames67555f72014-11-18 10:55:16 +00005531void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5532 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005534 InvokeRuntimeCallingConvention calling_convention;
5535 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5536}
5537
5538void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005539 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005540 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005541}
5542
5543void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5544 LocationSummary* locations =
5545 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5546 Primitive::Type input_type = conversion->GetInputType();
5547 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005548 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005549 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5550 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5551 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5552 }
5553
Alexandre Rames542361f2015-01-29 16:57:31 +00005554 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005555 locations->SetInAt(0, Location::RequiresFpuRegister());
5556 } else {
5557 locations->SetInAt(0, Location::RequiresRegister());
5558 }
5559
Alexandre Rames542361f2015-01-29 16:57:31 +00005560 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005561 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5562 } else {
5563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5564 }
5565}
5566
5567void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5568 Primitive::Type result_type = conversion->GetResultType();
5569 Primitive::Type input_type = conversion->GetInputType();
5570
5571 DCHECK_NE(input_type, result_type);
5572
Alexandre Rames542361f2015-01-29 16:57:31 +00005573 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005574 int result_size = Primitive::ComponentSize(result_type);
5575 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005576 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005577 Register output = OutputRegister(conversion);
5578 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005579 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005580 // 'int' values are used directly as W registers, discarding the top
5581 // bits, so we don't need to sign-extend and can just perform a move.
5582 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5583 // top 32 bits of the target register. We theoretically could leave those
5584 // bits unchanged, but we would have to make sure that no code uses a
5585 // 32bit input value as a 64bit value assuming that the top 32 bits are
5586 // zero.
5587 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005588 } else if (result_type == Primitive::kPrimChar ||
5589 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5590 __ Ubfx(output,
5591 output.IsX() ? source.X() : source.W(),
5592 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005593 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005594 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005595 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005596 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005597 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005598 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005599 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5600 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005601 } else if (Primitive::IsFloatingPointType(result_type) &&
5602 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005603 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5604 } else {
5605 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5606 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005607 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005608}
Alexandre Rames67555f72014-11-18 10:55:16 +00005609
Serban Constantinescu02164b32014-11-13 14:05:07 +00005610void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5611 HandleShift(ushr);
5612}
5613
5614void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5615 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005616}
5617
5618void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5619 HandleBinaryOp(instruction);
5620}
5621
5622void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5623 HandleBinaryOp(instruction);
5624}
5625
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005626void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005627 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005628 LOG(FATAL) << "Unreachable";
5629}
5630
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005631void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005632 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005633 LOG(FATAL) << "Unreachable";
5634}
5635
Mark Mendellfe57faa2015-09-18 09:26:15 -04005636// Simple implementation of packed switch - generate cascaded compare/jumps.
5637void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5638 LocationSummary* locations =
5639 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5640 locations->SetInAt(0, Location::RequiresRegister());
5641}
5642
5643void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5644 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005645 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005646 Register value_reg = InputRegisterAt(switch_instr, 0);
5647 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5648
Zheng Xu3927c8b2015-11-18 17:46:25 +08005649 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005650 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005651 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5652 // make sure we don't emit it if the target may run out of range.
5653 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5654 // ranges and emit the tables only as required.
5655 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005656
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005657 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005658 // Current instruction id is an upper bound of the number of HIRs in the graph.
5659 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5660 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005661 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5662 Register temp = temps.AcquireW();
5663 __ Subs(temp, value_reg, Operand(lower_bound));
5664
Zheng Xu3927c8b2015-11-18 17:46:25 +08005665 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005666 // Jump to successors[0] if value == lower_bound.
5667 __ B(eq, codegen_->GetLabelOf(successors[0]));
5668 int32_t last_index = 0;
5669 for (; num_entries - last_index > 2; last_index += 2) {
5670 __ Subs(temp, temp, Operand(2));
5671 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5672 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5673 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5674 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5675 }
5676 if (num_entries - last_index == 2) {
5677 // The last missing case_value.
5678 __ Cmp(temp, Operand(1));
5679 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005680 }
5681
5682 // And the default for any other value.
5683 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5684 __ B(codegen_->GetLabelOf(default_block));
5685 }
5686 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005687 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005688
5689 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5690
5691 // Below instructions should use at most one blocked register. Since there are two blocked
5692 // registers, we are free to block one.
5693 Register temp_w = temps.AcquireW();
5694 Register index;
5695 // Remove the bias.
5696 if (lower_bound != 0) {
5697 index = temp_w;
5698 __ Sub(index, value_reg, Operand(lower_bound));
5699 } else {
5700 index = value_reg;
5701 }
5702
5703 // Jump to default block if index is out of the range.
5704 __ Cmp(index, Operand(num_entries));
5705 __ B(hs, codegen_->GetLabelOf(default_block));
5706
5707 // In current VIXL implementation, it won't require any blocked registers to encode the
5708 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5709 // register pressure.
5710 Register table_base = temps.AcquireX();
5711 // Load jump offset from the table.
5712 __ Adr(table_base, jump_table->GetTableStartLabel());
5713 Register jump_offset = temp_w;
5714 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5715
5716 // Jump to target block by branching to table_base(pc related) + offset.
5717 Register target_address = table_base;
5718 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5719 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005720 }
5721}
5722
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005723void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5724 HInstruction* instruction,
5725 Location out,
5726 uint32_t offset,
5727 Location maybe_temp,
5728 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005729 Primitive::Type type = Primitive::kPrimNot;
5730 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005731 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005732 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005733 Register temp_reg = RegisterFrom(maybe_temp, type);
5734 if (kUseBakerReadBarrier) {
5735 // Load with fast path based Baker's read barrier.
5736 // /* HeapReference<Object> */ out = *(out + offset)
5737 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5738 out,
5739 out_reg,
5740 offset,
5741 temp_reg,
5742 /* needs_null_check */ false,
5743 /* use_load_acquire */ false);
5744 } else {
5745 // Load with slow path based read barrier.
5746 // Save the value of `out` into `maybe_temp` before overwriting it
5747 // in the following move operation, as we will need it for the
5748 // read barrier below.
5749 __ Mov(temp_reg, out_reg);
5750 // /* HeapReference<Object> */ out = *(out + offset)
5751 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5752 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5753 }
5754 } else {
5755 // Plain load with no read barrier.
5756 // /* HeapReference<Object> */ out = *(out + offset)
5757 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5758 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5759 }
5760}
5761
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005762void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5763 HInstruction* instruction,
5764 Location out,
5765 Location obj,
5766 uint32_t offset,
5767 Location maybe_temp,
5768 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005769 Primitive::Type type = Primitive::kPrimNot;
5770 Register out_reg = RegisterFrom(out, type);
5771 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005772 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005773 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005774 if (kUseBakerReadBarrier) {
5775 // Load with fast path based Baker's read barrier.
5776 Register temp_reg = RegisterFrom(maybe_temp, type);
5777 // /* HeapReference<Object> */ out = *(obj + offset)
5778 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5779 out,
5780 obj_reg,
5781 offset,
5782 temp_reg,
5783 /* needs_null_check */ false,
5784 /* use_load_acquire */ false);
5785 } else {
5786 // Load with slow path based read barrier.
5787 // /* HeapReference<Object> */ out = *(obj + offset)
5788 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5789 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5790 }
5791 } else {
5792 // Plain load with no read barrier.
5793 // /* HeapReference<Object> */ out = *(obj + offset)
5794 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5795 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5796 }
5797}
5798
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005799void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5800 HInstruction* instruction,
5801 Location root,
5802 Register obj,
5803 uint32_t offset,
5804 vixl::aarch64::Label* fixup_label,
5805 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005806 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005807 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005808 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005809 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005810 if (kUseBakerReadBarrier) {
5811 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillain1372c9f2017-01-13 11:47:39 +00005812 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005813 //
Roland Levillain1372c9f2017-01-13 11:47:39 +00005814 // Note that we do not actually check the value of
5815 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5816 // root or not. Instead, we load into `temp` the read barrier
5817 // mark entry point corresponding to register `root`. If `temp`
5818 // is null, it means that `GetIsGcMarking()` is false, and vice
5819 // versa.
5820 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005821 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain1372c9f2017-01-13 11:47:39 +00005822 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5823 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5824 // // Slow path.
5825 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005826 // }
5827
Roland Levillain1372c9f2017-01-13 11:47:39 +00005828 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5829 Register temp = lr;
5830 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5831 instruction, root, /* entrypoint */ LocationFrom(temp));
5832 codegen_->AddSlowPath(slow_path);
5833
5834 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5835 const int32_t entry_point_offset =
5836 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5837 // Loading the entrypoint does not require a load acquire since it is only changed when
5838 // threads are suspended or running a checkpoint.
5839 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5840
Roland Levillain44015862016-01-22 11:47:17 +00005841 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842 if (fixup_label == nullptr) {
5843 __ Ldr(root_reg, MemOperand(obj, offset));
5844 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005845 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 }
Roland Levillain44015862016-01-22 11:47:17 +00005847 static_assert(
5848 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5849 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5850 "have different sizes.");
5851 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5852 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5853 "have different sizes.");
5854
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005855 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5856 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005857 __ Cbnz(temp, slow_path->GetEntryLabel());
5858 __ Bind(slow_path->GetExitLabel());
5859 } else {
5860 // GC root loaded through a slow path for read barriers other
5861 // than Baker's.
5862 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863 if (fixup_label == nullptr) {
5864 __ Add(root_reg.X(), obj.X(), offset);
5865 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005866 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867 }
Roland Levillain44015862016-01-22 11:47:17 +00005868 // /* mirror::Object* */ root = root->Read()
5869 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5870 }
5871 } else {
5872 // Plain GC root load with no read barrier.
5873 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 if (fixup_label == nullptr) {
5875 __ Ldr(root_reg, MemOperand(obj, offset));
5876 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005877 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005878 }
Roland Levillain44015862016-01-22 11:47:17 +00005879 // Note that GC roots are not affected by heap poisoning, thus we
5880 // do not have to unpoison `root_reg` here.
5881 }
5882}
5883
5884void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5885 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005886 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005887 uint32_t offset,
5888 Register temp,
5889 bool needs_null_check,
5890 bool use_load_acquire) {
5891 DCHECK(kEmitCompilerReadBarrier);
5892 DCHECK(kUseBakerReadBarrier);
5893
5894 // /* HeapReference<Object> */ ref = *(obj + offset)
5895 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005896 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005897 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5898 ref,
5899 obj,
5900 offset,
5901 no_index,
5902 no_scale_factor,
5903 temp,
5904 needs_null_check,
5905 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005906}
5907
5908void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5909 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005910 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005911 uint32_t data_offset,
5912 Location index,
5913 Register temp,
5914 bool needs_null_check) {
5915 DCHECK(kEmitCompilerReadBarrier);
5916 DCHECK(kUseBakerReadBarrier);
5917
5918 // Array cells are never volatile variables, therefore array loads
5919 // never use Load-Acquire instructions on ARM64.
5920 const bool use_load_acquire = false;
5921
Roland Levillainbfea3352016-06-23 13:48:47 +01005922 static_assert(
5923 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5924 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005925 // /* HeapReference<Object> */ ref =
5926 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005927 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5928 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5929 ref,
5930 obj,
5931 data_offset,
5932 index,
5933 scale_factor,
5934 temp,
5935 needs_null_check,
5936 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005937}
5938
5939void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5940 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005941 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005942 uint32_t offset,
5943 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005944 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005945 Register temp,
5946 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005947 bool use_load_acquire,
5948 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005949 DCHECK(kEmitCompilerReadBarrier);
5950 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005951 // If we are emitting an array load, we should not be using a
5952 // Load Acquire instruction. In other words:
5953 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5954 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005955
Roland Levillain27b1f9c2017-01-17 16:56:34 +00005956 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
5957 // whether we need to enter the slow path to mark the reference.
5958 // Then, in the slow path, check the gray bit in the lock word of
5959 // the reference's holder (`obj`) to decide whether to mark `ref` or
5960 // not.
Roland Levillain44015862016-01-22 11:47:17 +00005961 //
Roland Levillain1372c9f2017-01-13 11:47:39 +00005962 // Note that we do not actually check the value of `GetIsGcMarking()`;
5963 // instead, we load into `temp2` the read barrier mark entry point
5964 // corresponding to register `ref`. If `temp2` is null, it means
5965 // that `GetIsGcMarking()` is false, and vice versa.
5966 //
5967 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain1372c9f2017-01-13 11:47:39 +00005968 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5969 // // Slow path.
Roland Levillain27b1f9c2017-01-17 16:56:34 +00005970 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5971 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5972 // HeapReference<mirror::Object> ref = *src; // Original reference load.
5973 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5974 // if (is_gray) {
5975 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
5976 // }
5977 // } else {
5978 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00005979 // }
Roland Levillain44015862016-01-22 11:47:17 +00005980
Roland Levillain1372c9f2017-01-13 11:47:39 +00005981 // Slow path marking the object `ref` when the GC is marking. The
5982 // entrypoint will already be loaded in `temp2`.
5983 Register temp2 = lr;
5984 Location temp2_loc = LocationFrom(temp2);
5985 SlowPathCodeARM64* slow_path;
5986 if (always_update_field) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +00005987 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
5988 // only supports address of the form `obj + field_offset`, where
5989 // `obj` is a register and `field_offset` is a register. Thus
5990 // `offset` and `scale_factor` above are expected to be null in
5991 // this code path.
Roland Levillain1372c9f2017-01-13 11:47:39 +00005992 DCHECK_EQ(offset, 0u);
5993 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain27b1f9c2017-01-17 16:56:34 +00005994 Location field_offset = index;
5995 slow_path =
5996 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
5997 instruction,
5998 ref,
5999 obj,
6000 offset,
6001 /* index */ field_offset,
6002 scale_factor,
6003 needs_null_check,
6004 use_load_acquire,
6005 temp,
6006 /* entrypoint */ temp2_loc);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006007 } else {
Roland Levillain27b1f9c2017-01-17 16:56:34 +00006008 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6009 instruction,
6010 ref,
6011 obj,
6012 offset,
6013 index,
6014 scale_factor,
6015 needs_null_check,
6016 use_load_acquire,
6017 temp,
6018 /* entrypoint */ temp2_loc);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006019 }
6020 AddSlowPath(slow_path);
6021
6022 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6023 const int32_t entry_point_offset =
6024 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6025 // Loading the entrypoint does not require a load acquire since it is only changed when
6026 // threads are suspended or running a checkpoint.
6027 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillain1372c9f2017-01-13 11:47:39 +00006028 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6029 // checking GetIsGcMarking.
6030 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain27b1f9c2017-01-17 16:56:34 +00006031 // Fast path: just load the reference.
6032 GenerateRawReferenceLoad(
6033 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006034 __ Bind(slow_path->GetExitLabel());
6035}
6036
6037void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6038 Location ref,
6039 Register obj,
6040 uint32_t offset,
6041 Location index,
6042 size_t scale_factor,
6043 bool needs_null_check,
6044 bool use_load_acquire) {
6045 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006046 Primitive::Type type = Primitive::kPrimNot;
6047 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006048
Roland Levillain1372c9f2017-01-13 11:47:39 +00006049 // If needed, vixl::EmissionCheckScope guards are used to ensure
6050 // that no pools are emitted between the load (macro) instruction
6051 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006052
Roland Levillain44015862016-01-22 11:47:17 +00006053 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006054 // Load types involving an "index": ArrayGet,
6055 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6056 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006057 if (use_load_acquire) {
6058 // UnsafeGetObjectVolatile intrinsic case.
6059 // Register `index` is not an index in an object array, but an
6060 // offset to an object reference field within object `obj`.
6061 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6062 DCHECK(instruction->GetLocations()->Intrinsified());
6063 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6064 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006065 DCHECK_EQ(offset, 0u);
6066 DCHECK_EQ(scale_factor, 0u);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006067 DCHECK_EQ(needs_null_check, false);
6068 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006069 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6070 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006071 } else {
Roland Levillain1372c9f2017-01-13 11:47:39 +00006072 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6073 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006074 if (index.IsConstant()) {
6075 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006076 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006077 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillain1372c9f2017-01-13 11:47:39 +00006078 if (needs_null_check) {
6079 MaybeRecordImplicitNullCheck(instruction);
6080 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006081 } else {
Roland Levillain1372c9f2017-01-13 11:47:39 +00006082 UseScratchRegisterScope temps(GetVIXLAssembler());
6083 Register temp = temps.AcquireW();
6084 __ Add(temp, obj, offset);
6085 {
6086 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6087 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6088 if (needs_null_check) {
6089 MaybeRecordImplicitNullCheck(instruction);
6090 }
6091 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006092 }
Roland Levillain44015862016-01-22 11:47:17 +00006093 }
Roland Levillain44015862016-01-22 11:47:17 +00006094 } else {
Roland Levillain1372c9f2017-01-13 11:47:39 +00006095 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006096 MemOperand field = HeapOperand(obj, offset);
6097 if (use_load_acquire) {
Roland Levillain1372c9f2017-01-13 11:47:39 +00006098 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6099 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006100 } else {
Roland Levillain1372c9f2017-01-13 11:47:39 +00006101 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006102 Load(type, ref_reg, field);
Roland Levillain1372c9f2017-01-13 11:47:39 +00006103 if (needs_null_check) {
6104 MaybeRecordImplicitNullCheck(instruction);
6105 }
Roland Levillain44015862016-01-22 11:47:17 +00006106 }
6107 }
6108
6109 // Object* ref = ref_addr->AsMirrorPtr()
6110 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006111}
6112
6113void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6114 Location out,
6115 Location ref,
6116 Location obj,
6117 uint32_t offset,
6118 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006119 DCHECK(kEmitCompilerReadBarrier);
6120
Roland Levillain44015862016-01-22 11:47:17 +00006121 // Insert a slow path based read barrier *after* the reference load.
6122 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006123 // If heap poisoning is enabled, the unpoisoning of the loaded
6124 // reference will be carried out by the runtime within the slow
6125 // path.
6126 //
6127 // Note that `ref` currently does not get unpoisoned (when heap
6128 // poisoning is enabled), which is alright as the `ref` argument is
6129 // not used by the artReadBarrierSlow entry point.
6130 //
6131 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6132 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6133 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6134 AddSlowPath(slow_path);
6135
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006136 __ B(slow_path->GetEntryLabel());
6137 __ Bind(slow_path->GetExitLabel());
6138}
6139
Roland Levillain44015862016-01-22 11:47:17 +00006140void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6141 Location out,
6142 Location ref,
6143 Location obj,
6144 uint32_t offset,
6145 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006146 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006147 // Baker's read barriers shall be handled by the fast path
6148 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6149 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006150 // If heap poisoning is enabled, unpoisoning will be taken care of
6151 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006152 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006153 } else if (kPoisonHeapReferences) {
6154 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6155 }
6156}
6157
Roland Levillain44015862016-01-22 11:47:17 +00006158void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6159 Location out,
6160 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006161 DCHECK(kEmitCompilerReadBarrier);
6162
Roland Levillain44015862016-01-22 11:47:17 +00006163 // Insert a slow path based read barrier *after* the GC root load.
6164 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006165 // Note that GC roots are not affected by heap poisoning, so we do
6166 // not need to do anything special for this here.
6167 SlowPathCodeARM64* slow_path =
6168 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6169 AddSlowPath(slow_path);
6170
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006171 __ B(slow_path->GetEntryLabel());
6172 __ Bind(slow_path->GetExitLabel());
6173}
6174
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006175void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6176 LocationSummary* locations =
6177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6178 locations->SetInAt(0, Location::RequiresRegister());
6179 locations->SetOut(Location::RequiresRegister());
6180}
6181
6182void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6183 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006185 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006186 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006187 __ Ldr(XRegisterFrom(locations->Out()),
6188 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006189 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006190 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006191 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006192 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6193 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006194 __ Ldr(XRegisterFrom(locations->Out()),
6195 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006196 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006197}
6198
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006199static void PatchJitRootUse(uint8_t* code,
6200 const uint8_t* roots_data,
6201 vixl::aarch64::Literal<uint32_t>* literal,
6202 uint64_t index_in_table) {
6203 uint32_t literal_offset = literal->GetOffset();
6204 uintptr_t address =
6205 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6206 uint8_t* data = code + literal_offset;
6207 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6208}
6209
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006210void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6211 for (const auto& entry : jit_string_patches_) {
6212 const auto& it = jit_string_roots_.find(entry.first);
6213 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006214 PatchJitRootUse(code, roots_data, entry.second, it->second);
6215 }
6216 for (const auto& entry : jit_class_patches_) {
6217 const auto& it = jit_class_roots_.find(entry.first);
6218 DCHECK(it != jit_class_roots_.end());
6219 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006220 }
6221}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006222
Alexandre Rames67555f72014-11-18 10:55:16 +00006223#undef __
6224#undef QUICK_ENTRY_POINT
6225
Alexandre Rames5319def2014-10-23 10:03:10 +01006226} // namespace arm64
6227} // namespace art