blob: 794e05c6706d6ef85238378712dadd725d76f911 [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);
Artem Serov7957d952017-04-04 15:44:09 +0100156 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
157 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800158
159 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
160 UseScratchRegisterScope temps(masm);
161
162 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100163 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
164 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800165 int64_t reg_size = kXRegSizeInBytes;
166 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
167 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100168 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800169 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
170 // If the offset does not fit in the instruction's immediate field, use an alternate register
171 // to compute the base address(float point registers spill base address).
172 Register new_base = temps.AcquireSameSizeAs(base);
173 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
174 base = new_base;
175 spill_offset = -core_spill_size;
176 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
177 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
178 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
179 }
180
181 if (is_save) {
182 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
183 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
184 } else {
185 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
186 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
187 }
188}
189
190void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800191 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100192 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
193 for (uint32_t i : LowToHighBits(core_spills)) {
194 // If the register holds an object, update the stack mask.
195 if (locations->RegisterContainsObject(i)) {
196 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800197 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100198 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
199 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
200 saved_core_stack_offsets_[i] = stack_offset;
201 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800202 }
203
Vladimir Marko804b03f2016-09-14 16:26:36 +0100204 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
205 for (uint32_t i : LowToHighBits(fp_spills)) {
206 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
207 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
208 saved_fpu_stack_offsets_[i] = stack_offset;
209 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800210 }
211
Vladimir Marko804b03f2016-09-14 16:26:36 +0100212 SaveRestoreLiveRegistersHelper(codegen,
213 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800214 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
215}
216
217void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100218 SaveRestoreLiveRegistersHelper(codegen,
219 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800220 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
221}
222
Alexandre Rames5319def2014-10-23 10:03:10 +0100223class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
224 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000225 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100226
Alexandre Rames67555f72014-11-18 10:55:16 +0000227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100228 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000229 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100230
Alexandre Rames5319def2014-10-23 10:03:10 +0100231 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000232 if (instruction_->CanThrowIntoCatchBlock()) {
233 // Live registers will be restored in the catch block if caught.
234 SaveLiveRegisters(codegen, instruction_->GetLocations());
235 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000236 // We're moving two locations to locations that could overlap, so we need a parallel
237 // move resolver.
238 InvokeRuntimeCallingConvention calling_convention;
239 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100240 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
241 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000242 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
243 ? kQuickThrowStringBounds
244 : kQuickThrowArrayBounds;
245 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100246 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800247 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100248 }
249
Alexandre Rames8158f282015-08-07 10:26:17 +0100250 bool IsFatal() const OVERRIDE { return true; }
251
Alexandre Rames9931f312015-06-19 14:47:01 +0100252 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
253
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
256};
257
Alexandre Rames67555f72014-11-18 10:55:16 +0000258class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
259 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000260 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000261
262 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
263 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
264 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000265 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 }
268
Alexandre Rames8158f282015-08-07 10:26:17 +0100269 bool IsFatal() const OVERRIDE { return true; }
270
Alexandre Rames9931f312015-06-19 14:47:01 +0100271 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
275};
276
277class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
278 public:
279 LoadClassSlowPathARM64(HLoadClass* cls,
280 HInstruction* at,
281 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000282 bool do_clinit,
283 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
284 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
285 : SlowPathCodeARM64(at),
286 cls_(cls),
287 dex_pc_(dex_pc),
288 do_clinit_(do_clinit),
289 bss_entry_temp_(bss_entry_temp),
290 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
292 }
293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000296 Location out = locations->Out();
297 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
299
Vladimir Markoea4c1262017-02-06 19:59:33 +0000300 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the page address of
301 // the entry which is in a scratch register. Make sure it's not used for saving/restoring
302 // registers. Exclude the scratch register also for non-Baker read barrier for simplicity.
303 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
304 bool is_load_class_bss_entry =
305 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
306 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
307 if (is_load_class_bss_entry) {
308 // This temp is a scratch register.
309 DCHECK(bss_entry_temp_.IsValid());
310 temps.Exclude(bss_entry_temp_);
311 }
312
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000314 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000315
316 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000317 dex::TypeIndex type_index = cls_->GetTypeIndex();
318 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000319 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
320 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000321 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100323 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800324 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100325 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800326 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000327
328 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 if (out.IsValid()) {
330 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000331 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000332 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000334 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000335 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000336 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000337 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000338 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000339 if (call_saves_everything_except_r0_ip0) {
340 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
341 } else {
342 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
343 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
344 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
345 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000346 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000347 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000348 {
349 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
350 __ Bind(strp_label);
351 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000352 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000353 }
354 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 __ B(GetExitLabel());
356 }
357
Alexandre Rames9931f312015-06-19 14:47:01 +0100358 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
359
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 private:
361 // The class this slow path will load.
362 HLoadClass* const cls_;
363
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 // The dex PC of `at_`.
365 const uint32_t dex_pc_;
366
367 // Whether to initialize the class.
368 const bool do_clinit_;
369
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
371 vixl::aarch64::Register bss_entry_temp_;
372 vixl::aarch64::Label* bss_entry_adrp_label_;
373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
375};
376
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
378 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100379 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
380 : SlowPathCodeARM64(instruction),
381 temp_(temp),
382 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383
384 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
385 LocationSummary* locations = instruction_->GetLocations();
386 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
387 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
388
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100389 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
390 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
391 temps.Exclude(temp_);
392
Vladimir Markoaad75c62016-10-03 08:46:48 +0000393 __ Bind(GetEntryLabel());
394 SaveLiveRegisters(codegen, locations);
395
396 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000397 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
398 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000399 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
400 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
401 Primitive::Type type = instruction_->GetType();
402 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
403
404 RestoreLiveRegisters(codegen, locations);
405
406 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000407 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100408 if (!kUseReadBarrier || kUseBakerReadBarrier) {
409 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
410 } else {
411 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
412 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
413 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
414 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000415 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100416 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000417 {
418 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
419 __ Bind(strp_label);
420 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100421 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000422 }
423
424 __ B(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
428
429 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100430 const Register temp_;
431 vixl::aarch64::Label* adrp_label_;
432
Vladimir Markoaad75c62016-10-03 08:46:48 +0000433 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
434};
435
Alexandre Rames5319def2014-10-23 10:03:10 +0100436class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
437 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100439
Alexandre Rames67555f72014-11-18 10:55:16 +0000440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000443 if (instruction_->CanThrowIntoCatchBlock()) {
444 // Live registers will be restored in the catch block if caught.
445 SaveLiveRegisters(codegen, instruction_->GetLocations());
446 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000447 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
448 instruction_,
449 instruction_->GetDexPc(),
450 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800451 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 }
453
Alexandre Rames8158f282015-08-07 10:26:17 +0100454 bool IsFatal() const OVERRIDE { return true; }
455
Alexandre Rames9931f312015-06-19 14:47:01 +0100456 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
457
Alexandre Rames5319def2014-10-23 10:03:10 +0100458 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
460};
461
462class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
463 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100464 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000465 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100466
Alexandre Rames67555f72014-11-18 10:55:16 +0000467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100468 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000469 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100470 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100471 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000472 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800473 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100474 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000475 if (successor_ == nullptr) {
476 __ B(GetReturnLabel());
477 } else {
478 __ B(arm64_codegen->GetLabelOf(successor_));
479 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100480 }
481
Scott Wakeling97c72b72016-06-24 16:19:36 +0100482 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DCHECK(successor_ == nullptr);
484 return &return_label_;
485 }
486
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100487 HBasicBlock* GetSuccessor() const {
488 return successor_;
489 }
490
Alexandre Rames9931f312015-06-19 14:47:01 +0100491 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
492
Alexandre Rames5319def2014-10-23 10:03:10 +0100493 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 // If not null, the block to branch to after the suspend check.
495 HBasicBlock* const successor_;
496
497 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100498 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100499
500 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
501};
502
Alexandre Rames67555f72014-11-18 10:55:16 +0000503class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
504 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000506 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000507
508 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000509 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800510
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 DCHECK(instruction_->IsCheckCast()
512 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
513 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100514 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000515
Alexandre Rames67555f72014-11-18 10:55:16 +0000516 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000517
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000518 if (!is_fatal_) {
519 SaveLiveRegisters(codegen, locations);
520 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000521
522 // We're moving two locations to locations that could overlap, so we need a parallel
523 // move resolver.
524 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800525 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800526 LocationFrom(calling_convention.GetRegisterAt(0)),
527 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800528 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800529 LocationFrom(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000531 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000532 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800533 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000534 Primitive::Type ret_type = instruction_->GetType();
535 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
536 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
537 } else {
538 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800539 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
540 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541 }
542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543 if (!is_fatal_) {
544 RestoreLiveRegisters(codegen, locations);
545 __ B(GetExitLabel());
546 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000547 }
548
Alexandre Rames9931f312015-06-19 14:47:01 +0100549 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100550 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100551
Alexandre Rames67555f72014-11-18 10:55:16 +0000552 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000553 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000554
Alexandre Rames67555f72014-11-18 10:55:16 +0000555 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
556};
557
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
559 public:
Aart Bik42249c32016-01-07 15:33:50 -0800560 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000561 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700562
563 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800564 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700565 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000566 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000567 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700568 }
569
Alexandre Rames9931f312015-06-19 14:47:01 +0100570 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
571
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700572 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700573 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
574};
575
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
577 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000578 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100579
580 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
581 LocationSummary* locations = instruction_->GetLocations();
582 __ Bind(GetEntryLabel());
583 SaveLiveRegisters(codegen, locations);
584
585 InvokeRuntimeCallingConvention calling_convention;
586 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
587 parallel_move.AddMove(
588 locations->InAt(0),
589 LocationFrom(calling_convention.GetRegisterAt(0)),
590 Primitive::kPrimNot,
591 nullptr);
592 parallel_move.AddMove(
593 locations->InAt(1),
594 LocationFrom(calling_convention.GetRegisterAt(1)),
595 Primitive::kPrimInt,
596 nullptr);
597 parallel_move.AddMove(
598 locations->InAt(2),
599 LocationFrom(calling_convention.GetRegisterAt(2)),
600 Primitive::kPrimNot,
601 nullptr);
602 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
603
604 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000605 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100606 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
607 RestoreLiveRegisters(codegen, locations);
608 __ B(GetExitLabel());
609 }
610
611 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
612
613 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100614 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
615};
616
Zheng Xu3927c8b2015-11-18 17:46:25 +0800617void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
618 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000619 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800620
621 // We are about to use the assembler to place literals directly. Make sure we have enough
622 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000623 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
624 num_entries * sizeof(int32_t),
625 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800626
627 __ Bind(&table_start_);
628 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
629 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100630 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800631 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100632 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800633 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
634 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
635 Literal<int32_t> literal(jump_offset);
636 __ place(&literal);
637 }
638}
639
Roland Levillain54f869e2017-03-06 13:54:11 +0000640// Abstract base class for read barrier slow paths marking a reference
641// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000642//
Roland Levillain54f869e2017-03-06 13:54:11 +0000643// Argument `entrypoint` must be a register location holding the read
644// barrier marking runtime entry point to be invoked.
645class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
646 protected:
647 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
648 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000649 DCHECK(kEmitCompilerReadBarrier);
650 }
651
Roland Levillain54f869e2017-03-06 13:54:11 +0000652 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000653
Roland Levillain54f869e2017-03-06 13:54:11 +0000654 // Generate assembly code calling the read barrier marking runtime
655 // entry point (ReadBarrierMarkRegX).
656 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000657 // No need to save live registers; it's taken care of by the
658 // entrypoint. Also, there is no need to update the stack mask,
659 // as this runtime call will not trigger a garbage collection.
660 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
661 DCHECK_NE(ref_.reg(), LR);
662 DCHECK_NE(ref_.reg(), WSP);
663 DCHECK_NE(ref_.reg(), WZR);
664 // IP0 is used internally by the ReadBarrierMarkRegX entry point
665 // as a temporary, it cannot be the entry point's input/output.
666 DCHECK_NE(ref_.reg(), IP0);
667 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
668 // "Compact" slow path, saving two moves.
669 //
670 // Instead of using the standard runtime calling convention (input
671 // and output in W0):
672 //
673 // W0 <- ref
674 // W0 <- ReadBarrierMark(W0)
675 // ref <- W0
676 //
677 // we just use rX (the register containing `ref`) as input and output
678 // of a dedicated entrypoint:
679 //
680 // rX <- ReadBarrierMarkRegX(rX)
681 //
682 if (entrypoint_.IsValid()) {
683 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
684 __ Blr(XRegisterFrom(entrypoint_));
685 } else {
686 // Entrypoint is not already loaded, load from the thread.
687 int32_t entry_point_offset =
688 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
689 // This runtime call does not require a stack map.
690 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
691 }
692 }
693
694 // The location (register) of the marked object reference.
695 const Location ref_;
696
697 // The location of the entrypoint if it is already loaded.
698 const Location entrypoint_;
699
Roland Levillain54f869e2017-03-06 13:54:11 +0000700 private:
701 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
702};
703
Alexandre Rames5319def2014-10-23 10:03:10 +0100704// Slow path marking an object reference `ref` during a read
705// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000706// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100707//
708// This means that after the execution of this slow path, `ref` will
709// always be up-to-date, but `obj.field` may not; i.e., after the
710// flip, `ref` will be a to-space reference, but `obj.field` will
711// probably still be a from-space reference (unless it gets updated by
712// another thread, or if another thread installed another object
713// reference (different from `ref`) in `obj.field`).
714//
715// If `entrypoint` is a valid location it is assumed to already be
716// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000717// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000718class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100719 public:
720 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
721 Location ref,
722 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000723 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100724 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100725 }
726
727 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
728
729 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000730 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100731 DCHECK(locations->CanCall());
732 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000733 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000734 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
735 << "Unexpected instruction in read barrier marking slow path: "
736 << instruction_->DebugName();
737
738 __ Bind(GetEntryLabel());
739 GenerateReadBarrierMarkRuntimeCall(codegen);
740 __ B(GetExitLabel());
741 }
742
743 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000744 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
745};
746
Roland Levillain54f869e2017-03-06 13:54:11 +0000747// Slow path loading `obj`'s lock word, loading a reference from
748// object `*(obj + offset + (index << scale_factor))` into `ref`, and
749// marking `ref` if `obj` is gray according to the lock word (Baker
750// read barrier). The field `obj.field` in the object `obj` holding
751// this reference does not get updated by this slow path after marking
752// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
753// below for that).
754//
755// This means that after the execution of this slow path, `ref` will
756// always be up-to-date, but `obj.field` may not; i.e., after the
757// flip, `ref` will be a to-space reference, but `obj.field` will
758// probably still be a from-space reference (unless it gets updated by
759// another thread, or if another thread installed another object
760// reference (different from `ref`) in `obj.field`).
761//
762// Argument `entrypoint` must be a register location holding the read
763// barrier marking runtime entry point to be invoked.
764class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
765 public:
766 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
767 Location ref,
768 Register obj,
769 uint32_t offset,
770 Location index,
771 size_t scale_factor,
772 bool needs_null_check,
773 bool use_load_acquire,
774 Register temp,
775 Location entrypoint)
776 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
777 obj_(obj),
778 offset_(offset),
779 index_(index),
780 scale_factor_(scale_factor),
781 needs_null_check_(needs_null_check),
782 use_load_acquire_(use_load_acquire),
783 temp_(temp) {
784 DCHECK(kEmitCompilerReadBarrier);
785 DCHECK(kUseBakerReadBarrier);
786 }
787
788 const char* GetDescription() const OVERRIDE {
789 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
790 }
791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 LocationSummary* locations = instruction_->GetLocations();
794 DCHECK(locations->CanCall());
795 DCHECK(ref_.IsRegister()) << ref_;
796 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
797 DCHECK(obj_.IsW());
798 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100799 DCHECK(instruction_->IsInstanceFieldGet() ||
800 instruction_->IsStaticFieldGet() ||
801 instruction_->IsArrayGet() ||
802 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100803 instruction_->IsInstanceOf() ||
804 instruction_->IsCheckCast() ||
805 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
806 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
807 << "Unexpected instruction in read barrier marking slow path: "
808 << instruction_->DebugName();
809 // The read barrier instrumentation of object ArrayGet
810 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000811 // instruction.
812 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000813 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
814
Roland Levillain54f869e2017-03-06 13:54:11 +0000815 // Temporary register `temp_`, used to store the lock word, must
816 // not be IP0 nor IP1, as we may use them to emit the reference
817 // load (in the call to GenerateRawReferenceLoad below), and we
818 // need the lock word to still be in `temp_` after the reference
819 // load.
820 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
821 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
822
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000824
825 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
826 // inserted after the original load. However, in fast path based
827 // Baker's read barriers, we need to perform the load of
828 // mirror::Object::monitor_ *before* the original reference load.
829 // This load-load ordering is required by the read barrier.
830 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100831 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000832 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
833 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
834 // HeapReference<mirror::Object> ref = *src; // Original reference load.
835 // bool is_gray = (rb_state == ReadBarrier::GrayState());
836 // if (is_gray) {
837 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
838 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000839 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000840 // Note: the original implementation in ReadBarrier::Barrier is
841 // slightly more complex as it performs additional checks that we do
842 // not do here for performance reasons.
843
844 // /* int32_t */ monitor = obj->monitor_
845 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
846 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
847 if (needs_null_check_) {
848 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100849 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000850 // /* LockWord */ lock_word = LockWord(monitor)
851 static_assert(sizeof(LockWord) == sizeof(int32_t),
852 "art::LockWord and int32_t have different sizes.");
853
854 // Introduce a dependency on the lock_word including rb_state,
855 // to prevent load-load reordering, and without using
856 // a memory barrier (which would be more expensive).
857 // `obj` is unchanged by this operation, but its value now depends
858 // on `temp`.
859 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
860
861 // The actual reference load.
862 // A possible implicit null check has already been handled above.
863 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
864 arm64_codegen->GenerateRawReferenceLoad(instruction_,
865 ref_,
866 obj_,
867 offset_,
868 index_,
869 scale_factor_,
870 /* needs_null_check */ false,
871 use_load_acquire_);
872
873 // Mark the object `ref` when `obj` is gray.
874 //
875 // if (rb_state == ReadBarrier::GrayState())
876 // ref = ReadBarrier::Mark(ref);
877 //
878 // Given the numeric representation, it's enough to check the low bit of the rb_state.
879 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
880 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
881 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
882 GenerateReadBarrierMarkRuntimeCall(codegen);
883
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000884 __ B(GetExitLabel());
885 }
886
887 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000888 // The register containing the object holding the marked object reference field.
889 Register obj_;
890 // The offset, index and scale factor to access the reference in `obj_`.
891 uint32_t offset_;
892 Location index_;
893 size_t scale_factor_;
894 // Is a null check required?
895 bool needs_null_check_;
896 // Should this reference load use Load-Acquire semantics?
897 bool use_load_acquire_;
898 // A temporary register used to hold the lock word of `obj_`.
899 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000900
Roland Levillain54f869e2017-03-06 13:54:11 +0000901 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000902};
903
Roland Levillain54f869e2017-03-06 13:54:11 +0000904// Slow path loading `obj`'s lock word, loading a reference from
905// object `*(obj + offset + (index << scale_factor))` into `ref`, and
906// marking `ref` if `obj` is gray according to the lock word (Baker
907// read barrier). If needed, this slow path also atomically updates
908// the field `obj.field` in the object `obj` holding this reference
909// after marking (contrary to
910// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
911// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100912//
913// This means that after the execution of this slow path, both `ref`
914// and `obj.field` will be up-to-date; i.e., after the flip, both will
915// hold the same to-space reference (unless another thread installed
916// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000917//
Roland Levillain54f869e2017-03-06 13:54:11 +0000918// Argument `entrypoint` must be a register location holding the read
919// barrier marking runtime entry point to be invoked.
920class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
921 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100922 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000923 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
924 Location ref,
925 Register obj,
926 uint32_t offset,
927 Location index,
928 size_t scale_factor,
929 bool needs_null_check,
930 bool use_load_acquire,
931 Register temp,
932 Location entrypoint)
933 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100934 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000935 offset_(offset),
936 index_(index),
937 scale_factor_(scale_factor),
938 needs_null_check_(needs_null_check),
939 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000940 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100941 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000942 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100943 }
944
945 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100947 }
948
949 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
950 LocationSummary* locations = instruction_->GetLocations();
951 Register ref_reg = WRegisterFrom(ref_);
952 DCHECK(locations->CanCall());
953 DCHECK(ref_.IsRegister()) << ref_;
954 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000955 DCHECK(obj_.IsW());
956 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
957
958 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100959 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
960 << "Unexpected instruction in read barrier marking and field updating slow path: "
961 << instruction_->DebugName();
962 DCHECK(instruction_->GetLocations()->Intrinsified());
963 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 DCHECK_EQ(offset_, 0u);
965 DCHECK_EQ(scale_factor_, 0u);
966 DCHECK_EQ(use_load_acquire_, false);
967 // The location of the offset of the marked reference field within `obj_`.
968 Location field_offset = index_;
969 DCHECK(field_offset.IsRegister()) << field_offset;
970
971 // Temporary register `temp_`, used to store the lock word, must
972 // not be IP0 nor IP1, as we may use them to emit the reference
973 // load (in the call to GenerateRawReferenceLoad below), and we
974 // need the lock word to still be in `temp_` after the reference
975 // load.
976 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
977 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978
979 __ Bind(GetEntryLabel());
980
Roland Levillain54f869e2017-03-06 13:54:11 +0000981 // /* int32_t */ monitor = obj->monitor_
982 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
983 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
984 if (needs_null_check_) {
985 codegen->MaybeRecordImplicitNullCheck(instruction_);
986 }
987 // /* LockWord */ lock_word = LockWord(monitor)
988 static_assert(sizeof(LockWord) == sizeof(int32_t),
989 "art::LockWord and int32_t have different sizes.");
990
991 // Introduce a dependency on the lock_word including rb_state,
992 // to prevent load-load reordering, and without using
993 // a memory barrier (which would be more expensive).
994 // `obj` is unchanged by this operation, but its value now depends
995 // on `temp`.
996 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
997
998 // The actual reference load.
999 // A possible implicit null check has already been handled above.
1000 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1001 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1002 ref_,
1003 obj_,
1004 offset_,
1005 index_,
1006 scale_factor_,
1007 /* needs_null_check */ false,
1008 use_load_acquire_);
1009
1010 // Mark the object `ref` when `obj` is gray.
1011 //
1012 // if (rb_state == ReadBarrier::GrayState())
1013 // ref = ReadBarrier::Mark(ref);
1014 //
1015 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1016 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1017 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1018 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1019
1020 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001021 // Note that we cannot use IP to save the old reference, as IP is
1022 // used internally by the ReadBarrierMarkRegX entry point, and we
1023 // need the old reference after the call to that entry point.
1024 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1025 __ Mov(temp_.W(), ref_reg);
1026
Roland Levillain54f869e2017-03-06 13:54:11 +00001027 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001028
1029 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001030 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001031 //
1032 // Note that this field could also hold a different object, if
1033 // another thread had concurrently changed it. In that case, the
1034 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1035 // (CAS) operation below would abort the CAS, leaving the field
1036 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001037 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001038 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001039
1040 // Update the the holder's field atomically. This may fail if
1041 // mutator updates before us, but it's OK. This is achieved
1042 // using a strong compare-and-set (CAS) operation with relaxed
1043 // memory synchronization ordering, where the expected value is
1044 // the old reference and the desired value is the new reference.
1045
1046 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1047 UseScratchRegisterScope temps(masm);
1048
1049 // Convenience aliases.
1050 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001051 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001052 Register expected = temp_.W();
1053 Register value = ref_reg;
1054 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1055 Register tmp_value = temps.AcquireW(); // Value in memory.
1056
1057 __ Add(tmp_ptr, base.X(), Operand(offset));
1058
1059 if (kPoisonHeapReferences) {
1060 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1061 if (value.Is(expected)) {
1062 // Do not poison `value`, as it is the same register as
1063 // `expected`, which has just been poisoned.
1064 } else {
1065 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1066 }
1067 }
1068
1069 // do {
1070 // tmp_value = [tmp_ptr] - expected;
1071 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1072
Roland Levillain24a4d112016-10-26 13:10:46 +01001073 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 __ Bind(&loop_head);
1075 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1076 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001077 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1079 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001080 __ B(&exit_loop);
1081 __ Bind(&comparison_failed);
1082 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001083 __ Bind(&exit_loop);
1084
1085 if (kPoisonHeapReferences) {
1086 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1087 if (value.Is(expected)) {
1088 // Do not unpoison `value`, as it is the same register as
1089 // `expected`, which has just been unpoisoned.
1090 } else {
1091 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1092 }
1093 }
1094
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095 __ B(GetExitLabel());
1096 }
1097
1098 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001099 // The register containing the object holding the marked object reference field.
1100 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001101 // The offset, index and scale factor to access the reference in `obj_`.
1102 uint32_t offset_;
1103 Location index_;
1104 size_t scale_factor_;
1105 // Is a null check required?
1106 bool needs_null_check_;
1107 // Should this reference load use Load-Acquire semantics?
1108 bool use_load_acquire_;
1109 // A temporary register used to hold the lock word of `obj_`; and
1110 // also to hold the original reference value, when the reference is
1111 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001112 const Register temp_;
1113
Roland Levillain54f869e2017-03-06 13:54:11 +00001114 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001115};
1116
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001117// Slow path generating a read barrier for a heap reference.
1118class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1119 public:
1120 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1121 Location out,
1122 Location ref,
1123 Location obj,
1124 uint32_t offset,
1125 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001126 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001127 out_(out),
1128 ref_(ref),
1129 obj_(obj),
1130 offset_(offset),
1131 index_(index) {
1132 DCHECK(kEmitCompilerReadBarrier);
1133 // If `obj` is equal to `out` or `ref`, it means the initial object
1134 // has been overwritten by (or after) the heap object reference load
1135 // to be instrumented, e.g.:
1136 //
1137 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001138 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001139 //
1140 // In that case, we have lost the information about the original
1141 // object, and the emitted read barrier cannot work properly.
1142 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1143 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1144 }
1145
1146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1147 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1148 LocationSummary* locations = instruction_->GetLocations();
1149 Primitive::Type type = Primitive::kPrimNot;
1150 DCHECK(locations->CanCall());
1151 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001152 DCHECK(instruction_->IsInstanceFieldGet() ||
1153 instruction_->IsStaticFieldGet() ||
1154 instruction_->IsArrayGet() ||
1155 instruction_->IsInstanceOf() ||
1156 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001157 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001158 << "Unexpected instruction in read barrier for heap reference slow path: "
1159 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001160 // The read barrier instrumentation of object ArrayGet
1161 // instructions does not support the HIntermediateAddress
1162 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001163 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001164 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001165
1166 __ Bind(GetEntryLabel());
1167
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 SaveLiveRegisters(codegen, locations);
1169
1170 // We may have to change the index's value, but as `index_` is a
1171 // constant member (like other "inputs" of this slow path),
1172 // introduce a copy of it, `index`.
1173 Location index = index_;
1174 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001175 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001176 if (instruction_->IsArrayGet()) {
1177 // Compute the actual memory offset and store it in `index`.
1178 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1179 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1180 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1181 // We are about to change the value of `index_reg` (see the
1182 // calls to vixl::MacroAssembler::Lsl and
1183 // vixl::MacroAssembler::Mov below), but it has
1184 // not been saved by the previous call to
1185 // art::SlowPathCode::SaveLiveRegisters, as it is a
1186 // callee-save register --
1187 // art::SlowPathCode::SaveLiveRegisters does not consider
1188 // callee-save registers, as it has been designed with the
1189 // assumption that callee-save registers are supposed to be
1190 // handled by the called function. So, as a callee-save
1191 // register, `index_reg` _would_ eventually be saved onto
1192 // the stack, but it would be too late: we would have
1193 // changed its value earlier. Therefore, we manually save
1194 // it here into another freely available register,
1195 // `free_reg`, chosen of course among the caller-save
1196 // registers (as a callee-save `free_reg` register would
1197 // exhibit the same problem).
1198 //
1199 // Note we could have requested a temporary register from
1200 // the register allocator instead; but we prefer not to, as
1201 // this is a slow path, and we know we can find a
1202 // caller-save register that is available.
1203 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1204 __ Mov(free_reg.W(), index_reg);
1205 index_reg = free_reg;
1206 index = LocationFrom(index_reg);
1207 } else {
1208 // The initial register stored in `index_` has already been
1209 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1210 // (as it is not a callee-save register), so we can freely
1211 // use it.
1212 }
1213 // Shifting the index value contained in `index_reg` by the scale
1214 // factor (2) cannot overflow in practice, as the runtime is
1215 // unable to allocate object arrays with a size larger than
1216 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1217 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1218 static_assert(
1219 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1220 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1221 __ Add(index_reg, index_reg, Operand(offset_));
1222 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001223 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1224 // intrinsics, `index_` is not shifted by a scale factor of 2
1225 // (as in the case of ArrayGet), as it is actually an offset
1226 // to an object field within an object.
1227 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001228 DCHECK(instruction_->GetLocations()->Intrinsified());
1229 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1230 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1231 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001232 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001233 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001234 }
1235 }
1236
1237 // We're moving two or three locations to locations that could
1238 // overlap, so we need a parallel move resolver.
1239 InvokeRuntimeCallingConvention calling_convention;
1240 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1241 parallel_move.AddMove(ref_,
1242 LocationFrom(calling_convention.GetRegisterAt(0)),
1243 type,
1244 nullptr);
1245 parallel_move.AddMove(obj_,
1246 LocationFrom(calling_convention.GetRegisterAt(1)),
1247 type,
1248 nullptr);
1249 if (index.IsValid()) {
1250 parallel_move.AddMove(index,
1251 LocationFrom(calling_convention.GetRegisterAt(2)),
1252 Primitive::kPrimInt,
1253 nullptr);
1254 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1255 } else {
1256 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1257 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1258 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001259 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001260 instruction_,
1261 instruction_->GetDexPc(),
1262 this);
1263 CheckEntrypointTypes<
1264 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1265 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1266
1267 RestoreLiveRegisters(codegen, locations);
1268
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001269 __ B(GetExitLabel());
1270 }
1271
1272 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1273
1274 private:
1275 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001276 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1277 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001278 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1279 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1280 return Register(VIXLRegCodeFromART(i), kXRegSize);
1281 }
1282 }
1283 // We shall never fail to find a free caller-save register, as
1284 // there are more than two core caller-save registers on ARM64
1285 // (meaning it is possible to find one which is different from
1286 // `ref` and `obj`).
1287 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1288 LOG(FATAL) << "Could not find a free register";
1289 UNREACHABLE();
1290 }
1291
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001292 const Location out_;
1293 const Location ref_;
1294 const Location obj_;
1295 const uint32_t offset_;
1296 // An additional location containing an index to an array.
1297 // Only used for HArrayGet and the UnsafeGetObject &
1298 // UnsafeGetObjectVolatile intrinsics.
1299 const Location index_;
1300
1301 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1302};
1303
1304// Slow path generating a read barrier for a GC root.
1305class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1306 public:
1307 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001308 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001309 DCHECK(kEmitCompilerReadBarrier);
1310 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001311
1312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1313 LocationSummary* locations = instruction_->GetLocations();
1314 Primitive::Type type = Primitive::kPrimNot;
1315 DCHECK(locations->CanCall());
1316 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001317 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1318 << "Unexpected instruction in read barrier for GC root slow path: "
1319 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001320
1321 __ Bind(GetEntryLabel());
1322 SaveLiveRegisters(codegen, locations);
1323
1324 InvokeRuntimeCallingConvention calling_convention;
1325 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1326 // The argument of the ReadBarrierForRootSlow is not a managed
1327 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1328 // thus we need a 64-bit move here, and we cannot use
1329 //
1330 // arm64_codegen->MoveLocation(
1331 // LocationFrom(calling_convention.GetRegisterAt(0)),
1332 // root_,
1333 // type);
1334 //
1335 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1336 // reference type (`Primitive::kPrimNot`).
1337 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001338 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001339 instruction_,
1340 instruction_->GetDexPc(),
1341 this);
1342 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1343 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1344
1345 RestoreLiveRegisters(codegen, locations);
1346 __ B(GetExitLabel());
1347 }
1348
1349 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1350
1351 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001352 const Location out_;
1353 const Location root_;
1354
1355 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1356};
1357
Alexandre Rames5319def2014-10-23 10:03:10 +01001358#undef __
1359
1360Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1361 Location next_location;
1362 if (type == Primitive::kPrimVoid) {
1363 LOG(FATAL) << "Unreachable type " << type;
1364 }
1365
1366 if (Primitive::IsFloatingPointType(type) &&
1367 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001368 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1369 } else if (!Primitive::IsFloatingPointType(type) &&
1370 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1371 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1372 } else {
1373 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001374 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1375 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 }
1377
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001378 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001379 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 return next_location;
1381}
1382
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001383Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001384 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001385}
1386
Serban Constantinescu579885a2015-02-22 20:51:33 +00001387CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1388 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001389 const CompilerOptions& compiler_options,
1390 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001391 : CodeGenerator(graph,
1392 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001393 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001394 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001395 callee_saved_core_registers.GetList(),
1396 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001397 compiler_options,
1398 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001399 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001400 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001401 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001402 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001403 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001404 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001405 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001406 uint32_literals_(std::less<uint32_t>(),
1407 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001408 uint64_literals_(std::less<uint64_t>(),
1409 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001410 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1411 boot_image_string_patches_(StringReferenceValueComparator(),
1412 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1413 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001414 boot_image_type_patches_(TypeReferenceValueComparator(),
1415 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1416 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001417 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001418 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001419 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1420 jit_class_patches_(TypeReferenceValueComparator(),
1421 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001422 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001423 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001424}
Alexandre Rames5319def2014-10-23 10:03:10 +01001425
Alexandre Rames67555f72014-11-18 10:55:16 +00001426#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001427
Zheng Xu3927c8b2015-11-18 17:46:25 +08001428void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001429 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001430 jump_table->EmitTable(this);
1431 }
1432}
1433
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001434void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001435 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001436 // Ensure we emit the literal pool.
1437 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001438
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001439 CodeGenerator::Finalize(allocator);
1440}
1441
Zheng Xuad4450e2015-04-17 18:48:56 +08001442void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1443 // Note: There are 6 kinds of moves:
1444 // 1. constant -> GPR/FPR (non-cycle)
1445 // 2. constant -> stack (non-cycle)
1446 // 3. GPR/FPR -> GPR/FPR
1447 // 4. GPR/FPR -> stack
1448 // 5. stack -> GPR/FPR
1449 // 6. stack -> stack (non-cycle)
1450 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1451 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1452 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1453 // dependency.
1454 vixl_temps_.Open(GetVIXLAssembler());
1455}
1456
1457void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1458 vixl_temps_.Close();
1459}
1460
1461Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1462 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1463 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1464 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1465 Location scratch = GetScratchLocation(kind);
1466 if (!scratch.Equals(Location::NoLocation())) {
1467 return scratch;
1468 }
1469 // Allocate from VIXL temp registers.
1470 if (kind == Location::kRegister) {
1471 scratch = LocationFrom(vixl_temps_.AcquireX());
1472 } else {
1473 DCHECK(kind == Location::kFpuRegister);
1474 scratch = LocationFrom(vixl_temps_.AcquireD());
1475 }
1476 AddScratchLocation(scratch);
1477 return scratch;
1478}
1479
1480void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1481 if (loc.IsRegister()) {
1482 vixl_temps_.Release(XRegisterFrom(loc));
1483 } else {
1484 DCHECK(loc.IsFpuRegister());
1485 vixl_temps_.Release(DRegisterFrom(loc));
1486 }
1487 RemoveScratchLocation(loc);
1488}
1489
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001491 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001492 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493}
1494
Alexandre Rames5319def2014-10-23 10:03:10 +01001495void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001496 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001497 __ Bind(&frame_entry_label_);
1498
Serban Constantinescu02164b32014-11-13 14:05:07 +00001499 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1500 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001501 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001502 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001503 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001504 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001505 {
1506 // Ensure that between load and RecordPcInfo there are no pools emitted.
1507 ExactAssemblyScope eas(GetVIXLAssembler(),
1508 kInstructionSize,
1509 CodeBufferCheckScope::kExactSize);
1510 __ ldr(wzr, MemOperand(temp, 0));
1511 RecordPcInfo(nullptr, 0);
1512 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001513 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001514
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001515 if (!HasEmptyFrame()) {
1516 int frame_size = GetFrameSize();
1517 // Stack layout:
1518 // sp[frame_size - 8] : lr.
1519 // ... : other preserved core registers.
1520 // ... : other preserved fp registers.
1521 // ... : reserved frame space.
1522 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001523
1524 // Save the current method if we need it. Note that we do not
1525 // do this in HCurrentMethod, as the instruction might have been removed
1526 // in the SSA graph.
1527 if (RequiresCurrentMethod()) {
1528 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001529 } else {
1530 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001531 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001532 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001533 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1534 frame_size - GetCoreSpillSize());
1535 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1536 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001537
1538 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1539 // Initialize should_deoptimize flag to 0.
1540 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1541 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1542 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001543 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001544}
1545
1546void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001547 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001548 if (!HasEmptyFrame()) {
1549 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001550 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1551 frame_size - FrameEntrySpillSize());
1552 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1553 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001554 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001555 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001556 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001557 __ Ret();
1558 GetAssembler()->cfi().RestoreState();
1559 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001560}
1561
Scott Wakeling97c72b72016-06-24 16:19:36 +01001562CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001563 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001564 return CPURegList(CPURegister::kRegister, kXRegSize,
1565 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001566}
1567
Scott Wakeling97c72b72016-06-24 16:19:36 +01001568CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001569 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1570 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001571 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1572 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001573}
1574
Alexandre Rames5319def2014-10-23 10:03:10 +01001575void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1576 __ Bind(GetLabelOf(block));
1577}
1578
Calin Juravle175dc732015-08-25 15:42:32 +01001579void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1580 DCHECK(location.IsRegister());
1581 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1582}
1583
Calin Juravlee460d1d2015-09-29 04:52:17 +01001584void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1585 if (location.IsRegister()) {
1586 locations->AddTemp(location);
1587 } else {
1588 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1589 }
1590}
1591
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001592void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001595 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001596 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001597 if (value_can_be_null) {
1598 __ Cbz(value, &done);
1599 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001600 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001602 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001603 if (value_can_be_null) {
1604 __ Bind(&done);
1605 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001606}
1607
David Brazdil58282f42016-01-14 12:45:10 +00001608void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001609 // Blocked core registers:
1610 // lr : Runtime reserved.
1611 // tr : Runtime reserved.
1612 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1613 // ip1 : VIXL core temp.
1614 // ip0 : VIXL core temp.
1615 //
1616 // Blocked fp registers:
1617 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001618 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1619 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001621 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001622 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001623
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001625 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001626 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001627 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001628
David Brazdil58282f42016-01-14 12:45:10 +00001629 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001630 // Stubs do not save callee-save floating point registers. If the graph
1631 // is debuggable, we need to deal with these registers differently. For
1632 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001633 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1634 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001635 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001636 }
1637 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001638}
1639
Alexandre Rames3e69f162014-12-10 10:36:50 +00001640size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1641 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1642 __ Str(reg, MemOperand(sp, stack_index));
1643 return kArm64WordSize;
1644}
1645
1646size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1647 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1648 __ Ldr(reg, MemOperand(sp, stack_index));
1649 return kArm64WordSize;
1650}
1651
1652size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1653 FPRegister reg = FPRegister(reg_id, kDRegSize);
1654 __ Str(reg, MemOperand(sp, stack_index));
1655 return kArm64WordSize;
1656}
1657
1658size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1659 FPRegister reg = FPRegister(reg_id, kDRegSize);
1660 __ Ldr(reg, MemOperand(sp, stack_index));
1661 return kArm64WordSize;
1662}
1663
Alexandre Rames5319def2014-10-23 10:03:10 +01001664void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001665 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001666}
1667
1668void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001669 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001670}
1671
Alexandre Rames67555f72014-11-18 10:55:16 +00001672void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001673 if (constant->IsIntConstant()) {
1674 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1675 } else if (constant->IsLongConstant()) {
1676 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1677 } else if (constant->IsNullConstant()) {
1678 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001679 } else if (constant->IsFloatConstant()) {
1680 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1681 } else {
1682 DCHECK(constant->IsDoubleConstant());
1683 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1684 }
1685}
1686
Alexandre Rames3e69f162014-12-10 10:36:50 +00001687
1688static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1689 DCHECK(constant.IsConstant());
1690 HConstant* cst = constant.GetConstant();
1691 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001692 // Null is mapped to a core W register, which we associate with kPrimInt.
1693 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001694 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1695 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1696 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1697}
1698
Roland Levillain558dea12017-01-27 19:40:44 +00001699// Allocate a scratch register from the VIXL pool, querying first into
1700// the floating-point register pool, and then the the core register
1701// pool. This is essentially a reimplementation of
1702// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1703// using a different allocation strategy.
1704static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1705 vixl::aarch64::UseScratchRegisterScope* temps,
1706 int size_in_bits) {
1707 return masm->GetScratchFPRegisterList()->IsEmpty()
1708 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1709 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1710}
1711
Calin Juravlee460d1d2015-09-29 04:52:17 +01001712void CodeGeneratorARM64::MoveLocation(Location destination,
1713 Location source,
1714 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001715 if (source.Equals(destination)) {
1716 return;
1717 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001718
1719 // A valid move can always be inferred from the destination and source
1720 // locations. When moving from and to a register, the argument type can be
1721 // used to generate 32bit instead of 64bit moves. In debug mode we also
1722 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001723 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001724
1725 if (destination.IsRegister() || destination.IsFpuRegister()) {
1726 if (unspecified_type) {
1727 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1728 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001729 (src_cst != nullptr && (src_cst->IsIntConstant()
1730 || src_cst->IsFloatConstant()
1731 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001732 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001733 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001734 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001735 // If the source is a double stack slot or a 64bit constant, a 64bit
1736 // type is appropriate. Else the source is a register, and since the
1737 // type has not been specified, we chose a 64bit type to force a 64bit
1738 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001739 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001742 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1743 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1744 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001745 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1746 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1747 __ Ldr(dst, StackOperandFrom(source));
1748 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001749 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001750 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001751 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001752 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001753 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001754 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001755 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001756 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1757 ? Primitive::kPrimLong
1758 : Primitive::kPrimInt;
1759 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1760 }
1761 } else {
1762 DCHECK(source.IsFpuRegister());
1763 if (destination.IsRegister()) {
1764 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1765 ? Primitive::kPrimDouble
1766 : Primitive::kPrimFloat;
1767 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1768 } else {
1769 DCHECK(destination.IsFpuRegister());
1770 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771 }
1772 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001773 } else { // The destination is not a register. It must be a stack slot.
1774 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1775 if (source.IsRegister() || source.IsFpuRegister()) {
1776 if (unspecified_type) {
1777 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001778 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001781 }
1782 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001783 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1784 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1785 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001787 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1788 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001789 UseScratchRegisterScope temps(GetVIXLAssembler());
1790 HConstant* src_cst = source.GetConstant();
1791 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001792 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001793 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1794 ? Register(xzr)
1795 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001797 if (src_cst->IsIntConstant()) {
1798 temp = temps.AcquireW();
1799 } else if (src_cst->IsLongConstant()) {
1800 temp = temps.AcquireX();
1801 } else if (src_cst->IsFloatConstant()) {
1802 temp = temps.AcquireS();
1803 } else {
1804 DCHECK(src_cst->IsDoubleConstant());
1805 temp = temps.AcquireD();
1806 }
1807 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001808 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001809 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001810 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001811 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001812 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001813 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001814 // Use any scratch register (a core or a floating-point one)
1815 // from VIXL scratch register pools as a temporary.
1816 //
1817 // We used to only use the FP scratch register pool, but in some
1818 // rare cases the only register from this pool (D31) would
1819 // already be used (e.g. within a ParallelMove instruction, when
1820 // a move is blocked by a another move requiring a scratch FP
1821 // register, which would reserve D31). To prevent this issue, we
1822 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001823 //
1824 // Also, we start by asking for a FP scratch register first, as the
1825 // demand of scratch core registers is higher. This is why we
1826 // use AcquireFPOrCoreCPURegisterOfSize instead of
1827 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1828 // allocates core scratch registers first.
1829 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1830 GetVIXLAssembler(),
1831 &temps,
1832 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001833 __ Ldr(temp, StackOperandFrom(source));
1834 __ Str(temp, StackOperandFrom(destination));
1835 }
1836 }
1837}
1838
1839void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001840 CPURegister dst,
1841 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001842 switch (type) {
1843 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001844 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001845 break;
1846 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001847 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001848 break;
1849 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001850 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001851 break;
1852 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001853 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001854 break;
1855 case Primitive::kPrimInt:
1856 case Primitive::kPrimNot:
1857 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001858 case Primitive::kPrimFloat:
1859 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001860 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001861 __ Ldr(dst, src);
1862 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001863 case Primitive::kPrimVoid:
1864 LOG(FATAL) << "Unreachable type " << type;
1865 }
1866}
1867
Calin Juravle77520bc2015-01-12 18:45:46 +00001868void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001869 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001870 const MemOperand& src,
1871 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001872 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001873 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001874 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001875 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001876
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001877 DCHECK(!src.IsPreIndex());
1878 DCHECK(!src.IsPostIndex());
1879
1880 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001881 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001882 {
1883 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1884 MemOperand base = MemOperand(temp_base);
1885 switch (type) {
1886 case Primitive::kPrimBoolean:
1887 {
1888 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1889 __ ldarb(Register(dst), base);
1890 if (needs_null_check) {
1891 MaybeRecordImplicitNullCheck(instruction);
1892 }
1893 }
1894 break;
1895 case Primitive::kPrimByte:
1896 {
1897 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1898 __ ldarb(Register(dst), base);
1899 if (needs_null_check) {
1900 MaybeRecordImplicitNullCheck(instruction);
1901 }
1902 }
1903 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1904 break;
1905 case Primitive::kPrimChar:
1906 {
1907 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1908 __ ldarh(Register(dst), base);
1909 if (needs_null_check) {
1910 MaybeRecordImplicitNullCheck(instruction);
1911 }
1912 }
1913 break;
1914 case Primitive::kPrimShort:
1915 {
1916 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1917 __ ldarh(Register(dst), base);
1918 if (needs_null_check) {
1919 MaybeRecordImplicitNullCheck(instruction);
1920 }
1921 }
1922 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1923 break;
1924 case Primitive::kPrimInt:
1925 case Primitive::kPrimNot:
1926 case Primitive::kPrimLong:
1927 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1928 {
1929 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1930 __ ldar(Register(dst), base);
1931 if (needs_null_check) {
1932 MaybeRecordImplicitNullCheck(instruction);
1933 }
1934 }
1935 break;
1936 case Primitive::kPrimFloat:
1937 case Primitive::kPrimDouble: {
1938 DCHECK(dst.IsFPRegister());
1939 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001940
Artem Serov914d7a82017-02-07 14:33:49 +00001941 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1942 {
1943 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1944 __ ldar(temp, base);
1945 if (needs_null_check) {
1946 MaybeRecordImplicitNullCheck(instruction);
1947 }
1948 }
1949 __ Fmov(FPRegister(dst), temp);
1950 break;
Roland Levillain44015862016-01-22 11:47:17 +00001951 }
Artem Serov914d7a82017-02-07 14:33:49 +00001952 case Primitive::kPrimVoid:
1953 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001954 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001955 }
1956}
1957
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001958void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001959 CPURegister src,
1960 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001961 switch (type) {
1962 case Primitive::kPrimBoolean:
1963 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001964 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001965 break;
1966 case Primitive::kPrimChar:
1967 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001968 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001969 break;
1970 case Primitive::kPrimInt:
1971 case Primitive::kPrimNot:
1972 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001973 case Primitive::kPrimFloat:
1974 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001975 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001976 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001977 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001978 case Primitive::kPrimVoid:
1979 LOG(FATAL) << "Unreachable type " << type;
1980 }
1981}
1982
Artem Serov914d7a82017-02-07 14:33:49 +00001983void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
1984 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001985 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001986 const MemOperand& dst,
1987 bool needs_null_check) {
1988 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001989 UseScratchRegisterScope temps(GetVIXLAssembler());
1990 Register temp_base = temps.AcquireX();
1991
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001992 DCHECK(!dst.IsPreIndex());
1993 DCHECK(!dst.IsPostIndex());
1994
1995 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001996 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001997 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001998 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001999 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002000 switch (type) {
2001 case Primitive::kPrimBoolean:
2002 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002003 {
2004 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2005 __ stlrb(Register(src), base);
2006 if (needs_null_check) {
2007 MaybeRecordImplicitNullCheck(instruction);
2008 }
2009 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002010 break;
2011 case Primitive::kPrimChar:
2012 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002013 {
2014 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2015 __ stlrh(Register(src), base);
2016 if (needs_null_check) {
2017 MaybeRecordImplicitNullCheck(instruction);
2018 }
2019 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002020 break;
2021 case Primitive::kPrimInt:
2022 case Primitive::kPrimNot:
2023 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002024 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002025 {
2026 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2027 __ stlr(Register(src), base);
2028 if (needs_null_check) {
2029 MaybeRecordImplicitNullCheck(instruction);
2030 }
2031 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 break;
2033 case Primitive::kPrimFloat:
2034 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002035 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002036 Register temp_src;
2037 if (src.IsZero()) {
2038 // The zero register is used to avoid synthesizing zero constants.
2039 temp_src = Register(src);
2040 } else {
2041 DCHECK(src.IsFPRegister());
2042 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2043 __ Fmov(temp_src, FPRegister(src));
2044 }
Artem Serov914d7a82017-02-07 14:33:49 +00002045 {
2046 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2047 __ stlr(temp_src, base);
2048 if (needs_null_check) {
2049 MaybeRecordImplicitNullCheck(instruction);
2050 }
2051 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002052 break;
2053 }
2054 case Primitive::kPrimVoid:
2055 LOG(FATAL) << "Unreachable type " << type;
2056 }
2057}
2058
Calin Juravle175dc732015-08-25 15:42:32 +01002059void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2060 HInstruction* instruction,
2061 uint32_t dex_pc,
2062 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002063 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002064
2065 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2066 {
2067 // Ensure the pc position is recorded immediately after the `blr` instruction.
2068 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2069 __ blr(lr);
2070 if (EntrypointRequiresStackMap(entrypoint)) {
2071 RecordPcInfo(instruction, dex_pc, slow_path);
2072 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002073 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002074}
2075
Roland Levillaindec8f632016-07-22 17:10:06 +01002076void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2077 HInstruction* instruction,
2078 SlowPathCode* slow_path) {
2079 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002080 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2081 __ Blr(lr);
2082}
2083
Alexandre Rames67555f72014-11-18 10:55:16 +00002084void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002085 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002086 UseScratchRegisterScope temps(GetVIXLAssembler());
2087 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002088 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2089
Serban Constantinescu02164b32014-11-13 14:05:07 +00002090 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002091 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2092 __ Add(temp, class_reg, status_offset);
2093 __ Ldar(temp, HeapOperand(temp));
2094 __ Cmp(temp, mirror::Class::kStatusInitialized);
2095 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002096 __ Bind(slow_path->GetExitLabel());
2097}
Alexandre Rames5319def2014-10-23 10:03:10 +01002098
Roland Levillain44015862016-01-22 11:47:17 +00002099void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002100 BarrierType type = BarrierAll;
2101
2102 switch (kind) {
2103 case MemBarrierKind::kAnyAny:
2104 case MemBarrierKind::kAnyStore: {
2105 type = BarrierAll;
2106 break;
2107 }
2108 case MemBarrierKind::kLoadAny: {
2109 type = BarrierReads;
2110 break;
2111 }
2112 case MemBarrierKind::kStoreStore: {
2113 type = BarrierWrites;
2114 break;
2115 }
2116 default:
2117 LOG(FATAL) << "Unexpected memory barrier " << kind;
2118 }
2119 __ Dmb(InnerShareable, type);
2120}
2121
Serban Constantinescu02164b32014-11-13 14:05:07 +00002122void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2123 HBasicBlock* successor) {
2124 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002125 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2126 if (slow_path == nullptr) {
2127 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2128 instruction->SetSlowPath(slow_path);
2129 codegen_->AddSlowPath(slow_path);
2130 if (successor != nullptr) {
2131 DCHECK(successor->IsLoopHeader());
2132 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2133 }
2134 } else {
2135 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2136 }
2137
Serban Constantinescu02164b32014-11-13 14:05:07 +00002138 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2139 Register temp = temps.AcquireW();
2140
Andreas Gampe542451c2016-07-26 09:02:02 -07002141 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002142 if (successor == nullptr) {
2143 __ Cbnz(temp, slow_path->GetEntryLabel());
2144 __ Bind(slow_path->GetReturnLabel());
2145 } else {
2146 __ Cbz(temp, codegen_->GetLabelOf(successor));
2147 __ B(slow_path->GetEntryLabel());
2148 // slow_path will return to GetLabelOf(successor).
2149 }
2150}
2151
Alexandre Rames5319def2014-10-23 10:03:10 +01002152InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2153 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002154 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002155 assembler_(codegen->GetAssembler()),
2156 codegen_(codegen) {}
2157
2158#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002159 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002160
2161#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2162
2163enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002164 // Using a base helps identify when we hit such breakpoints.
2165 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002166#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2167 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2168#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2169};
2170
2171#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002172 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002173 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2174 } \
2175 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2176 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2177 locations->SetOut(Location::Any()); \
2178 }
2179 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2180#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2181
2182#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002183#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002184
Alexandre Rames67555f72014-11-18 10:55:16 +00002185void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002186 DCHECK_EQ(instr->InputCount(), 2U);
2187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2188 Primitive::Type type = instr->GetResultType();
2189 switch (type) {
2190 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002191 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002192 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002193 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002195 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002196
2197 case Primitive::kPrimFloat:
2198 case Primitive::kPrimDouble:
2199 locations->SetInAt(0, Location::RequiresFpuRegister());
2200 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002202 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002203
Alexandre Rames5319def2014-10-23 10:03:10 +01002204 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002205 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002206 }
2207}
2208
Alexandre Rames09a99962015-04-15 11:47:56 +01002209void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002210 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2211
2212 bool object_field_get_with_read_barrier =
2213 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002214 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002215 new (GetGraph()->GetArena()) LocationSummary(instruction,
2216 object_field_get_with_read_barrier ?
2217 LocationSummary::kCallOnSlowPath :
2218 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002219 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002220 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002221 // We need a temporary register for the read barrier marking slow
2222 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2223 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002224 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002225 locations->SetInAt(0, Location::RequiresRegister());
2226 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2227 locations->SetOut(Location::RequiresFpuRegister());
2228 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002229 // The output overlaps for an object field get when read barriers
2230 // are enabled: we do not want the load to overwrite the object's
2231 // location, as we need it to emit the read barrier.
2232 locations->SetOut(
2233 Location::RequiresRegister(),
2234 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002235 }
2236}
2237
2238void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2239 const FieldInfo& field_info) {
2240 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002241 LocationSummary* locations = instruction->GetLocations();
2242 Location base_loc = locations->InAt(0);
2243 Location out = locations->Out();
2244 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002245 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002246 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002247
Roland Levillain44015862016-01-22 11:47:17 +00002248 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2249 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002250 // /* HeapReference<Object> */ out = *(base + offset)
2251 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002252 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002253 // Note that potential implicit null checks are handled in this
2254 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2255 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2256 instruction,
2257 out,
2258 base,
2259 offset,
2260 temp,
2261 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002262 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002263 } else {
2264 // General case.
2265 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002266 // Note that a potential implicit null check is handled in this
2267 // CodeGeneratorARM64::LoadAcquire call.
2268 // NB: LoadAcquire will record the pc info if needed.
2269 codegen_->LoadAcquire(
2270 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002271 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002272 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2273 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002274 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002276 }
Roland Levillain44015862016-01-22 11:47:17 +00002277 if (field_type == Primitive::kPrimNot) {
2278 // If read barriers are enabled, emit read barriers other than
2279 // Baker's using a slow path (and also unpoison the loaded
2280 // reference, if heap poisoning is enabled).
2281 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2282 }
Roland Levillain4d027112015-07-01 15:41:14 +01002283 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002284}
2285
2286void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2287 LocationSummary* locations =
2288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2289 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002290 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2291 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2292 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002293 locations->SetInAt(1, Location::RequiresFpuRegister());
2294 } else {
2295 locations->SetInAt(1, Location::RequiresRegister());
2296 }
2297}
2298
2299void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002300 const FieldInfo& field_info,
2301 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002302 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2303
2304 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002305 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002306 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002307 Offset offset = field_info.GetFieldOffset();
2308 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002309
Roland Levillain4d027112015-07-01 15:41:14 +01002310 {
2311 // We use a block to end the scratch scope before the write barrier, thus
2312 // freeing the temporary registers so they can be used in `MarkGCCard`.
2313 UseScratchRegisterScope temps(GetVIXLAssembler());
2314
2315 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2316 DCHECK(value.IsW());
2317 Register temp = temps.AcquireW();
2318 __ Mov(temp, value.W());
2319 GetAssembler()->PoisonHeapReference(temp.W());
2320 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002321 }
Roland Levillain4d027112015-07-01 15:41:14 +01002322
2323 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002324 codegen_->StoreRelease(
2325 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002326 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002327 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2328 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002329 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2330 codegen_->MaybeRecordImplicitNullCheck(instruction);
2331 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002332 }
2333
2334 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002335 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002336 }
2337}
2338
Alexandre Rames67555f72014-11-18 10:55:16 +00002339void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002340 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002341
2342 switch (type) {
2343 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002344 case Primitive::kPrimLong: {
2345 Register dst = OutputRegister(instr);
2346 Register lhs = InputRegisterAt(instr, 0);
2347 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002348 if (instr->IsAdd()) {
2349 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002350 } else if (instr->IsAnd()) {
2351 __ And(dst, lhs, rhs);
2352 } else if (instr->IsOr()) {
2353 __ Orr(dst, lhs, rhs);
2354 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002355 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002356 } else if (instr->IsRor()) {
2357 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002358 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002359 __ Ror(dst, lhs, shift);
2360 } else {
2361 // Ensure shift distance is in the same size register as the result. If
2362 // we are rotating a long and the shift comes in a w register originally,
2363 // we don't need to sxtw for use as an x since the shift distances are
2364 // all & reg_bits - 1.
2365 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2366 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002367 } else {
2368 DCHECK(instr->IsXor());
2369 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002370 }
2371 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002372 }
2373 case Primitive::kPrimFloat:
2374 case Primitive::kPrimDouble: {
2375 FPRegister dst = OutputFPRegister(instr);
2376 FPRegister lhs = InputFPRegisterAt(instr, 0);
2377 FPRegister rhs = InputFPRegisterAt(instr, 1);
2378 if (instr->IsAdd()) {
2379 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002380 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002381 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002382 } else {
2383 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002384 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002385 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002386 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002387 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002388 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002389 }
2390}
2391
Serban Constantinescu02164b32014-11-13 14:05:07 +00002392void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2393 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2394
2395 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2396 Primitive::Type type = instr->GetResultType();
2397 switch (type) {
2398 case Primitive::kPrimInt:
2399 case Primitive::kPrimLong: {
2400 locations->SetInAt(0, Location::RequiresRegister());
2401 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002403 break;
2404 }
2405 default:
2406 LOG(FATAL) << "Unexpected shift type " << type;
2407 }
2408}
2409
2410void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2411 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2412
2413 Primitive::Type type = instr->GetType();
2414 switch (type) {
2415 case Primitive::kPrimInt:
2416 case Primitive::kPrimLong: {
2417 Register dst = OutputRegister(instr);
2418 Register lhs = InputRegisterAt(instr, 0);
2419 Operand rhs = InputOperandAt(instr, 1);
2420 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002421 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002422 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002423 if (instr->IsShl()) {
2424 __ Lsl(dst, lhs, shift_value);
2425 } else if (instr->IsShr()) {
2426 __ Asr(dst, lhs, shift_value);
2427 } else {
2428 __ Lsr(dst, lhs, shift_value);
2429 }
2430 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002431 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002432
2433 if (instr->IsShl()) {
2434 __ Lsl(dst, lhs, rhs_reg);
2435 } else if (instr->IsShr()) {
2436 __ Asr(dst, lhs, rhs_reg);
2437 } else {
2438 __ Lsr(dst, lhs, rhs_reg);
2439 }
2440 }
2441 break;
2442 }
2443 default:
2444 LOG(FATAL) << "Unexpected shift operation type " << type;
2445 }
2446}
2447
Alexandre Rames5319def2014-10-23 10:03:10 +01002448void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002449 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002450}
2451
2452void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002453 HandleBinaryOp(instruction);
2454}
2455
2456void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2457 HandleBinaryOp(instruction);
2458}
2459
2460void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2461 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002462}
2463
Artem Serov7fc63502016-02-09 17:15:29 +00002464void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002465 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2467 locations->SetInAt(0, Location::RequiresRegister());
2468 // There is no immediate variant of negated bitwise instructions in AArch64.
2469 locations->SetInAt(1, Location::RequiresRegister());
2470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2471}
2472
Artem Serov7fc63502016-02-09 17:15:29 +00002473void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002474 Register dst = OutputRegister(instr);
2475 Register lhs = InputRegisterAt(instr, 0);
2476 Register rhs = InputRegisterAt(instr, 1);
2477
2478 switch (instr->GetOpKind()) {
2479 case HInstruction::kAnd:
2480 __ Bic(dst, lhs, rhs);
2481 break;
2482 case HInstruction::kOr:
2483 __ Orn(dst, lhs, rhs);
2484 break;
2485 case HInstruction::kXor:
2486 __ Eon(dst, lhs, rhs);
2487 break;
2488 default:
2489 LOG(FATAL) << "Unreachable";
2490 }
2491}
2492
Anton Kirilov74234da2017-01-13 14:42:47 +00002493void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2494 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002495 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2496 instruction->GetType() == Primitive::kPrimLong);
2497 LocationSummary* locations =
2498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2499 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2500 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2501 } else {
2502 locations->SetInAt(0, Location::RequiresRegister());
2503 }
2504 locations->SetInAt(1, Location::RequiresRegister());
2505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2506}
2507
Anton Kirilov74234da2017-01-13 14:42:47 +00002508void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2509 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002510 Primitive::Type type = instruction->GetType();
2511 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2512 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2513 Register out = OutputRegister(instruction);
2514 Register left;
2515 if (kind != HInstruction::kNeg) {
2516 left = InputRegisterAt(instruction, 0);
2517 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002518 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002519 // shifter operand operation, the IR generating `right_reg` (input to the type
2520 // conversion) can have a different type from the current instruction's type,
2521 // so we manually indicate the type.
2522 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002523 Operand right_operand(0);
2524
Anton Kirilov74234da2017-01-13 14:42:47 +00002525 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2526 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002527 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2528 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002529 right_operand = Operand(right_reg,
2530 helpers::ShiftFromOpKind(op_kind),
2531 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002532 }
2533
2534 // Logical binary operations do not support extension operations in the
2535 // operand. Note that VIXL would still manage if it was passed by generating
2536 // the extension as a separate instruction.
2537 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2538 DCHECK(!right_operand.IsExtendedRegister() ||
2539 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2540 kind != HInstruction::kNeg));
2541 switch (kind) {
2542 case HInstruction::kAdd:
2543 __ Add(out, left, right_operand);
2544 break;
2545 case HInstruction::kAnd:
2546 __ And(out, left, right_operand);
2547 break;
2548 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002549 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002550 __ Neg(out, right_operand);
2551 break;
2552 case HInstruction::kOr:
2553 __ Orr(out, left, right_operand);
2554 break;
2555 case HInstruction::kSub:
2556 __ Sub(out, left, right_operand);
2557 break;
2558 case HInstruction::kXor:
2559 __ Eor(out, left, right_operand);
2560 break;
2561 default:
2562 LOG(FATAL) << "Unexpected operation kind: " << kind;
2563 UNREACHABLE();
2564 }
2565}
2566
Artem Serov328429f2016-07-06 16:23:04 +01002567void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002568 LocationSummary* locations =
2569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2570 locations->SetInAt(0, Location::RequiresRegister());
2571 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002573}
2574
Roland Levillain19c54192016-11-04 13:44:09 +00002575void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002576 __ Add(OutputRegister(instruction),
2577 InputRegisterAt(instruction, 0),
2578 Operand(InputOperandAt(instruction, 1)));
2579}
2580
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002581void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002582 LocationSummary* locations =
2583 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002584 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2585 if (instr->GetOpKind() == HInstruction::kSub &&
2586 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002587 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002588 // Don't allocate register for Mneg instruction.
2589 } else {
2590 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2591 Location::RequiresRegister());
2592 }
2593 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2594 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2596}
2597
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002598void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002599 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002600 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2601 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002602
2603 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2604 // This fixup should be carried out for all multiply-accumulate instructions:
2605 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2606 if (instr->GetType() == Primitive::kPrimLong &&
2607 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2608 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002609 vixl::aarch64::Instruction* prev =
2610 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002611 if (prev->IsLoadOrStore()) {
2612 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002613 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002614 __ nop();
2615 }
2616 }
2617
2618 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002619 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002620 __ Madd(res, mul_left, mul_right, accumulator);
2621 } else {
2622 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002623 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002624 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002625 __ Mneg(res, mul_left, mul_right);
2626 } else {
2627 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2628 __ Msub(res, mul_left, mul_right, accumulator);
2629 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002630 }
2631}
2632
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002633void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002634 bool object_array_get_with_read_barrier =
2635 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002636 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002637 new (GetGraph()->GetArena()) LocationSummary(instruction,
2638 object_array_get_with_read_barrier ?
2639 LocationSummary::kCallOnSlowPath :
2640 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002641 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002642 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002643 // We need a temporary register for the read barrier marking slow
2644 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
2645 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002646 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002649 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2650 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2651 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002652 // The output overlaps in the case of an object array get with
2653 // read barriers enabled: we do not want the move to overwrite the
2654 // array's location, as we need it to emit the read barrier.
2655 locations->SetOut(
2656 Location::RequiresRegister(),
2657 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002658 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002659}
2660
2661void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002662 Primitive::Type type = instruction->GetType();
2663 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002664 LocationSummary* locations = instruction->GetLocations();
2665 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002666 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002667 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002668 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2669 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002670 MacroAssembler* masm = GetVIXLAssembler();
2671 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002672
Roland Levillain19c54192016-11-04 13:44:09 +00002673 // The read barrier instrumentation of object ArrayGet instructions
2674 // does not support the HIntermediateAddress instruction.
2675 DCHECK(!((type == Primitive::kPrimNot) &&
2676 instruction->GetArray()->IsIntermediateAddress() &&
2677 kEmitCompilerReadBarrier));
2678
Roland Levillain44015862016-01-22 11:47:17 +00002679 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2680 // Object ArrayGet with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +00002681 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002682 // Note that a potential implicit null check is handled in the
2683 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2684 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2685 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002686 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002687 // General case.
2688 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002689 Register length;
2690 if (maybe_compressed_char_at) {
2691 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2692 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002693 {
2694 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2695 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2696
2697 if (instruction->GetArray()->IsIntermediateAddress()) {
2698 DCHECK_LT(count_offset, offset);
2699 int64_t adjusted_offset =
2700 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2701 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2702 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2703 } else {
2704 __ Ldr(length, HeapOperand(obj, count_offset));
2705 }
2706 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002707 }
jessicahandojo05765752016-09-09 19:01:32 -07002708 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002709 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002710 if (maybe_compressed_char_at) {
2711 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002712 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2713 "Expecting 0=compressed, 1=uncompressed");
2714 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002715 __ Ldrb(Register(OutputCPURegister(instruction)),
2716 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2717 __ B(&done);
2718 __ Bind(&uncompressed_load);
2719 __ Ldrh(Register(OutputCPURegister(instruction)),
2720 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2721 __ Bind(&done);
2722 } else {
2723 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2724 source = HeapOperand(obj, offset);
2725 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002726 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002727 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002728 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002729 // We do not need to compute the intermediate address from the array: the
2730 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002731 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002732 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002733 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002734 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2735 }
2736 temp = obj;
2737 } else {
2738 __ Add(temp, obj, offset);
2739 }
jessicahandojo05765752016-09-09 19:01:32 -07002740 if (maybe_compressed_char_at) {
2741 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002742 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2743 "Expecting 0=compressed, 1=uncompressed");
2744 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002745 __ Ldrb(Register(OutputCPURegister(instruction)),
2746 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2747 __ B(&done);
2748 __ Bind(&uncompressed_load);
2749 __ Ldrh(Register(OutputCPURegister(instruction)),
2750 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2751 __ Bind(&done);
2752 } else {
2753 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2754 }
Roland Levillain44015862016-01-22 11:47:17 +00002755 }
jessicahandojo05765752016-09-09 19:01:32 -07002756 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002757 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2758 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002759 codegen_->Load(type, OutputCPURegister(instruction), source);
2760 codegen_->MaybeRecordImplicitNullCheck(instruction);
2761 }
Roland Levillain44015862016-01-22 11:47:17 +00002762
2763 if (type == Primitive::kPrimNot) {
2764 static_assert(
2765 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2766 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2767 Location obj_loc = locations->InAt(0);
2768 if (index.IsConstant()) {
2769 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2770 } else {
2771 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2772 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002773 }
Roland Levillain4d027112015-07-01 15:41:14 +01002774 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002775}
2776
Alexandre Rames5319def2014-10-23 10:03:10 +01002777void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2779 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002781}
2782
2783void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002784 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002785 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002786 {
2787 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2788 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2789 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2790 codegen_->MaybeRecordImplicitNullCheck(instruction);
2791 }
jessicahandojo05765752016-09-09 19:01:32 -07002792 // Mask out compression flag from String's array length.
2793 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002794 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002795 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002796}
2797
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002798void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002799 Primitive::Type value_type = instruction->GetComponentType();
2800
2801 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2803 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002804 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002805 LocationSummary::kCallOnSlowPath :
2806 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002807 locations->SetInAt(0, Location::RequiresRegister());
2808 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002809 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2810 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2811 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002812 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002814 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002815 }
2816}
2817
2818void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2819 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002820 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002821 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002822 bool needs_write_barrier =
2823 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002824
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002825 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002826 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002827 CPURegister source = value;
2828 Location index = locations->InAt(1);
2829 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2830 MemOperand destination = HeapOperand(array);
2831 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002832
2833 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002834 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002835 if (index.IsConstant()) {
2836 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2837 destination = HeapOperand(array, offset);
2838 } else {
2839 UseScratchRegisterScope temps(masm);
2840 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002841 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002842 // We do not need to compute the intermediate address from the array: the
2843 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002844 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002845 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002846 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002847 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2848 }
2849 temp = array;
2850 } else {
2851 __ Add(temp, array, offset);
2852 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002853 destination = HeapOperand(temp,
2854 XRegisterFrom(index),
2855 LSL,
2856 Primitive::ComponentSizeShift(value_type));
2857 }
Artem Serov914d7a82017-02-07 14:33:49 +00002858 {
2859 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2860 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2861 codegen_->Store(value_type, value, destination);
2862 codegen_->MaybeRecordImplicitNullCheck(instruction);
2863 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002864 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002865 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002866 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002867 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002868 {
2869 // We use a block to end the scratch scope before the write barrier, thus
2870 // freeing the temporary registers so they can be used in `MarkGCCard`.
2871 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002872 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002873 if (index.IsConstant()) {
2874 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002875 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002876 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002877 destination = HeapOperand(temp,
2878 XRegisterFrom(index),
2879 LSL,
2880 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002881 }
2882
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002883 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2884 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2885 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2886
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002887 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002888 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2889 codegen_->AddSlowPath(slow_path);
2890 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002891 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002892 __ Cbnz(Register(value), &non_zero);
2893 if (!index.IsConstant()) {
2894 __ Add(temp, array, offset);
2895 }
Artem Serov914d7a82017-02-07 14:33:49 +00002896 {
2897 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2898 // emitted.
2899 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2900 __ Str(wzr, destination);
2901 codegen_->MaybeRecordImplicitNullCheck(instruction);
2902 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002903 __ B(&done);
2904 __ Bind(&non_zero);
2905 }
2906
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002907 // Note that when Baker read barriers are enabled, the type
2908 // checks are performed without read barriers. This is fine,
2909 // even in the case where a class object is in the from-space
2910 // after the flip, as a comparison involving such a type would
2911 // not produce a false positive; it may of course produce a
2912 // false negative, in which case we would take the ArraySet
2913 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002914
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002915 Register temp2 = temps.AcquireSameSizeAs(array);
2916 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002917 {
2918 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2919 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2920 __ Ldr(temp, HeapOperand(array, class_offset));
2921 codegen_->MaybeRecordImplicitNullCheck(instruction);
2922 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002923 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002924
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002925 // /* HeapReference<Class> */ temp = temp->component_type_
2926 __ Ldr(temp, HeapOperand(temp, component_offset));
2927 // /* HeapReference<Class> */ temp2 = value->klass_
2928 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2929 // If heap poisoning is enabled, no need to unpoison `temp`
2930 // nor `temp2`, as we are comparing two poisoned references.
2931 __ Cmp(temp, temp2);
2932 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002933
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002934 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2935 vixl::aarch64::Label do_put;
2936 __ B(eq, &do_put);
2937 // If heap poisoning is enabled, the `temp` reference has
2938 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002939 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2940
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002941 // /* HeapReference<Class> */ temp = temp->super_class_
2942 __ Ldr(temp, HeapOperand(temp, super_offset));
2943 // If heap poisoning is enabled, no need to unpoison
2944 // `temp`, as we are comparing against null below.
2945 __ Cbnz(temp, slow_path->GetEntryLabel());
2946 __ Bind(&do_put);
2947 } else {
2948 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002949 }
2950 }
2951
2952 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002953 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002955 __ Mov(temp2, value.W());
2956 GetAssembler()->PoisonHeapReference(temp2);
2957 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002958 }
2959
2960 if (!index.IsConstant()) {
2961 __ Add(temp, array, offset);
2962 }
Artem Serov914d7a82017-02-07 14:33:49 +00002963 {
2964 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2965 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2966 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967
Artem Serov914d7a82017-02-07 14:33:49 +00002968 if (!may_need_runtime_call_for_type_check) {
2969 codegen_->MaybeRecordImplicitNullCheck(instruction);
2970 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002971 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002972 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002973
2974 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2975
2976 if (done.IsLinked()) {
2977 __ Bind(&done);
2978 }
2979
2980 if (slow_path != nullptr) {
2981 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002982 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002983 }
2984}
2985
Alexandre Rames67555f72014-11-18 10:55:16 +00002986void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002987 RegisterSet caller_saves = RegisterSet::Empty();
2988 InvokeRuntimeCallingConvention calling_convention;
2989 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2990 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2991 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002992 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002993 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002994}
2995
2996void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002997 BoundsCheckSlowPathARM64* slow_path =
2998 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002999 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003000 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3001 __ B(slow_path->GetEntryLabel(), hs);
3002}
3003
Alexandre Rames67555f72014-11-18 10:55:16 +00003004void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3005 LocationSummary* locations =
3006 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3007 locations->SetInAt(0, Location::RequiresRegister());
3008 if (check->HasUses()) {
3009 locations->SetOut(Location::SameAsFirstInput());
3010 }
3011}
3012
3013void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3014 // We assume the class is not null.
3015 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3016 check->GetLoadClass(), check, check->GetDexPc(), true);
3017 codegen_->AddSlowPath(slow_path);
3018 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3019}
3020
Roland Levillain1a653882016-03-18 18:05:57 +00003021static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3022 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3023 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3024}
3025
3026void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3027 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3028 Location rhs_loc = instruction->GetLocations()->InAt(1);
3029 if (rhs_loc.IsConstant()) {
3030 // 0.0 is the only immediate that can be encoded directly in
3031 // an FCMP instruction.
3032 //
3033 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3034 // specify that in a floating-point comparison, positive zero
3035 // and negative zero are considered equal, so we can use the
3036 // literal 0.0 for both cases here.
3037 //
3038 // Note however that some methods (Float.equal, Float.compare,
3039 // Float.compareTo, Double.equal, Double.compare,
3040 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3041 // StrictMath.min) consider 0.0 to be (strictly) greater than
3042 // -0.0. So if we ever translate calls to these methods into a
3043 // HCompare instruction, we must handle the -0.0 case with
3044 // care here.
3045 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3046 __ Fcmp(lhs_reg, 0.0);
3047 } else {
3048 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3049 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003050}
3051
Serban Constantinescu02164b32014-11-13 14:05:07 +00003052void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003053 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003054 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3055 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003056 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003057 case Primitive::kPrimBoolean:
3058 case Primitive::kPrimByte:
3059 case Primitive::kPrimShort:
3060 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003061 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003062 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003063 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003064 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3066 break;
3067 }
3068 case Primitive::kPrimFloat:
3069 case Primitive::kPrimDouble: {
3070 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003071 locations->SetInAt(1,
3072 IsFloatingPointZeroConstant(compare->InputAt(1))
3073 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3074 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003075 locations->SetOut(Location::RequiresRegister());
3076 break;
3077 }
3078 default:
3079 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3080 }
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3084 Primitive::Type in_type = compare->InputAt(0)->GetType();
3085
3086 // 0 if: left == right
3087 // 1 if: left > right
3088 // -1 if: left < right
3089 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003090 case Primitive::kPrimBoolean:
3091 case Primitive::kPrimByte:
3092 case Primitive::kPrimShort:
3093 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003094 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003095 case Primitive::kPrimLong: {
3096 Register result = OutputRegister(compare);
3097 Register left = InputRegisterAt(compare, 0);
3098 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003099 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003100 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3101 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003102 break;
3103 }
3104 case Primitive::kPrimFloat:
3105 case Primitive::kPrimDouble: {
3106 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003107 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003108 __ Cset(result, ne);
3109 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003110 break;
3111 }
3112 default:
3113 LOG(FATAL) << "Unimplemented compare type " << in_type;
3114 }
3115}
3116
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003117void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003119
3120 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3121 locations->SetInAt(0, Location::RequiresFpuRegister());
3122 locations->SetInAt(1,
3123 IsFloatingPointZeroConstant(instruction->InputAt(1))
3124 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3125 : Location::RequiresFpuRegister());
3126 } else {
3127 // Integer cases.
3128 locations->SetInAt(0, Location::RequiresRegister());
3129 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3130 }
3131
David Brazdilb3e773e2016-01-26 11:28:37 +00003132 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003134 }
3135}
3136
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003137void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003138 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003139 return;
3140 }
3141
3142 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003143 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003144 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003145
Roland Levillain7f63c522015-07-13 15:54:55 +00003146 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003147 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003148 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003149 } else {
3150 // Integer cases.
3151 Register lhs = InputRegisterAt(instruction, 0);
3152 Operand rhs = InputOperandAt(instruction, 1);
3153 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003154 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003155 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003156}
3157
3158#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3159 M(Equal) \
3160 M(NotEqual) \
3161 M(LessThan) \
3162 M(LessThanOrEqual) \
3163 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003164 M(GreaterThanOrEqual) \
3165 M(Below) \
3166 M(BelowOrEqual) \
3167 M(Above) \
3168 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003169#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003170void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3171void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003172FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003173#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003174#undef FOR_EACH_CONDITION_INSTRUCTION
3175
Zheng Xuc6667102015-05-15 16:08:45 +08003176void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3177 DCHECK(instruction->IsDiv() || instruction->IsRem());
3178
3179 LocationSummary* locations = instruction->GetLocations();
3180 Location second = locations->InAt(1);
3181 DCHECK(second.IsConstant());
3182
3183 Register out = OutputRegister(instruction);
3184 Register dividend = InputRegisterAt(instruction, 0);
3185 int64_t imm = Int64FromConstant(second.GetConstant());
3186 DCHECK(imm == 1 || imm == -1);
3187
3188 if (instruction->IsRem()) {
3189 __ Mov(out, 0);
3190 } else {
3191 if (imm == 1) {
3192 __ Mov(out, dividend);
3193 } else {
3194 __ Neg(out, dividend);
3195 }
3196 }
3197}
3198
3199void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3200 DCHECK(instruction->IsDiv() || instruction->IsRem());
3201
3202 LocationSummary* locations = instruction->GetLocations();
3203 Location second = locations->InAt(1);
3204 DCHECK(second.IsConstant());
3205
3206 Register out = OutputRegister(instruction);
3207 Register dividend = InputRegisterAt(instruction, 0);
3208 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003209 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003210 int ctz_imm = CTZ(abs_imm);
3211
3212 UseScratchRegisterScope temps(GetVIXLAssembler());
3213 Register temp = temps.AcquireSameSizeAs(out);
3214
3215 if (instruction->IsDiv()) {
3216 __ Add(temp, dividend, abs_imm - 1);
3217 __ Cmp(dividend, 0);
3218 __ Csel(out, temp, dividend, lt);
3219 if (imm > 0) {
3220 __ Asr(out, out, ctz_imm);
3221 } else {
3222 __ Neg(out, Operand(out, ASR, ctz_imm));
3223 }
3224 } else {
3225 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3226 __ Asr(temp, dividend, bits - 1);
3227 __ Lsr(temp, temp, bits - ctz_imm);
3228 __ Add(out, dividend, temp);
3229 __ And(out, out, abs_imm - 1);
3230 __ Sub(out, out, temp);
3231 }
3232}
3233
3234void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3235 DCHECK(instruction->IsDiv() || instruction->IsRem());
3236
3237 LocationSummary* locations = instruction->GetLocations();
3238 Location second = locations->InAt(1);
3239 DCHECK(second.IsConstant());
3240
3241 Register out = OutputRegister(instruction);
3242 Register dividend = InputRegisterAt(instruction, 0);
3243 int64_t imm = Int64FromConstant(second.GetConstant());
3244
3245 Primitive::Type type = instruction->GetResultType();
3246 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3247
3248 int64_t magic;
3249 int shift;
3250 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3251
3252 UseScratchRegisterScope temps(GetVIXLAssembler());
3253 Register temp = temps.AcquireSameSizeAs(out);
3254
3255 // temp = get_high(dividend * magic)
3256 __ Mov(temp, magic);
3257 if (type == Primitive::kPrimLong) {
3258 __ Smulh(temp, dividend, temp);
3259 } else {
3260 __ Smull(temp.X(), dividend, temp);
3261 __ Lsr(temp.X(), temp.X(), 32);
3262 }
3263
3264 if (imm > 0 && magic < 0) {
3265 __ Add(temp, temp, dividend);
3266 } else if (imm < 0 && magic > 0) {
3267 __ Sub(temp, temp, dividend);
3268 }
3269
3270 if (shift != 0) {
3271 __ Asr(temp, temp, shift);
3272 }
3273
3274 if (instruction->IsDiv()) {
3275 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3276 } else {
3277 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3278 // TODO: Strength reduction for msub.
3279 Register temp_imm = temps.AcquireSameSizeAs(out);
3280 __ Mov(temp_imm, imm);
3281 __ Msub(out, temp, temp_imm, dividend);
3282 }
3283}
3284
3285void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3286 DCHECK(instruction->IsDiv() || instruction->IsRem());
3287 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003288 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003289
3290 LocationSummary* locations = instruction->GetLocations();
3291 Register out = OutputRegister(instruction);
3292 Location second = locations->InAt(1);
3293
3294 if (second.IsConstant()) {
3295 int64_t imm = Int64FromConstant(second.GetConstant());
3296
3297 if (imm == 0) {
3298 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3299 } else if (imm == 1 || imm == -1) {
3300 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003301 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003302 DivRemByPowerOfTwo(instruction);
3303 } else {
3304 DCHECK(imm <= -2 || imm >= 2);
3305 GenerateDivRemWithAnyConstant(instruction);
3306 }
3307 } else {
3308 Register dividend = InputRegisterAt(instruction, 0);
3309 Register divisor = InputRegisterAt(instruction, 1);
3310 if (instruction->IsDiv()) {
3311 __ Sdiv(out, dividend, divisor);
3312 } else {
3313 UseScratchRegisterScope temps(GetVIXLAssembler());
3314 Register temp = temps.AcquireSameSizeAs(out);
3315 __ Sdiv(temp, dividend, divisor);
3316 __ Msub(out, temp, divisor, dividend);
3317 }
3318 }
3319}
3320
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003321void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3322 LocationSummary* locations =
3323 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3324 switch (div->GetResultType()) {
3325 case Primitive::kPrimInt:
3326 case Primitive::kPrimLong:
3327 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003328 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3330 break;
3331
3332 case Primitive::kPrimFloat:
3333 case Primitive::kPrimDouble:
3334 locations->SetInAt(0, Location::RequiresFpuRegister());
3335 locations->SetInAt(1, Location::RequiresFpuRegister());
3336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3337 break;
3338
3339 default:
3340 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3341 }
3342}
3343
3344void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3345 Primitive::Type type = div->GetResultType();
3346 switch (type) {
3347 case Primitive::kPrimInt:
3348 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003349 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003350 break;
3351
3352 case Primitive::kPrimFloat:
3353 case Primitive::kPrimDouble:
3354 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3355 break;
3356
3357 default:
3358 LOG(FATAL) << "Unexpected div type " << type;
3359 }
3360}
3361
Alexandre Rames67555f72014-11-18 10:55:16 +00003362void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003363 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003364 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003365}
3366
3367void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3368 SlowPathCodeARM64* slow_path =
3369 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3370 codegen_->AddSlowPath(slow_path);
3371 Location value = instruction->GetLocations()->InAt(0);
3372
Alexandre Rames3e69f162014-12-10 10:36:50 +00003373 Primitive::Type type = instruction->GetType();
3374
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003375 if (!Primitive::IsIntegralType(type)) {
3376 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003377 return;
3378 }
3379
Alexandre Rames67555f72014-11-18 10:55:16 +00003380 if (value.IsConstant()) {
3381 int64_t divisor = Int64ConstantFrom(value);
3382 if (divisor == 0) {
3383 __ B(slow_path->GetEntryLabel());
3384 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003385 // A division by a non-null constant is valid. We don't need to perform
3386 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003387 }
3388 } else {
3389 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3390 }
3391}
3392
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003393void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3396 locations->SetOut(Location::ConstantLocation(constant));
3397}
3398
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003399void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3400 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003401 // Will be generated at use site.
3402}
3403
Alexandre Rames5319def2014-10-23 10:03:10 +01003404void LocationsBuilderARM64::VisitExit(HExit* exit) {
3405 exit->SetLocations(nullptr);
3406}
3407
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003408void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003409}
3410
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003411void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3412 LocationSummary* locations =
3413 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3414 locations->SetOut(Location::ConstantLocation(constant));
3415}
3416
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003417void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003418 // Will be generated at use site.
3419}
3420
David Brazdilfc6a86a2015-06-26 10:33:45 +00003421void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003422 DCHECK(!successor->IsExitBlock());
3423 HBasicBlock* block = got->GetBlock();
3424 HInstruction* previous = got->GetPrevious();
3425 HLoopInformation* info = block->GetLoopInformation();
3426
David Brazdil46e2a392015-03-16 17:31:52 +00003427 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003428 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3429 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3430 return;
3431 }
3432 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3433 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3434 }
3435 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003436 __ B(codegen_->GetLabelOf(successor));
3437 }
3438}
3439
David Brazdilfc6a86a2015-06-26 10:33:45 +00003440void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3441 got->SetLocations(nullptr);
3442}
3443
3444void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3445 HandleGoto(got, got->GetSuccessor());
3446}
3447
3448void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3449 try_boundary->SetLocations(nullptr);
3450}
3451
3452void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3453 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3454 if (!successor->IsExitBlock()) {
3455 HandleGoto(try_boundary, successor);
3456 }
3457}
3458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003459void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003460 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003461 vixl::aarch64::Label* true_target,
3462 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003463 // FP branching requires both targets to be explicit. If either of the targets
3464 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003465 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003466 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003467
David Brazdil0debae72015-11-12 18:37:00 +00003468 if (true_target == nullptr && false_target == nullptr) {
3469 // Nothing to do. The code always falls through.
3470 return;
3471 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003472 // Constant condition, statically compared against "true" (integer value 1).
3473 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003474 if (true_target != nullptr) {
3475 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003476 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003477 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003478 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003479 if (false_target != nullptr) {
3480 __ B(false_target);
3481 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003482 }
David Brazdil0debae72015-11-12 18:37:00 +00003483 return;
3484 }
3485
3486 // The following code generates these patterns:
3487 // (1) true_target == nullptr && false_target != nullptr
3488 // - opposite condition true => branch to false_target
3489 // (2) true_target != nullptr && false_target == nullptr
3490 // - condition true => branch to true_target
3491 // (3) true_target != nullptr && false_target != nullptr
3492 // - condition true => branch to true_target
3493 // - branch to false_target
3494 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003495 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003496 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003497 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003498 if (true_target == nullptr) {
3499 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3500 } else {
3501 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3502 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003503 } else {
3504 // The condition instruction has not been materialized, use its inputs as
3505 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003506 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003507
David Brazdil0debae72015-11-12 18:37:00 +00003508 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003509 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003510 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003511 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003512 IfCondition opposite_condition = condition->GetOppositeCondition();
3513 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003514 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003515 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003516 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003517 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003518 // Integer cases.
3519 Register lhs = InputRegisterAt(condition, 0);
3520 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003521
3522 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003523 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003524 if (true_target == nullptr) {
3525 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3526 non_fallthrough_target = false_target;
3527 } else {
3528 arm64_cond = ARM64Condition(condition->GetCondition());
3529 non_fallthrough_target = true_target;
3530 }
3531
Aart Bik086d27e2016-01-20 17:02:00 -08003532 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003533 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003534 switch (arm64_cond) {
3535 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003536 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003537 break;
3538 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003539 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003540 break;
3541 case lt:
3542 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003543 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003544 break;
3545 case ge:
3546 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003547 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003548 break;
3549 default:
3550 // Without the `static_cast` the compiler throws an error for
3551 // `-Werror=sign-promo`.
3552 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3553 }
3554 } else {
3555 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003556 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003557 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003558 }
3559 }
David Brazdil0debae72015-11-12 18:37:00 +00003560
3561 // If neither branch falls through (case 3), the conditional branch to `true_target`
3562 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3563 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003564 __ B(false_target);
3565 }
David Brazdil0debae72015-11-12 18:37:00 +00003566
3567 if (fallthrough_target.IsLinked()) {
3568 __ Bind(&fallthrough_target);
3569 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003570}
3571
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003572void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003574 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003575 locations->SetInAt(0, Location::RequiresRegister());
3576 }
3577}
3578
3579void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003580 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3581 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003582 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3583 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3584 true_target = nullptr;
3585 }
3586 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3587 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3588 false_target = nullptr;
3589 }
David Brazdil0debae72015-11-12 18:37:00 +00003590 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003591}
3592
3593void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3594 LocationSummary* locations = new (GetGraph()->GetArena())
3595 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003596 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003597 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003598 locations->SetInAt(0, Location::RequiresRegister());
3599 }
3600}
3601
3602void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003603 SlowPathCodeARM64* slow_path =
3604 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003605 GenerateTestAndBranch(deoptimize,
3606 /* condition_input_index */ 0,
3607 slow_path->GetEntryLabel(),
3608 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003609}
3610
Mingyao Yang063fc772016-08-02 11:02:54 -07003611void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3612 LocationSummary* locations = new (GetGraph()->GetArena())
3613 LocationSummary(flag, LocationSummary::kNoCall);
3614 locations->SetOut(Location::RequiresRegister());
3615}
3616
3617void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3618 __ Ldr(OutputRegister(flag),
3619 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3620}
3621
David Brazdilc0b601b2016-02-08 14:20:45 +00003622static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3623 return condition->IsCondition() &&
3624 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3625}
3626
Alexandre Rames880f1192016-06-13 16:04:50 +01003627static inline Condition GetConditionForSelect(HCondition* condition) {
3628 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003629 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3630 : ARM64Condition(cond);
3631}
3632
David Brazdil74eb1b22015-12-14 11:44:01 +00003633void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003635 if (Primitive::IsFloatingPointType(select->GetType())) {
3636 locations->SetInAt(0, Location::RequiresFpuRegister());
3637 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003638 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003639 } else {
3640 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3641 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3642 bool is_true_value_constant = cst_true_value != nullptr;
3643 bool is_false_value_constant = cst_false_value != nullptr;
3644 // Ask VIXL whether we should synthesize constants in registers.
3645 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3646 Operand true_op = is_true_value_constant ?
3647 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3648 Operand false_op = is_false_value_constant ?
3649 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3650 bool true_value_in_register = false;
3651 bool false_value_in_register = false;
3652 MacroAssembler::GetCselSynthesisInformation(
3653 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3654 true_value_in_register |= !is_true_value_constant;
3655 false_value_in_register |= !is_false_value_constant;
3656
3657 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3658 : Location::ConstantLocation(cst_true_value));
3659 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3660 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003662 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003663
David Brazdil74eb1b22015-12-14 11:44:01 +00003664 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3665 locations->SetInAt(2, Location::RequiresRegister());
3666 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003667}
3668
3669void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003670 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003671 Condition csel_cond;
3672
3673 if (IsBooleanValueOrMaterializedCondition(cond)) {
3674 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003675 // Use the condition flags set by the previous instruction.
3676 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003677 } else {
3678 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003679 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003680 }
3681 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003682 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003683 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003684 } else {
3685 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003686 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003687 }
3688
Alexandre Rames880f1192016-06-13 16:04:50 +01003689 if (Primitive::IsFloatingPointType(select->GetType())) {
3690 __ Fcsel(OutputFPRegister(select),
3691 InputFPRegisterAt(select, 1),
3692 InputFPRegisterAt(select, 0),
3693 csel_cond);
3694 } else {
3695 __ Csel(OutputRegister(select),
3696 InputOperandAt(select, 1),
3697 InputOperandAt(select, 0),
3698 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003699 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003700}
3701
David Srbecky0cf44932015-12-09 14:09:59 +00003702void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3703 new (GetGraph()->GetArena()) LocationSummary(info);
3704}
3705
David Srbeckyd28f4a02016-03-14 17:14:24 +00003706void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3707 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003708}
3709
3710void CodeGeneratorARM64::GenerateNop() {
3711 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003712}
3713
Alexandre Rames5319def2014-10-23 10:03:10 +01003714void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003715 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003716}
3717
3718void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003719 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003720}
3721
3722void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003723 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003724}
3725
3726void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003727 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003728}
3729
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003730// Temp is used for read barrier.
3731static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3732 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003733 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003734 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3735 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3736 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3737 return 1;
3738 }
3739 return 0;
3740}
3741
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003742// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003743// interface pointer, one for loading the current interface.
3744// The other checks have one temp for loading the object's class.
3745static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3746 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3747 return 3;
3748 }
3749 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003750}
3751
Alexandre Rames67555f72014-11-18 10:55:16 +00003752void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003753 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003755 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003756 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003757 case TypeCheckKind::kExactCheck:
3758 case TypeCheckKind::kAbstractClassCheck:
3759 case TypeCheckKind::kClassHierarchyCheck:
3760 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003761 call_kind =
3762 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003763 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 break;
3765 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003766 case TypeCheckKind::kUnresolvedCheck:
3767 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003768 call_kind = LocationSummary::kCallOnSlowPath;
3769 break;
3770 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003771
Alexandre Rames67555f72014-11-18 10:55:16 +00003772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003773 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003774 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003775 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003776 locations->SetInAt(0, Location::RequiresRegister());
3777 locations->SetInAt(1, Location::RequiresRegister());
3778 // The "out" register is used as a temporary, so it overlaps with the inputs.
3779 // Note that TypeCheckSlowPathARM64 uses this register too.
3780 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003781 // Add temps if necessary for read barriers.
3782 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003783}
3784
3785void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003786 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003787 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003788 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003789 Register obj = InputRegisterAt(instruction, 0);
3790 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003791 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003792 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003793 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3794 DCHECK_LE(num_temps, 1u);
3795 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003796 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3797 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3798 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3799 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003800
Scott Wakeling97c72b72016-06-24 16:19:36 +01003801 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003802 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003803
3804 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003805 // Avoid null check if we know `obj` is not null.
3806 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003807 __ Cbz(obj, &zero);
3808 }
3809
Roland Levillain44015862016-01-22 11:47:17 +00003810 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003811 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003812 // /* HeapReference<Class> */ out = obj->klass_
3813 GenerateReferenceLoadTwoRegisters(instruction,
3814 out_loc,
3815 obj_loc,
3816 class_offset,
3817 maybe_temp_loc,
3818 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 __ Cmp(out, cls);
3820 __ Cset(out, eq);
3821 if (zero.IsLinked()) {
3822 __ B(&done);
3823 }
3824 break;
3825 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003826
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003827 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003828 // /* HeapReference<Class> */ out = obj->klass_
3829 GenerateReferenceLoadTwoRegisters(instruction,
3830 out_loc,
3831 obj_loc,
3832 class_offset,
3833 maybe_temp_loc,
3834 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835 // If the class is abstract, we eagerly fetch the super class of the
3836 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003837 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003838 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003839 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003840 GenerateReferenceLoadOneRegister(instruction,
3841 out_loc,
3842 super_offset,
3843 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003844 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003845 // If `out` is null, we use it for the result, and jump to `done`.
3846 __ Cbz(out, &done);
3847 __ Cmp(out, cls);
3848 __ B(ne, &loop);
3849 __ Mov(out, 1);
3850 if (zero.IsLinked()) {
3851 __ B(&done);
3852 }
3853 break;
3854 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003856 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003857 // /* HeapReference<Class> */ out = obj->klass_
3858 GenerateReferenceLoadTwoRegisters(instruction,
3859 out_loc,
3860 obj_loc,
3861 class_offset,
3862 maybe_temp_loc,
3863 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003864 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003866 __ Bind(&loop);
3867 __ Cmp(out, cls);
3868 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003870 GenerateReferenceLoadOneRegister(instruction,
3871 out_loc,
3872 super_offset,
3873 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003874 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003875 __ Cbnz(out, &loop);
3876 // If `out` is null, we use it for the result, and jump to `done`.
3877 __ B(&done);
3878 __ Bind(&success);
3879 __ Mov(out, 1);
3880 if (zero.IsLinked()) {
3881 __ B(&done);
3882 }
3883 break;
3884 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003886 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003887 // /* HeapReference<Class> */ out = obj->klass_
3888 GenerateReferenceLoadTwoRegisters(instruction,
3889 out_loc,
3890 obj_loc,
3891 class_offset,
3892 maybe_temp_loc,
3893 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003894 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003895 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003896 __ Cmp(out, cls);
3897 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003898 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003899 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003900 GenerateReferenceLoadOneRegister(instruction,
3901 out_loc,
3902 component_offset,
3903 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003904 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 // If `out` is null, we use it for the result, and jump to `done`.
3906 __ Cbz(out, &done);
3907 __ Ldrh(out, HeapOperand(out, primitive_offset));
3908 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3909 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003910 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003911 __ Mov(out, 1);
3912 __ B(&done);
3913 break;
3914 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003917 // No read barrier since the slow path will retry upon failure.
3918 // /* HeapReference<Class> */ out = obj->klass_
3919 GenerateReferenceLoadTwoRegisters(instruction,
3920 out_loc,
3921 obj_loc,
3922 class_offset,
3923 maybe_temp_loc,
3924 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 __ Cmp(out, cls);
3926 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003927 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3928 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003929 codegen_->AddSlowPath(slow_path);
3930 __ B(ne, slow_path->GetEntryLabel());
3931 __ Mov(out, 1);
3932 if (zero.IsLinked()) {
3933 __ B(&done);
3934 }
3935 break;
3936 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937
Calin Juravle98893e12015-10-02 21:05:03 +01003938 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 case TypeCheckKind::kInterfaceCheck: {
3940 // Note that we indeed only call on slow path, but we always go
3941 // into the slow path for the unresolved and interface check
3942 // cases.
3943 //
3944 // We cannot directly call the InstanceofNonTrivial runtime
3945 // entry point without resorting to a type checking slow path
3946 // here (i.e. by calling InvokeRuntime directly), as it would
3947 // require to assign fixed registers for the inputs of this
3948 // HInstanceOf instruction (following the runtime calling
3949 // convention), which might be cluttered by the potential first
3950 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003951 //
3952 // TODO: Introduce a new runtime entry point taking the object
3953 // to test (instead of its class) as argument, and let it deal
3954 // with the read barrier issues. This will let us refactor this
3955 // case of the `switch` code as it was previously (with a direct
3956 // call to the runtime not using a type checking slow path).
3957 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958 DCHECK(locations->OnlyCallsOnSlowPath());
3959 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3960 /* is_fatal */ false);
3961 codegen_->AddSlowPath(slow_path);
3962 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 if (zero.IsLinked()) {
3964 __ B(&done);
3965 }
3966 break;
3967 }
3968 }
3969
3970 if (zero.IsLinked()) {
3971 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003972 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003973 }
3974
3975 if (done.IsLinked()) {
3976 __ Bind(&done);
3977 }
3978
3979 if (slow_path != nullptr) {
3980 __ Bind(slow_path->GetExitLabel());
3981 }
3982}
3983
3984void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3985 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3986 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3987
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003988 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3989 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 case TypeCheckKind::kExactCheck:
3991 case TypeCheckKind::kAbstractClassCheck:
3992 case TypeCheckKind::kClassHierarchyCheck:
3993 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003994 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3995 LocationSummary::kCallOnSlowPath :
3996 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003997 break;
3998 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999 case TypeCheckKind::kUnresolvedCheck:
4000 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004001 call_kind = LocationSummary::kCallOnSlowPath;
4002 break;
4003 }
4004
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4006 locations->SetInAt(0, Location::RequiresRegister());
4007 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004008 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4009 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010}
4011
4012void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004013 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004015 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 Register obj = InputRegisterAt(instruction, 0);
4017 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004018 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4019 DCHECK_GE(num_temps, 1u);
4020 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004022 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4023 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004024 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004025 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4026 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4027 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4028 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4029 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4030 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4031 const uint32_t object_array_data_offset =
4032 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004033
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004034 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004035 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4036 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4037 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004038 if (!kEmitCompilerReadBarrier) {
4039 is_type_check_slow_path_fatal =
4040 (type_check_kind == TypeCheckKind::kExactCheck ||
4041 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4042 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4043 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4044 !instruction->CanThrowIntoCatchBlock();
4045 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004046 SlowPathCodeARM64* type_check_slow_path =
4047 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4048 is_type_check_slow_path_fatal);
4049 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004050
Scott Wakeling97c72b72016-06-24 16:19:36 +01004051 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004052 // Avoid null check if we know obj is not null.
4053 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004054 __ Cbz(obj, &done);
4055 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004056
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004058 case TypeCheckKind::kExactCheck:
4059 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004060 // /* HeapReference<Class> */ temp = obj->klass_
4061 GenerateReferenceLoadTwoRegisters(instruction,
4062 temp_loc,
4063 obj_loc,
4064 class_offset,
4065 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004066 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004067
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004068 __ Cmp(temp, cls);
4069 // Jump to slow path for throwing the exception or doing a
4070 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004071 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004072 break;
4073 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004074
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004075 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004076 // /* HeapReference<Class> */ temp = obj->klass_
4077 GenerateReferenceLoadTwoRegisters(instruction,
4078 temp_loc,
4079 obj_loc,
4080 class_offset,
4081 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004082 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004083
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 // If the class is abstract, we eagerly fetch the super class of the
4085 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004086 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004087 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004089 GenerateReferenceLoadOneRegister(instruction,
4090 temp_loc,
4091 super_offset,
4092 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004093 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004095 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4096 // exception.
4097 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4098 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004099 __ Cmp(temp, cls);
4100 __ B(ne, &loop);
4101 break;
4102 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004104 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004105 // /* HeapReference<Class> */ temp = obj->klass_
4106 GenerateReferenceLoadTwoRegisters(instruction,
4107 temp_loc,
4108 obj_loc,
4109 class_offset,
4110 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004111 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004112
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004114 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 __ Bind(&loop);
4116 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004117 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004119 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004120 GenerateReferenceLoadOneRegister(instruction,
4121 temp_loc,
4122 super_offset,
4123 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004124 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004125
4126 // If the class reference currently in `temp` is not null, jump
4127 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004128 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004129 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004130 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131 break;
4132 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004134 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004135 // /* HeapReference<Class> */ temp = obj->klass_
4136 GenerateReferenceLoadTwoRegisters(instruction,
4137 temp_loc,
4138 obj_loc,
4139 class_offset,
4140 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004141 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004142
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004143 // Do an exact check.
4144 __ Cmp(temp, cls);
4145 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146
4147 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004149 GenerateReferenceLoadOneRegister(instruction,
4150 temp_loc,
4151 component_offset,
4152 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004153 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004154
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004155 // If the component type is null, jump to the slow path to throw the exception.
4156 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4157 // Otherwise, the object is indeed an array. Further check that this component type is not a
4158 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004159 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4160 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004161 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162 break;
4163 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004164
Calin Juravle98893e12015-10-02 21:05:03 +01004165 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167 //
4168 // We cannot directly call the CheckCast runtime entry point
4169 // without resorting to a type checking slow path here (i.e. by
4170 // calling InvokeRuntime directly), as it would require to
4171 // assign fixed registers for the inputs of this HInstanceOf
4172 // instruction (following the runtime calling convention), which
4173 // might be cluttered by the potential first read barrier
4174 // emission at the beginning of this method.
4175 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004176 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177 case TypeCheckKind::kInterfaceCheck: {
4178 // /* HeapReference<Class> */ temp = obj->klass_
4179 GenerateReferenceLoadTwoRegisters(instruction,
4180 temp_loc,
4181 obj_loc,
4182 class_offset,
4183 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004184 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004185
4186 // /* HeapReference<Class> */ temp = temp->iftable_
4187 GenerateReferenceLoadTwoRegisters(instruction,
4188 temp_loc,
4189 temp_loc,
4190 iftable_offset,
4191 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004192 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004193 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004194 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004195 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004196 vixl::aarch64::Label start_loop;
4197 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004198 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4200 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004201 // Go to next interface.
4202 __ Add(temp, temp, 2 * kHeapReferenceSize);
4203 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004204 // Compare the classes and continue the loop if they do not match.
4205 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4206 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004207 break;
4208 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004210 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004211
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004213}
4214
Alexandre Rames5319def2014-10-23 10:03:10 +01004215void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4217 locations->SetOut(Location::ConstantLocation(constant));
4218}
4219
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004220void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004221 // Will be generated at use site.
4222}
4223
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004224void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4226 locations->SetOut(Location::ConstantLocation(constant));
4227}
4228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004229void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004230 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004231}
4232
Calin Juravle175dc732015-08-25 15:42:32 +01004233void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4234 // The trampoline uses the same calling convention as dex calling conventions,
4235 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4236 // the method_idx.
4237 HandleInvoke(invoke);
4238}
4239
4240void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4241 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4242}
4243
Alexandre Rames5319def2014-10-23 10:03:10 +01004244void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004245 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004246 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004247}
4248
Alexandre Rames67555f72014-11-18 10:55:16 +00004249void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4250 HandleInvoke(invoke);
4251}
4252
4253void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4254 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255 LocationSummary* locations = invoke->GetLocations();
4256 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004258 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004259 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004260
4261 // The register ip1 is required to be used for the hidden argument in
4262 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004263 MacroAssembler* masm = GetVIXLAssembler();
4264 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004265 scratch_scope.Exclude(ip1);
4266 __ Mov(ip1, invoke->GetDexMethodIndex());
4267
Artem Serov914d7a82017-02-07 14:33:49 +00004268 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004269 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004270 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004271 {
4272 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4273 // /* HeapReference<Class> */ temp = temp->klass_
4274 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4275 codegen_->MaybeRecordImplicitNullCheck(invoke);
4276 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004277 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004278 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004280 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004281 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004282 }
Artem Serov914d7a82017-02-07 14:33:49 +00004283
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284 // Instead of simply (possibly) unpoisoning `temp` here, we should
4285 // emit a read barrier for the previous class reference load.
4286 // However this is not required in practice, as this is an
4287 // intermediate/temporary reference and because the current
4288 // concurrent copying collector keeps the from-space memory
4289 // intact/accessible until the end of the marking phase (the
4290 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004291 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004292 __ Ldr(temp,
4293 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4294 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004295 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004296 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004297 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004298 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004299 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004300
4301 {
4302 // Ensure the pc position is recorded immediately after the `blr` instruction.
4303 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4304
4305 // lr();
4306 __ blr(lr);
4307 DCHECK(!codegen_->IsLeafMethod());
4308 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4309 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004310}
4311
4312void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004313 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004314 if (intrinsic.TryDispatch(invoke)) {
4315 return;
4316 }
4317
Alexandre Rames67555f72014-11-18 10:55:16 +00004318 HandleInvoke(invoke);
4319}
4320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004321void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004322 // Explicit clinit checks triggered by static invokes must have been pruned by
4323 // art::PrepareForRegisterAllocation.
4324 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004325
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004326 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004327 if (intrinsic.TryDispatch(invoke)) {
4328 return;
4329 }
4330
Alexandre Rames67555f72014-11-18 10:55:16 +00004331 HandleInvoke(invoke);
4332}
4333
Andreas Gampe878d58c2015-01-15 23:24:00 -08004334static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4335 if (invoke->GetLocations()->Intrinsified()) {
4336 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4337 intrinsic.Dispatch(invoke);
4338 return true;
4339 }
4340 return false;
4341}
4342
Vladimir Markodc151b22015-10-15 18:02:30 +01004343HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4344 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004345 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004346 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004347 return desired_dispatch_info;
4348}
4349
TatWai Chongd8c052a2016-11-02 16:12:48 +08004350Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4351 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004352 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004353 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4354 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004355 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4356 uint32_t offset =
4357 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004358 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004359 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004360 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004361 }
Vladimir Marko58155012015-08-19 12:49:41 +00004362 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004363 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004364 break;
4365 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4366 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004367 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004368 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004369 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4370 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004371 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004372 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004373 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004374 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004375 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004376 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004377 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004378 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004379 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004380 }
Vladimir Marko58155012015-08-19 12:49:41 +00004381 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004382 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004383 Register reg = XRegisterFrom(temp);
4384 Register method_reg;
4385 if (current_method.IsRegister()) {
4386 method_reg = XRegisterFrom(current_method);
4387 } else {
4388 DCHECK(invoke->GetLocations()->Intrinsified());
4389 DCHECK(!current_method.IsValid());
4390 method_reg = reg;
4391 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4392 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004393
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004394 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004395 __ Ldr(reg.X(),
4396 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004397 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004398 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004399 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4400 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004401 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4402 break;
4403 }
4404 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004405 return callee_method;
4406}
4407
4408void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4409 // All registers are assumed to be correctly set up.
4410 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004411
4412 switch (invoke->GetCodePtrLocation()) {
4413 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4414 __ Bl(&frame_entry_label_);
4415 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004416 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4417 // LR = callee_method->entry_point_from_quick_compiled_code_;
4418 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004419 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004420 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004421 {
4422 // To ensure that the pc position is recorded immediately after the `blr` instruction
4423 // BLR must be the last instruction emitted in this function.
4424 // Recording the pc will occur right after returning from this function.
4425 ExactAssemblyScope eas(GetVIXLAssembler(),
4426 kInstructionSize,
4427 CodeBufferCheckScope::kExactSize);
4428 // lr()
4429 __ blr(lr);
4430 }
Vladimir Marko58155012015-08-19 12:49:41 +00004431 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004432 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004433
Andreas Gampe878d58c2015-01-15 23:24:00 -08004434 DCHECK(!IsLeafMethod());
4435}
4436
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004437void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004438 // Use the calling convention instead of the location of the receiver, as
4439 // intrinsics may have put the receiver in a different register. In the intrinsics
4440 // slow path, the arguments have been moved to the right place, so here we are
4441 // guaranteed that the receiver is the first register of the calling convention.
4442 InvokeDexCallingConvention calling_convention;
4443 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004444 Register temp = XRegisterFrom(temp_in);
4445 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4446 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4447 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004448 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004449
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004450 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004451
4452 {
4453 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4454 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4455 // /* HeapReference<Class> */ temp = receiver->klass_
4456 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4457 MaybeRecordImplicitNullCheck(invoke);
4458 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004459 // Instead of simply (possibly) unpoisoning `temp` here, we should
4460 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004461 // intermediate/temporary reference and because the current
4462 // concurrent copying collector keeps the from-space memory
4463 // intact/accessible until the end of the marking phase (the
4464 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004465 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4466 // temp = temp->GetMethodAt(method_offset);
4467 __ Ldr(temp, MemOperand(temp, method_offset));
4468 // lr = temp->GetEntryPoint();
4469 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004470 {
4471 // To ensure that the pc position is recorded immediately after the `blr` instruction
4472 // BLR should be the last instruction emitted in this function.
4473 // Recording the pc will occur right after returning from this function.
4474 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4475 // lr();
4476 __ blr(lr);
4477 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004478}
4479
Orion Hodsonac141392017-01-13 11:53:47 +00004480void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4481 HandleInvoke(invoke);
4482}
4483
4484void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4485 codegen_->GenerateInvokePolymorphicCall(invoke);
4486}
4487
Scott Wakeling97c72b72016-06-24 16:19:36 +01004488vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4489 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004490 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004491 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004492 return
4493 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004494}
4495
Scott Wakeling97c72b72016-06-24 16:19:36 +01004496vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4497 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004498 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004499 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004500 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004501}
4502
Vladimir Marko1998cd02017-01-13 13:02:58 +00004503vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4504 const DexFile& dex_file,
4505 dex::TypeIndex type_index,
4506 vixl::aarch64::Label* adrp_label) {
4507 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4508}
4509
Scott Wakeling97c72b72016-06-24 16:19:36 +01004510vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4511 const DexFile& dex_file,
4512 uint32_t element_offset,
4513 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004514 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4515}
4516
Scott Wakeling97c72b72016-06-24 16:19:36 +01004517vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4518 const DexFile& dex_file,
4519 uint32_t offset_or_index,
4520 vixl::aarch64::Label* adrp_label,
4521 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004522 // Add a patch entry and return the label.
4523 patches->emplace_back(dex_file, offset_or_index);
4524 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004525 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4527 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4528 return label;
4529}
4530
Scott Wakeling97c72b72016-06-24 16:19:36 +01004531vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004532 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004533 return boot_image_string_patches_.GetOrCreate(
4534 StringReference(&dex_file, string_index),
4535 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4536}
4537
Scott Wakeling97c72b72016-06-24 16:19:36 +01004538vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004539 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004540 return boot_image_type_patches_.GetOrCreate(
4541 TypeReference(&dex_file, type_index),
4542 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4543}
4544
Scott Wakeling97c72b72016-06-24 16:19:36 +01004545vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4546 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004547 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004548}
4549
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004550vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004551 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4552 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4553 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004554 return jit_string_patches_.GetOrCreate(
4555 StringReference(&dex_file, string_index),
4556 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4557}
4558
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004559vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004560 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4561 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4562 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004563 return jit_class_patches_.GetOrCreate(
4564 TypeReference(&dex_file, type_index),
4565 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4566}
4567
Vladimir Markoaad75c62016-10-03 08:46:48 +00004568void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4569 vixl::aarch64::Register reg) {
4570 DCHECK(reg.IsX());
4571 SingleEmissionCheckScope guard(GetVIXLAssembler());
4572 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004573 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004574}
4575
4576void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4577 vixl::aarch64::Register out,
4578 vixl::aarch64::Register base) {
4579 DCHECK(out.IsX());
4580 DCHECK(base.IsX());
4581 SingleEmissionCheckScope guard(GetVIXLAssembler());
4582 __ Bind(fixup_label);
4583 __ add(out, base, Operand(/* offset placeholder */ 0));
4584}
4585
4586void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4587 vixl::aarch64::Register out,
4588 vixl::aarch64::Register base) {
4589 DCHECK(base.IsX());
4590 SingleEmissionCheckScope guard(GetVIXLAssembler());
4591 __ Bind(fixup_label);
4592 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4593}
4594
4595template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4596inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4597 const ArenaDeque<PcRelativePatchInfo>& infos,
4598 ArenaVector<LinkerPatch>* linker_patches) {
4599 for (const PcRelativePatchInfo& info : infos) {
4600 linker_patches->push_back(Factory(info.label.GetLocation(),
4601 &info.target_dex_file,
4602 info.pc_insn_label->GetLocation(),
4603 info.offset_or_index));
4604 }
4605}
4606
Vladimir Marko58155012015-08-19 12:49:41 +00004607void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4608 DCHECK(linker_patches->empty());
4609 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004610 pc_relative_dex_cache_patches_.size() +
4611 boot_image_string_patches_.size() +
4612 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004613 boot_image_type_patches_.size() +
4614 pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00004615 type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004616 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004618 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004619 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004620 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004621 info.offset_or_index));
4622 }
4623 for (const auto& entry : boot_image_string_patches_) {
4624 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004625 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4626 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004628 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004629 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004631 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004632 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4633 linker_patches);
4634 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004635 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4636 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004637 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4638 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004639 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004640 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4641 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004642 for (const auto& entry : boot_image_type_patches_) {
4643 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4645 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004646 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004647 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004648 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004649 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004650}
4651
Scott Wakeling97c72b72016-06-24 16:19:36 +01004652vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004653 Uint32ToLiteralMap* map) {
4654 return map->GetOrCreate(
4655 value,
4656 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4657}
4658
Scott Wakeling97c72b72016-06-24 16:19:36 +01004659vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004660 return uint64_literals_.GetOrCreate(
4661 value,
4662 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004663}
4664
Scott Wakeling97c72b72016-06-24 16:19:36 +01004665vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004666 MethodReference target_method,
4667 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004668 return map->GetOrCreate(
4669 target_method,
4670 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004671}
4672
Andreas Gampe878d58c2015-01-15 23:24:00 -08004673void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004674 // Explicit clinit checks triggered by static invokes must have been pruned by
4675 // art::PrepareForRegisterAllocation.
4676 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004677
Andreas Gampe878d58c2015-01-15 23:24:00 -08004678 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4679 return;
4680 }
4681
Artem Serov914d7a82017-02-07 14:33:49 +00004682 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4683 // are no pools emitted.
4684 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004685 LocationSummary* locations = invoke->GetLocations();
4686 codegen_->GenerateStaticOrDirectCall(
4687 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004688 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004689}
4690
4691void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004692 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4693 return;
4694 }
4695
Artem Serov914d7a82017-02-07 14:33:49 +00004696 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4697 // are no pools emitted.
4698 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004699 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004700 DCHECK(!codegen_->IsLeafMethod());
4701 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4702}
4703
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004704HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4705 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004706 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004707 case HLoadClass::LoadKind::kInvalid:
4708 LOG(FATAL) << "UNREACHABLE";
4709 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004710 case HLoadClass::LoadKind::kReferrersClass:
4711 break;
4712 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4713 DCHECK(!GetCompilerOptions().GetCompilePic());
4714 break;
4715 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4716 DCHECK(GetCompilerOptions().GetCompilePic());
4717 break;
4718 case HLoadClass::LoadKind::kBootImageAddress:
4719 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004720 case HLoadClass::LoadKind::kBssEntry:
4721 DCHECK(!Runtime::Current()->UseJitCompilation());
4722 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004723 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004724 DCHECK(Runtime::Current()->UseJitCompilation());
4725 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004726 case HLoadClass::LoadKind::kDexCacheViaMethod:
4727 break;
4728 }
4729 return desired_class_load_kind;
4730}
4731
Alexandre Rames67555f72014-11-18 10:55:16 +00004732void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004733 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4734 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004735 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004736 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004737 cls,
4738 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004739 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004740 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004741 return;
4742 }
Vladimir Marko41559982017-01-06 14:04:23 +00004743 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004744
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004745 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4746 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004747 ? LocationSummary::kCallOnSlowPath
4748 : LocationSummary::kNoCall;
4749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004750 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004751 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004752 }
4753
Vladimir Marko41559982017-01-06 14:04:23 +00004754 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004755 locations->SetInAt(0, Location::RequiresRegister());
4756 }
4757 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004758 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4759 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4760 // Rely on the type resolution or initialization and marking to save everything we need.
4761 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4762 // to the custom calling convention) or by marking, so we shall use IP1.
4763 RegisterSet caller_saves = RegisterSet::Empty();
4764 InvokeRuntimeCallingConvention calling_convention;
4765 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4766 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4767 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4768 Primitive::kPrimNot).GetCode());
4769 locations->SetCustomSlowPathCallerSaves(caller_saves);
4770 } else {
4771 // For non-Baker read barrier we have a temp-clobbering call.
4772 }
4773 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004774}
4775
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004776// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4777// move.
4778void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004779 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4780 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4781 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004782 return;
4783 }
Vladimir Marko41559982017-01-06 14:04:23 +00004784 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004785
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004786 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004787 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004788 Register bss_entry_temp;
4789 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004790
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004791 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4792 ? kWithoutReadBarrier
4793 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004794 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004795 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004796 case HLoadClass::LoadKind::kReferrersClass: {
4797 DCHECK(!cls->CanCallRuntime());
4798 DCHECK(!cls->MustGenerateClinitCheck());
4799 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4800 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004801 GenerateGcRootFieldLoad(cls,
4802 out_loc,
4803 current_method,
4804 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004805 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004806 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004807 break;
4808 }
4809 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004810 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004811 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4812 cls->GetTypeIndex()));
4813 break;
4814 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004815 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004816 // Add ADRP with its PC-relative type patch.
4817 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004818 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004819 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004820 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004821 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004822 vixl::aarch64::Label* add_label =
4823 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004824 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004825 break;
4826 }
4827 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004828 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004829 uint32_t address = dchecked_integral_cast<uint32_t>(
4830 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4831 DCHECK_NE(address, 0u);
4832 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004833 break;
4834 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004835 case HLoadClass::LoadKind::kBssEntry: {
4836 // Add ADRP with its PC-relative Class .bss entry patch.
4837 const DexFile& dex_file = cls->GetDexFile();
4838 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004839 // We can go to slow path even with non-zero reference and in that case marking
4840 // can clobber IP0, so we need to use IP1 which shall be preserved.
4841 bss_entry_temp = ip1;
4842 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4843 temps.Exclude(bss_entry_temp);
4844 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4845 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004846 // Add LDR with its PC-relative Class patch.
4847 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004848 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004849 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4850 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004851 out_loc,
4852 bss_entry_temp,
4853 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004854 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004855 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004856 generate_null_check = true;
4857 break;
4858 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004859 case HLoadClass::LoadKind::kJitTableAddress: {
4860 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4861 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004862 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004863 GenerateGcRootFieldLoad(cls,
4864 out_loc,
4865 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004866 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004867 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004868 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 break;
4870 }
Vladimir Marko41559982017-01-06 14:04:23 +00004871 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004872 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004873 LOG(FATAL) << "UNREACHABLE";
4874 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004875 }
4876
Vladimir Markoea4c1262017-02-06 19:59:33 +00004877 bool do_clinit = cls->MustGenerateClinitCheck();
4878 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004879 DCHECK(cls->CanCallRuntime());
4880 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004881 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004882 codegen_->AddSlowPath(slow_path);
4883 if (generate_null_check) {
4884 __ Cbz(out, slow_path->GetEntryLabel());
4885 }
4886 if (cls->MustGenerateClinitCheck()) {
4887 GenerateClassInitializationCheck(slow_path, out);
4888 } else {
4889 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004890 }
4891 }
4892}
4893
David Brazdilcb1c0552015-08-04 16:22:25 +01004894static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004895 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004896}
4897
Alexandre Rames67555f72014-11-18 10:55:16 +00004898void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4899 LocationSummary* locations =
4900 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4901 locations->SetOut(Location::RequiresRegister());
4902}
4903
4904void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004905 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4906}
4907
4908void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4909 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4910}
4911
4912void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4913 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004914}
4915
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004916HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4917 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004918 switch (desired_string_load_kind) {
4919 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4920 DCHECK(!GetCompilerOptions().GetCompilePic());
4921 break;
4922 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4923 DCHECK(GetCompilerOptions().GetCompilePic());
4924 break;
4925 case HLoadString::LoadKind::kBootImageAddress:
4926 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004927 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004928 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004929 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004930 case HLoadString::LoadKind::kJitTableAddress:
4931 DCHECK(Runtime::Current()->UseJitCompilation());
4932 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004933 case HLoadString::LoadKind::kDexCacheViaMethod:
4934 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004935 }
4936 return desired_string_load_kind;
4937}
4938
Alexandre Rames67555f72014-11-18 10:55:16 +00004939void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004940 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004941 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004942 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004943 InvokeRuntimeCallingConvention calling_convention;
4944 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4945 } else {
4946 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004947 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4948 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004949 // Rely on the pResolveString and marking to save everything we need.
4950 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4951 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004952 RegisterSet caller_saves = RegisterSet::Empty();
4953 InvokeRuntimeCallingConvention calling_convention;
4954 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4955 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4956 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4957 Primitive::kPrimNot).GetCode());
4958 locations->SetCustomSlowPathCallerSaves(caller_saves);
4959 } else {
4960 // For non-Baker read barrier we have a temp-clobbering call.
4961 }
4962 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004963 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004964}
4965
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004966// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4967// move.
4968void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004969 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004970 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004971
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004972 switch (load->GetLoadKind()) {
4973 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004974 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4975 load->GetStringIndex()));
4976 return; // No dex cache slow path.
4977 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004978 // Add ADRP with its PC-relative String patch.
4979 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004980 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004981 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004982 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004983 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004984 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004985 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004986 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004987 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004988 return; // No dex cache slow path.
4989 }
4990 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004991 uint32_t address = dchecked_integral_cast<uint32_t>(
4992 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4993 DCHECK_NE(address, 0u);
4994 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004995 return; // No dex cache slow path.
4996 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004997 case HLoadString::LoadKind::kBssEntry: {
4998 // Add ADRP with its PC-relative String .bss entry patch.
4999 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005000 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005001 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005002 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
5003 // that's when we need the slow path. But let's not rely on such details and use IP1.
5004 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005005 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005006 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005007 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005008 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005009 // Add LDR with its PC-relative String patch.
5010 vixl::aarch64::Label* ldr_label =
5011 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005012 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005013 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005014 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005015 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005016 /* offset placeholder */ 0u,
5017 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005018 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005019 SlowPathCodeARM64* slow_path =
5020 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005021 codegen_->AddSlowPath(slow_path);
5022 __ Cbz(out.X(), slow_path->GetEntryLabel());
5023 __ Bind(slow_path->GetExitLabel());
5024 return;
5025 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005026 case HLoadString::LoadKind::kJitTableAddress: {
5027 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005028 load->GetStringIndex(),
5029 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005030 GenerateGcRootFieldLoad(load,
5031 out_loc,
5032 out.X(),
5033 /* offset */ 0,
5034 /* fixup_label */ nullptr,
5035 kCompilerReadBarrierOption);
5036 return;
5037 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005038 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005039 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005041
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005042 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005043 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005044 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005045 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005046 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5047 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005048}
5049
Alexandre Rames5319def2014-10-23 10:03:10 +01005050void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5051 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5052 locations->SetOut(Location::ConstantLocation(constant));
5053}
5054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005055void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005056 // Will be generated at use site.
5057}
5058
Alexandre Rames67555f72014-11-18 10:55:16 +00005059void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5060 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005062 InvokeRuntimeCallingConvention calling_convention;
5063 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5064}
5065
5066void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005067 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005068 instruction,
5069 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005070 if (instruction->IsEnter()) {
5071 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5072 } else {
5073 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5074 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005075}
5076
Alexandre Rames42d641b2014-10-27 14:00:51 +00005077void LocationsBuilderARM64::VisitMul(HMul* mul) {
5078 LocationSummary* locations =
5079 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5080 switch (mul->GetResultType()) {
5081 case Primitive::kPrimInt:
5082 case Primitive::kPrimLong:
5083 locations->SetInAt(0, Location::RequiresRegister());
5084 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005086 break;
5087
5088 case Primitive::kPrimFloat:
5089 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005090 locations->SetInAt(0, Location::RequiresFpuRegister());
5091 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005093 break;
5094
5095 default:
5096 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5097 }
5098}
5099
5100void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5101 switch (mul->GetResultType()) {
5102 case Primitive::kPrimInt:
5103 case Primitive::kPrimLong:
5104 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5105 break;
5106
5107 case Primitive::kPrimFloat:
5108 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005109 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005110 break;
5111
5112 default:
5113 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5114 }
5115}
5116
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005117void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5118 LocationSummary* locations =
5119 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5120 switch (neg->GetResultType()) {
5121 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005122 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005123 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005125 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005126
5127 case Primitive::kPrimFloat:
5128 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005129 locations->SetInAt(0, Location::RequiresFpuRegister());
5130 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005131 break;
5132
5133 default:
5134 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5135 }
5136}
5137
5138void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5139 switch (neg->GetResultType()) {
5140 case Primitive::kPrimInt:
5141 case Primitive::kPrimLong:
5142 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5143 break;
5144
5145 case Primitive::kPrimFloat:
5146 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005147 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005148 break;
5149
5150 default:
5151 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5152 }
5153}
5154
5155void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5156 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005157 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005158 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005159 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005160 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5161 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005162}
5163
5164void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005165 // Note: if heap poisoning is enabled, the entry point takes cares
5166 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005167 QuickEntrypointEnum entrypoint =
5168 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5169 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005170 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005171}
5172
Alexandre Rames5319def2014-10-23 10:03:10 +01005173void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5174 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005176 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005177 if (instruction->IsStringAlloc()) {
5178 locations->AddTemp(LocationFrom(kArtMethodRegister));
5179 } else {
5180 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005181 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005182 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5183}
5184
5185void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005186 // Note: if heap poisoning is enabled, the entry point takes cares
5187 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005188 if (instruction->IsStringAlloc()) {
5189 // String is allocated through StringFactory. Call NewEmptyString entry point.
5190 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005191 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005192 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5193 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005194
5195 {
5196 // Ensure the pc position is recorded immediately after the `blr` instruction.
5197 ExactAssemblyScope eas(GetVIXLAssembler(),
5198 kInstructionSize,
5199 CodeBufferCheckScope::kExactSize);
5200 __ blr(lr);
5201 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5202 }
David Brazdil6de19382016-01-08 17:37:10 +00005203 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005204 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005205 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005206 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005207}
5208
5209void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005211 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005213}
5214
5215void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005216 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005217 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005218 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005219 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005220 break;
5221
5222 default:
5223 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5224 }
5225}
5226
David Brazdil66d126e2015-04-03 16:02:44 +01005227void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5229 locations->SetInAt(0, Location::RequiresRegister());
5230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5231}
5232
5233void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005234 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005235}
5236
Alexandre Rames5319def2014-10-23 10:03:10 +01005237void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005238 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5239 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005240}
5241
Calin Juravle2ae48182016-03-16 14:05:09 +00005242void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5243 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005244 return;
5245 }
Artem Serov914d7a82017-02-07 14:33:49 +00005246 {
5247 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5248 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5249 Location obj = instruction->GetLocations()->InAt(0);
5250 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5251 RecordPcInfo(instruction, instruction->GetDexPc());
5252 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005253}
5254
Calin Juravle2ae48182016-03-16 14:05:09 +00005255void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005256 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005257 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005258
5259 LocationSummary* locations = instruction->GetLocations();
5260 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005261
5262 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005263}
5264
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005265void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005266 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005267}
5268
Alexandre Rames67555f72014-11-18 10:55:16 +00005269void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5270 HandleBinaryOp(instruction);
5271}
5272
5273void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5274 HandleBinaryOp(instruction);
5275}
5276
Alexandre Rames3e69f162014-12-10 10:36:50 +00005277void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5278 LOG(FATAL) << "Unreachable";
5279}
5280
5281void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5282 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5283}
5284
Alexandre Rames5319def2014-10-23 10:03:10 +01005285void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5287 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5288 if (location.IsStackSlot()) {
5289 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5290 } else if (location.IsDoubleStackSlot()) {
5291 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5292 }
5293 locations->SetOut(location);
5294}
5295
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005296void InstructionCodeGeneratorARM64::VisitParameterValue(
5297 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005298 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005299}
5300
5301void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5302 LocationSummary* locations =
5303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005304 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005305}
5306
5307void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5308 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5309 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005310}
5311
5312void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005314 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005315 locations->SetInAt(i, Location::Any());
5316 }
5317 locations->SetOut(Location::Any());
5318}
5319
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005320void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005321 LOG(FATAL) << "Unreachable";
5322}
5323
Serban Constantinescu02164b32014-11-13 14:05:07 +00005324void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005325 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005326 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005327 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5328 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5330
5331 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005332 case Primitive::kPrimInt:
5333 case Primitive::kPrimLong:
5334 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005335 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5337 break;
5338
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005339 case Primitive::kPrimFloat:
5340 case Primitive::kPrimDouble: {
5341 InvokeRuntimeCallingConvention calling_convention;
5342 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5343 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5344 locations->SetOut(calling_convention.GetReturnLocation(type));
5345
5346 break;
5347 }
5348
Serban Constantinescu02164b32014-11-13 14:05:07 +00005349 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005350 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005351 }
5352}
5353
5354void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5355 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005356
Serban Constantinescu02164b32014-11-13 14:05:07 +00005357 switch (type) {
5358 case Primitive::kPrimInt:
5359 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005360 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005361 break;
5362 }
5363
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005364 case Primitive::kPrimFloat:
5365 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005366 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5367 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005368 if (type == Primitive::kPrimFloat) {
5369 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5370 } else {
5371 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5372 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005373 break;
5374 }
5375
Serban Constantinescu02164b32014-11-13 14:05:07 +00005376 default:
5377 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005378 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005379 }
5380}
5381
Calin Juravle27df7582015-04-17 19:12:31 +01005382void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5383 memory_barrier->SetLocations(nullptr);
5384}
5385
5386void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005387 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005388}
5389
Alexandre Rames5319def2014-10-23 10:03:10 +01005390void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5392 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005393 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005394}
5395
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005396void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005397 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005398}
5399
5400void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5401 instruction->SetLocations(nullptr);
5402}
5403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005404void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005405 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005406}
5407
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005408void LocationsBuilderARM64::VisitRor(HRor* ror) {
5409 HandleBinaryOp(ror);
5410}
5411
5412void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5413 HandleBinaryOp(ror);
5414}
5415
Serban Constantinescu02164b32014-11-13 14:05:07 +00005416void LocationsBuilderARM64::VisitShl(HShl* shl) {
5417 HandleShift(shl);
5418}
5419
5420void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5421 HandleShift(shl);
5422}
5423
5424void LocationsBuilderARM64::VisitShr(HShr* shr) {
5425 HandleShift(shr);
5426}
5427
5428void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5429 HandleShift(shr);
5430}
5431
Alexandre Rames5319def2014-10-23 10:03:10 +01005432void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005433 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005434}
5435
5436void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005437 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005438}
5439
Alexandre Rames67555f72014-11-18 10:55:16 +00005440void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005441 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005442}
5443
5444void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005445 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005446}
5447
5448void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005449 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005450}
5451
Alexandre Rames67555f72014-11-18 10:55:16 +00005452void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005453 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005454}
5455
Calin Juravlee460d1d2015-09-29 04:52:17 +01005456void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5457 HUnresolvedInstanceFieldGet* instruction) {
5458 FieldAccessCallingConventionARM64 calling_convention;
5459 codegen_->CreateUnresolvedFieldLocationSummary(
5460 instruction, instruction->GetFieldType(), calling_convention);
5461}
5462
5463void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5464 HUnresolvedInstanceFieldGet* instruction) {
5465 FieldAccessCallingConventionARM64 calling_convention;
5466 codegen_->GenerateUnresolvedFieldAccess(instruction,
5467 instruction->GetFieldType(),
5468 instruction->GetFieldIndex(),
5469 instruction->GetDexPc(),
5470 calling_convention);
5471}
5472
5473void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5474 HUnresolvedInstanceFieldSet* instruction) {
5475 FieldAccessCallingConventionARM64 calling_convention;
5476 codegen_->CreateUnresolvedFieldLocationSummary(
5477 instruction, instruction->GetFieldType(), calling_convention);
5478}
5479
5480void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5481 HUnresolvedInstanceFieldSet* instruction) {
5482 FieldAccessCallingConventionARM64 calling_convention;
5483 codegen_->GenerateUnresolvedFieldAccess(instruction,
5484 instruction->GetFieldType(),
5485 instruction->GetFieldIndex(),
5486 instruction->GetDexPc(),
5487 calling_convention);
5488}
5489
5490void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5491 HUnresolvedStaticFieldGet* instruction) {
5492 FieldAccessCallingConventionARM64 calling_convention;
5493 codegen_->CreateUnresolvedFieldLocationSummary(
5494 instruction, instruction->GetFieldType(), calling_convention);
5495}
5496
5497void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5498 HUnresolvedStaticFieldGet* instruction) {
5499 FieldAccessCallingConventionARM64 calling_convention;
5500 codegen_->GenerateUnresolvedFieldAccess(instruction,
5501 instruction->GetFieldType(),
5502 instruction->GetFieldIndex(),
5503 instruction->GetDexPc(),
5504 calling_convention);
5505}
5506
5507void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5508 HUnresolvedStaticFieldSet* instruction) {
5509 FieldAccessCallingConventionARM64 calling_convention;
5510 codegen_->CreateUnresolvedFieldLocationSummary(
5511 instruction, instruction->GetFieldType(), calling_convention);
5512}
5513
5514void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5515 HUnresolvedStaticFieldSet* instruction) {
5516 FieldAccessCallingConventionARM64 calling_convention;
5517 codegen_->GenerateUnresolvedFieldAccess(instruction,
5518 instruction->GetFieldType(),
5519 instruction->GetFieldIndex(),
5520 instruction->GetDexPc(),
5521 calling_convention);
5522}
5523
Alexandre Rames5319def2014-10-23 10:03:10 +01005524void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005525 LocationSummary* locations =
5526 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005527 // In suspend check slow path, usually there are no caller-save registers at all.
5528 // If SIMD instructions are present, however, we force spilling all live SIMD
5529 // registers in full width (since the runtime only saves/restores lower part).
5530 locations->SetCustomSlowPathCallerSaves(
5531 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005532}
5533
5534void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005535 HBasicBlock* block = instruction->GetBlock();
5536 if (block->GetLoopInformation() != nullptr) {
5537 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5538 // The back edge will generate the suspend check.
5539 return;
5540 }
5541 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5542 // The goto will generate the suspend check.
5543 return;
5544 }
5545 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005546}
5547
Alexandre Rames67555f72014-11-18 10:55:16 +00005548void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5549 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005551 InvokeRuntimeCallingConvention calling_convention;
5552 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5553}
5554
5555void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005556 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005557 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005558}
5559
5560void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5561 LocationSummary* locations =
5562 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5563 Primitive::Type input_type = conversion->GetInputType();
5564 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005565 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005566 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5567 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5568 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5569 }
5570
Alexandre Rames542361f2015-01-29 16:57:31 +00005571 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005572 locations->SetInAt(0, Location::RequiresFpuRegister());
5573 } else {
5574 locations->SetInAt(0, Location::RequiresRegister());
5575 }
5576
Alexandre Rames542361f2015-01-29 16:57:31 +00005577 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5579 } else {
5580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5581 }
5582}
5583
5584void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5585 Primitive::Type result_type = conversion->GetResultType();
5586 Primitive::Type input_type = conversion->GetInputType();
5587
5588 DCHECK_NE(input_type, result_type);
5589
Alexandre Rames542361f2015-01-29 16:57:31 +00005590 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005591 int result_size = Primitive::ComponentSize(result_type);
5592 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005593 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005594 Register output = OutputRegister(conversion);
5595 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005596 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005597 // 'int' values are used directly as W registers, discarding the top
5598 // bits, so we don't need to sign-extend and can just perform a move.
5599 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5600 // top 32 bits of the target register. We theoretically could leave those
5601 // bits unchanged, but we would have to make sure that no code uses a
5602 // 32bit input value as a 64bit value assuming that the top 32 bits are
5603 // zero.
5604 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005605 } else if (result_type == Primitive::kPrimChar ||
5606 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5607 __ Ubfx(output,
5608 output.IsX() ? source.X() : source.W(),
5609 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005610 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005611 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005612 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005613 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005614 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005615 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005616 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5617 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005618 } else if (Primitive::IsFloatingPointType(result_type) &&
5619 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005620 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5621 } else {
5622 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5623 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005624 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005625}
Alexandre Rames67555f72014-11-18 10:55:16 +00005626
Serban Constantinescu02164b32014-11-13 14:05:07 +00005627void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5628 HandleShift(ushr);
5629}
5630
5631void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5632 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005633}
5634
5635void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5636 HandleBinaryOp(instruction);
5637}
5638
5639void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5640 HandleBinaryOp(instruction);
5641}
5642
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005643void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005644 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005645 LOG(FATAL) << "Unreachable";
5646}
5647
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005648void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005649 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005650 LOG(FATAL) << "Unreachable";
5651}
5652
Mark Mendellfe57faa2015-09-18 09:26:15 -04005653// Simple implementation of packed switch - generate cascaded compare/jumps.
5654void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5655 LocationSummary* locations =
5656 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5657 locations->SetInAt(0, Location::RequiresRegister());
5658}
5659
5660void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5661 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005662 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005663 Register value_reg = InputRegisterAt(switch_instr, 0);
5664 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5665
Zheng Xu3927c8b2015-11-18 17:46:25 +08005666 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005667 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005668 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5669 // make sure we don't emit it if the target may run out of range.
5670 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5671 // ranges and emit the tables only as required.
5672 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005673
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005674 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005675 // Current instruction id is an upper bound of the number of HIRs in the graph.
5676 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5677 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005678 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5679 Register temp = temps.AcquireW();
5680 __ Subs(temp, value_reg, Operand(lower_bound));
5681
Zheng Xu3927c8b2015-11-18 17:46:25 +08005682 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005683 // Jump to successors[0] if value == lower_bound.
5684 __ B(eq, codegen_->GetLabelOf(successors[0]));
5685 int32_t last_index = 0;
5686 for (; num_entries - last_index > 2; last_index += 2) {
5687 __ Subs(temp, temp, Operand(2));
5688 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5689 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5690 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5691 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5692 }
5693 if (num_entries - last_index == 2) {
5694 // The last missing case_value.
5695 __ Cmp(temp, Operand(1));
5696 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005697 }
5698
5699 // And the default for any other value.
5700 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5701 __ B(codegen_->GetLabelOf(default_block));
5702 }
5703 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005704 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005705
5706 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5707
5708 // Below instructions should use at most one blocked register. Since there are two blocked
5709 // registers, we are free to block one.
5710 Register temp_w = temps.AcquireW();
5711 Register index;
5712 // Remove the bias.
5713 if (lower_bound != 0) {
5714 index = temp_w;
5715 __ Sub(index, value_reg, Operand(lower_bound));
5716 } else {
5717 index = value_reg;
5718 }
5719
5720 // Jump to default block if index is out of the range.
5721 __ Cmp(index, Operand(num_entries));
5722 __ B(hs, codegen_->GetLabelOf(default_block));
5723
5724 // In current VIXL implementation, it won't require any blocked registers to encode the
5725 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5726 // register pressure.
5727 Register table_base = temps.AcquireX();
5728 // Load jump offset from the table.
5729 __ Adr(table_base, jump_table->GetTableStartLabel());
5730 Register jump_offset = temp_w;
5731 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5732
5733 // Jump to target block by branching to table_base(pc related) + offset.
5734 Register target_address = table_base;
5735 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5736 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005737 }
5738}
5739
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005740void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5741 HInstruction* instruction,
5742 Location out,
5743 uint32_t offset,
5744 Location maybe_temp,
5745 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005746 Primitive::Type type = Primitive::kPrimNot;
5747 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005748 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005749 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005750 Register temp_reg = RegisterFrom(maybe_temp, type);
5751 if (kUseBakerReadBarrier) {
5752 // Load with fast path based Baker's read barrier.
5753 // /* HeapReference<Object> */ out = *(out + offset)
5754 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5755 out,
5756 out_reg,
5757 offset,
5758 temp_reg,
5759 /* needs_null_check */ false,
5760 /* use_load_acquire */ false);
5761 } else {
5762 // Load with slow path based read barrier.
5763 // Save the value of `out` into `maybe_temp` before overwriting it
5764 // in the following move operation, as we will need it for the
5765 // read barrier below.
5766 __ Mov(temp_reg, out_reg);
5767 // /* HeapReference<Object> */ out = *(out + offset)
5768 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5769 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5770 }
5771 } else {
5772 // Plain load with no read barrier.
5773 // /* HeapReference<Object> */ out = *(out + offset)
5774 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5775 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5776 }
5777}
5778
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005779void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5780 HInstruction* instruction,
5781 Location out,
5782 Location obj,
5783 uint32_t offset,
5784 Location maybe_temp,
5785 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005786 Primitive::Type type = Primitive::kPrimNot;
5787 Register out_reg = RegisterFrom(out, type);
5788 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005789 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005790 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005791 if (kUseBakerReadBarrier) {
5792 // Load with fast path based Baker's read barrier.
5793 Register temp_reg = RegisterFrom(maybe_temp, type);
5794 // /* HeapReference<Object> */ out = *(obj + offset)
5795 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5796 out,
5797 obj_reg,
5798 offset,
5799 temp_reg,
5800 /* needs_null_check */ false,
5801 /* use_load_acquire */ false);
5802 } else {
5803 // Load with slow path based read barrier.
5804 // /* HeapReference<Object> */ out = *(obj + offset)
5805 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5806 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5807 }
5808 } else {
5809 // Plain load with no read barrier.
5810 // /* HeapReference<Object> */ out = *(obj + offset)
5811 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5812 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5813 }
5814}
5815
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005816void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5817 HInstruction* instruction,
5818 Location root,
5819 Register obj,
5820 uint32_t offset,
5821 vixl::aarch64::Label* fixup_label,
5822 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005823 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005824 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005825 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005826 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005827 if (kUseBakerReadBarrier) {
5828 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005829 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005830 //
Roland Levillainba650a42017-03-06 13:52:32 +00005831 // Note that we do not actually check the value of
5832 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5833 // root or not. Instead, we load into `temp` the read barrier
5834 // mark entry point corresponding to register `root`. If `temp`
5835 // is null, it means that `GetIsGcMarking()` is false, and vice
5836 // versa.
5837 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005838 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005839 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5840 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5841 // // Slow path.
5842 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005843 // }
5844
Roland Levillainba650a42017-03-06 13:52:32 +00005845 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5846 Register temp = lr;
5847 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5848 instruction, root, /* entrypoint */ LocationFrom(temp));
5849 codegen_->AddSlowPath(slow_path);
5850
5851 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5852 const int32_t entry_point_offset =
5853 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5854 // Loading the entrypoint does not require a load acquire since it is only changed when
5855 // threads are suspended or running a checkpoint.
5856 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5857
Roland Levillain44015862016-01-22 11:47:17 +00005858 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005859 if (fixup_label == nullptr) {
5860 __ Ldr(root_reg, MemOperand(obj, offset));
5861 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005862 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863 }
Roland Levillain44015862016-01-22 11:47:17 +00005864 static_assert(
5865 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5866 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5867 "have different sizes.");
5868 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5869 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5870 "have different sizes.");
5871
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005872 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5873 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005874 __ Cbnz(temp, slow_path->GetEntryLabel());
5875 __ Bind(slow_path->GetExitLabel());
5876 } else {
5877 // GC root loaded through a slow path for read barriers other
5878 // than Baker's.
5879 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880 if (fixup_label == nullptr) {
5881 __ Add(root_reg.X(), obj.X(), offset);
5882 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005883 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884 }
Roland Levillain44015862016-01-22 11:47:17 +00005885 // /* mirror::Object* */ root = root->Read()
5886 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5887 }
5888 } else {
5889 // Plain GC root load with no read barrier.
5890 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 if (fixup_label == nullptr) {
5892 __ Ldr(root_reg, MemOperand(obj, offset));
5893 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005894 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005895 }
Roland Levillain44015862016-01-22 11:47:17 +00005896 // Note that GC roots are not affected by heap poisoning, thus we
5897 // do not have to unpoison `root_reg` here.
5898 }
5899}
5900
5901void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5902 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005903 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005904 uint32_t offset,
5905 Register temp,
5906 bool needs_null_check,
5907 bool use_load_acquire) {
5908 DCHECK(kEmitCompilerReadBarrier);
5909 DCHECK(kUseBakerReadBarrier);
5910
5911 // /* HeapReference<Object> */ ref = *(obj + offset)
5912 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005913 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005914 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5915 ref,
5916 obj,
5917 offset,
5918 no_index,
5919 no_scale_factor,
5920 temp,
5921 needs_null_check,
5922 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005923}
5924
5925void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5926 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005927 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005928 uint32_t data_offset,
5929 Location index,
5930 Register temp,
5931 bool needs_null_check) {
5932 DCHECK(kEmitCompilerReadBarrier);
5933 DCHECK(kUseBakerReadBarrier);
5934
5935 // Array cells are never volatile variables, therefore array loads
5936 // never use Load-Acquire instructions on ARM64.
5937 const bool use_load_acquire = false;
5938
Roland Levillainbfea3352016-06-23 13:48:47 +01005939 static_assert(
5940 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5941 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005942 // /* HeapReference<Object> */ ref =
5943 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005944 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5945 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5946 ref,
5947 obj,
5948 data_offset,
5949 index,
5950 scale_factor,
5951 temp,
5952 needs_null_check,
5953 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005954}
5955
5956void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5957 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005958 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005959 uint32_t offset,
5960 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005961 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005962 Register temp,
5963 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005964 bool use_load_acquire,
5965 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005966 DCHECK(kEmitCompilerReadBarrier);
5967 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005968 // If we are emitting an array load, we should not be using a
5969 // Load Acquire instruction. In other words:
5970 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5971 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005972
Roland Levillain54f869e2017-03-06 13:54:11 +00005973 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
5974 // whether we need to enter the slow path to mark the reference.
5975 // Then, in the slow path, check the gray bit in the lock word of
5976 // the reference's holder (`obj`) to decide whether to mark `ref` or
5977 // not.
Roland Levillain44015862016-01-22 11:47:17 +00005978 //
Roland Levillainba650a42017-03-06 13:52:32 +00005979 // Note that we do not actually check the value of `GetIsGcMarking()`;
5980 // instead, we load into `temp2` the read barrier mark entry point
5981 // corresponding to register `ref`. If `temp2` is null, it means
5982 // that `GetIsGcMarking()` is false, and vice versa.
5983 //
5984 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005985 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5986 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00005987 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5988 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5989 // HeapReference<mirror::Object> ref = *src; // Original reference load.
5990 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5991 // if (is_gray) {
5992 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
5993 // }
5994 // } else {
5995 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00005996 // }
Roland Levillain44015862016-01-22 11:47:17 +00005997
Roland Levillainba650a42017-03-06 13:52:32 +00005998 // Slow path marking the object `ref` when the GC is marking. The
5999 // entrypoint will already be loaded in `temp2`.
6000 Register temp2 = lr;
6001 Location temp2_loc = LocationFrom(temp2);
6002 SlowPathCodeARM64* slow_path;
6003 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006004 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6005 // only supports address of the form `obj + field_offset`, where
6006 // `obj` is a register and `field_offset` is a register. Thus
6007 // `offset` and `scale_factor` above are expected to be null in
6008 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006009 DCHECK_EQ(offset, 0u);
6010 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006011 Location field_offset = index;
6012 slow_path =
6013 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6014 instruction,
6015 ref,
6016 obj,
6017 offset,
6018 /* index */ field_offset,
6019 scale_factor,
6020 needs_null_check,
6021 use_load_acquire,
6022 temp,
6023 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006024 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006025 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6026 instruction,
6027 ref,
6028 obj,
6029 offset,
6030 index,
6031 scale_factor,
6032 needs_null_check,
6033 use_load_acquire,
6034 temp,
6035 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006036 }
6037 AddSlowPath(slow_path);
6038
6039 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6040 const int32_t entry_point_offset =
6041 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6042 // Loading the entrypoint does not require a load acquire since it is only changed when
6043 // threads are suspended or running a checkpoint.
6044 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006045 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6046 // checking GetIsGcMarking.
6047 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006048 // Fast path: just load the reference.
6049 GenerateRawReferenceLoad(
6050 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006051 __ Bind(slow_path->GetExitLabel());
6052}
6053
6054void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6055 Location ref,
6056 Register obj,
6057 uint32_t offset,
6058 Location index,
6059 size_t scale_factor,
6060 bool needs_null_check,
6061 bool use_load_acquire) {
6062 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006063 Primitive::Type type = Primitive::kPrimNot;
6064 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006065
Roland Levillainba650a42017-03-06 13:52:32 +00006066 // If needed, vixl::EmissionCheckScope guards are used to ensure
6067 // that no pools are emitted between the load (macro) instruction
6068 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006069
Roland Levillain44015862016-01-22 11:47:17 +00006070 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006071 // Load types involving an "index": ArrayGet,
6072 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6073 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006074 if (use_load_acquire) {
6075 // UnsafeGetObjectVolatile intrinsic case.
6076 // Register `index` is not an index in an object array, but an
6077 // offset to an object reference field within object `obj`.
6078 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6079 DCHECK(instruction->GetLocations()->Intrinsified());
6080 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6081 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006082 DCHECK_EQ(offset, 0u);
6083 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006084 DCHECK_EQ(needs_null_check, false);
6085 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006086 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6087 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006088 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006089 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6090 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006091 if (index.IsConstant()) {
6092 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006093 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006094 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006095 if (needs_null_check) {
6096 MaybeRecordImplicitNullCheck(instruction);
6097 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006098 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006099 UseScratchRegisterScope temps(GetVIXLAssembler());
6100 Register temp = temps.AcquireW();
6101 __ Add(temp, obj, offset);
6102 {
6103 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6104 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6105 if (needs_null_check) {
6106 MaybeRecordImplicitNullCheck(instruction);
6107 }
6108 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006109 }
Roland Levillain44015862016-01-22 11:47:17 +00006110 }
Roland Levillain44015862016-01-22 11:47:17 +00006111 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006112 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006113 MemOperand field = HeapOperand(obj, offset);
6114 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006115 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6116 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006117 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006118 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006119 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006120 if (needs_null_check) {
6121 MaybeRecordImplicitNullCheck(instruction);
6122 }
Roland Levillain44015862016-01-22 11:47:17 +00006123 }
6124 }
6125
6126 // Object* ref = ref_addr->AsMirrorPtr()
6127 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006128}
6129
6130void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6131 Location out,
6132 Location ref,
6133 Location obj,
6134 uint32_t offset,
6135 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006136 DCHECK(kEmitCompilerReadBarrier);
6137
Roland Levillain44015862016-01-22 11:47:17 +00006138 // Insert a slow path based read barrier *after* the reference load.
6139 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006140 // If heap poisoning is enabled, the unpoisoning of the loaded
6141 // reference will be carried out by the runtime within the slow
6142 // path.
6143 //
6144 // Note that `ref` currently does not get unpoisoned (when heap
6145 // poisoning is enabled), which is alright as the `ref` argument is
6146 // not used by the artReadBarrierSlow entry point.
6147 //
6148 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6149 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6150 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6151 AddSlowPath(slow_path);
6152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006153 __ B(slow_path->GetEntryLabel());
6154 __ Bind(slow_path->GetExitLabel());
6155}
6156
Roland Levillain44015862016-01-22 11:47:17 +00006157void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6158 Location out,
6159 Location ref,
6160 Location obj,
6161 uint32_t offset,
6162 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006163 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006164 // Baker's read barriers shall be handled by the fast path
6165 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6166 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006167 // If heap poisoning is enabled, unpoisoning will be taken care of
6168 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006169 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006170 } else if (kPoisonHeapReferences) {
6171 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6172 }
6173}
6174
Roland Levillain44015862016-01-22 11:47:17 +00006175void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6176 Location out,
6177 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006178 DCHECK(kEmitCompilerReadBarrier);
6179
Roland Levillain44015862016-01-22 11:47:17 +00006180 // Insert a slow path based read barrier *after* the GC root load.
6181 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006182 // Note that GC roots are not affected by heap poisoning, so we do
6183 // not need to do anything special for this here.
6184 SlowPathCodeARM64* slow_path =
6185 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6186 AddSlowPath(slow_path);
6187
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006188 __ B(slow_path->GetEntryLabel());
6189 __ Bind(slow_path->GetExitLabel());
6190}
6191
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006192void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6193 LocationSummary* locations =
6194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6195 locations->SetInAt(0, Location::RequiresRegister());
6196 locations->SetOut(Location::RequiresRegister());
6197}
6198
6199void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6200 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006201 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006202 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006203 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006204 __ Ldr(XRegisterFrom(locations->Out()),
6205 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006206 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006207 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006208 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006209 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6210 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006211 __ Ldr(XRegisterFrom(locations->Out()),
6212 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006213 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006214}
6215
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006216static void PatchJitRootUse(uint8_t* code,
6217 const uint8_t* roots_data,
6218 vixl::aarch64::Literal<uint32_t>* literal,
6219 uint64_t index_in_table) {
6220 uint32_t literal_offset = literal->GetOffset();
6221 uintptr_t address =
6222 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6223 uint8_t* data = code + literal_offset;
6224 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6225}
6226
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006227void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6228 for (const auto& entry : jit_string_patches_) {
6229 const auto& it = jit_string_roots_.find(entry.first);
6230 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006231 PatchJitRootUse(code, roots_data, entry.second, it->second);
6232 }
6233 for (const auto& entry : jit_class_patches_) {
6234 const auto& it = jit_class_roots_.find(entry.first);
6235 DCHECK(it != jit_class_roots_.end());
6236 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006237 }
6238}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006239
Alexandre Rames67555f72014-11-18 10:55:16 +00006240#undef __
6241#undef QUICK_ENTRY_POINT
6242
Alexandre Rames5319def2014-10-23 10:03:10 +01006243} // namespace arm64
6244} // namespace art