blob: 93fe1f26111d8cf091ac9979bde3c2fbaffb4df1 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 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_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "common_arm.h"
23#include "compiled_method.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010026#include "intrinsics_arm_vixl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "thread.h"
30#include "utils/arm/assembler_arm_vixl.h"
31#include "utils/arm/managed_register_arm.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35namespace art {
36namespace arm {
37
38namespace vixl32 = vixl::aarch32;
39using namespace vixl32; // NOLINT(build/namespaces)
40
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010041using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010042using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010043using helpers::HighDRegisterFrom;
44using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010045using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010046using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010047using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000049using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000051using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000052using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::LocationFrom;
54using helpers::LowRegisterFrom;
55using helpers::LowSRegisterFrom;
56using helpers::OutputRegister;
57using helpers::OutputSRegister;
58using helpers::OutputVRegister;
59using helpers::RegisterFrom;
60using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000061using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010062
Artem Serov0fb37192016-12-06 18:13:40 +000063using vixl::ExactAssemblyScope;
64using vixl::CodeBufferCheckScope;
65
Scott Wakelingfe885462016-09-22 10:24:38 +010066using RegisterList = vixl32::RegisterList;
67
68static bool ExpectedPairLayout(Location location) {
69 // We expected this for both core and fpu register pairs.
70 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
71}
Artem Serovd4cc5b22016-11-04 11:19:09 +000072// Use a local definition to prevent copying mistakes.
73static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
74static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010075static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010077
78#ifdef __
79#error "ARM Codegen VIXL macro-assembler macro already defined."
80#endif
81
Scott Wakelingfe885462016-09-22 10:24:38 +010082// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
83#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
84#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
85
86// Marker that code is yet to be, and must, be implemented.
87#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
88
Scott Wakelinga7812ae2016-10-17 10:03:36 +010089// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
90// for each live D registers they treat two corresponding S registers as live ones.
91//
92// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
93// from a list of contiguous S registers a list of contiguous D registers (processing first/last
94// S registers corner cases) and save/restore this new list treating them as D registers.
95// - decreasing code size
96// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
97// restored and then used in regular non SlowPath code as D register.
98//
99// For the following example (v means the S register is live):
100// D names: | D0 | D1 | D2 | D4 | ...
101// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
102// Live? | | v | v | v | v | v | v | | ...
103//
104// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
105// as D registers.
106//
107// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
108// for lists of floating-point registers.
109static size_t SaveContiguousSRegisterList(size_t first,
110 size_t last,
111 CodeGenerator* codegen,
112 size_t stack_offset) {
113 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
114 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
115 DCHECK_LE(first, last);
116 if ((first == last) && (first == 0)) {
117 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
118 return stack_offset + kSRegSizeInBytes;
119 }
120 if (first % 2 == 1) {
121 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
122 stack_offset += kSRegSizeInBytes;
123 }
124
125 bool save_last = false;
126 if (last % 2 == 0) {
127 save_last = true;
128 --last;
129 }
130
131 if (first < last) {
132 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
133 DCHECK_EQ((last - first + 1) % 2, 0u);
134 size_t number_of_d_regs = (last - first + 1) / 2;
135
136 if (number_of_d_regs == 1) {
137 __ Vstr(d_reg, MemOperand(sp, stack_offset));
138 } else if (number_of_d_regs > 1) {
139 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
140 vixl32::Register base = sp;
141 if (stack_offset != 0) {
142 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000143 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100144 }
145 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
146 }
147 stack_offset += number_of_d_regs * kDRegSizeInBytes;
148 }
149
150 if (save_last) {
151 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
152 stack_offset += kSRegSizeInBytes;
153 }
154
155 return stack_offset;
156}
157
158static size_t RestoreContiguousSRegisterList(size_t first,
159 size_t last,
160 CodeGenerator* codegen,
161 size_t stack_offset) {
162 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
163 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
164 DCHECK_LE(first, last);
165 if ((first == last) && (first == 0)) {
166 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
167 return stack_offset + kSRegSizeInBytes;
168 }
169 if (first % 2 == 1) {
170 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
171 stack_offset += kSRegSizeInBytes;
172 }
173
174 bool restore_last = false;
175 if (last % 2 == 0) {
176 restore_last = true;
177 --last;
178 }
179
180 if (first < last) {
181 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
182 DCHECK_EQ((last - first + 1) % 2, 0u);
183 size_t number_of_d_regs = (last - first + 1) / 2;
184 if (number_of_d_regs == 1) {
185 __ Vldr(d_reg, MemOperand(sp, stack_offset));
186 } else if (number_of_d_regs > 1) {
187 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
188 vixl32::Register base = sp;
189 if (stack_offset != 0) {
190 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000191 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100192 }
193 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
194 }
195 stack_offset += number_of_d_regs * kDRegSizeInBytes;
196 }
197
198 if (restore_last) {
199 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
200 stack_offset += kSRegSizeInBytes;
201 }
202
203 return stack_offset;
204}
205
206void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
207 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
208 size_t orig_offset = stack_offset;
209
210 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
211 for (uint32_t i : LowToHighBits(core_spills)) {
212 // If the register holds an object, update the stack mask.
213 if (locations->RegisterContainsObject(i)) {
214 locations->SetStackBit(stack_offset / kVRegSize);
215 }
216 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
217 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
218 saved_core_stack_offsets_[i] = stack_offset;
219 stack_offset += kArmWordSize;
220 }
221
222 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
223 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
224
225 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
226 orig_offset = stack_offset;
227 for (uint32_t i : LowToHighBits(fp_spills)) {
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_fpu_stack_offsets_[i] = stack_offset;
230 stack_offset += kArmWordSize;
231 }
232
233 stack_offset = orig_offset;
234 while (fp_spills != 0u) {
235 uint32_t begin = CTZ(fp_spills);
236 uint32_t tmp = fp_spills + (1u << begin);
237 fp_spills &= tmp; // Clear the contiguous range of 1s.
238 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
239 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
240 }
241 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
242}
243
244void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
245 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
246 size_t orig_offset = stack_offset;
247
248 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
249 for (uint32_t i : LowToHighBits(core_spills)) {
250 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
251 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
252 stack_offset += kArmWordSize;
253 }
254
255 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
256 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
257 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
258
259 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
260 while (fp_spills != 0u) {
261 uint32_t begin = CTZ(fp_spills);
262 uint32_t tmp = fp_spills + (1u << begin);
263 fp_spills &= tmp; // Clear the contiguous range of 1s.
264 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
265 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
266 }
267 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
268}
269
270class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
271 public:
272 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
273
274 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
275 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
276 __ Bind(GetEntryLabel());
277 if (instruction_->CanThrowIntoCatchBlock()) {
278 // Live registers will be restored in the catch block if caught.
279 SaveLiveRegisters(codegen, instruction_->GetLocations());
280 }
281 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
282 instruction_,
283 instruction_->GetDexPc(),
284 this);
285 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
286 }
287
288 bool IsFatal() const OVERRIDE { return true; }
289
290 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
291
292 private:
293 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
294};
295
Scott Wakelingfe885462016-09-22 10:24:38 +0100296class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
297 public:
298 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
299 : SlowPathCodeARMVIXL(instruction) {}
300
301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100302 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100303 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100304 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100305 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
306 }
307
308 bool IsFatal() const OVERRIDE { return true; }
309
310 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
311
312 private:
313 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
314};
315
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100316class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
317 public:
318 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
319 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
323 __ Bind(GetEntryLabel());
324 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
325 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
326 if (successor_ == nullptr) {
327 __ B(GetReturnLabel());
328 } else {
329 __ B(arm_codegen->GetLabelOf(successor_));
330 }
331 }
332
333 vixl32::Label* GetReturnLabel() {
334 DCHECK(successor_ == nullptr);
335 return &return_label_;
336 }
337
338 HBasicBlock* GetSuccessor() const {
339 return successor_;
340 }
341
342 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
343
344 private:
345 // If not null, the block to branch to after the suspend check.
346 HBasicBlock* const successor_;
347
348 // If `successor_` is null, the label to branch to after the suspend check.
349 vixl32::Label return_label_;
350
351 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
352};
353
Scott Wakelingc34dba72016-10-03 10:14:44 +0100354class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
355 public:
356 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
357 : SlowPathCodeARMVIXL(instruction) {}
358
359 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
360 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
361 LocationSummary* locations = instruction_->GetLocations();
362
363 __ Bind(GetEntryLabel());
364 if (instruction_->CanThrowIntoCatchBlock()) {
365 // Live registers will be restored in the catch block if caught.
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
367 }
368 // We're moving two locations to locations that could overlap, so we need a parallel
369 // move resolver.
370 InvokeRuntimeCallingConventionARMVIXL calling_convention;
371 codegen->EmitParallelMoves(
372 locations->InAt(0),
373 LocationFrom(calling_convention.GetRegisterAt(0)),
374 Primitive::kPrimInt,
375 locations->InAt(1),
376 LocationFrom(calling_convention.GetRegisterAt(1)),
377 Primitive::kPrimInt);
378 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
379 ? kQuickThrowStringBounds
380 : kQuickThrowArrayBounds;
381 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
382 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
383 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
384 }
385
386 bool IsFatal() const OVERRIDE { return true; }
387
388 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
389
390 private:
391 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
392};
393
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100394class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
395 public:
396 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000397 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100398 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
399 }
400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 LocationSummary* locations = instruction_->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100403
404 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
405 __ Bind(GetEntryLabel());
406 SaveLiveRegisters(codegen, locations);
407
408 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000409 dex::TypeIndex type_index = cls_->GetTypeIndex();
410 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100411 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
412 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000413 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100414 if (do_clinit_) {
415 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
416 } else {
417 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
418 }
419
420 // Move the class to the desired location.
421 Location out = locations->Out();
422 if (out.IsValid()) {
423 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
424 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
425 }
426 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000427 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
428 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
429 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
430 DCHECK(out.IsValid());
431 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
432 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
433 // so that we can avoid another calculation here.
434 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
435 arm_codegen->NewPcRelativeTypePatch(cls_->GetDexFile(), type_index);
436 arm_codegen->EmitMovwMovtPlaceholder(labels, ip);
437 __ Str(OutputRegister(cls_), MemOperand(ip));
438 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100439 __ B(GetExitLabel());
440 }
441
442 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
443
444 private:
445 // The class this slow path will load.
446 HLoadClass* const cls_;
447
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100448 // The dex PC of `at_`.
449 const uint32_t dex_pc_;
450
451 // Whether to initialize the class.
452 const bool do_clinit_;
453
454 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
455};
456
Artem Serovd4cc5b22016-11-04 11:19:09 +0000457class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
458 public:
459 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
460 : SlowPathCodeARMVIXL(instruction) {}
461
462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 LocationSummary* locations = instruction_->GetLocations();
464 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
465 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000467 vixl32::Register out = OutputRegister(load);
468 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
469 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
470
471 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
472 __ Bind(GetEntryLabel());
473 SaveLiveRegisters(codegen, locations);
474
475 InvokeRuntimeCallingConventionARMVIXL calling_convention;
476 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
477 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
478 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
479 vixl32::Register entry_address = temp_is_r0 ? out : temp;
480 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
481 if (call_saves_everything_except_r0 && temp_is_r0) {
482 __ Mov(entry_address, temp);
483 }
484
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000485 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000486 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
487 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
488
489 // Store the resolved String to the .bss entry.
490 if (call_saves_everything_except_r0) {
491 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
492 __ Str(r0, MemOperand(entry_address));
493 } else {
494 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
495 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
496 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
497 arm_codegen->EmitMovwMovtPlaceholder(labels, out);
498 __ Str(r0, MemOperand(entry_address));
499 }
500
501 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
502 RestoreLiveRegisters(codegen, locations);
503
504 __ B(GetExitLabel());
505 }
506
507 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
508
509 private:
510 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
511};
512
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100513class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
514 public:
515 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
516 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
517
518 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
519 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100520 DCHECK(instruction_->IsCheckCast()
521 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
522
523 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
524 __ Bind(GetEntryLabel());
525
526 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100527 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100528 }
529
530 // We're moving two locations to locations that could overlap, so we need a parallel
531 // move resolver.
532 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100533
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800534 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800535 LocationFrom(calling_convention.GetRegisterAt(0)),
536 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800537 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800538 LocationFrom(calling_convention.GetRegisterAt(1)),
539 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100540 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100541 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
542 instruction_,
543 instruction_->GetDexPc(),
544 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800545 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100546 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100547 } else {
548 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800549 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
550 instruction_,
551 instruction_->GetDexPc(),
552 this);
553 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100554 }
555
556 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100557 RestoreLiveRegisters(codegen, locations);
558 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100559 }
560 }
561
562 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
563
564 bool IsFatal() const OVERRIDE { return is_fatal_; }
565
566 private:
567 const bool is_fatal_;
568
569 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
570};
571
Scott Wakelingc34dba72016-10-03 10:14:44 +0100572class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
573 public:
574 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
575 : SlowPathCodeARMVIXL(instruction) {}
576
577 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
578 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
579 __ Bind(GetEntryLabel());
580 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
581 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
582 }
583
584 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
585
586 private:
587 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
588};
589
590class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
591 public:
592 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
593
594 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
595 LocationSummary* locations = instruction_->GetLocations();
596 __ Bind(GetEntryLabel());
597 SaveLiveRegisters(codegen, locations);
598
599 InvokeRuntimeCallingConventionARMVIXL calling_convention;
600 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
601 parallel_move.AddMove(
602 locations->InAt(0),
603 LocationFrom(calling_convention.GetRegisterAt(0)),
604 Primitive::kPrimNot,
605 nullptr);
606 parallel_move.AddMove(
607 locations->InAt(1),
608 LocationFrom(calling_convention.GetRegisterAt(1)),
609 Primitive::kPrimInt,
610 nullptr);
611 parallel_move.AddMove(
612 locations->InAt(2),
613 LocationFrom(calling_convention.GetRegisterAt(2)),
614 Primitive::kPrimNot,
615 nullptr);
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617
618 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
619 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
620 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
621 RestoreLiveRegisters(codegen, locations);
622 __ B(GetExitLabel());
623 }
624
625 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
626
627 private:
628 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
629};
630
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000631// Slow path marking an object reference `ref` during a read
632// barrier. The field `obj.field` in the object `obj` holding this
633// reference does not get updated by this slow path after marking (see
634// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
635//
636// This means that after the execution of this slow path, `ref` will
637// always be up-to-date, but `obj.field` may not; i.e., after the
638// flip, `ref` will be a to-space reference, but `obj.field` will
639// probably still be a from-space reference (unless it gets updated by
640// another thread, or if another thread installed another object
641// reference (different from `ref`) in `obj.field`).
642class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
643 public:
644 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
645 Location ref,
646 Location entrypoint = Location::NoLocation())
647 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
648 DCHECK(kEmitCompilerReadBarrier);
649 }
650
651 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
652
653 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
654 LocationSummary* locations = instruction_->GetLocations();
655 vixl32::Register ref_reg = RegisterFrom(ref_);
656 DCHECK(locations->CanCall());
657 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
658 DCHECK(instruction_->IsInstanceFieldGet() ||
659 instruction_->IsStaticFieldGet() ||
660 instruction_->IsArrayGet() ||
661 instruction_->IsArraySet() ||
662 instruction_->IsLoadClass() ||
663 instruction_->IsLoadString() ||
664 instruction_->IsInstanceOf() ||
665 instruction_->IsCheckCast() ||
666 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
667 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
668 << "Unexpected instruction in read barrier marking slow path: "
669 << instruction_->DebugName();
670 // The read barrier instrumentation of object ArrayGet
671 // instructions does not support the HIntermediateAddress
672 // instruction.
673 DCHECK(!(instruction_->IsArrayGet() &&
674 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
675
676 __ Bind(GetEntryLabel());
677 // No need to save live registers; it's taken care of by the
678 // entrypoint. Also, there is no need to update the stack mask,
679 // as this runtime call will not trigger a garbage collection.
680 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
681 DCHECK(!ref_reg.Is(sp));
682 DCHECK(!ref_reg.Is(lr));
683 DCHECK(!ref_reg.Is(pc));
684 // IP is used internally by the ReadBarrierMarkRegX entry point
685 // as a temporary, it cannot be the entry point's input/output.
686 DCHECK(!ref_reg.Is(ip));
687 DCHECK(ref_reg.IsRegister()) << ref_reg;
688 // "Compact" slow path, saving two moves.
689 //
690 // Instead of using the standard runtime calling convention (input
691 // and output in R0):
692 //
693 // R0 <- ref
694 // R0 <- ReadBarrierMark(R0)
695 // ref <- R0
696 //
697 // we just use rX (the register containing `ref`) as input and output
698 // of a dedicated entrypoint:
699 //
700 // rX <- ReadBarrierMarkRegX(rX)
701 //
702 if (entrypoint_.IsValid()) {
703 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
704 __ Blx(RegisterFrom(entrypoint_));
705 } else {
706 int32_t entry_point_offset =
707 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
708 // This runtime call does not require a stack map.
709 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
710 }
711 __ B(GetExitLabel());
712 }
713
714 private:
715 // The location (register) of the marked object reference.
716 const Location ref_;
717
718 // The location of the entrypoint if already loaded.
719 const Location entrypoint_;
720
721 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
722};
723
724// Slow path marking an object reference `ref` during a read barrier,
725// and if needed, atomically updating the field `obj.field` in the
726// object `obj` holding this reference after marking (contrary to
727// ReadBarrierMarkSlowPathARM above, which never tries to update
728// `obj.field`).
729//
730// This means that after the execution of this slow path, both `ref`
731// and `obj.field` will be up-to-date; i.e., after the flip, both will
732// hold the same to-space reference (unless another thread installed
733// another object reference (different from `ref`) in `obj.field`).
734class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
735 public:
736 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
737 Location ref,
738 vixl32::Register obj,
739 Location field_offset,
740 vixl32::Register temp1,
741 vixl32::Register temp2)
742 : SlowPathCodeARMVIXL(instruction),
743 ref_(ref),
744 obj_(obj),
745 field_offset_(field_offset),
746 temp1_(temp1),
747 temp2_(temp2) {
748 DCHECK(kEmitCompilerReadBarrier);
749 }
750
751 const char* GetDescription() const OVERRIDE {
752 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
753 }
754
755 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
756 LocationSummary* locations = instruction_->GetLocations();
757 vixl32::Register ref_reg = RegisterFrom(ref_);
758 DCHECK(locations->CanCall());
759 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
760 // This slow path is only used by the UnsafeCASObject intrinsic.
761 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
762 << "Unexpected instruction in read barrier marking and field updating slow path: "
763 << instruction_->DebugName();
764 DCHECK(instruction_->GetLocations()->Intrinsified());
765 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
766 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
767
768 __ Bind(GetEntryLabel());
769
770 // Save the old reference.
771 // Note that we cannot use IP to save the old reference, as IP is
772 // used internally by the ReadBarrierMarkRegX entry point, and we
773 // need the old reference after the call to that entry point.
774 DCHECK(!temp1_.Is(ip));
775 __ Mov(temp1_, ref_reg);
776
777 // No need to save live registers; it's taken care of by the
778 // entrypoint. Also, there is no need to update the stack mask,
779 // as this runtime call will not trigger a garbage collection.
780 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
781 DCHECK(!ref_reg.Is(sp));
782 DCHECK(!ref_reg.Is(lr));
783 DCHECK(!ref_reg.Is(pc));
784 // IP is used internally by the ReadBarrierMarkRegX entry point
785 // as a temporary, it cannot be the entry point's input/output.
786 DCHECK(!ref_reg.Is(ip));
787 DCHECK(ref_reg.IsRegister()) << ref_reg;
788 // "Compact" slow path, saving two moves.
789 //
790 // Instead of using the standard runtime calling convention (input
791 // and output in R0):
792 //
793 // R0 <- ref
794 // R0 <- ReadBarrierMark(R0)
795 // ref <- R0
796 //
797 // we just use rX (the register containing `ref`) as input and output
798 // of a dedicated entrypoint:
799 //
800 // rX <- ReadBarrierMarkRegX(rX)
801 //
802 int32_t entry_point_offset =
803 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
804 // This runtime call does not require a stack map.
805 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
806
807 // If the new reference is different from the old reference,
808 // update the field in the holder (`*(obj_ + field_offset_)`).
809 //
810 // Note that this field could also hold a different object, if
811 // another thread had concurrently changed it. In that case, the
812 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
813 // (CAS) operation below would abort the CAS, leaving the field
814 // as-is.
815 vixl32::Label done;
816 __ Cmp(temp1_, ref_reg);
Artem Serov517d9f62016-12-12 15:51:15 +0000817 __ B(eq, &done, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000818
819 // Update the the holder's field atomically. This may fail if
820 // mutator updates before us, but it's OK. This is achieved
821 // using a strong compare-and-set (CAS) operation with relaxed
822 // memory synchronization ordering, where the expected value is
823 // the old reference and the desired value is the new reference.
824
825 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
826 // Convenience aliases.
827 vixl32::Register base = obj_;
828 // The UnsafeCASObject intrinsic uses a register pair as field
829 // offset ("long offset"), of which only the low part contains
830 // data.
831 vixl32::Register offset = LowRegisterFrom(field_offset_);
832 vixl32::Register expected = temp1_;
833 vixl32::Register value = ref_reg;
834 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
835 vixl32::Register tmp = temp2_; // Value in memory.
836
837 __ Add(tmp_ptr, base, offset);
838
839 if (kPoisonHeapReferences) {
840 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
841 if (value.Is(expected)) {
842 // Do not poison `value`, as it is the same register as
843 // `expected`, which has just been poisoned.
844 } else {
845 arm_codegen->GetAssembler()->PoisonHeapReference(value);
846 }
847 }
848
849 // do {
850 // tmp = [r_ptr] - expected;
851 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
852
853 vixl32::Label loop_head, exit_loop;
854 __ Bind(&loop_head);
855
856 __ Ldrex(tmp, MemOperand(tmp_ptr));
857
858 __ Subs(tmp, tmp, expected);
859
860 {
Artem Serov0fb37192016-12-06 18:13:40 +0000861 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
862 2 * kMaxInstructionSizeInBytes,
863 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000864
865 __ it(ne);
866 __ clrex(ne);
867 }
868
Artem Serov517d9f62016-12-12 15:51:15 +0000869 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000870
871 __ Strex(tmp, value, MemOperand(tmp_ptr));
872 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +0000873 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000874
875 __ Bind(&exit_loop);
876
877 if (kPoisonHeapReferences) {
878 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
879 if (value.Is(expected)) {
880 // Do not unpoison `value`, as it is the same register as
881 // `expected`, which has just been unpoisoned.
882 } else {
883 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
884 }
885 }
886
887 __ Bind(&done);
888 __ B(GetExitLabel());
889 }
890
891 private:
892 // The location (register) of the marked object reference.
893 const Location ref_;
894 // The register containing the object holding the marked object reference field.
895 const vixl32::Register obj_;
896 // The location of the offset of the marked reference field within `obj_`.
897 Location field_offset_;
898
899 const vixl32::Register temp1_;
900 const vixl32::Register temp2_;
901
902 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
903};
904
905// Slow path generating a read barrier for a heap reference.
906class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
907 public:
908 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
909 Location out,
910 Location ref,
911 Location obj,
912 uint32_t offset,
913 Location index)
914 : SlowPathCodeARMVIXL(instruction),
915 out_(out),
916 ref_(ref),
917 obj_(obj),
918 offset_(offset),
919 index_(index) {
920 DCHECK(kEmitCompilerReadBarrier);
921 // If `obj` is equal to `out` or `ref`, it means the initial object
922 // has been overwritten by (or after) the heap object reference load
923 // to be instrumented, e.g.:
924 //
925 // __ LoadFromOffset(kLoadWord, out, out, offset);
926 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
927 //
928 // In that case, we have lost the information about the original
929 // object, and the emitted read barrier cannot work properly.
930 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
931 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
932 }
933
934 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
935 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
936 LocationSummary* locations = instruction_->GetLocations();
937 vixl32::Register reg_out = RegisterFrom(out_);
938 DCHECK(locations->CanCall());
939 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
940 DCHECK(instruction_->IsInstanceFieldGet() ||
941 instruction_->IsStaticFieldGet() ||
942 instruction_->IsArrayGet() ||
943 instruction_->IsInstanceOf() ||
944 instruction_->IsCheckCast() ||
945 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
946 << "Unexpected instruction in read barrier for heap reference slow path: "
947 << instruction_->DebugName();
948 // The read barrier instrumentation of object ArrayGet
949 // instructions does not support the HIntermediateAddress
950 // instruction.
951 DCHECK(!(instruction_->IsArrayGet() &&
952 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
953
954 __ Bind(GetEntryLabel());
955 SaveLiveRegisters(codegen, locations);
956
957 // We may have to change the index's value, but as `index_` is a
958 // constant member (like other "inputs" of this slow path),
959 // introduce a copy of it, `index`.
960 Location index = index_;
961 if (index_.IsValid()) {
962 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
963 if (instruction_->IsArrayGet()) {
964 // Compute the actual memory offset and store it in `index`.
965 vixl32::Register index_reg = RegisterFrom(index_);
966 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
967 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
968 // We are about to change the value of `index_reg` (see the
969 // calls to art::arm::Thumb2Assembler::Lsl and
970 // art::arm::Thumb2Assembler::AddConstant below), but it has
971 // not been saved by the previous call to
972 // art::SlowPathCode::SaveLiveRegisters, as it is a
973 // callee-save register --
974 // art::SlowPathCode::SaveLiveRegisters does not consider
975 // callee-save registers, as it has been designed with the
976 // assumption that callee-save registers are supposed to be
977 // handled by the called function. So, as a callee-save
978 // register, `index_reg` _would_ eventually be saved onto
979 // the stack, but it would be too late: we would have
980 // changed its value earlier. Therefore, we manually save
981 // it here into another freely available register,
982 // `free_reg`, chosen of course among the caller-save
983 // registers (as a callee-save `free_reg` register would
984 // exhibit the same problem).
985 //
986 // Note we could have requested a temporary register from
987 // the register allocator instead; but we prefer not to, as
988 // this is a slow path, and we know we can find a
989 // caller-save register that is available.
990 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
991 __ Mov(free_reg, index_reg);
992 index_reg = free_reg;
993 index = LocationFrom(index_reg);
994 } else {
995 // The initial register stored in `index_` has already been
996 // saved in the call to art::SlowPathCode::SaveLiveRegisters
997 // (as it is not a callee-save register), so we can freely
998 // use it.
999 }
1000 // Shifting the index value contained in `index_reg` by the scale
1001 // factor (2) cannot overflow in practice, as the runtime is
1002 // unable to allocate object arrays with a size larger than
1003 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1004 __ Lsl(index_reg, index_reg, TIMES_4);
1005 static_assert(
1006 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1007 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1008 __ Add(index_reg, index_reg, offset_);
1009 } else {
1010 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1011 // intrinsics, `index_` is not shifted by a scale factor of 2
1012 // (as in the case of ArrayGet), as it is actually an offset
1013 // to an object field within an object.
1014 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1015 DCHECK(instruction_->GetLocations()->Intrinsified());
1016 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1017 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1018 << instruction_->AsInvoke()->GetIntrinsic();
1019 DCHECK_EQ(offset_, 0U);
1020 DCHECK(index_.IsRegisterPair());
1021 // UnsafeGet's offset location is a register pair, the low
1022 // part contains the correct offset.
1023 index = index_.ToLow();
1024 }
1025 }
1026
1027 // We're moving two or three locations to locations that could
1028 // overlap, so we need a parallel move resolver.
1029 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1030 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1031 parallel_move.AddMove(ref_,
1032 LocationFrom(calling_convention.GetRegisterAt(0)),
1033 Primitive::kPrimNot,
1034 nullptr);
1035 parallel_move.AddMove(obj_,
1036 LocationFrom(calling_convention.GetRegisterAt(1)),
1037 Primitive::kPrimNot,
1038 nullptr);
1039 if (index.IsValid()) {
1040 parallel_move.AddMove(index,
1041 LocationFrom(calling_convention.GetRegisterAt(2)),
1042 Primitive::kPrimInt,
1043 nullptr);
1044 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1045 } else {
1046 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1047 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1048 }
1049 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1050 CheckEntrypointTypes<
1051 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1052 arm_codegen->Move32(out_, LocationFrom(r0));
1053
1054 RestoreLiveRegisters(codegen, locations);
1055 __ B(GetExitLabel());
1056 }
1057
1058 const char* GetDescription() const OVERRIDE {
1059 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1060 }
1061
1062 private:
1063 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1064 uint32_t ref = RegisterFrom(ref_).GetCode();
1065 uint32_t obj = RegisterFrom(obj_).GetCode();
1066 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1067 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1068 return vixl32::Register(i);
1069 }
1070 }
1071 // We shall never fail to find a free caller-save register, as
1072 // there are more than two core caller-save registers on ARM
1073 // (meaning it is possible to find one which is different from
1074 // `ref` and `obj`).
1075 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1076 LOG(FATAL) << "Could not find a free caller-save register";
1077 UNREACHABLE();
1078 }
1079
1080 const Location out_;
1081 const Location ref_;
1082 const Location obj_;
1083 const uint32_t offset_;
1084 // An additional location containing an index to an array.
1085 // Only used for HArrayGet and the UnsafeGetObject &
1086 // UnsafeGetObjectVolatile intrinsics.
1087 const Location index_;
1088
1089 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1090};
1091
1092// Slow path generating a read barrier for a GC root.
1093class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1094 public:
1095 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1096 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1097 DCHECK(kEmitCompilerReadBarrier);
1098 }
1099
1100 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1101 LocationSummary* locations = instruction_->GetLocations();
1102 vixl32::Register reg_out = RegisterFrom(out_);
1103 DCHECK(locations->CanCall());
1104 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1105 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1106 << "Unexpected instruction in read barrier for GC root slow path: "
1107 << instruction_->DebugName();
1108
1109 __ Bind(GetEntryLabel());
1110 SaveLiveRegisters(codegen, locations);
1111
1112 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1113 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1114 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1115 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1116 instruction_,
1117 instruction_->GetDexPc(),
1118 this);
1119 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1120 arm_codegen->Move32(out_, LocationFrom(r0));
1121
1122 RestoreLiveRegisters(codegen, locations);
1123 __ B(GetExitLabel());
1124 }
1125
1126 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1127
1128 private:
1129 const Location out_;
1130 const Location root_;
1131
1132 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1133};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001134
Scott Wakelingfe885462016-09-22 10:24:38 +01001135inline vixl32::Condition ARMCondition(IfCondition cond) {
1136 switch (cond) {
1137 case kCondEQ: return eq;
1138 case kCondNE: return ne;
1139 case kCondLT: return lt;
1140 case kCondLE: return le;
1141 case kCondGT: return gt;
1142 case kCondGE: return ge;
1143 case kCondB: return lo;
1144 case kCondBE: return ls;
1145 case kCondA: return hi;
1146 case kCondAE: return hs;
1147 }
1148 LOG(FATAL) << "Unreachable";
1149 UNREACHABLE();
1150}
1151
1152// Maps signed condition to unsigned condition.
1153inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1154 switch (cond) {
1155 case kCondEQ: return eq;
1156 case kCondNE: return ne;
1157 // Signed to unsigned.
1158 case kCondLT: return lo;
1159 case kCondLE: return ls;
1160 case kCondGT: return hi;
1161 case kCondGE: return hs;
1162 // Unsigned remain unchanged.
1163 case kCondB: return lo;
1164 case kCondBE: return ls;
1165 case kCondA: return hi;
1166 case kCondAE: return hs;
1167 }
1168 LOG(FATAL) << "Unreachable";
1169 UNREACHABLE();
1170}
1171
1172inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1173 // The ARM condition codes can express all the necessary branches, see the
1174 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1175 // There is no dex instruction or HIR that would need the missing conditions
1176 // "equal or unordered" or "not equal".
1177 switch (cond) {
1178 case kCondEQ: return eq;
1179 case kCondNE: return ne /* unordered */;
1180 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1181 case kCondLE: return gt_bias ? ls : le /* unordered */;
1182 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1183 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1184 default:
1185 LOG(FATAL) << "UNREACHABLE";
1186 UNREACHABLE();
1187 }
1188}
1189
Scott Wakelingfe885462016-09-22 10:24:38 +01001190void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1191 stream << vixl32::Register(reg);
1192}
1193
1194void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1195 stream << vixl32::SRegister(reg);
1196}
1197
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001198static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001199 uint32_t mask = 0;
1200 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1201 i <= regs.GetLastSRegister().GetCode();
1202 ++i) {
1203 mask |= (1 << i);
1204 }
1205 return mask;
1206}
1207
Artem Serovd4cc5b22016-11-04 11:19:09 +00001208// Saves the register in the stack. Returns the size taken on stack.
1209size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1210 uint32_t reg_id ATTRIBUTE_UNUSED) {
1211 TODO_VIXL32(FATAL);
1212 return 0;
1213}
1214
1215// Restores the register from the stack. Returns the size taken on stack.
1216size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1217 uint32_t reg_id ATTRIBUTE_UNUSED) {
1218 TODO_VIXL32(FATAL);
1219 return 0;
1220}
1221
1222size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1223 uint32_t reg_id ATTRIBUTE_UNUSED) {
1224 TODO_VIXL32(FATAL);
1225 return 0;
1226}
1227
1228size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1229 uint32_t reg_id ATTRIBUTE_UNUSED) {
1230 TODO_VIXL32(FATAL);
1231 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001232}
1233
Scott Wakelingfe885462016-09-22 10:24:38 +01001234#undef __
1235
1236CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1237 const ArmInstructionSetFeatures& isa_features,
1238 const CompilerOptions& compiler_options,
1239 OptimizingCompilerStats* stats)
1240 : CodeGenerator(graph,
1241 kNumberOfCoreRegisters,
1242 kNumberOfSRegisters,
1243 kNumberOfRegisterPairs,
1244 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001245 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001246 compiler_options,
1247 stats),
1248 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001249 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001250 location_builder_(graph, this),
1251 instruction_visitor_(graph, this),
1252 move_resolver_(graph->GetArena(), this),
1253 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001254 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001255 uint32_literals_(std::less<uint32_t>(),
1256 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001257 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001258 boot_image_string_patches_(StringReferenceValueComparator(),
1259 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001260 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001261 boot_image_type_patches_(TypeReferenceValueComparator(),
1262 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1263 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1264 boot_image_address_patches_(std::less<uint32_t>(),
1265 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1266 jit_string_patches_(StringReferenceValueComparator(),
1267 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1268 jit_class_patches_(TypeReferenceValueComparator(),
1269 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001270 // Always save the LR register to mimic Quick.
1271 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001272 // Give d14 and d15 as scratch registers to VIXL.
1273 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1274 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1275 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1276 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1277 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1278 // also want to investigate giving those 14 other D registers to the allocator.
1279 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1280 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001281}
1282
Artem Serov551b28f2016-10-18 19:11:30 +01001283void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1284 uint32_t num_entries = switch_instr_->GetNumEntries();
1285 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1286
1287 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001288 // underlying code buffer and we have generated a jump table of the right size, using
1289 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001290 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1291 num_entries * sizeof(int32_t),
1292 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001293 // TODO(VIXL): Check that using lower case bind is fine here.
1294 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001295 for (uint32_t i = 0; i < num_entries; i++) {
1296 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1297 }
1298}
1299
1300void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1301 uint32_t num_entries = switch_instr_->GetNumEntries();
1302 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1303
Artem Serov551b28f2016-10-18 19:11:30 +01001304 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1305 for (uint32_t i = 0; i < num_entries; i++) {
1306 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1307 DCHECK(target_label->IsBound());
1308 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1309 // When doing BX to address we need to have lower bit set to 1 in T32.
1310 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1311 jump_offset++;
1312 }
1313 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1314 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001315
Scott Wakelingb77051e2016-11-21 19:46:00 +00001316 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001317 }
1318}
1319
Artem Serov09a940d2016-11-11 16:15:11 +00001320void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001321 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001322 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001323 }
1324}
1325
Andreas Gampeca620d72016-11-08 08:09:33 -08001326#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001327
1328void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001329 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001330 GetAssembler()->FinalizeCode();
1331 CodeGenerator::Finalize(allocator);
1332}
1333
1334void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001335 // Stack register, LR and PC are always reserved.
1336 blocked_core_registers_[SP] = true;
1337 blocked_core_registers_[LR] = true;
1338 blocked_core_registers_[PC] = true;
1339
1340 // Reserve thread register.
1341 blocked_core_registers_[TR] = true;
1342
1343 // Reserve temp register.
1344 blocked_core_registers_[IP] = true;
1345
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001346 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1347 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1348 blocked_fpu_registers_[28] = true;
1349 blocked_fpu_registers_[29] = true;
1350 blocked_fpu_registers_[30] = true;
1351 blocked_fpu_registers_[31] = true;
1352
Scott Wakelingfe885462016-09-22 10:24:38 +01001353 if (GetGraph()->IsDebuggable()) {
1354 // Stubs do not save callee-save floating point registers. If the graph
1355 // is debuggable, we need to deal with these registers differently. For
1356 // now, just block them.
1357 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1358 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1359 ++i) {
1360 blocked_fpu_registers_[i] = true;
1361 }
1362 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001363}
1364
Scott Wakelingfe885462016-09-22 10:24:38 +01001365InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1366 CodeGeneratorARMVIXL* codegen)
1367 : InstructionCodeGenerator(graph, codegen),
1368 assembler_(codegen->GetAssembler()),
1369 codegen_(codegen) {}
1370
1371void CodeGeneratorARMVIXL::ComputeSpillMask() {
1372 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1373 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1374 // There is no easy instruction to restore just the PC on thumb2. We spill and
1375 // restore another arbitrary register.
1376 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1377 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1378 // We use vpush and vpop for saving and restoring floating point registers, which take
1379 // a SRegister and the number of registers to save/restore after that SRegister. We
1380 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1381 // but in the range.
1382 if (fpu_spill_mask_ != 0) {
1383 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1384 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1385 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1386 fpu_spill_mask_ |= (1 << i);
1387 }
1388 }
1389}
1390
1391void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1392 bool skip_overflow_check =
1393 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1394 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1395 __ Bind(&frame_entry_label_);
1396
1397 if (HasEmptyFrame()) {
1398 return;
1399 }
1400
Scott Wakelingfe885462016-09-22 10:24:38 +01001401 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001402 UseScratchRegisterScope temps(GetVIXLAssembler());
1403 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001404 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001405 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001406 ExactAssemblyScope aas(GetVIXLAssembler(),
1407 vixl32::kMaxInstructionSizeInBytes,
1408 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001409 __ ldr(temp, MemOperand(temp));
1410 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001411 }
1412
1413 __ Push(RegisterList(core_spill_mask_));
1414 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1415 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1416 0,
1417 core_spill_mask_,
1418 kArmWordSize);
1419 if (fpu_spill_mask_ != 0) {
1420 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1421
1422 // Check that list is contiguous.
1423 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1424
1425 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1426 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001427 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001428 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001429
1430 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1431 UseScratchRegisterScope temps(GetVIXLAssembler());
1432 vixl32::Register temp = temps.Acquire();
1433 // Initialize should_deoptimize flag to 0.
1434 __ Mov(temp, 0);
1435 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1436 }
1437
Scott Wakelingfe885462016-09-22 10:24:38 +01001438 int adjust = GetFrameSize() - FrameEntrySpillSize();
1439 __ Sub(sp, sp, adjust);
1440 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001441
1442 // Save the current method if we need it. Note that we do not
1443 // do this in HCurrentMethod, as the instruction might have been removed
1444 // in the SSA graph.
1445 if (RequiresCurrentMethod()) {
1446 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1447 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001448}
1449
1450void CodeGeneratorARMVIXL::GenerateFrameExit() {
1451 if (HasEmptyFrame()) {
1452 __ Bx(lr);
1453 return;
1454 }
1455 GetAssembler()->cfi().RememberState();
1456 int adjust = GetFrameSize() - FrameEntrySpillSize();
1457 __ Add(sp, sp, adjust);
1458 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1459 if (fpu_spill_mask_ != 0) {
1460 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1461
1462 // Check that list is contiguous.
1463 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1464
1465 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1466 GetAssembler()->cfi().AdjustCFAOffset(
1467 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001468 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001469 }
1470 // Pop LR into PC to return.
1471 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1472 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1473 __ Pop(RegisterList(pop_mask));
1474 GetAssembler()->cfi().RestoreState();
1475 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1476}
1477
1478void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1479 __ Bind(GetLabelOf(block));
1480}
1481
Artem Serovd4cc5b22016-11-04 11:19:09 +00001482Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1483 switch (type) {
1484 case Primitive::kPrimBoolean:
1485 case Primitive::kPrimByte:
1486 case Primitive::kPrimChar:
1487 case Primitive::kPrimShort:
1488 case Primitive::kPrimInt:
1489 case Primitive::kPrimNot: {
1490 uint32_t index = gp_index_++;
1491 uint32_t stack_index = stack_index_++;
1492 if (index < calling_convention.GetNumberOfRegisters()) {
1493 return LocationFrom(calling_convention.GetRegisterAt(index));
1494 } else {
1495 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1496 }
1497 }
1498
1499 case Primitive::kPrimLong: {
1500 uint32_t index = gp_index_;
1501 uint32_t stack_index = stack_index_;
1502 gp_index_ += 2;
1503 stack_index_ += 2;
1504 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1505 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1506 // Skip R1, and use R2_R3 instead.
1507 gp_index_++;
1508 index++;
1509 }
1510 }
1511 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1512 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1513 calling_convention.GetRegisterAt(index + 1).GetCode());
1514
1515 return LocationFrom(calling_convention.GetRegisterAt(index),
1516 calling_convention.GetRegisterAt(index + 1));
1517 } else {
1518 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1519 }
1520 }
1521
1522 case Primitive::kPrimFloat: {
1523 uint32_t stack_index = stack_index_++;
1524 if (float_index_ % 2 == 0) {
1525 float_index_ = std::max(double_index_, float_index_);
1526 }
1527 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1528 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1529 } else {
1530 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1531 }
1532 }
1533
1534 case Primitive::kPrimDouble: {
1535 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1536 uint32_t stack_index = stack_index_;
1537 stack_index_ += 2;
1538 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1539 uint32_t index = double_index_;
1540 double_index_ += 2;
1541 Location result = LocationFrom(
1542 calling_convention.GetFpuRegisterAt(index),
1543 calling_convention.GetFpuRegisterAt(index + 1));
1544 DCHECK(ExpectedPairLayout(result));
1545 return result;
1546 } else {
1547 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1548 }
1549 }
1550
1551 case Primitive::kPrimVoid:
1552 LOG(FATAL) << "Unexpected parameter type " << type;
1553 break;
1554 }
1555 return Location::NoLocation();
1556}
1557
1558Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1559 switch (type) {
1560 case Primitive::kPrimBoolean:
1561 case Primitive::kPrimByte:
1562 case Primitive::kPrimChar:
1563 case Primitive::kPrimShort:
1564 case Primitive::kPrimInt:
1565 case Primitive::kPrimNot: {
1566 return LocationFrom(r0);
1567 }
1568
1569 case Primitive::kPrimFloat: {
1570 return LocationFrom(s0);
1571 }
1572
1573 case Primitive::kPrimLong: {
1574 return LocationFrom(r0, r1);
1575 }
1576
1577 case Primitive::kPrimDouble: {
1578 return LocationFrom(s0, s1);
1579 }
1580
1581 case Primitive::kPrimVoid:
1582 return Location::NoLocation();
1583 }
1584
1585 UNREACHABLE();
1586}
1587
1588Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1589 return LocationFrom(kMethodRegister);
1590}
1591
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001592void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1593 if (source.Equals(destination)) {
1594 return;
1595 }
1596 if (destination.IsRegister()) {
1597 if (source.IsRegister()) {
1598 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1599 } else if (source.IsFpuRegister()) {
1600 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1601 } else {
1602 GetAssembler()->LoadFromOffset(kLoadWord,
1603 RegisterFrom(destination),
1604 sp,
1605 source.GetStackIndex());
1606 }
1607 } else if (destination.IsFpuRegister()) {
1608 if (source.IsRegister()) {
1609 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1610 } else if (source.IsFpuRegister()) {
1611 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1612 } else {
1613 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1614 }
1615 } else {
1616 DCHECK(destination.IsStackSlot()) << destination;
1617 if (source.IsRegister()) {
1618 GetAssembler()->StoreToOffset(kStoreWord,
1619 RegisterFrom(source),
1620 sp,
1621 destination.GetStackIndex());
1622 } else if (source.IsFpuRegister()) {
1623 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1624 } else {
1625 DCHECK(source.IsStackSlot()) << source;
1626 UseScratchRegisterScope temps(GetVIXLAssembler());
1627 vixl32::Register temp = temps.Acquire();
1628 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1629 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1630 }
1631 }
1632}
1633
Artem Serovcfbe9132016-10-14 15:58:56 +01001634void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1635 DCHECK(location.IsRegister());
1636 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001637}
1638
1639void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001640 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1641 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1642 HParallelMove move(GetGraph()->GetArena());
1643 move.AddMove(src, dst, dst_type, nullptr);
1644 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001645}
1646
Artem Serovcfbe9132016-10-14 15:58:56 +01001647void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1648 if (location.IsRegister()) {
1649 locations->AddTemp(location);
1650 } else if (location.IsRegisterPair()) {
1651 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1652 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1653 } else {
1654 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1655 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001656}
1657
1658void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1659 HInstruction* instruction,
1660 uint32_t dex_pc,
1661 SlowPathCode* slow_path) {
1662 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001663 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1664 // Ensure the pc position is recorded immediately after the `blx` instruction.
1665 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00001666 ExactAssemblyScope aas(GetVIXLAssembler(),
1667 vixl32::k16BitT32InstructionSizeInBytes,
1668 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001669 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001670 if (EntrypointRequiresStackMap(entrypoint)) {
1671 RecordPcInfo(instruction, dex_pc, slow_path);
1672 }
1673}
1674
1675void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1676 HInstruction* instruction,
1677 SlowPathCode* slow_path) {
1678 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001679 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001680 __ Blx(lr);
1681}
1682
Scott Wakelingfe885462016-09-22 10:24:38 +01001683void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1684 DCHECK(!successor->IsExitBlock());
1685 HBasicBlock* block = got->GetBlock();
1686 HInstruction* previous = got->GetPrevious();
1687 HLoopInformation* info = block->GetLoopInformation();
1688
1689 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1690 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1691 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1692 return;
1693 }
1694 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1695 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1696 }
1697 if (!codegen_->GoesToNextBlock(block, successor)) {
1698 __ B(codegen_->GetLabelOf(successor));
1699 }
1700}
1701
1702void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1703 got->SetLocations(nullptr);
1704}
1705
1706void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1707 HandleGoto(got, got->GetSuccessor());
1708}
1709
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001710void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1711 try_boundary->SetLocations(nullptr);
1712}
1713
1714void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1715 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1716 if (!successor->IsExitBlock()) {
1717 HandleGoto(try_boundary, successor);
1718 }
1719}
1720
Scott Wakelingfe885462016-09-22 10:24:38 +01001721void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1722 exit->SetLocations(nullptr);
1723}
1724
1725void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1726}
1727
1728void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1729 Primitive::Type type = instruction->InputAt(0)->GetType();
1730 Location lhs_loc = instruction->GetLocations()->InAt(0);
1731 Location rhs_loc = instruction->GetLocations()->InAt(1);
1732 if (rhs_loc.IsConstant()) {
1733 // 0.0 is the only immediate that can be encoded directly in
1734 // a VCMP instruction.
1735 //
1736 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1737 // specify that in a floating-point comparison, positive zero
1738 // and negative zero are considered equal, so we can use the
1739 // literal 0.0 for both cases here.
1740 //
1741 // Note however that some methods (Float.equal, Float.compare,
1742 // Float.compareTo, Double.equal, Double.compare,
1743 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1744 // StrictMath.min) consider 0.0 to be (strictly) greater than
1745 // -0.0. So if we ever translate calls to these methods into a
1746 // HCompare instruction, we must handle the -0.0 case with
1747 // care here.
1748 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1749 if (type == Primitive::kPrimFloat) {
1750 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1751 } else {
1752 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001753 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001754 }
1755 } else {
1756 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001757 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001758 } else {
1759 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001760 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001761 }
1762 }
1763}
1764
1765void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1766 vixl32::Label* true_label,
1767 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1768 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1769 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1770 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1771}
1772
1773void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1774 vixl32::Label* true_label,
1775 vixl32::Label* false_label) {
1776 LocationSummary* locations = cond->GetLocations();
1777 Location left = locations->InAt(0);
1778 Location right = locations->InAt(1);
1779 IfCondition if_cond = cond->GetCondition();
1780
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001781 vixl32::Register left_high = HighRegisterFrom(left);
1782 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01001783 IfCondition true_high_cond = if_cond;
1784 IfCondition false_high_cond = cond->GetOppositeCondition();
1785 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
1786
1787 // Set the conditions for the test, remembering that == needs to be
1788 // decided using the low words.
1789 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1790 switch (if_cond) {
1791 case kCondEQ:
1792 case kCondNE:
1793 // Nothing to do.
1794 break;
1795 case kCondLT:
1796 false_high_cond = kCondGT;
1797 break;
1798 case kCondLE:
1799 true_high_cond = kCondLT;
1800 break;
1801 case kCondGT:
1802 false_high_cond = kCondLT;
1803 break;
1804 case kCondGE:
1805 true_high_cond = kCondGT;
1806 break;
1807 case kCondB:
1808 false_high_cond = kCondA;
1809 break;
1810 case kCondBE:
1811 true_high_cond = kCondB;
1812 break;
1813 case kCondA:
1814 false_high_cond = kCondB;
1815 break;
1816 case kCondAE:
1817 true_high_cond = kCondA;
1818 break;
1819 }
1820 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001821 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001822 int32_t val_low = Low32Bits(value);
1823 int32_t val_high = High32Bits(value);
1824
1825 __ Cmp(left_high, val_high);
1826 if (if_cond == kCondNE) {
1827 __ B(ARMCondition(true_high_cond), true_label);
1828 } else if (if_cond == kCondEQ) {
1829 __ B(ARMCondition(false_high_cond), false_label);
1830 } else {
1831 __ B(ARMCondition(true_high_cond), true_label);
1832 __ B(ARMCondition(false_high_cond), false_label);
1833 }
1834 // Must be equal high, so compare the lows.
1835 __ Cmp(left_low, val_low);
1836 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001837 vixl32::Register right_high = HighRegisterFrom(right);
1838 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001839
1840 __ Cmp(left_high, right_high);
1841 if (if_cond == kCondNE) {
1842 __ B(ARMCondition(true_high_cond), true_label);
1843 } else if (if_cond == kCondEQ) {
1844 __ B(ARMCondition(false_high_cond), false_label);
1845 } else {
1846 __ B(ARMCondition(true_high_cond), true_label);
1847 __ B(ARMCondition(false_high_cond), false_label);
1848 }
1849 // Must be equal high, so compare the lows.
1850 __ Cmp(left_low, right_low);
1851 }
1852 // The last comparison might be unsigned.
1853 // TODO: optimize cases where this is always true/false
1854 __ B(final_condition, true_label);
1855}
1856
1857void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1858 vixl32::Label* true_target_in,
1859 vixl32::Label* false_target_in) {
1860 // Generated branching requires both targets to be explicit. If either of the
1861 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1862 vixl32::Label fallthrough;
1863 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1864 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1865
1866 Primitive::Type type = condition->InputAt(0)->GetType();
1867 switch (type) {
1868 case Primitive::kPrimLong:
1869 GenerateLongComparesAndJumps(condition, true_target, false_target);
1870 break;
1871 case Primitive::kPrimFloat:
1872 case Primitive::kPrimDouble:
1873 GenerateVcmp(condition);
1874 GenerateFPJumps(condition, true_target, false_target);
1875 break;
1876 default:
1877 LOG(FATAL) << "Unexpected compare type " << type;
1878 }
1879
1880 if (false_target != &fallthrough) {
1881 __ B(false_target);
1882 }
1883
1884 if (true_target_in == nullptr || false_target_in == nullptr) {
1885 __ Bind(&fallthrough);
1886 }
1887}
1888
1889void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1890 size_t condition_input_index,
1891 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00001892 vixl32::Label* false_target,
1893 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001894 HInstruction* cond = instruction->InputAt(condition_input_index);
1895
1896 if (true_target == nullptr && false_target == nullptr) {
1897 // Nothing to do. The code always falls through.
1898 return;
1899 } else if (cond->IsIntConstant()) {
1900 // Constant condition, statically compared against "true" (integer value 1).
1901 if (cond->AsIntConstant()->IsTrue()) {
1902 if (true_target != nullptr) {
1903 __ B(true_target);
1904 }
1905 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00001906 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01001907 if (false_target != nullptr) {
1908 __ B(false_target);
1909 }
1910 }
1911 return;
1912 }
1913
1914 // The following code generates these patterns:
1915 // (1) true_target == nullptr && false_target != nullptr
1916 // - opposite condition true => branch to false_target
1917 // (2) true_target != nullptr && false_target == nullptr
1918 // - condition true => branch to true_target
1919 // (3) true_target != nullptr && false_target != nullptr
1920 // - condition true => branch to true_target
1921 // - branch to false_target
1922 if (IsBooleanValueOrMaterializedCondition(cond)) {
1923 // Condition has been materialized, compare the output to 0.
1924 if (kIsDebugBuild) {
1925 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1926 DCHECK(cond_val.IsRegister());
1927 }
1928 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001929 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
1930 false_target,
1931 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001932 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001933 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
1934 true_target,
1935 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001936 }
1937 } else {
1938 // Condition has not been materialized. Use its inputs as the comparison and
1939 // its condition as the branch condition.
1940 HCondition* condition = cond->AsCondition();
1941
1942 // If this is a long or FP comparison that has been folded into
1943 // the HCondition, generate the comparison directly.
1944 Primitive::Type type = condition->InputAt(0)->GetType();
1945 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1946 GenerateCompareTestAndBranch(condition, true_target, false_target);
1947 return;
1948 }
1949
1950 LocationSummary* locations = cond->GetLocations();
1951 DCHECK(locations->InAt(0).IsRegister());
1952 vixl32::Register left = InputRegisterAt(cond, 0);
1953 Location right = locations->InAt(1);
1954 if (right.IsRegister()) {
1955 __ Cmp(left, InputRegisterAt(cond, 1));
1956 } else {
1957 DCHECK(right.IsConstant());
1958 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1959 }
1960 if (true_target == nullptr) {
1961 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1962 } else {
1963 __ B(ARMCondition(condition->GetCondition()), true_target);
1964 }
1965 }
1966
1967 // If neither branch falls through (case 3), the conditional branch to `true_target`
1968 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1969 if (true_target != nullptr && false_target != nullptr) {
1970 __ B(false_target);
1971 }
1972}
1973
1974void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1975 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1976 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 }
1979}
1980
1981void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1982 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1983 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001984 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1985 nullptr : codegen_->GetLabelOf(true_successor);
1986 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1987 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001988 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1989}
1990
Scott Wakelingc34dba72016-10-03 10:14:44 +01001991void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1992 LocationSummary* locations = new (GetGraph()->GetArena())
1993 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1994 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1995 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 }
1998}
1999
2000void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2001 SlowPathCodeARMVIXL* slow_path =
2002 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2003 GenerateTestAndBranch(deoptimize,
2004 /* condition_input_index */ 0,
2005 slow_path->GetEntryLabel(),
2006 /* false_target */ nullptr);
2007}
2008
Artem Serovd4cc5b22016-11-04 11:19:09 +00002009void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2010 LocationSummary* locations = new (GetGraph()->GetArena())
2011 LocationSummary(flag, LocationSummary::kNoCall);
2012 locations->SetOut(Location::RequiresRegister());
2013}
2014
2015void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2016 GetAssembler()->LoadFromOffset(kLoadWord,
2017 OutputRegister(flag),
2018 sp,
2019 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2020}
2021
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002022void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2024 if (Primitive::IsFloatingPointType(select->GetType())) {
2025 locations->SetInAt(0, Location::RequiresFpuRegister());
2026 locations->SetInAt(1, Location::RequiresFpuRegister());
2027 } else {
2028 locations->SetInAt(0, Location::RequiresRegister());
2029 locations->SetInAt(1, Location::RequiresRegister());
2030 }
2031 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2032 locations->SetInAt(2, Location::RequiresRegister());
2033 }
2034 locations->SetOut(Location::SameAsFirstInput());
2035}
2036
2037void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2038 LocationSummary* locations = select->GetLocations();
2039 vixl32::Label false_target;
2040 GenerateTestAndBranch(select,
2041 /* condition_input_index */ 2,
2042 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002043 &false_target,
2044 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002045 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2046 __ Bind(&false_target);
2047}
2048
Artem Serov551b28f2016-10-18 19:11:30 +01002049void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2050 new (GetGraph()->GetArena()) LocationSummary(info);
2051}
2052
2053void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2054 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2055}
2056
Scott Wakelingfe885462016-09-22 10:24:38 +01002057void CodeGeneratorARMVIXL::GenerateNop() {
2058 __ Nop();
2059}
2060
2061void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2064 // Handle the long/FP comparisons made in instruction simplification.
2065 switch (cond->InputAt(0)->GetType()) {
2066 case Primitive::kPrimLong:
2067 locations->SetInAt(0, Location::RequiresRegister());
2068 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2069 if (!cond->IsEmittedAtUseSite()) {
2070 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2071 }
2072 break;
2073
Scott Wakelingfe885462016-09-22 10:24:38 +01002074 case Primitive::kPrimFloat:
2075 case Primitive::kPrimDouble:
2076 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002077 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002078 if (!cond->IsEmittedAtUseSite()) {
2079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2080 }
2081 break;
2082
2083 default:
2084 locations->SetInAt(0, Location::RequiresRegister());
2085 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2086 if (!cond->IsEmittedAtUseSite()) {
2087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2088 }
2089 }
2090}
2091
2092void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2093 if (cond->IsEmittedAtUseSite()) {
2094 return;
2095 }
2096
Artem Serov657022c2016-11-23 14:19:38 +00002097 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002098 vixl32::Register out = OutputRegister(cond);
2099 vixl32::Label true_label, false_label;
2100
2101 switch (cond->InputAt(0)->GetType()) {
2102 default: {
2103 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002104 if (right.IsRegister()) {
2105 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2106 } else {
2107 DCHECK(right.IsConstant());
2108 __ Cmp(InputRegisterAt(cond, 0),
2109 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2110 }
Artem Serov0fb37192016-12-06 18:13:40 +00002111 ExactAssemblyScope aas(GetVIXLAssembler(),
2112 3 * vixl32::kMaxInstructionSizeInBytes,
2113 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002114 __ ite(ARMCondition(cond->GetCondition()));
2115 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2116 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002117 return;
2118 }
2119 case Primitive::kPrimLong:
2120 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2121 break;
2122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble:
2124 GenerateVcmp(cond);
2125 GenerateFPJumps(cond, &true_label, &false_label);
2126 break;
2127 }
2128
2129 // Convert the jumps into the result.
2130 vixl32::Label done_label;
2131
2132 // False case: result = 0.
2133 __ Bind(&false_label);
2134 __ Mov(out, 0);
2135 __ B(&done_label);
2136
2137 // True case: result = 1.
2138 __ Bind(&true_label);
2139 __ Mov(out, 1);
2140 __ Bind(&done_label);
2141}
2142
2143void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2144 HandleCondition(comp);
2145}
2146
2147void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2148 HandleCondition(comp);
2149}
2150
2151void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2152 HandleCondition(comp);
2153}
2154
2155void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2156 HandleCondition(comp);
2157}
2158
2159void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2160 HandleCondition(comp);
2161}
2162
2163void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2164 HandleCondition(comp);
2165}
2166
2167void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2168 HandleCondition(comp);
2169}
2170
2171void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2172 HandleCondition(comp);
2173}
2174
2175void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2176 HandleCondition(comp);
2177}
2178
2179void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2180 HandleCondition(comp);
2181}
2182
2183void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2184 HandleCondition(comp);
2185}
2186
2187void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2188 HandleCondition(comp);
2189}
2190
2191void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2192 HandleCondition(comp);
2193}
2194
2195void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2196 HandleCondition(comp);
2197}
2198
2199void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2200 HandleCondition(comp);
2201}
2202
2203void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2204 HandleCondition(comp);
2205}
2206
2207void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2208 HandleCondition(comp);
2209}
2210
2211void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2212 HandleCondition(comp);
2213}
2214
2215void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2216 HandleCondition(comp);
2217}
2218
2219void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2220 HandleCondition(comp);
2221}
2222
2223void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2226 locations->SetOut(Location::ConstantLocation(constant));
2227}
2228
2229void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2230 // Will be generated at use site.
2231}
2232
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002233void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2236 locations->SetOut(Location::ConstantLocation(constant));
2237}
2238
2239void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2240 // Will be generated at use site.
2241}
2242
Scott Wakelingfe885462016-09-22 10:24:38 +01002243void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2246 locations->SetOut(Location::ConstantLocation(constant));
2247}
2248
2249void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2250 // Will be generated at use site.
2251}
2252
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002253void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2254 LocationSummary* locations =
2255 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2256 locations->SetOut(Location::ConstantLocation(constant));
2257}
2258
Scott Wakelingc34dba72016-10-03 10:14:44 +01002259void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2260 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002261 // Will be generated at use site.
2262}
2263
2264void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2265 LocationSummary* locations =
2266 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2267 locations->SetOut(Location::ConstantLocation(constant));
2268}
2269
Scott Wakelingc34dba72016-10-03 10:14:44 +01002270void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2271 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002272 // Will be generated at use site.
2273}
2274
Scott Wakelingfe885462016-09-22 10:24:38 +01002275void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2276 memory_barrier->SetLocations(nullptr);
2277}
2278
2279void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2280 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2281}
2282
2283void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2284 ret->SetLocations(nullptr);
2285}
2286
2287void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2288 codegen_->GenerateFrameExit();
2289}
2290
2291void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2292 LocationSummary* locations =
2293 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2294 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2295}
2296
2297void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2298 codegen_->GenerateFrameExit();
2299}
2300
Artem Serovcfbe9132016-10-14 15:58:56 +01002301void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 // The trampoline uses the same calling convention as dex calling conventions,
2303 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2304 // the method_idx.
2305 HandleInvoke(invoke);
2306}
2307
2308void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2309 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2310}
2311
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002312void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2313 // Explicit clinit checks triggered by static invokes must have been pruned by
2314 // art::PrepareForRegisterAllocation.
2315 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2316
Anton Kirilov5ec62182016-10-13 20:16:02 +01002317 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2318 if (intrinsic.TryDispatch(invoke)) {
2319 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2320 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2321 }
2322 return;
2323 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002324
2325 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002326
Artem Serovd4cc5b22016-11-04 11:19:09 +00002327 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2328 if (invoke->HasPcRelativeDexCache()) {
2329 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2330 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002331}
2332
Anton Kirilov5ec62182016-10-13 20:16:02 +01002333static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2334 if (invoke->GetLocations()->Intrinsified()) {
2335 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2336 intrinsic.Dispatch(invoke);
2337 return true;
2338 }
2339 return false;
2340}
2341
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002342void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2343 // Explicit clinit checks triggered by static invokes must have been pruned by
2344 // art::PrepareForRegisterAllocation.
2345 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2346
Anton Kirilov5ec62182016-10-13 20:16:02 +01002347 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2348 return;
2349 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002350
2351 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002352 codegen_->GenerateStaticOrDirectCall(
2353 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002354 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2355}
2356
2357void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002358 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002359 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2360}
2361
2362void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002363 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2364 if (intrinsic.TryDispatch(invoke)) {
2365 return;
2366 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002367
2368 HandleInvoke(invoke);
2369}
2370
2371void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002372 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2373 return;
2374 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002375
2376 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002377 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002378 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002379}
2380
Artem Serovcfbe9132016-10-14 15:58:56 +01002381void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2382 HandleInvoke(invoke);
2383 // Add the hidden argument.
2384 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2385}
2386
2387void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2388 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2389 LocationSummary* locations = invoke->GetLocations();
2390 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2391 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2392 Location receiver = locations->InAt(0);
2393 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2394
2395 DCHECK(!receiver.IsStackSlot());
2396
Alexandre Rames374ddf32016-11-04 10:40:49 +00002397 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2398 {
Artem Serov0fb37192016-12-06 18:13:40 +00002399 ExactAssemblyScope aas(GetVIXLAssembler(),
2400 vixl32::kMaxInstructionSizeInBytes,
2401 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002402 // /* HeapReference<Class> */ temp = receiver->klass_
2403 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2404 codegen_->MaybeRecordImplicitNullCheck(invoke);
2405 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002406 // Instead of simply (possibly) unpoisoning `temp` here, we should
2407 // emit a read barrier for the previous class reference load.
2408 // However this is not required in practice, as this is an
2409 // intermediate/temporary reference and because the current
2410 // concurrent copying collector keeps the from-space memory
2411 // intact/accessible until the end of the marking phase (the
2412 // concurrent copying collector may not in the future).
2413 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2414 GetAssembler()->LoadFromOffset(kLoadWord,
2415 temp,
2416 temp,
2417 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2418 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2419 invoke->GetImtIndex(), kArmPointerSize));
2420 // temp = temp->GetImtEntryAt(method_offset);
2421 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2422 uint32_t entry_point =
2423 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2424 // LR = temp->GetEntryPoint();
2425 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2426
2427 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2428 // instruction from clobbering it as they might use r12 as a scratch register.
2429 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002430
2431 {
2432 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2433 // so it checks if the application is using them (by passing them to the macro assembler
2434 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2435 // what is available, and is the opposite of the standard usage: Instead of requesting a
2436 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2437 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2438 // (to materialize the constant), since the destination register becomes available for such use
2439 // internally for the duration of the macro instruction.
2440 UseScratchRegisterScope temps(GetVIXLAssembler());
2441 temps.Exclude(hidden_reg);
2442 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2443 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002444 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002445 // Ensure the pc position is recorded immediately after the `blx` instruction.
2446 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002447 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002448 vixl32::k16BitT32InstructionSizeInBytes,
2449 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002450 // LR();
2451 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002452 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002453 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002454 }
2455}
2456
Orion Hodsonac141392017-01-13 11:53:47 +00002457void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2458 HandleInvoke(invoke);
2459}
2460
2461void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2462 codegen_->GenerateInvokePolymorphicCall(invoke);
2463}
2464
Artem Serov02109dd2016-09-23 17:17:54 +01002465void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2466 LocationSummary* locations =
2467 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2468 switch (neg->GetResultType()) {
2469 case Primitive::kPrimInt: {
2470 locations->SetInAt(0, Location::RequiresRegister());
2471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2472 break;
2473 }
2474 case Primitive::kPrimLong: {
2475 locations->SetInAt(0, Location::RequiresRegister());
2476 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2477 break;
2478 }
2479
2480 case Primitive::kPrimFloat:
2481 case Primitive::kPrimDouble:
2482 locations->SetInAt(0, Location::RequiresFpuRegister());
2483 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2484 break;
2485
2486 default:
2487 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2488 }
2489}
2490
2491void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2492 LocationSummary* locations = neg->GetLocations();
2493 Location out = locations->Out();
2494 Location in = locations->InAt(0);
2495 switch (neg->GetResultType()) {
2496 case Primitive::kPrimInt:
2497 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2498 break;
2499
2500 case Primitive::kPrimLong:
2501 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2502 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2503 // We cannot emit an RSC (Reverse Subtract with Carry)
2504 // instruction here, as it does not exist in the Thumb-2
2505 // instruction set. We use the following approach
2506 // using SBC and SUB instead.
2507 //
2508 // out.hi = -C
2509 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2510 // out.hi = out.hi - in.hi
2511 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2512 break;
2513
2514 case Primitive::kPrimFloat:
2515 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00002516 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01002517 break;
2518
2519 default:
2520 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2521 }
2522}
2523
Scott Wakelingfe885462016-09-22 10:24:38 +01002524void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2525 Primitive::Type result_type = conversion->GetResultType();
2526 Primitive::Type input_type = conversion->GetInputType();
2527 DCHECK_NE(result_type, input_type);
2528
2529 // The float-to-long, double-to-long and long-to-float type conversions
2530 // rely on a call to the runtime.
2531 LocationSummary::CallKind call_kind =
2532 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2533 && result_type == Primitive::kPrimLong)
2534 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2535 ? LocationSummary::kCallOnMainOnly
2536 : LocationSummary::kNoCall;
2537 LocationSummary* locations =
2538 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2539
2540 // The Java language does not allow treating boolean as an integral type but
2541 // our bit representation makes it safe.
2542
2543 switch (result_type) {
2544 case Primitive::kPrimByte:
2545 switch (input_type) {
2546 case Primitive::kPrimLong:
2547 // Type conversion from long to byte is a result of code transformations.
2548 case Primitive::kPrimBoolean:
2549 // Boolean input is a result of code transformations.
2550 case Primitive::kPrimShort:
2551 case Primitive::kPrimInt:
2552 case Primitive::kPrimChar:
2553 // Processing a Dex `int-to-byte' instruction.
2554 locations->SetInAt(0, Location::RequiresRegister());
2555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562 break;
2563
2564 case Primitive::kPrimShort:
2565 switch (input_type) {
2566 case Primitive::kPrimLong:
2567 // Type conversion from long to short is a result of code transformations.
2568 case Primitive::kPrimBoolean:
2569 // Boolean input is a result of code transformations.
2570 case Primitive::kPrimByte:
2571 case Primitive::kPrimInt:
2572 case Primitive::kPrimChar:
2573 // Processing a Dex `int-to-short' instruction.
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected type conversion from " << input_type
2580 << " to " << result_type;
2581 }
2582 break;
2583
2584 case Primitive::kPrimInt:
2585 switch (input_type) {
2586 case Primitive::kPrimLong:
2587 // Processing a Dex `long-to-int' instruction.
2588 locations->SetInAt(0, Location::Any());
2589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2590 break;
2591
2592 case Primitive::kPrimFloat:
2593 // Processing a Dex `float-to-int' instruction.
2594 locations->SetInAt(0, Location::RequiresFpuRegister());
2595 locations->SetOut(Location::RequiresRegister());
2596 locations->AddTemp(Location::RequiresFpuRegister());
2597 break;
2598
2599 case Primitive::kPrimDouble:
2600 // Processing a Dex `double-to-int' instruction.
2601 locations->SetInAt(0, Location::RequiresFpuRegister());
2602 locations->SetOut(Location::RequiresRegister());
2603 locations->AddTemp(Location::RequiresFpuRegister());
2604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type
2608 << " to " << result_type;
2609 }
2610 break;
2611
2612 case Primitive::kPrimLong:
2613 switch (input_type) {
2614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
2616 case Primitive::kPrimByte:
2617 case Primitive::kPrimShort:
2618 case Primitive::kPrimInt:
2619 case Primitive::kPrimChar:
2620 // Processing a Dex `int-to-long' instruction.
2621 locations->SetInAt(0, Location::RequiresRegister());
2622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2623 break;
2624
2625 case Primitive::kPrimFloat: {
2626 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002627 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2628 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2629 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002630 break;
2631 }
2632
2633 case Primitive::kPrimDouble: {
2634 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002635 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2636 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2637 calling_convention.GetFpuRegisterAt(1)));
2638 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002639 break;
2640 }
2641
2642 default:
2643 LOG(FATAL) << "Unexpected type conversion from " << input_type
2644 << " to " << result_type;
2645 }
2646 break;
2647
2648 case Primitive::kPrimChar:
2649 switch (input_type) {
2650 case Primitive::kPrimLong:
2651 // Type conversion from long to char is a result of code transformations.
2652 case Primitive::kPrimBoolean:
2653 // Boolean input is a result of code transformations.
2654 case Primitive::kPrimByte:
2655 case Primitive::kPrimShort:
2656 case Primitive::kPrimInt:
2657 // Processing a Dex `int-to-char' instruction.
2658 locations->SetInAt(0, Location::RequiresRegister());
2659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2660 break;
2661
2662 default:
2663 LOG(FATAL) << "Unexpected type conversion from " << input_type
2664 << " to " << result_type;
2665 }
2666 break;
2667
2668 case Primitive::kPrimFloat:
2669 switch (input_type) {
2670 case Primitive::kPrimBoolean:
2671 // Boolean input is a result of code transformations.
2672 case Primitive::kPrimByte:
2673 case Primitive::kPrimShort:
2674 case Primitive::kPrimInt:
2675 case Primitive::kPrimChar:
2676 // Processing a Dex `int-to-float' instruction.
2677 locations->SetInAt(0, Location::RequiresRegister());
2678 locations->SetOut(Location::RequiresFpuRegister());
2679 break;
2680
2681 case Primitive::kPrimLong: {
2682 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002683 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2684 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2685 calling_convention.GetRegisterAt(1)));
2686 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002687 break;
2688 }
2689
2690 case Primitive::kPrimDouble:
2691 // Processing a Dex `double-to-float' instruction.
2692 locations->SetInAt(0, Location::RequiresFpuRegister());
2693 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 };
2700 break;
2701
2702 case Primitive::kPrimDouble:
2703 switch (input_type) {
2704 case Primitive::kPrimBoolean:
2705 // Boolean input is a result of code transformations.
2706 case Primitive::kPrimByte:
2707 case Primitive::kPrimShort:
2708 case Primitive::kPrimInt:
2709 case Primitive::kPrimChar:
2710 // Processing a Dex `int-to-double' instruction.
2711 locations->SetInAt(0, Location::RequiresRegister());
2712 locations->SetOut(Location::RequiresFpuRegister());
2713 break;
2714
2715 case Primitive::kPrimLong:
2716 // Processing a Dex `long-to-double' instruction.
2717 locations->SetInAt(0, Location::RequiresRegister());
2718 locations->SetOut(Location::RequiresFpuRegister());
2719 locations->AddTemp(Location::RequiresFpuRegister());
2720 locations->AddTemp(Location::RequiresFpuRegister());
2721 break;
2722
2723 case Primitive::kPrimFloat:
2724 // Processing a Dex `float-to-double' instruction.
2725 locations->SetInAt(0, Location::RequiresFpuRegister());
2726 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2727 break;
2728
2729 default:
2730 LOG(FATAL) << "Unexpected type conversion from " << input_type
2731 << " to " << result_type;
2732 };
2733 break;
2734
2735 default:
2736 LOG(FATAL) << "Unexpected type conversion from " << input_type
2737 << " to " << result_type;
2738 }
2739}
2740
2741void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2742 LocationSummary* locations = conversion->GetLocations();
2743 Location out = locations->Out();
2744 Location in = locations->InAt(0);
2745 Primitive::Type result_type = conversion->GetResultType();
2746 Primitive::Type input_type = conversion->GetInputType();
2747 DCHECK_NE(result_type, input_type);
2748 switch (result_type) {
2749 case Primitive::kPrimByte:
2750 switch (input_type) {
2751 case Primitive::kPrimLong:
2752 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002753 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002754 break;
2755 case Primitive::kPrimBoolean:
2756 // Boolean input is a result of code transformations.
2757 case Primitive::kPrimShort:
2758 case Primitive::kPrimInt:
2759 case Primitive::kPrimChar:
2760 // Processing a Dex `int-to-byte' instruction.
2761 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
2768 break;
2769
2770 case Primitive::kPrimShort:
2771 switch (input_type) {
2772 case Primitive::kPrimLong:
2773 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002774 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002775 break;
2776 case Primitive::kPrimBoolean:
2777 // Boolean input is a result of code transformations.
2778 case Primitive::kPrimByte:
2779 case Primitive::kPrimInt:
2780 case Primitive::kPrimChar:
2781 // Processing a Dex `int-to-short' instruction.
2782 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2783 break;
2784
2785 default:
2786 LOG(FATAL) << "Unexpected type conversion from " << input_type
2787 << " to " << result_type;
2788 }
2789 break;
2790
2791 case Primitive::kPrimInt:
2792 switch (input_type) {
2793 case Primitive::kPrimLong:
2794 // Processing a Dex `long-to-int' instruction.
2795 DCHECK(out.IsRegister());
2796 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002797 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002798 } else if (in.IsDoubleStackSlot()) {
2799 GetAssembler()->LoadFromOffset(kLoadWord,
2800 OutputRegister(conversion),
2801 sp,
2802 in.GetStackIndex());
2803 } else {
2804 DCHECK(in.IsConstant());
2805 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00002806 int32_t value = Int32ConstantFrom(in);
2807 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002808 }
2809 break;
2810
2811 case Primitive::kPrimFloat: {
2812 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002813 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002814 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002815 __ Vmov(OutputRegister(conversion), temp);
2816 break;
2817 }
2818
2819 case Primitive::kPrimDouble: {
2820 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002821 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002822 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002823 __ Vmov(OutputRegister(conversion), temp_s);
2824 break;
2825 }
2826
2827 default:
2828 LOG(FATAL) << "Unexpected type conversion from " << input_type
2829 << " to " << result_type;
2830 }
2831 break;
2832
2833 case Primitive::kPrimLong:
2834 switch (input_type) {
2835 case Primitive::kPrimBoolean:
2836 // Boolean input is a result of code transformations.
2837 case Primitive::kPrimByte:
2838 case Primitive::kPrimShort:
2839 case Primitive::kPrimInt:
2840 case Primitive::kPrimChar:
2841 // Processing a Dex `int-to-long' instruction.
2842 DCHECK(out.IsRegisterPair());
2843 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002844 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002845 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002846 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01002847 break;
2848
2849 case Primitive::kPrimFloat:
2850 // Processing a Dex `float-to-long' instruction.
2851 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
2852 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2853 break;
2854
2855 case Primitive::kPrimDouble:
2856 // Processing a Dex `double-to-long' instruction.
2857 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
2858 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2859 break;
2860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 }
2865 break;
2866
2867 case Primitive::kPrimChar:
2868 switch (input_type) {
2869 case Primitive::kPrimLong:
2870 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002871 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002872 break;
2873 case Primitive::kPrimBoolean:
2874 // Boolean input is a result of code transformations.
2875 case Primitive::kPrimByte:
2876 case Primitive::kPrimShort:
2877 case Primitive::kPrimInt:
2878 // Processing a Dex `int-to-char' instruction.
2879 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2880 break;
2881
2882 default:
2883 LOG(FATAL) << "Unexpected type conversion from " << input_type
2884 << " to " << result_type;
2885 }
2886 break;
2887
2888 case Primitive::kPrimFloat:
2889 switch (input_type) {
2890 case Primitive::kPrimBoolean:
2891 // Boolean input is a result of code transformations.
2892 case Primitive::kPrimByte:
2893 case Primitive::kPrimShort:
2894 case Primitive::kPrimInt:
2895 case Primitive::kPrimChar: {
2896 // Processing a Dex `int-to-float' instruction.
2897 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002898 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01002899 break;
2900 }
2901
2902 case Primitive::kPrimLong:
2903 // Processing a Dex `long-to-float' instruction.
2904 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
2905 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2906 break;
2907
2908 case Primitive::kPrimDouble:
2909 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002910 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002911 break;
2912
2913 default:
2914 LOG(FATAL) << "Unexpected type conversion from " << input_type
2915 << " to " << result_type;
2916 };
2917 break;
2918
2919 case Primitive::kPrimDouble:
2920 switch (input_type) {
2921 case Primitive::kPrimBoolean:
2922 // Boolean input is a result of code transformations.
2923 case Primitive::kPrimByte:
2924 case Primitive::kPrimShort:
2925 case Primitive::kPrimInt:
2926 case Primitive::kPrimChar: {
2927 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002928 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002929 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01002930 break;
2931 }
2932
2933 case Primitive::kPrimLong: {
2934 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002935 vixl32::Register low = LowRegisterFrom(in);
2936 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002937 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002938 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002939 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002940 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002941 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002942
2943 // temp_d = int-to-double(high)
2944 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002945 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01002946 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002947 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002948 // out_d = unsigned-to-double(low)
2949 __ Vmov(out_s, low);
2950 __ Vcvt(F64, U32, out_d, out_s);
2951 // out_d += temp_d * constant_d
2952 __ Vmla(F64, out_d, temp_d, constant_d);
2953 break;
2954 }
2955
2956 case Primitive::kPrimFloat:
2957 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002958 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002959 break;
2960
2961 default:
2962 LOG(FATAL) << "Unexpected type conversion from " << input_type
2963 << " to " << result_type;
2964 };
2965 break;
2966
2967 default:
2968 LOG(FATAL) << "Unexpected type conversion from " << input_type
2969 << " to " << result_type;
2970 }
2971}
2972
2973void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2974 LocationSummary* locations =
2975 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2976 switch (add->GetResultType()) {
2977 case Primitive::kPrimInt: {
2978 locations->SetInAt(0, Location::RequiresRegister());
2979 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2981 break;
2982 }
2983
Scott Wakelingfe885462016-09-22 10:24:38 +01002984 case Primitive::kPrimLong: {
2985 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00002986 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01002987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2988 break;
2989 }
2990
2991 case Primitive::kPrimFloat:
2992 case Primitive::kPrimDouble: {
2993 locations->SetInAt(0, Location::RequiresFpuRegister());
2994 locations->SetInAt(1, Location::RequiresFpuRegister());
2995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2996 break;
2997 }
2998
2999 default:
3000 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3001 }
3002}
3003
3004void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3005 LocationSummary* locations = add->GetLocations();
3006 Location out = locations->Out();
3007 Location first = locations->InAt(0);
3008 Location second = locations->InAt(1);
3009
3010 switch (add->GetResultType()) {
3011 case Primitive::kPrimInt: {
3012 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3013 }
3014 break;
3015
Scott Wakelingfe885462016-09-22 10:24:38 +01003016 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003017 if (second.IsConstant()) {
3018 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3019 GenerateAddLongConst(out, first, value);
3020 } else {
3021 DCHECK(second.IsRegisterPair());
3022 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3023 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3024 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003025 break;
3026 }
3027
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003028 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003029 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003030 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003031 break;
3032
3033 default:
3034 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3035 }
3036}
3037
3038void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3039 LocationSummary* locations =
3040 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3041 switch (sub->GetResultType()) {
3042 case Primitive::kPrimInt: {
3043 locations->SetInAt(0, Location::RequiresRegister());
3044 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3045 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3046 break;
3047 }
3048
Scott Wakelingfe885462016-09-22 10:24:38 +01003049 case Primitive::kPrimLong: {
3050 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003051 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3053 break;
3054 }
3055 case Primitive::kPrimFloat:
3056 case Primitive::kPrimDouble: {
3057 locations->SetInAt(0, Location::RequiresFpuRegister());
3058 locations->SetInAt(1, Location::RequiresFpuRegister());
3059 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3060 break;
3061 }
3062 default:
3063 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3064 }
3065}
3066
3067void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3068 LocationSummary* locations = sub->GetLocations();
3069 Location out = locations->Out();
3070 Location first = locations->InAt(0);
3071 Location second = locations->InAt(1);
3072 switch (sub->GetResultType()) {
3073 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003074 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003075 break;
3076 }
3077
Scott Wakelingfe885462016-09-22 10:24:38 +01003078 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003079 if (second.IsConstant()) {
3080 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3081 GenerateAddLongConst(out, first, -value);
3082 } else {
3083 DCHECK(second.IsRegisterPair());
3084 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3085 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3086 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003087 break;
3088 }
3089
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003090 case Primitive::kPrimFloat:
3091 case Primitive::kPrimDouble:
3092 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003093 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003094
3095 default:
3096 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3097 }
3098}
3099
3100void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3101 LocationSummary* locations =
3102 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3103 switch (mul->GetResultType()) {
3104 case Primitive::kPrimInt:
3105 case Primitive::kPrimLong: {
3106 locations->SetInAt(0, Location::RequiresRegister());
3107 locations->SetInAt(1, Location::RequiresRegister());
3108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3109 break;
3110 }
3111
3112 case Primitive::kPrimFloat:
3113 case Primitive::kPrimDouble: {
3114 locations->SetInAt(0, Location::RequiresFpuRegister());
3115 locations->SetInAt(1, Location::RequiresFpuRegister());
3116 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3117 break;
3118 }
3119
3120 default:
3121 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3122 }
3123}
3124
3125void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3126 LocationSummary* locations = mul->GetLocations();
3127 Location out = locations->Out();
3128 Location first = locations->InAt(0);
3129 Location second = locations->InAt(1);
3130 switch (mul->GetResultType()) {
3131 case Primitive::kPrimInt: {
3132 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3133 break;
3134 }
3135 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003136 vixl32::Register out_hi = HighRegisterFrom(out);
3137 vixl32::Register out_lo = LowRegisterFrom(out);
3138 vixl32::Register in1_hi = HighRegisterFrom(first);
3139 vixl32::Register in1_lo = LowRegisterFrom(first);
3140 vixl32::Register in2_hi = HighRegisterFrom(second);
3141 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003142
3143 // Extra checks to protect caused by the existence of R1_R2.
3144 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3145 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003146 DCHECK(!out_hi.Is(in1_lo));
3147 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003148
3149 // input: in1 - 64 bits, in2 - 64 bits
3150 // output: out
3151 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3152 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3153 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3154
3155 UseScratchRegisterScope temps(GetVIXLAssembler());
3156 vixl32::Register temp = temps.Acquire();
3157 // temp <- in1.lo * in2.hi
3158 __ Mul(temp, in1_lo, in2_hi);
3159 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3160 __ Mla(out_hi, in1_hi, in2_lo, temp);
3161 // out.lo <- (in1.lo * in2.lo)[31:0];
3162 __ Umull(out_lo, temp, in1_lo, in2_lo);
3163 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003164 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003165 break;
3166 }
3167
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003168 case Primitive::kPrimFloat:
3169 case Primitive::kPrimDouble:
3170 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003171 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003172
3173 default:
3174 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3175 }
3176}
3177
Scott Wakelingfe885462016-09-22 10:24:38 +01003178void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3179 DCHECK(instruction->IsDiv() || instruction->IsRem());
3180 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3181
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003182 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003183 DCHECK(second.IsConstant());
3184
3185 vixl32::Register out = OutputRegister(instruction);
3186 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003187 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003188 DCHECK(imm == 1 || imm == -1);
3189
3190 if (instruction->IsRem()) {
3191 __ Mov(out, 0);
3192 } else {
3193 if (imm == 1) {
3194 __ Mov(out, dividend);
3195 } else {
3196 __ Rsb(out, dividend, 0);
3197 }
3198 }
3199}
3200
3201void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3202 DCHECK(instruction->IsDiv() || instruction->IsRem());
3203 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3204
3205 LocationSummary* locations = instruction->GetLocations();
3206 Location second = locations->InAt(1);
3207 DCHECK(second.IsConstant());
3208
3209 vixl32::Register out = OutputRegister(instruction);
3210 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003211 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003212 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003213 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3214 int ctz_imm = CTZ(abs_imm);
3215
3216 if (ctz_imm == 1) {
3217 __ Lsr(temp, dividend, 32 - ctz_imm);
3218 } else {
3219 __ Asr(temp, dividend, 31);
3220 __ Lsr(temp, temp, 32 - ctz_imm);
3221 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003222 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003223
3224 if (instruction->IsDiv()) {
3225 __ Asr(out, out, ctz_imm);
3226 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003227 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003228 }
3229 } else {
3230 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003231 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003232 }
3233}
3234
3235void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3236 DCHECK(instruction->IsDiv() || instruction->IsRem());
3237 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3238
3239 LocationSummary* locations = instruction->GetLocations();
3240 Location second = locations->InAt(1);
3241 DCHECK(second.IsConstant());
3242
3243 vixl32::Register out = OutputRegister(instruction);
3244 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003245 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3246 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003247 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003248
3249 int64_t magic;
3250 int shift;
3251 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3252
Anton Kirilovdda43962016-11-21 19:55:20 +00003253 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3254 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003255 __ Smull(temp2, temp1, dividend, temp1);
3256
3257 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003258 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003259 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003260 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003261 }
3262
3263 if (shift != 0) {
3264 __ Asr(temp1, temp1, shift);
3265 }
3266
3267 if (instruction->IsDiv()) {
3268 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3269 } else {
3270 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3271 // TODO: Strength reduction for mls.
3272 __ Mov(temp2, imm);
3273 __ Mls(out, temp1, temp2, dividend);
3274 }
3275}
3276
3277void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3278 HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3281
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003282 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003283 DCHECK(second.IsConstant());
3284
Anton Kirilov644032c2016-12-06 17:51:43 +00003285 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003286 if (imm == 0) {
3287 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3288 } else if (imm == 1 || imm == -1) {
3289 DivRemOneOrMinusOne(instruction);
3290 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3291 DivRemByPowerOfTwo(instruction);
3292 } else {
3293 DCHECK(imm <= -2 || imm >= 2);
3294 GenerateDivRemWithAnyConstant(instruction);
3295 }
3296}
3297
3298void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3299 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3300 if (div->GetResultType() == Primitive::kPrimLong) {
3301 // pLdiv runtime call.
3302 call_kind = LocationSummary::kCallOnMainOnly;
3303 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3304 // sdiv will be replaced by other instruction sequence.
3305 } else if (div->GetResultType() == Primitive::kPrimInt &&
3306 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3307 // pIdivmod runtime call.
3308 call_kind = LocationSummary::kCallOnMainOnly;
3309 }
3310
3311 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3312
3313 switch (div->GetResultType()) {
3314 case Primitive::kPrimInt: {
3315 if (div->InputAt(1)->IsConstant()) {
3316 locations->SetInAt(0, Location::RequiresRegister());
3317 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003319 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003320 if (value == 1 || value == 0 || value == -1) {
3321 // No temp register required.
3322 } else {
3323 locations->AddTemp(Location::RequiresRegister());
3324 if (!IsPowerOfTwo(AbsOrMin(value))) {
3325 locations->AddTemp(Location::RequiresRegister());
3326 }
3327 }
3328 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3329 locations->SetInAt(0, Location::RequiresRegister());
3330 locations->SetInAt(1, Location::RequiresRegister());
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3332 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003333 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3334 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3335 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3336 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3337 // we only need the former.
3338 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003339 }
3340 break;
3341 }
3342 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003343 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3344 locations->SetInAt(0, LocationFrom(
3345 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3346 locations->SetInAt(1, LocationFrom(
3347 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3348 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003349 break;
3350 }
3351 case Primitive::kPrimFloat:
3352 case Primitive::kPrimDouble: {
3353 locations->SetInAt(0, Location::RequiresFpuRegister());
3354 locations->SetInAt(1, Location::RequiresFpuRegister());
3355 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3356 break;
3357 }
3358
3359 default:
3360 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3361 }
3362}
3363
3364void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003365 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003366 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003367
3368 switch (div->GetResultType()) {
3369 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003370 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003371 GenerateDivRemConstantIntegral(div);
3372 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3373 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3374 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003375 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3376 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3377 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3378 DCHECK(r0.Is(OutputRegister(div)));
3379
3380 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3381 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003382 }
3383 break;
3384 }
3385
3386 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003387 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3388 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3389 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3390 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3391 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3392 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3393 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3394
3395 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3396 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003397 break;
3398 }
3399
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003400 case Primitive::kPrimFloat:
3401 case Primitive::kPrimDouble:
3402 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003403 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003404
3405 default:
3406 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3407 }
3408}
3409
Artem Serov551b28f2016-10-18 19:11:30 +01003410void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3411 Primitive::Type type = rem->GetResultType();
3412
3413 // Most remainders are implemented in the runtime.
3414 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3415 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3416 // sdiv will be replaced by other instruction sequence.
3417 call_kind = LocationSummary::kNoCall;
3418 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3419 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3420 // Have hardware divide instruction for int, do it with three instructions.
3421 call_kind = LocationSummary::kNoCall;
3422 }
3423
3424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3425
3426 switch (type) {
3427 case Primitive::kPrimInt: {
3428 if (rem->InputAt(1)->IsConstant()) {
3429 locations->SetInAt(0, Location::RequiresRegister());
3430 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003432 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01003433 if (value == 1 || value == 0 || value == -1) {
3434 // No temp register required.
3435 } else {
3436 locations->AddTemp(Location::RequiresRegister());
3437 if (!IsPowerOfTwo(AbsOrMin(value))) {
3438 locations->AddTemp(Location::RequiresRegister());
3439 }
3440 }
3441 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3442 locations->SetInAt(0, Location::RequiresRegister());
3443 locations->SetInAt(1, Location::RequiresRegister());
3444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3445 locations->AddTemp(Location::RequiresRegister());
3446 } else {
3447 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3448 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3449 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3450 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3451 // we only need the latter.
3452 locations->SetOut(LocationFrom(r1));
3453 }
3454 break;
3455 }
3456 case Primitive::kPrimLong: {
3457 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3458 locations->SetInAt(0, LocationFrom(
3459 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3460 locations->SetInAt(1, LocationFrom(
3461 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3462 // The runtime helper puts the output in R2,R3.
3463 locations->SetOut(LocationFrom(r2, r3));
3464 break;
3465 }
3466 case Primitive::kPrimFloat: {
3467 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3468 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3469 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3470 locations->SetOut(LocationFrom(s0));
3471 break;
3472 }
3473
3474 case Primitive::kPrimDouble: {
3475 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3476 locations->SetInAt(0, LocationFrom(
3477 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3478 locations->SetInAt(1, LocationFrom(
3479 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3480 locations->SetOut(LocationFrom(s0, s1));
3481 break;
3482 }
3483
3484 default:
3485 LOG(FATAL) << "Unexpected rem type " << type;
3486 }
3487}
3488
3489void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3490 LocationSummary* locations = rem->GetLocations();
3491 Location second = locations->InAt(1);
3492
3493 Primitive::Type type = rem->GetResultType();
3494 switch (type) {
3495 case Primitive::kPrimInt: {
3496 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3497 vixl32::Register out_reg = OutputRegister(rem);
3498 if (second.IsConstant()) {
3499 GenerateDivRemConstantIntegral(rem);
3500 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3501 vixl32::Register reg2 = RegisterFrom(second);
3502 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3503
3504 // temp = reg1 / reg2 (integer division)
3505 // dest = reg1 - temp * reg2
3506 __ Sdiv(temp, reg1, reg2);
3507 __ Mls(out_reg, temp, reg2, reg1);
3508 } else {
3509 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3510 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3511 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3512 DCHECK(out_reg.Is(r1));
3513
3514 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3515 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3516 }
3517 break;
3518 }
3519
3520 case Primitive::kPrimLong: {
3521 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3522 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3523 break;
3524 }
3525
3526 case Primitive::kPrimFloat: {
3527 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3528 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3529 break;
3530 }
3531
3532 case Primitive::kPrimDouble: {
3533 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3534 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3535 break;
3536 }
3537
3538 default:
3539 LOG(FATAL) << "Unexpected rem type " << type;
3540 }
3541}
3542
3543
Scott Wakelingfe885462016-09-22 10:24:38 +01003544void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003545 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003546 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003547}
3548
3549void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3550 DivZeroCheckSlowPathARMVIXL* slow_path =
3551 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3552 codegen_->AddSlowPath(slow_path);
3553
3554 LocationSummary* locations = instruction->GetLocations();
3555 Location value = locations->InAt(0);
3556
3557 switch (instruction->GetType()) {
3558 case Primitive::kPrimBoolean:
3559 case Primitive::kPrimByte:
3560 case Primitive::kPrimChar:
3561 case Primitive::kPrimShort:
3562 case Primitive::kPrimInt: {
3563 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003564 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003565 } else {
3566 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003567 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003568 __ B(slow_path->GetEntryLabel());
3569 }
3570 }
3571 break;
3572 }
3573 case Primitive::kPrimLong: {
3574 if (value.IsRegisterPair()) {
3575 UseScratchRegisterScope temps(GetVIXLAssembler());
3576 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003577 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003578 __ B(eq, slow_path->GetEntryLabel());
3579 } else {
3580 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003581 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003582 __ B(slow_path->GetEntryLabel());
3583 }
3584 }
3585 break;
3586 }
3587 default:
3588 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3589 }
3590}
3591
Artem Serov02109dd2016-09-23 17:17:54 +01003592void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3593 LocationSummary* locations = ror->GetLocations();
3594 vixl32::Register in = InputRegisterAt(ror, 0);
3595 Location rhs = locations->InAt(1);
3596 vixl32::Register out = OutputRegister(ror);
3597
3598 if (rhs.IsConstant()) {
3599 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3600 // so map all rotations to a +ve. equivalent in that range.
3601 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3602 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3603 if (rot) {
3604 // Rotate, mapping left rotations to right equivalents if necessary.
3605 // (e.g. left by 2 bits == right by 30.)
3606 __ Ror(out, in, rot);
3607 } else if (!out.Is(in)) {
3608 __ Mov(out, in);
3609 }
3610 } else {
3611 __ Ror(out, in, RegisterFrom(rhs));
3612 }
3613}
3614
3615// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3616// rotates by swapping input regs (effectively rotating by the first 32-bits of
3617// a larger rotation) or flipping direction (thus treating larger right/left
3618// rotations as sub-word sized rotations in the other direction) as appropriate.
3619void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3620 LocationSummary* locations = ror->GetLocations();
3621 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3622 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3623 Location rhs = locations->InAt(1);
3624 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3625 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3626
3627 if (rhs.IsConstant()) {
3628 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3629 // Map all rotations to +ve. equivalents on the interval [0,63].
3630 rot &= kMaxLongShiftDistance;
3631 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3632 // logic below to a simple pair of binary orr.
3633 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3634 if (rot >= kArmBitsPerWord) {
3635 rot -= kArmBitsPerWord;
3636 std::swap(in_reg_hi, in_reg_lo);
3637 }
3638 // Rotate, or mov to out for zero or word size rotations.
3639 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003640 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003641 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003642 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003643 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3644 } else {
3645 __ Mov(out_reg_lo, in_reg_lo);
3646 __ Mov(out_reg_hi, in_reg_hi);
3647 }
3648 } else {
3649 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3650 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3651 vixl32::Label end;
3652 vixl32::Label shift_by_32_plus_shift_right;
3653
3654 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3655 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003656 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00003657 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01003658
3659 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3660 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3661 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3662 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3663 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3664 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3665 __ Lsr(shift_left, in_reg_hi, shift_right);
3666 __ Add(out_reg_lo, out_reg_lo, shift_left);
3667 __ B(&end);
3668
3669 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3670 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3671 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3672 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3673 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3674 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3675 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3676 __ Lsl(shift_right, in_reg_hi, shift_left);
3677 __ Add(out_reg_lo, out_reg_lo, shift_right);
3678
3679 __ Bind(&end);
3680 }
3681}
3682
3683void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3684 LocationSummary* locations =
3685 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3686 switch (ror->GetResultType()) {
3687 case Primitive::kPrimInt: {
3688 locations->SetInAt(0, Location::RequiresRegister());
3689 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3691 break;
3692 }
3693 case Primitive::kPrimLong: {
3694 locations->SetInAt(0, Location::RequiresRegister());
3695 if (ror->InputAt(1)->IsConstant()) {
3696 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3697 } else {
3698 locations->SetInAt(1, Location::RequiresRegister());
3699 locations->AddTemp(Location::RequiresRegister());
3700 locations->AddTemp(Location::RequiresRegister());
3701 }
3702 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3703 break;
3704 }
3705 default:
3706 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3707 }
3708}
3709
3710void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3711 Primitive::Type type = ror->GetResultType();
3712 switch (type) {
3713 case Primitive::kPrimInt: {
3714 HandleIntegerRotate(ror);
3715 break;
3716 }
3717 case Primitive::kPrimLong: {
3718 HandleLongRotate(ror);
3719 break;
3720 }
3721 default:
3722 LOG(FATAL) << "Unexpected operation type " << type;
3723 UNREACHABLE();
3724 }
3725}
3726
Artem Serov02d37832016-10-25 15:25:33 +01003727void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3728 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3729
3730 LocationSummary* locations =
3731 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3732
3733 switch (op->GetResultType()) {
3734 case Primitive::kPrimInt: {
3735 locations->SetInAt(0, Location::RequiresRegister());
3736 if (op->InputAt(1)->IsConstant()) {
3737 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3739 } else {
3740 locations->SetInAt(1, Location::RequiresRegister());
3741 // Make the output overlap, as it will be used to hold the masked
3742 // second input.
3743 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3744 }
3745 break;
3746 }
3747 case Primitive::kPrimLong: {
3748 locations->SetInAt(0, Location::RequiresRegister());
3749 if (op->InputAt(1)->IsConstant()) {
3750 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3751 // For simplicity, use kOutputOverlap even though we only require that low registers
3752 // don't clash with high registers which the register allocator currently guarantees.
3753 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3754 } else {
3755 locations->SetInAt(1, Location::RequiresRegister());
3756 locations->AddTemp(Location::RequiresRegister());
3757 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3758 }
3759 break;
3760 }
3761 default:
3762 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3763 }
3764}
3765
3766void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3767 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3768
3769 LocationSummary* locations = op->GetLocations();
3770 Location out = locations->Out();
3771 Location first = locations->InAt(0);
3772 Location second = locations->InAt(1);
3773
3774 Primitive::Type type = op->GetResultType();
3775 switch (type) {
3776 case Primitive::kPrimInt: {
3777 vixl32::Register out_reg = OutputRegister(op);
3778 vixl32::Register first_reg = InputRegisterAt(op, 0);
3779 if (second.IsRegister()) {
3780 vixl32::Register second_reg = RegisterFrom(second);
3781 // ARM doesn't mask the shift count so we need to do it ourselves.
3782 __ And(out_reg, second_reg, kMaxIntShiftDistance);
3783 if (op->IsShl()) {
3784 __ Lsl(out_reg, first_reg, out_reg);
3785 } else if (op->IsShr()) {
3786 __ Asr(out_reg, first_reg, out_reg);
3787 } else {
3788 __ Lsr(out_reg, first_reg, out_reg);
3789 }
3790 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00003791 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003792 uint32_t shift_value = cst & kMaxIntShiftDistance;
3793 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
3794 __ Mov(out_reg, first_reg);
3795 } else if (op->IsShl()) {
3796 __ Lsl(out_reg, first_reg, shift_value);
3797 } else if (op->IsShr()) {
3798 __ Asr(out_reg, first_reg, shift_value);
3799 } else {
3800 __ Lsr(out_reg, first_reg, shift_value);
3801 }
3802 }
3803 break;
3804 }
3805 case Primitive::kPrimLong: {
3806 vixl32::Register o_h = HighRegisterFrom(out);
3807 vixl32::Register o_l = LowRegisterFrom(out);
3808
3809 vixl32::Register high = HighRegisterFrom(first);
3810 vixl32::Register low = LowRegisterFrom(first);
3811
3812 if (second.IsRegister()) {
3813 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3814
3815 vixl32::Register second_reg = RegisterFrom(second);
3816
3817 if (op->IsShl()) {
3818 __ And(o_l, second_reg, kMaxLongShiftDistance);
3819 // Shift the high part
3820 __ Lsl(o_h, high, o_l);
3821 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003822 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003823 __ Lsr(temp, low, temp);
3824 __ Orr(o_h, o_h, temp);
3825 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003826 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003827 {
Artem Serov0fb37192016-12-06 18:13:40 +00003828 ExactAssemblyScope guard(GetVIXLAssembler(),
3829 2 * vixl32::kMaxInstructionSizeInBytes,
3830 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003831 __ it(pl);
3832 __ lsl(pl, o_h, low, temp);
3833 }
3834 // Shift the low part
3835 __ Lsl(o_l, low, o_l);
3836 } else if (op->IsShr()) {
3837 __ And(o_h, second_reg, kMaxLongShiftDistance);
3838 // Shift the low part
3839 __ Lsr(o_l, low, o_h);
3840 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003841 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003842 __ Lsl(temp, high, temp);
3843 __ Orr(o_l, o_l, temp);
3844 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003845 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003846 {
Artem Serov0fb37192016-12-06 18:13:40 +00003847 ExactAssemblyScope guard(GetVIXLAssembler(),
3848 2 * vixl32::kMaxInstructionSizeInBytes,
3849 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003850 __ it(pl);
3851 __ asr(pl, o_l, high, temp);
3852 }
3853 // Shift the high part
3854 __ Asr(o_h, high, o_h);
3855 } else {
3856 __ And(o_h, second_reg, kMaxLongShiftDistance);
3857 // same as Shr except we use `Lsr`s and not `Asr`s
3858 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003859 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003860 __ Lsl(temp, high, temp);
3861 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003862 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003863 {
Artem Serov0fb37192016-12-06 18:13:40 +00003864 ExactAssemblyScope guard(GetVIXLAssembler(),
3865 2 * vixl32::kMaxInstructionSizeInBytes,
3866 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003867 __ it(pl);
3868 __ lsr(pl, o_l, high, temp);
3869 }
3870 __ Lsr(o_h, high, o_h);
3871 }
3872 } else {
3873 // Register allocator doesn't create partial overlap.
3874 DCHECK(!o_l.Is(high));
3875 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00003876 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003877 uint32_t shift_value = cst & kMaxLongShiftDistance;
3878 if (shift_value > 32) {
3879 if (op->IsShl()) {
3880 __ Lsl(o_h, low, shift_value - 32);
3881 __ Mov(o_l, 0);
3882 } else if (op->IsShr()) {
3883 __ Asr(o_l, high, shift_value - 32);
3884 __ Asr(o_h, high, 31);
3885 } else {
3886 __ Lsr(o_l, high, shift_value - 32);
3887 __ Mov(o_h, 0);
3888 }
3889 } else if (shift_value == 32) {
3890 if (op->IsShl()) {
3891 __ Mov(o_h, low);
3892 __ Mov(o_l, 0);
3893 } else if (op->IsShr()) {
3894 __ Mov(o_l, high);
3895 __ Asr(o_h, high, 31);
3896 } else {
3897 __ Mov(o_l, high);
3898 __ Mov(o_h, 0);
3899 }
3900 } else if (shift_value == 1) {
3901 if (op->IsShl()) {
3902 __ Lsls(o_l, low, 1);
3903 __ Adc(o_h, high, high);
3904 } else if (op->IsShr()) {
3905 __ Asrs(o_h, high, 1);
3906 __ Rrx(o_l, low);
3907 } else {
3908 __ Lsrs(o_h, high, 1);
3909 __ Rrx(o_l, low);
3910 }
3911 } else {
3912 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3913 if (op->IsShl()) {
3914 __ Lsl(o_h, high, shift_value);
3915 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
3916 __ Lsl(o_l, low, shift_value);
3917 } else if (op->IsShr()) {
3918 __ Lsr(o_l, low, shift_value);
3919 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3920 __ Asr(o_h, high, shift_value);
3921 } else {
3922 __ Lsr(o_l, low, shift_value);
3923 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3924 __ Lsr(o_h, high, shift_value);
3925 }
3926 }
3927 }
3928 break;
3929 }
3930 default:
3931 LOG(FATAL) << "Unexpected operation type " << type;
3932 UNREACHABLE();
3933 }
3934}
3935
3936void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
3937 HandleShift(shl);
3938}
3939
3940void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
3941 HandleShift(shl);
3942}
3943
3944void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
3945 HandleShift(shr);
3946}
3947
3948void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
3949 HandleShift(shr);
3950}
3951
3952void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
3953 HandleShift(ushr);
3954}
3955
3956void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
3957 HandleShift(ushr);
3958}
3959
3960void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3963 if (instruction->IsStringAlloc()) {
3964 locations->AddTemp(LocationFrom(kMethodRegister));
3965 } else {
3966 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3967 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01003968 }
3969 locations->SetOut(LocationFrom(r0));
3970}
3971
3972void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3973 // Note: if heap poisoning is enabled, the entry point takes cares
3974 // of poisoning the reference.
3975 if (instruction->IsStringAlloc()) {
3976 // String is allocated through StringFactory. Call NewEmptyString entry point.
3977 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
3978 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3979 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
3980 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003981 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00003982 ExactAssemblyScope aas(GetVIXLAssembler(),
3983 vixl32::k16BitT32InstructionSizeInBytes,
3984 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01003985 __ blx(lr);
3986 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3987 } else {
3988 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003989 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01003990 }
3991}
3992
3993void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
3994 LocationSummary* locations =
3995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3996 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3997 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
3998 locations->SetOut(LocationFrom(r0));
3999 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
4000 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
4001}
4002
4003void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
4004 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004005 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01004006 // Note: if heap poisoning is enabled, the entry point takes cares
4007 // of poisoning the reference.
4008 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4009 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
4010}
4011
4012void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4013 LocationSummary* locations =
4014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4015 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4016 if (location.IsStackSlot()) {
4017 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4018 } else if (location.IsDoubleStackSlot()) {
4019 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4020 }
4021 locations->SetOut(location);
4022}
4023
4024void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4025 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4026 // Nothing to do, the parameter is already at its location.
4027}
4028
4029void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4032 locations->SetOut(LocationFrom(kMethodRegister));
4033}
4034
4035void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4036 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4037 // Nothing to do, the method is already at its location.
4038}
4039
4040void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4041 LocationSummary* locations =
4042 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4043 locations->SetInAt(0, Location::RequiresRegister());
4044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4045}
4046
4047void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4048 LocationSummary* locations = not_->GetLocations();
4049 Location out = locations->Out();
4050 Location in = locations->InAt(0);
4051 switch (not_->GetResultType()) {
4052 case Primitive::kPrimInt:
4053 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4054 break;
4055
4056 case Primitive::kPrimLong:
4057 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4058 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4059 break;
4060
4061 default:
4062 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4063 }
4064}
4065
Scott Wakelingc34dba72016-10-03 10:14:44 +01004066void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4069 locations->SetInAt(0, Location::RequiresRegister());
4070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4071}
4072
4073void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4074 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4075}
4076
Artem Serov02d37832016-10-25 15:25:33 +01004077void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4080 switch (compare->InputAt(0)->GetType()) {
4081 case Primitive::kPrimBoolean:
4082 case Primitive::kPrimByte:
4083 case Primitive::kPrimShort:
4084 case Primitive::kPrimChar:
4085 case Primitive::kPrimInt:
4086 case Primitive::kPrimLong: {
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::RequiresRegister());
4089 // Output overlaps because it is written before doing the low comparison.
4090 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4091 break;
4092 }
4093 case Primitive::kPrimFloat:
4094 case Primitive::kPrimDouble: {
4095 locations->SetInAt(0, Location::RequiresFpuRegister());
4096 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4097 locations->SetOut(Location::RequiresRegister());
4098 break;
4099 }
4100 default:
4101 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4102 }
4103}
4104
4105void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4106 LocationSummary* locations = compare->GetLocations();
4107 vixl32::Register out = OutputRegister(compare);
4108 Location left = locations->InAt(0);
4109 Location right = locations->InAt(1);
4110
4111 vixl32::Label less, greater, done;
4112 Primitive::Type type = compare->InputAt(0)->GetType();
4113 vixl32::Condition less_cond = vixl32::Condition(kNone);
4114 switch (type) {
4115 case Primitive::kPrimBoolean:
4116 case Primitive::kPrimByte:
4117 case Primitive::kPrimShort:
4118 case Primitive::kPrimChar:
4119 case Primitive::kPrimInt: {
4120 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4121 __ Mov(out, 0);
4122 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4123 less_cond = lt;
4124 break;
4125 }
4126 case Primitive::kPrimLong: {
4127 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004128 __ B(lt, &less, /* far_target */ false);
4129 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004130 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4131 __ Mov(out, 0);
4132 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4133 less_cond = lo;
4134 break;
4135 }
4136 case Primitive::kPrimFloat:
4137 case Primitive::kPrimDouble: {
4138 __ Mov(out, 0);
4139 GenerateVcmp(compare);
4140 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4141 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4142 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4143 break;
4144 }
4145 default:
4146 LOG(FATAL) << "Unexpected compare type " << type;
4147 UNREACHABLE();
4148 }
4149
Artem Serov517d9f62016-12-12 15:51:15 +00004150 __ B(eq, &done, /* far_target */ false);
4151 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004152
4153 __ Bind(&greater);
4154 __ Mov(out, 1);
4155 __ B(&done);
4156
4157 __ Bind(&less);
4158 __ Mov(out, -1);
4159
4160 __ Bind(&done);
4161}
4162
4163void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4164 LocationSummary* locations =
4165 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4166 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4167 locations->SetInAt(i, Location::Any());
4168 }
4169 locations->SetOut(Location::Any());
4170}
4171
4172void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4173 LOG(FATAL) << "Unreachable";
4174}
4175
4176void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4177 // TODO (ported from quick): revisit ARM barrier kinds.
4178 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4179 switch (kind) {
4180 case MemBarrierKind::kAnyStore:
4181 case MemBarrierKind::kLoadAny:
4182 case MemBarrierKind::kAnyAny: {
4183 flavor = DmbOptions::ISH;
4184 break;
4185 }
4186 case MemBarrierKind::kStoreStore: {
4187 flavor = DmbOptions::ISHST;
4188 break;
4189 }
4190 default:
4191 LOG(FATAL) << "Unexpected memory barrier " << kind;
4192 }
4193 __ Dmb(flavor);
4194}
4195
4196void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4197 uint32_t offset,
4198 vixl32::Register out_lo,
4199 vixl32::Register out_hi) {
4200 UseScratchRegisterScope temps(GetVIXLAssembler());
4201 if (offset != 0) {
4202 vixl32::Register temp = temps.Acquire();
4203 __ Add(temp, addr, offset);
4204 addr = temp;
4205 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004206 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004207}
4208
4209void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4210 uint32_t offset,
4211 vixl32::Register value_lo,
4212 vixl32::Register value_hi,
4213 vixl32::Register temp1,
4214 vixl32::Register temp2,
4215 HInstruction* instruction) {
4216 UseScratchRegisterScope temps(GetVIXLAssembler());
4217 vixl32::Label fail;
4218 if (offset != 0) {
4219 vixl32::Register temp = temps.Acquire();
4220 __ Add(temp, addr, offset);
4221 addr = temp;
4222 }
4223 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004224 {
4225 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004226 ExactAssemblyScope aas(GetVIXLAssembler(),
4227 vixl32::kMaxInstructionSizeInBytes,
4228 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004229 // We need a load followed by store. (The address used in a STREX instruction must
4230 // be the same as the address in the most recently executed LDREX instruction.)
4231 __ ldrexd(temp1, temp2, MemOperand(addr));
4232 codegen_->MaybeRecordImplicitNullCheck(instruction);
4233 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004234 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004235 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004236}
Artem Serov02109dd2016-09-23 17:17:54 +01004237
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004238void LocationsBuilderARMVIXL::HandleFieldSet(
4239 HInstruction* instruction, const FieldInfo& field_info) {
4240 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4241
4242 LocationSummary* locations =
4243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4244 locations->SetInAt(0, Location::RequiresRegister());
4245
4246 Primitive::Type field_type = field_info.GetFieldType();
4247 if (Primitive::IsFloatingPointType(field_type)) {
4248 locations->SetInAt(1, Location::RequiresFpuRegister());
4249 } else {
4250 locations->SetInAt(1, Location::RequiresRegister());
4251 }
4252
4253 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4254 bool generate_volatile = field_info.IsVolatile()
4255 && is_wide
4256 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4257 bool needs_write_barrier =
4258 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4259 // Temporary registers for the write barrier.
4260 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4261 if (needs_write_barrier) {
4262 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4263 locations->AddTemp(Location::RequiresRegister());
4264 } else if (generate_volatile) {
4265 // ARM encoding have some additional constraints for ldrexd/strexd:
4266 // - registers need to be consecutive
4267 // - the first register should be even but not R14.
4268 // We don't test for ARM yet, and the assertion makes sure that we
4269 // revisit this if we ever enable ARM encoding.
4270 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4271
4272 locations->AddTemp(Location::RequiresRegister());
4273 locations->AddTemp(Location::RequiresRegister());
4274 if (field_type == Primitive::kPrimDouble) {
4275 // For doubles we need two more registers to copy the value.
4276 locations->AddTemp(LocationFrom(r2));
4277 locations->AddTemp(LocationFrom(r3));
4278 }
4279 }
4280}
4281
4282void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4283 const FieldInfo& field_info,
4284 bool value_can_be_null) {
4285 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4286
4287 LocationSummary* locations = instruction->GetLocations();
4288 vixl32::Register base = InputRegisterAt(instruction, 0);
4289 Location value = locations->InAt(1);
4290
4291 bool is_volatile = field_info.IsVolatile();
4292 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4293 Primitive::Type field_type = field_info.GetFieldType();
4294 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4295 bool needs_write_barrier =
4296 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4297
4298 if (is_volatile) {
4299 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4300 }
4301
4302 switch (field_type) {
4303 case Primitive::kPrimBoolean:
4304 case Primitive::kPrimByte: {
4305 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4306 break;
4307 }
4308
4309 case Primitive::kPrimShort:
4310 case Primitive::kPrimChar: {
4311 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4312 break;
4313 }
4314
4315 case Primitive::kPrimInt:
4316 case Primitive::kPrimNot: {
4317 if (kPoisonHeapReferences && needs_write_barrier) {
4318 // Note that in the case where `value` is a null reference,
4319 // we do not enter this block, as a null reference does not
4320 // need poisoning.
4321 DCHECK_EQ(field_type, Primitive::kPrimNot);
4322 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4323 __ Mov(temp, RegisterFrom(value));
4324 GetAssembler()->PoisonHeapReference(temp);
4325 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4326 } else {
4327 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4328 }
4329 break;
4330 }
4331
4332 case Primitive::kPrimLong: {
4333 if (is_volatile && !atomic_ldrd_strd) {
4334 GenerateWideAtomicStore(base,
4335 offset,
4336 LowRegisterFrom(value),
4337 HighRegisterFrom(value),
4338 RegisterFrom(locations->GetTemp(0)),
4339 RegisterFrom(locations->GetTemp(1)),
4340 instruction);
4341 } else {
4342 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4343 codegen_->MaybeRecordImplicitNullCheck(instruction);
4344 }
4345 break;
4346 }
4347
4348 case Primitive::kPrimFloat: {
4349 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4350 break;
4351 }
4352
4353 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004354 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004355 if (is_volatile && !atomic_ldrd_strd) {
4356 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4357 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4358
4359 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4360
4361 GenerateWideAtomicStore(base,
4362 offset,
4363 value_reg_lo,
4364 value_reg_hi,
4365 RegisterFrom(locations->GetTemp(2)),
4366 RegisterFrom(locations->GetTemp(3)),
4367 instruction);
4368 } else {
4369 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4370 codegen_->MaybeRecordImplicitNullCheck(instruction);
4371 }
4372 break;
4373 }
4374
4375 case Primitive::kPrimVoid:
4376 LOG(FATAL) << "Unreachable type " << field_type;
4377 UNREACHABLE();
4378 }
4379
4380 // Longs and doubles are handled in the switch.
4381 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004382 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4383 // should use a scope and the assembler to emit the store instruction to guarantee that we
4384 // record the pc at the correct position. But the `Assembler` does not automatically handle
4385 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4386 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004387 codegen_->MaybeRecordImplicitNullCheck(instruction);
4388 }
4389
4390 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4391 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4392 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4393 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4394 }
4395
4396 if (is_volatile) {
4397 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4398 }
4399}
4400
Artem Serov02d37832016-10-25 15:25:33 +01004401void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4402 const FieldInfo& field_info) {
4403 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4404
4405 bool object_field_get_with_read_barrier =
4406 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4407 LocationSummary* locations =
4408 new (GetGraph()->GetArena()) LocationSummary(instruction,
4409 object_field_get_with_read_barrier ?
4410 LocationSummary::kCallOnSlowPath :
4411 LocationSummary::kNoCall);
4412 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4413 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4414 }
4415 locations->SetInAt(0, Location::RequiresRegister());
4416
4417 bool volatile_for_double = field_info.IsVolatile()
4418 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4419 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4420 // The output overlaps in case of volatile long: we don't want the
4421 // code generated by GenerateWideAtomicLoad to overwrite the
4422 // object's location. Likewise, in the case of an object field get
4423 // with read barriers enabled, we do not want the load to overwrite
4424 // the object's location, as we need it to emit the read barrier.
4425 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4426 object_field_get_with_read_barrier;
4427
4428 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4429 locations->SetOut(Location::RequiresFpuRegister());
4430 } else {
4431 locations->SetOut(Location::RequiresRegister(),
4432 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4433 }
4434 if (volatile_for_double) {
4435 // ARM encoding have some additional constraints for ldrexd/strexd:
4436 // - registers need to be consecutive
4437 // - the first register should be even but not R14.
4438 // We don't test for ARM yet, and the assertion makes sure that we
4439 // revisit this if we ever enable ARM encoding.
4440 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4441 locations->AddTemp(Location::RequiresRegister());
4442 locations->AddTemp(Location::RequiresRegister());
4443 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4444 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004445 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004446 locations->AddTemp(Location::RequiresRegister());
4447 }
4448}
4449
4450Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4451 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4452 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4453 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4454 return Location::ConstantLocation(input->AsConstant());
4455 } else {
4456 return Location::RequiresFpuRegister();
4457 }
4458}
4459
Artem Serov02109dd2016-09-23 17:17:54 +01004460Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4461 Opcode opcode) {
4462 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4463 if (constant->IsConstant() &&
4464 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4465 return Location::ConstantLocation(constant->AsConstant());
4466 }
4467 return Location::RequiresRegister();
4468}
4469
4470bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4471 Opcode opcode) {
4472 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4473 if (Primitive::Is64BitType(input_cst->GetType())) {
4474 Opcode high_opcode = opcode;
4475 SetCc low_set_cc = kCcDontCare;
4476 switch (opcode) {
4477 case SUB:
4478 // Flip the operation to an ADD.
4479 value = -value;
4480 opcode = ADD;
4481 FALLTHROUGH_INTENDED;
4482 case ADD:
4483 if (Low32Bits(value) == 0u) {
4484 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4485 }
4486 high_opcode = ADC;
4487 low_set_cc = kCcSet;
4488 break;
4489 default:
4490 break;
4491 }
4492 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4493 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4494 } else {
4495 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4496 }
4497}
4498
4499// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4500// enabled.
4501bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4502 Opcode opcode,
4503 SetCc set_cc) {
4504 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4505 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4506 return true;
4507 }
4508 Opcode neg_opcode = kNoOperand;
4509 switch (opcode) {
4510 case AND: neg_opcode = BIC; value = ~value; break;
4511 case ORR: neg_opcode = ORN; value = ~value; break;
4512 case ADD: neg_opcode = SUB; value = -value; break;
4513 case ADC: neg_opcode = SBC; value = ~value; break;
4514 case SUB: neg_opcode = ADD; value = -value; break;
4515 case SBC: neg_opcode = ADC; value = ~value; break;
4516 default:
4517 return false;
4518 }
4519 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4520}
4521
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004522void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4523 const FieldInfo& field_info) {
4524 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4525
4526 LocationSummary* locations = instruction->GetLocations();
4527 vixl32::Register base = InputRegisterAt(instruction, 0);
4528 Location out = locations->Out();
4529 bool is_volatile = field_info.IsVolatile();
4530 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4531 Primitive::Type field_type = field_info.GetFieldType();
4532 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4533
4534 switch (field_type) {
4535 case Primitive::kPrimBoolean:
4536 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4537 break;
4538
4539 case Primitive::kPrimByte:
4540 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4541 break;
4542
4543 case Primitive::kPrimShort:
4544 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4545 break;
4546
4547 case Primitive::kPrimChar:
4548 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4549 break;
4550
4551 case Primitive::kPrimInt:
4552 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4553 break;
4554
4555 case Primitive::kPrimNot: {
4556 // /* HeapReference<Object> */ out = *(base + offset)
4557 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004558 Location temp_loc = locations->GetTemp(0);
4559 // Note that a potential implicit null check is handled in this
4560 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4561 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4562 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4563 if (is_volatile) {
4564 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4565 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004566 } else {
4567 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004568 codegen_->MaybeRecordImplicitNullCheck(instruction);
4569 if (is_volatile) {
4570 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4571 }
4572 // If read barriers are enabled, emit read barriers other than
4573 // Baker's using a slow path (and also unpoison the loaded
4574 // reference, if heap poisoning is enabled).
4575 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4576 }
4577 break;
4578 }
4579
4580 case Primitive::kPrimLong:
4581 if (is_volatile && !atomic_ldrd_strd) {
4582 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4583 } else {
4584 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4585 }
4586 break;
4587
4588 case Primitive::kPrimFloat:
4589 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4590 break;
4591
4592 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004593 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004594 if (is_volatile && !atomic_ldrd_strd) {
4595 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4596 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4597 GenerateWideAtomicLoad(base, offset, lo, hi);
4598 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4599 // scope.
4600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 __ Vmov(out_dreg, lo, hi);
4602 } else {
4603 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004604 codegen_->MaybeRecordImplicitNullCheck(instruction);
4605 }
4606 break;
4607 }
4608
4609 case Primitive::kPrimVoid:
4610 LOG(FATAL) << "Unreachable type " << field_type;
4611 UNREACHABLE();
4612 }
4613
4614 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4615 // Potential implicit null checks, in the case of reference or
4616 // double fields, are handled in the previous switch statement.
4617 } else {
4618 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004619 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4620 // should use a scope and the assembler to emit the load instruction to guarantee that we
4621 // record the pc at the correct position. But the `Assembler` does not automatically handle
4622 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4623 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004624 codegen_->MaybeRecordImplicitNullCheck(instruction);
4625 }
4626
4627 if (is_volatile) {
4628 if (field_type == Primitive::kPrimNot) {
4629 // Memory barriers, in the case of references, are also handled
4630 // in the previous switch statement.
4631 } else {
4632 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4633 }
4634 }
4635}
4636
4637void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4638 HandleFieldSet(instruction, instruction->GetFieldInfo());
4639}
4640
4641void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4642 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4643}
4644
4645void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4646 HandleFieldGet(instruction, instruction->GetFieldInfo());
4647}
4648
4649void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4650 HandleFieldGet(instruction, instruction->GetFieldInfo());
4651}
4652
4653void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4654 HandleFieldGet(instruction, instruction->GetFieldInfo());
4655}
4656
4657void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4658 HandleFieldGet(instruction, instruction->GetFieldInfo());
4659}
4660
Scott Wakelingc34dba72016-10-03 10:14:44 +01004661void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4662 HandleFieldSet(instruction, instruction->GetFieldInfo());
4663}
4664
4665void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4666 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4667}
4668
Artem Serovcfbe9132016-10-14 15:58:56 +01004669void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4670 HUnresolvedInstanceFieldGet* instruction) {
4671 FieldAccessCallingConventionARMVIXL calling_convention;
4672 codegen_->CreateUnresolvedFieldLocationSummary(
4673 instruction, instruction->GetFieldType(), calling_convention);
4674}
4675
4676void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4677 HUnresolvedInstanceFieldGet* instruction) {
4678 FieldAccessCallingConventionARMVIXL calling_convention;
4679 codegen_->GenerateUnresolvedFieldAccess(instruction,
4680 instruction->GetFieldType(),
4681 instruction->GetFieldIndex(),
4682 instruction->GetDexPc(),
4683 calling_convention);
4684}
4685
4686void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4687 HUnresolvedInstanceFieldSet* instruction) {
4688 FieldAccessCallingConventionARMVIXL calling_convention;
4689 codegen_->CreateUnresolvedFieldLocationSummary(
4690 instruction, instruction->GetFieldType(), calling_convention);
4691}
4692
4693void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4694 HUnresolvedInstanceFieldSet* instruction) {
4695 FieldAccessCallingConventionARMVIXL calling_convention;
4696 codegen_->GenerateUnresolvedFieldAccess(instruction,
4697 instruction->GetFieldType(),
4698 instruction->GetFieldIndex(),
4699 instruction->GetDexPc(),
4700 calling_convention);
4701}
4702
4703void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4704 HUnresolvedStaticFieldGet* instruction) {
4705 FieldAccessCallingConventionARMVIXL calling_convention;
4706 codegen_->CreateUnresolvedFieldLocationSummary(
4707 instruction, instruction->GetFieldType(), calling_convention);
4708}
4709
4710void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4711 HUnresolvedStaticFieldGet* instruction) {
4712 FieldAccessCallingConventionARMVIXL calling_convention;
4713 codegen_->GenerateUnresolvedFieldAccess(instruction,
4714 instruction->GetFieldType(),
4715 instruction->GetFieldIndex(),
4716 instruction->GetDexPc(),
4717 calling_convention);
4718}
4719
4720void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4721 HUnresolvedStaticFieldSet* instruction) {
4722 FieldAccessCallingConventionARMVIXL calling_convention;
4723 codegen_->CreateUnresolvedFieldLocationSummary(
4724 instruction, instruction->GetFieldType(), calling_convention);
4725}
4726
4727void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4728 HUnresolvedStaticFieldSet* instruction) {
4729 FieldAccessCallingConventionARMVIXL calling_convention;
4730 codegen_->GenerateUnresolvedFieldAccess(instruction,
4731 instruction->GetFieldType(),
4732 instruction->GetFieldIndex(),
4733 instruction->GetDexPc(),
4734 calling_convention);
4735}
4736
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004737void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004738 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004739 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004740}
4741
4742void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4743 if (CanMoveNullCheckToUser(instruction)) {
4744 return;
4745 }
4746
4747 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004748 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004749 ExactAssemblyScope aas(GetVIXLAssembler(),
4750 vixl32::kMaxInstructionSizeInBytes,
4751 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004752 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4753 RecordPcInfo(instruction, instruction->GetDexPc());
4754}
4755
4756void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4757 NullCheckSlowPathARMVIXL* slow_path =
4758 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4759 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004760 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004761}
4762
4763void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4764 codegen_->GenerateNullCheck(instruction);
4765}
4766
Scott Wakelingc34dba72016-10-03 10:14:44 +01004767static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4768 switch (type) {
4769 case Primitive::kPrimNot:
4770 return kLoadWord;
4771 case Primitive::kPrimBoolean:
4772 return kLoadUnsignedByte;
4773 case Primitive::kPrimByte:
4774 return kLoadSignedByte;
4775 case Primitive::kPrimChar:
4776 return kLoadUnsignedHalfword;
4777 case Primitive::kPrimShort:
4778 return kLoadSignedHalfword;
4779 case Primitive::kPrimInt:
4780 return kLoadWord;
4781 case Primitive::kPrimLong:
4782 return kLoadWordPair;
4783 case Primitive::kPrimFloat:
4784 return kLoadSWord;
4785 case Primitive::kPrimDouble:
4786 return kLoadDWord;
4787 default:
4788 LOG(FATAL) << "Unreachable type " << type;
4789 UNREACHABLE();
4790 }
4791}
4792
4793static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4794 switch (type) {
4795 case Primitive::kPrimNot:
4796 return kStoreWord;
4797 case Primitive::kPrimBoolean:
4798 case Primitive::kPrimByte:
4799 return kStoreByte;
4800 case Primitive::kPrimChar:
4801 case Primitive::kPrimShort:
4802 return kStoreHalfword;
4803 case Primitive::kPrimInt:
4804 return kStoreWord;
4805 case Primitive::kPrimLong:
4806 return kStoreWordPair;
4807 case Primitive::kPrimFloat:
4808 return kStoreSWord;
4809 case Primitive::kPrimDouble:
4810 return kStoreDWord;
4811 default:
4812 LOG(FATAL) << "Unreachable type " << type;
4813 UNREACHABLE();
4814 }
4815}
4816
4817void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
4818 Location out_loc,
4819 vixl32::Register base,
4820 vixl32::Register reg_index,
4821 vixl32::Condition cond) {
4822 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4823 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4824
4825 switch (type) {
4826 case Primitive::kPrimByte:
4827 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
4828 break;
4829 case Primitive::kPrimBoolean:
4830 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
4831 break;
4832 case Primitive::kPrimShort:
4833 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
4834 break;
4835 case Primitive::kPrimChar:
4836 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
4837 break;
4838 case Primitive::kPrimNot:
4839 case Primitive::kPrimInt:
4840 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
4841 break;
4842 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4843 case Primitive::kPrimLong:
4844 case Primitive::kPrimFloat:
4845 case Primitive::kPrimDouble:
4846 default:
4847 LOG(FATAL) << "Unreachable type " << type;
4848 UNREACHABLE();
4849 }
4850}
4851
4852void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
4853 Location loc,
4854 vixl32::Register base,
4855 vixl32::Register reg_index,
4856 vixl32::Condition cond) {
4857 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4858 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4859
4860 switch (type) {
4861 case Primitive::kPrimByte:
4862 case Primitive::kPrimBoolean:
4863 __ Strb(cond, RegisterFrom(loc), mem_address);
4864 break;
4865 case Primitive::kPrimShort:
4866 case Primitive::kPrimChar:
4867 __ Strh(cond, RegisterFrom(loc), mem_address);
4868 break;
4869 case Primitive::kPrimNot:
4870 case Primitive::kPrimInt:
4871 __ Str(cond, RegisterFrom(loc), mem_address);
4872 break;
4873 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4874 case Primitive::kPrimLong:
4875 case Primitive::kPrimFloat:
4876 case Primitive::kPrimDouble:
4877 default:
4878 LOG(FATAL) << "Unreachable type " << type;
4879 UNREACHABLE();
4880 }
4881}
4882
4883void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4884 bool object_array_get_with_read_barrier =
4885 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4886 LocationSummary* locations =
4887 new (GetGraph()->GetArena()) LocationSummary(instruction,
4888 object_array_get_with_read_barrier ?
4889 LocationSummary::kCallOnSlowPath :
4890 LocationSummary::kNoCall);
4891 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004892 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004893 }
4894 locations->SetInAt(0, Location::RequiresRegister());
4895 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4896 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4897 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4898 } else {
4899 // The output overlaps in the case of an object array get with
4900 // read barriers enabled: we do not want the move to overwrite the
4901 // array's location, as we need it to emit the read barrier.
4902 locations->SetOut(
4903 Location::RequiresRegister(),
4904 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4905 }
4906 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004907 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004908 // Also need for String compression feature.
4909 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4910 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004911 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004912 }
4913}
4914
4915void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004916 LocationSummary* locations = instruction->GetLocations();
4917 Location obj_loc = locations->InAt(0);
4918 vixl32::Register obj = InputRegisterAt(instruction, 0);
4919 Location index = locations->InAt(1);
4920 Location out_loc = locations->Out();
4921 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4922 Primitive::Type type = instruction->GetType();
4923 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4924 instruction->IsStringCharAt();
4925 HInstruction* array_instr = instruction->GetArray();
4926 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01004927
4928 switch (type) {
4929 case Primitive::kPrimBoolean:
4930 case Primitive::kPrimByte:
4931 case Primitive::kPrimShort:
4932 case Primitive::kPrimChar:
4933 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004934 vixl32::Register length;
4935 if (maybe_compressed_char_at) {
4936 length = RegisterFrom(locations->GetTemp(0));
4937 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4938 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
4939 codegen_->MaybeRecordImplicitNullCheck(instruction);
4940 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01004941 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00004942 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004943 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004944 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004945 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4946 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4947 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00004948 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004949 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
4950 RegisterFrom(out_loc),
4951 obj,
4952 data_offset + const_index);
4953 __ B(&done);
4954 __ Bind(&uncompressed_load);
4955 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4956 RegisterFrom(out_loc),
4957 obj,
4958 data_offset + (const_index << 1));
4959 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004960 } else {
4961 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4962
4963 LoadOperandType load_type = GetLoadOperandType(type);
4964 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
4965 }
4966 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004967 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004968 vixl32::Register temp = temps.Acquire();
4969
4970 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01004971 // We do not need to compute the intermediate address from the array: the
4972 // input instruction has done it already. See the comment in
4973 // `TryExtractArrayAccessAddress()`.
4974 if (kIsDebugBuild) {
4975 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00004976 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01004977 }
4978 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01004979 } else {
4980 __ Add(temp, obj, data_offset);
4981 }
4982 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004983 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004984 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4985 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4986 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00004987 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004988 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
4989 __ B(&done);
4990 __ Bind(&uncompressed_load);
4991 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
4992 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004993 } else {
4994 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4995 }
4996 }
4997 break;
4998 }
4999
5000 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005001 // The read barrier instrumentation of object ArrayGet
5002 // instructions does not support the HIntermediateAddress
5003 // instruction.
5004 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5005
Scott Wakelingc34dba72016-10-03 10:14:44 +01005006 static_assert(
5007 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5008 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5009 // /* HeapReference<Object> */ out =
5010 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5011 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005012 Location temp = locations->GetTemp(0);
5013 // Note that a potential implicit null check is handled in this
5014 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5015 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5016 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005017 } else {
5018 vixl32::Register out = OutputRegister(instruction);
5019 if (index.IsConstant()) {
5020 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005021 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005022 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005023 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5024 // we should use a scope and the assembler to emit the load instruction to guarantee that
5025 // we record the pc at the correct position. But the `Assembler` does not automatically
5026 // handle unencodable offsets. Practically, everything is fine because the helper and
5027 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005028 codegen_->MaybeRecordImplicitNullCheck(instruction);
5029 // If read barriers are enabled, emit read barriers other than
5030 // Baker's using a slow path (and also unpoison the loaded
5031 // reference, if heap poisoning is enabled).
5032 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5033 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005034 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005035 vixl32::Register temp = temps.Acquire();
5036
5037 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005038 // We do not need to compute the intermediate address from the array: the
5039 // input instruction has done it already. See the comment in
5040 // `TryExtractArrayAccessAddress()`.
5041 if (kIsDebugBuild) {
5042 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005043 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005044 }
5045 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005046 } else {
5047 __ Add(temp, obj, data_offset);
5048 }
5049 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005050 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005051 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5052 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5053 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005054 codegen_->MaybeRecordImplicitNullCheck(instruction);
5055 // If read barriers are enabled, emit read barriers other than
5056 // Baker's using a slow path (and also unpoison the loaded
5057 // reference, if heap poisoning is enabled).
5058 codegen_->MaybeGenerateReadBarrierSlow(
5059 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5060 }
5061 }
5062 break;
5063 }
5064
5065 case Primitive::kPrimLong: {
5066 if (index.IsConstant()) {
5067 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005068 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005069 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5070 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005071 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005072 vixl32::Register temp = temps.Acquire();
5073 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5074 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5075 }
5076 break;
5077 }
5078
5079 case Primitive::kPrimFloat: {
5080 vixl32::SRegister out = SRegisterFrom(out_loc);
5081 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005082 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005083 GetAssembler()->LoadSFromOffset(out, obj, offset);
5084 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005085 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005086 vixl32::Register temp = temps.Acquire();
5087 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5088 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5089 }
5090 break;
5091 }
5092
5093 case Primitive::kPrimDouble: {
5094 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005095 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005096 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5097 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005098 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005099 vixl32::Register temp = temps.Acquire();
5100 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5101 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5102 }
5103 break;
5104 }
5105
5106 case Primitive::kPrimVoid:
5107 LOG(FATAL) << "Unreachable type " << type;
5108 UNREACHABLE();
5109 }
5110
5111 if (type == Primitive::kPrimNot) {
5112 // Potential implicit null checks, in the case of reference
5113 // arrays, are handled in the previous switch statement.
5114 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005115 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5116 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005117 codegen_->MaybeRecordImplicitNullCheck(instruction);
5118 }
5119}
5120
5121void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5122 Primitive::Type value_type = instruction->GetComponentType();
5123
5124 bool needs_write_barrier =
5125 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5126 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5127
5128 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5129 instruction,
5130 may_need_runtime_call_for_type_check ?
5131 LocationSummary::kCallOnSlowPath :
5132 LocationSummary::kNoCall);
5133
5134 locations->SetInAt(0, Location::RequiresRegister());
5135 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5136 if (Primitive::IsFloatingPointType(value_type)) {
5137 locations->SetInAt(2, Location::RequiresFpuRegister());
5138 } else {
5139 locations->SetInAt(2, Location::RequiresRegister());
5140 }
5141 if (needs_write_barrier) {
5142 // Temporary registers for the write barrier.
5143 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5144 locations->AddTemp(Location::RequiresRegister());
5145 }
5146}
5147
5148void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005149 LocationSummary* locations = instruction->GetLocations();
5150 vixl32::Register array = InputRegisterAt(instruction, 0);
5151 Location index = locations->InAt(1);
5152 Primitive::Type value_type = instruction->GetComponentType();
5153 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5154 bool needs_write_barrier =
5155 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5156 uint32_t data_offset =
5157 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5158 Location value_loc = locations->InAt(2);
5159 HInstruction* array_instr = instruction->GetArray();
5160 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005161
5162 switch (value_type) {
5163 case Primitive::kPrimBoolean:
5164 case Primitive::kPrimByte:
5165 case Primitive::kPrimShort:
5166 case Primitive::kPrimChar:
5167 case Primitive::kPrimInt: {
5168 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005169 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005170 uint32_t full_offset =
5171 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5172 StoreOperandType store_type = GetStoreOperandType(value_type);
5173 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5174 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005175 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005176 vixl32::Register temp = temps.Acquire();
5177
5178 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005179 // We do not need to compute the intermediate address from the array: the
5180 // input instruction has done it already. See the comment in
5181 // `TryExtractArrayAccessAddress()`.
5182 if (kIsDebugBuild) {
5183 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005184 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005185 }
5186 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005187 } else {
5188 __ Add(temp, array, data_offset);
5189 }
5190 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5191 }
5192 break;
5193 }
5194
5195 case Primitive::kPrimNot: {
5196 vixl32::Register value = RegisterFrom(value_loc);
5197 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5198 // See the comment in instruction_simplifier_shared.cc.
5199 DCHECK(!has_intermediate_address);
5200
5201 if (instruction->InputAt(2)->IsNullConstant()) {
5202 // Just setting null.
5203 if (index.IsConstant()) {
5204 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005205 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005206 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5207 } else {
5208 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005209 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005210 vixl32::Register temp = temps.Acquire();
5211 __ Add(temp, array, data_offset);
5212 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5213 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005214 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5215 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005216 codegen_->MaybeRecordImplicitNullCheck(instruction);
5217 DCHECK(!needs_write_barrier);
5218 DCHECK(!may_need_runtime_call_for_type_check);
5219 break;
5220 }
5221
5222 DCHECK(needs_write_barrier);
5223 Location temp1_loc = locations->GetTemp(0);
5224 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5225 Location temp2_loc = locations->GetTemp(1);
5226 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5227 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5228 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5229 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5230 vixl32::Label done;
5231 SlowPathCodeARMVIXL* slow_path = nullptr;
5232
5233 if (may_need_runtime_call_for_type_check) {
5234 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5235 codegen_->AddSlowPath(slow_path);
5236 if (instruction->GetValueCanBeNull()) {
5237 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005238 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005239 if (index.IsConstant()) {
5240 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005241 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005242 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5243 } else {
5244 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005245 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005246 vixl32::Register temp = temps.Acquire();
5247 __ Add(temp, array, data_offset);
5248 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5249 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005250 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5251 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005252 codegen_->MaybeRecordImplicitNullCheck(instruction);
5253 __ B(&done);
5254 __ Bind(&non_zero);
5255 }
5256
5257 // Note that when read barriers are enabled, the type checks
5258 // are performed without read barriers. This is fine, even in
5259 // the case where a class object is in the from-space after
5260 // the flip, as a comparison involving such a type would not
5261 // produce a false positive; it may of course produce a false
5262 // negative, in which case we would take the ArraySet slow
5263 // path.
5264
Alexandre Rames374ddf32016-11-04 10:40:49 +00005265 {
5266 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005267 ExactAssemblyScope aas(GetVIXLAssembler(),
5268 vixl32::kMaxInstructionSizeInBytes,
5269 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005270 // /* HeapReference<Class> */ temp1 = array->klass_
5271 __ ldr(temp1, MemOperand(array, class_offset));
5272 codegen_->MaybeRecordImplicitNullCheck(instruction);
5273 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005274 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5275
5276 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5277 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5278 // /* HeapReference<Class> */ temp2 = value->klass_
5279 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5280 // If heap poisoning is enabled, no need to unpoison `temp1`
5281 // nor `temp2`, as we are comparing two poisoned references.
5282 __ Cmp(temp1, temp2);
5283
5284 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5285 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00005286 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005287 // If heap poisoning is enabled, the `temp1` reference has
5288 // not been unpoisoned yet; unpoison it now.
5289 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5290
5291 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5292 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5293 // If heap poisoning is enabled, no need to unpoison
5294 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005295 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005296 __ Bind(&do_put);
5297 } else {
5298 __ B(ne, slow_path->GetEntryLabel());
5299 }
5300 }
5301
5302 vixl32::Register source = value;
5303 if (kPoisonHeapReferences) {
5304 // Note that in the case where `value` is a null reference,
5305 // we do not enter this block, as a null reference does not
5306 // need poisoning.
5307 DCHECK_EQ(value_type, Primitive::kPrimNot);
5308 __ Mov(temp1, value);
5309 GetAssembler()->PoisonHeapReference(temp1);
5310 source = temp1;
5311 }
5312
5313 if (index.IsConstant()) {
5314 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005315 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005316 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5317 } else {
5318 DCHECK(index.IsRegister()) << index;
5319
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005320 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005321 vixl32::Register temp = temps.Acquire();
5322 __ Add(temp, array, data_offset);
5323 codegen_->StoreToShiftedRegOffset(value_type,
5324 LocationFrom(source),
5325 temp,
5326 RegisterFrom(index));
5327 }
5328
5329 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005330 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5331 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005332 codegen_->MaybeRecordImplicitNullCheck(instruction);
5333 }
5334
5335 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5336
5337 if (done.IsReferenced()) {
5338 __ Bind(&done);
5339 }
5340
5341 if (slow_path != nullptr) {
5342 __ Bind(slow_path->GetExitLabel());
5343 }
5344
5345 break;
5346 }
5347
5348 case Primitive::kPrimLong: {
5349 Location value = locations->InAt(2);
5350 if (index.IsConstant()) {
5351 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005352 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005353 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5354 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005355 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005356 vixl32::Register temp = temps.Acquire();
5357 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5358 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5359 }
5360 break;
5361 }
5362
5363 case Primitive::kPrimFloat: {
5364 Location value = locations->InAt(2);
5365 DCHECK(value.IsFpuRegister());
5366 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005367 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005368 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5369 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005370 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005371 vixl32::Register temp = temps.Acquire();
5372 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5373 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5374 }
5375 break;
5376 }
5377
5378 case Primitive::kPrimDouble: {
5379 Location value = locations->InAt(2);
5380 DCHECK(value.IsFpuRegisterPair());
5381 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005382 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005383 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5384 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005385 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005386 vixl32::Register temp = temps.Acquire();
5387 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5388 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5389 }
5390 break;
5391 }
5392
5393 case Primitive::kPrimVoid:
5394 LOG(FATAL) << "Unreachable type " << value_type;
5395 UNREACHABLE();
5396 }
5397
5398 // Objects are handled in the switch.
5399 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005400 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5401 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 }
5404}
5405
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005406void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5407 LocationSummary* locations =
5408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5409 locations->SetInAt(0, Location::RequiresRegister());
5410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5411}
5412
5413void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5414 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5415 vixl32::Register obj = InputRegisterAt(instruction, 0);
5416 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005417 {
Artem Serov0fb37192016-12-06 18:13:40 +00005418 ExactAssemblyScope aas(GetVIXLAssembler(),
5419 vixl32::kMaxInstructionSizeInBytes,
5420 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005421 __ ldr(out, MemOperand(obj, offset));
5422 codegen_->MaybeRecordImplicitNullCheck(instruction);
5423 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005424 // Mask out compression flag from String's array length.
5425 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005426 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005427 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005428}
5429
Artem Serov2bbc9532016-10-21 11:51:50 +01005430void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005431 LocationSummary* locations =
5432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5433
5434 locations->SetInAt(0, Location::RequiresRegister());
5435 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5437}
5438
5439void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5440 vixl32::Register out = OutputRegister(instruction);
5441 vixl32::Register first = InputRegisterAt(instruction, 0);
5442 Location second = instruction->GetLocations()->InAt(1);
5443
Artem Serov2bbc9532016-10-21 11:51:50 +01005444 if (second.IsRegister()) {
5445 __ Add(out, first, RegisterFrom(second));
5446 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005447 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01005448 }
5449}
5450
Scott Wakelingc34dba72016-10-03 10:14:44 +01005451void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5452 RegisterSet caller_saves = RegisterSet::Empty();
5453 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5454 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5455 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5456 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5457 locations->SetInAt(0, Location::RequiresRegister());
5458 locations->SetInAt(1, Location::RequiresRegister());
5459}
5460
5461void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5462 SlowPathCodeARMVIXL* slow_path =
5463 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5464 codegen_->AddSlowPath(slow_path);
5465
5466 vixl32::Register index = InputRegisterAt(instruction, 0);
5467 vixl32::Register length = InputRegisterAt(instruction, 1);
5468
5469 __ Cmp(index, length);
5470 __ B(hs, slow_path->GetEntryLabel());
5471}
5472
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005473void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5474 vixl32::Register card,
5475 vixl32::Register object,
5476 vixl32::Register value,
5477 bool can_be_null) {
5478 vixl32::Label is_null;
5479 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005480 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005481 }
5482 GetAssembler()->LoadFromOffset(
5483 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005484 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005485 __ Strb(card, MemOperand(card, temp));
5486 if (can_be_null) {
5487 __ Bind(&is_null);
5488 }
5489}
5490
Scott Wakelingfe885462016-09-22 10:24:38 +01005491void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5492 LOG(FATAL) << "Unreachable";
5493}
5494
5495void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5496 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5497}
5498
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005499void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005500 LocationSummary* locations =
5501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005503}
5504
5505void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5506 HBasicBlock* block = instruction->GetBlock();
5507 if (block->GetLoopInformation() != nullptr) {
5508 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5509 // The back edge will generate the suspend check.
5510 return;
5511 }
5512 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5513 // The goto will generate the suspend check.
5514 return;
5515 }
5516 GenerateSuspendCheck(instruction, nullptr);
5517}
5518
5519void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5520 HBasicBlock* successor) {
5521 SuspendCheckSlowPathARMVIXL* slow_path =
5522 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5523 if (slow_path == nullptr) {
5524 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5525 instruction->SetSlowPath(slow_path);
5526 codegen_->AddSlowPath(slow_path);
5527 if (successor != nullptr) {
5528 DCHECK(successor->IsLoopHeader());
5529 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5530 }
5531 } else {
5532 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5533 }
5534
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005535 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005536 vixl32::Register temp = temps.Acquire();
5537 GetAssembler()->LoadFromOffset(
5538 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5539 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005540 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005541 __ Bind(slow_path->GetReturnLabel());
5542 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005543 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005544 __ B(slow_path->GetEntryLabel());
5545 }
5546}
5547
Scott Wakelingfe885462016-09-22 10:24:38 +01005548ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5549 return codegen_->GetAssembler();
5550}
5551
5552void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005553 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005554 MoveOperands* move = moves_[index];
5555 Location source = move->GetSource();
5556 Location destination = move->GetDestination();
5557
5558 if (source.IsRegister()) {
5559 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005560 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005561 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005562 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005563 } else {
5564 DCHECK(destination.IsStackSlot());
5565 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005566 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005567 sp,
5568 destination.GetStackIndex());
5569 }
5570 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005571 if (destination.IsRegister()) {
5572 GetAssembler()->LoadFromOffset(kLoadWord,
5573 RegisterFrom(destination),
5574 sp,
5575 source.GetStackIndex());
5576 } else if (destination.IsFpuRegister()) {
5577 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5578 } else {
5579 DCHECK(destination.IsStackSlot());
5580 vixl32::Register temp = temps.Acquire();
5581 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5582 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5583 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005584 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005585 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005586 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005587 } else if (destination.IsFpuRegister()) {
5588 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5589 } else {
5590 DCHECK(destination.IsStackSlot());
5591 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5592 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005593 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005594 if (destination.IsDoubleStackSlot()) {
5595 vixl32::DRegister temp = temps.AcquireD();
5596 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5597 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5598 } else if (destination.IsRegisterPair()) {
5599 DCHECK(ExpectedPairLayout(destination));
5600 GetAssembler()->LoadFromOffset(
5601 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5602 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005603 DCHECK(destination.IsFpuRegisterPair()) << destination;
5604 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005605 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005606 } else if (source.IsRegisterPair()) {
5607 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005608 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5609 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005610 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005611 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005612 } else {
5613 DCHECK(destination.IsDoubleStackSlot()) << destination;
5614 DCHECK(ExpectedPairLayout(source));
5615 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005616 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005617 sp,
5618 destination.GetStackIndex());
5619 }
5620 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005621 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005622 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005623 } else if (destination.IsFpuRegisterPair()) {
5624 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5625 } else {
5626 DCHECK(destination.IsDoubleStackSlot()) << destination;
5627 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5628 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005629 } else {
5630 DCHECK(source.IsConstant()) << source;
5631 HConstant* constant = source.GetConstant();
5632 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5633 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5634 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005635 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005636 } else {
5637 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005638 vixl32::Register temp = temps.Acquire();
5639 __ Mov(temp, value);
5640 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5641 }
5642 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005643 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01005644 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005645 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5646 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005647 } else {
5648 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005649 vixl32::Register temp = temps.Acquire();
5650 __ Mov(temp, Low32Bits(value));
5651 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5652 __ Mov(temp, High32Bits(value));
5653 GetAssembler()->StoreToOffset(kStoreWord,
5654 temp,
5655 sp,
5656 destination.GetHighStackIndex(kArmWordSize));
5657 }
5658 } else if (constant->IsDoubleConstant()) {
5659 double value = constant->AsDoubleConstant()->GetValue();
5660 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005661 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005662 } else {
5663 DCHECK(destination.IsDoubleStackSlot()) << destination;
5664 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005665 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005667 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005668 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005669 GetAssembler()->StoreToOffset(kStoreWord,
5670 temp,
5671 sp,
5672 destination.GetHighStackIndex(kArmWordSize));
5673 }
5674 } else {
5675 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5676 float value = constant->AsFloatConstant()->GetValue();
5677 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005678 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005679 } else {
5680 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005681 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005682 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005683 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5684 }
5685 }
5686 }
5687}
5688
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005689void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5690 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5691 vixl32::Register temp = temps.Acquire();
5692 __ Mov(temp, reg);
5693 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5694 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005695}
5696
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005697void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5698 // TODO(VIXL32): Double check the performance of this implementation.
5699 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Artem Serov4593f7d2016-12-29 16:21:49 +00005700 vixl32::SRegister temp_1 = temps.AcquireS();
5701 vixl32::SRegister temp_2 = temps.AcquireS();
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005702
Artem Serov4593f7d2016-12-29 16:21:49 +00005703 __ Vldr(temp_1, MemOperand(sp, mem1));
5704 __ Vldr(temp_2, MemOperand(sp, mem2));
5705 __ Vstr(temp_1, MemOperand(sp, mem2));
5706 __ Vstr(temp_2, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005707}
5708
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005709void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5710 MoveOperands* move = moves_[index];
5711 Location source = move->GetSource();
5712 Location destination = move->GetDestination();
5713 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5714
5715 if (source.IsRegister() && destination.IsRegister()) {
5716 vixl32::Register temp = temps.Acquire();
5717 DCHECK(!RegisterFrom(source).Is(temp));
5718 DCHECK(!RegisterFrom(destination).Is(temp));
5719 __ Mov(temp, RegisterFrom(destination));
5720 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5721 __ Mov(RegisterFrom(source), temp);
5722 } else if (source.IsRegister() && destination.IsStackSlot()) {
5723 Exchange(RegisterFrom(source), destination.GetStackIndex());
5724 } else if (source.IsStackSlot() && destination.IsRegister()) {
5725 Exchange(RegisterFrom(destination), source.GetStackIndex());
5726 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005727 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005728 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005729 vixl32::SRegister temp = temps.AcquireS();
5730 __ Vmov(temp, SRegisterFrom(source));
5731 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5732 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005733 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5734 vixl32::DRegister temp = temps.AcquireD();
5735 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5736 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5737 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5738 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5739 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5740 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5741 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5742 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5743 vixl32::DRegister temp = temps.AcquireD();
5744 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5745 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5746 GetAssembler()->StoreDToOffset(temp, sp, mem);
5747 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005748 vixl32::DRegister first = DRegisterFrom(source);
5749 vixl32::DRegister second = DRegisterFrom(destination);
5750 vixl32::DRegister temp = temps.AcquireD();
5751 __ Vmov(temp, first);
5752 __ Vmov(first, second);
5753 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005754 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005755 vixl32::DRegister reg = source.IsFpuRegisterPair()
5756 ? DRegisterFrom(source)
5757 : DRegisterFrom(destination);
5758 int mem = source.IsFpuRegisterPair()
5759 ? destination.GetStackIndex()
5760 : source.GetStackIndex();
5761 vixl32::DRegister temp = temps.AcquireD();
5762 __ Vmov(temp, reg);
5763 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5764 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005765 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005766 vixl32::SRegister reg = source.IsFpuRegister()
5767 ? SRegisterFrom(source)
5768 : SRegisterFrom(destination);
5769 int mem = source.IsFpuRegister()
5770 ? destination.GetStackIndex()
5771 : source.GetStackIndex();
5772 vixl32::Register temp = temps.Acquire();
5773 __ Vmov(temp, reg);
5774 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5775 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005776 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5777 vixl32::DRegister temp1 = temps.AcquireD();
5778 vixl32::DRegister temp2 = temps.AcquireD();
5779 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
5780 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
5781 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
5782 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
5783 } else {
5784 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
5785 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005786}
5787
5788void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
5789 TODO_VIXL32(FATAL);
5790}
5791
5792void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
5793 TODO_VIXL32(FATAL);
5794}
5795
Artem Serov02d37832016-10-25 15:25:33 +01005796HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005797 HLoadClass::LoadKind desired_class_load_kind) {
5798 switch (desired_class_load_kind) {
5799 case HLoadClass::LoadKind::kReferrersClass:
5800 break;
5801 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005802 DCHECK(!GetCompilerOptions().GetCompilePic());
5803 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005804 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5805 DCHECK(GetCompilerOptions().GetCompilePic());
5806 break;
5807 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005808 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005809 case HLoadClass::LoadKind::kBssEntry:
5810 DCHECK(!Runtime::Current()->UseJitCompilation());
5811 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005812 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005813 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005814 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005815 case HLoadClass::LoadKind::kDexCacheViaMethod:
5816 break;
5817 }
5818 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005819}
5820
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005821void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005822 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5823 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005824 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005825 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005826 cls,
5827 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005828 LocationFrom(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 return;
5830 }
Vladimir Marko41559982017-01-06 14:04:23 +00005831 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01005832
Artem Serovd4cc5b22016-11-04 11:19:09 +00005833 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5834 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005835 ? LocationSummary::kCallOnSlowPath
5836 : LocationSummary::kNoCall;
5837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005838 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005839 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00005840 }
5841
Vladimir Marko41559982017-01-06 14:04:23 +00005842 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005843 locations->SetInAt(0, Location::RequiresRegister());
5844 }
5845 locations->SetOut(Location::RequiresRegister());
5846}
5847
5848void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005849 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5850 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5851 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005852 return;
5853 }
Vladimir Marko41559982017-01-06 14:04:23 +00005854 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005855
Vladimir Marko41559982017-01-06 14:04:23 +00005856 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005857 Location out_loc = locations->Out();
5858 vixl32::Register out = OutputRegister(cls);
5859
Artem Serovd4cc5b22016-11-04 11:19:09 +00005860 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5861 ? kWithoutReadBarrier
5862 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005863 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005864 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005865 case HLoadClass::LoadKind::kReferrersClass: {
5866 DCHECK(!cls->CanCallRuntime());
5867 DCHECK(!cls->MustGenerateClinitCheck());
5868 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5869 vixl32::Register current_method = InputRegisterAt(cls, 0);
5870 GenerateGcRootFieldLoad(cls,
5871 out_loc,
5872 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01005873 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00005874 read_barrier_option);
5875 break;
5876 }
5877 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005878 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00005879 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5880 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5881 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005882 break;
5883 }
5884 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005885 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00005886 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5887 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
5888 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5889 codegen_->EmitMovwMovtPlaceholder(labels, out);
5890 break;
5891 }
5892 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005893 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5894 DCHECK_NE(cls->GetAddress(), 0u);
5895 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5896 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005897 break;
5898 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005899 case HLoadClass::LoadKind::kBssEntry: {
5900 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5901 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
5902 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5903 codegen_->EmitMovwMovtPlaceholder(labels, out);
5904 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5905 generate_null_check = true;
5906 break;
5907 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005908 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005909 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5910 cls->GetTypeIndex(),
5911 cls->GetAddress()));
5912 // /* GcRoot<mirror::Class> */ out = *out
5913 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005914 break;
5915 }
Vladimir Marko41559982017-01-06 14:04:23 +00005916 case HLoadClass::LoadKind::kDexCacheViaMethod:
5917 LOG(FATAL) << "UNREACHABLE";
5918 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005919 }
5920
5921 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5922 DCHECK(cls->CanCallRuntime());
5923 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
5924 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5925 codegen_->AddSlowPath(slow_path);
5926 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005927 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005928 }
5929 if (cls->MustGenerateClinitCheck()) {
5930 GenerateClassInitializationCheck(slow_path, out);
5931 } else {
5932 __ Bind(slow_path->GetExitLabel());
5933 }
5934 }
5935}
5936
Artem Serov02d37832016-10-25 15:25:33 +01005937void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5938 LocationSummary* locations =
5939 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5940 locations->SetInAt(0, Location::RequiresRegister());
5941 if (check->HasUses()) {
5942 locations->SetOut(Location::SameAsFirstInput());
5943 }
5944}
5945
5946void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5947 // We assume the class is not null.
5948 LoadClassSlowPathARMVIXL* slow_path =
5949 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
5950 check,
5951 check->GetDexPc(),
5952 /* do_clinit */ true);
5953 codegen_->AddSlowPath(slow_path);
5954 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
5955}
5956
5957void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
5958 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
5959 UseScratchRegisterScope temps(GetVIXLAssembler());
5960 vixl32::Register temp = temps.Acquire();
5961 GetAssembler()->LoadFromOffset(kLoadWord,
5962 temp,
5963 class_reg,
5964 mirror::Class::StatusOffset().Int32Value());
5965 __ Cmp(temp, mirror::Class::kStatusInitialized);
5966 __ B(lt, slow_path->GetEntryLabel());
5967 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5968 // properly. Therefore, we do a memory fence.
5969 __ Dmb(ISH);
5970 __ Bind(slow_path->GetExitLabel());
5971}
5972
Artem Serov02d37832016-10-25 15:25:33 +01005973HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005974 HLoadString::LoadKind desired_string_load_kind) {
5975 switch (desired_string_load_kind) {
5976 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005977 DCHECK(!GetCompilerOptions().GetCompilePic());
5978 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005979 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5980 DCHECK(GetCompilerOptions().GetCompilePic());
5981 break;
5982 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005983 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005984 case HLoadString::LoadKind::kBssEntry:
5985 DCHECK(!Runtime::Current()->UseJitCompilation());
5986 break;
5987 case HLoadString::LoadKind::kJitTableAddress:
5988 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005989 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005990 case HLoadString::LoadKind::kDexCacheViaMethod:
5991 break;
5992 }
5993 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005994}
5995
5996void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00005997 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01005998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01005999 HLoadString::LoadKind load_kind = load->GetLoadKind();
6000 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006001 locations->SetOut(LocationFrom(r0));
6002 } else {
6003 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006004 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6005 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6006 // Rely on the pResolveString and/or marking to save everything, including temps.
6007 // Note that IP may theoretically be clobbered by saving/restoring the live register
6008 // (only one thanks to the custom calling convention), so we request a different temp.
6009 locations->AddTemp(Location::RequiresRegister());
6010 RegisterSet caller_saves = RegisterSet::Empty();
6011 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6012 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6013 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6014 // that the the kPrimNot result register is the same as the first argument register.
6015 locations->SetCustomSlowPathCallerSaves(caller_saves);
6016 } else {
6017 // For non-Baker read barrier we have a temp-clobbering call.
6018 }
6019 }
Artem Serov02d37832016-10-25 15:25:33 +01006020 }
6021}
6022
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006023// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6024// move.
6025void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006026 LocationSummary* locations = load->GetLocations();
6027 Location out_loc = locations->Out();
6028 vixl32::Register out = OutputRegister(load);
6029 HLoadString::LoadKind load_kind = load->GetLoadKind();
6030
6031 switch (load_kind) {
6032 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006033 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6034 load->GetStringIndex()));
6035 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006036 }
6037 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6038 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6039 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006040 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006041 codegen_->EmitMovwMovtPlaceholder(labels, out);
6042 return; // No dex cache slow path.
6043 }
6044 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006045 uint32_t address = dchecked_integral_cast<uint32_t>(
6046 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6047 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006048 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6049 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006050 }
6051 case HLoadString::LoadKind::kBssEntry: {
6052 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6053 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
6054 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006055 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006056 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6057 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6058 LoadStringSlowPathARMVIXL* slow_path =
6059 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6060 codegen_->AddSlowPath(slow_path);
6061 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6062 __ Bind(slow_path->GetExitLabel());
6063 return;
6064 }
6065 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006066 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006067 load->GetStringIndex(),
6068 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006069 // /* GcRoot<mirror::String> */ out = *out
6070 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6071 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006072 }
6073 default:
6074 break;
6075 }
Artem Serov02d37832016-10-25 15:25:33 +01006076
6077 // TODO: Re-add the compiler code to do string dex cache lookup again.
6078 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6079 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006080 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006081 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6082 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6083}
6084
6085static int32_t GetExceptionTlsOffset() {
6086 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6087}
6088
6089void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6090 LocationSummary* locations =
6091 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6092 locations->SetOut(Location::RequiresRegister());
6093}
6094
6095void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6096 vixl32::Register out = OutputRegister(load);
6097 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6098}
6099
6100
6101void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6102 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6103}
6104
6105void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6106 UseScratchRegisterScope temps(GetVIXLAssembler());
6107 vixl32::Register temp = temps.Acquire();
6108 __ Mov(temp, 0);
6109 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6110}
6111
6112void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6113 LocationSummary* locations =
6114 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6115 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6116 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6117}
6118
6119void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6120 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6121 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6122}
6123
Artem Serov657022c2016-11-23 14:19:38 +00006124// Temp is used for read barrier.
6125static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6126 if (kEmitCompilerReadBarrier &&
6127 (kUseBakerReadBarrier ||
6128 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6129 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6130 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6131 return 1;
6132 }
6133 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006134}
6135
Artem Serov657022c2016-11-23 14:19:38 +00006136// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6137// interface pointer, one for loading the current interface.
6138// The other checks have one temp for loading the object's class.
6139static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6140 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6141 return 3;
6142 }
6143 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6144}
Artem Serovcfbe9132016-10-14 15:58:56 +01006145
6146void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6147 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6148 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6149 bool baker_read_barrier_slow_path = false;
6150 switch (type_check_kind) {
6151 case TypeCheckKind::kExactCheck:
6152 case TypeCheckKind::kAbstractClassCheck:
6153 case TypeCheckKind::kClassHierarchyCheck:
6154 case TypeCheckKind::kArrayObjectCheck:
6155 call_kind =
6156 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6157 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6158 break;
6159 case TypeCheckKind::kArrayCheck:
6160 case TypeCheckKind::kUnresolvedCheck:
6161 case TypeCheckKind::kInterfaceCheck:
6162 call_kind = LocationSummary::kCallOnSlowPath;
6163 break;
6164 }
6165
6166 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6167 if (baker_read_barrier_slow_path) {
6168 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6169 }
6170 locations->SetInAt(0, Location::RequiresRegister());
6171 locations->SetInAt(1, Location::RequiresRegister());
6172 // The "out" register is used as a temporary, so it overlaps with the inputs.
6173 // Note that TypeCheckSlowPathARM uses this register too.
6174 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006175 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006176}
6177
6178void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6179 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6180 LocationSummary* locations = instruction->GetLocations();
6181 Location obj_loc = locations->InAt(0);
6182 vixl32::Register obj = InputRegisterAt(instruction, 0);
6183 vixl32::Register cls = InputRegisterAt(instruction, 1);
6184 Location out_loc = locations->Out();
6185 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006186 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6187 DCHECK_LE(num_temps, 1u);
6188 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6190 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6191 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6192 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6193 vixl32::Label done, zero;
6194 SlowPathCodeARMVIXL* slow_path = nullptr;
6195
6196 // Return 0 if `obj` is null.
6197 // avoid null check if we know obj is not null.
6198 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006199 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006200 }
6201
Artem Serovcfbe9132016-10-14 15:58:56 +01006202 switch (type_check_kind) {
6203 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006204 // /* HeapReference<Class> */ out = obj->klass_
6205 GenerateReferenceLoadTwoRegisters(instruction,
6206 out_loc,
6207 obj_loc,
6208 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006209 maybe_temp_loc,
6210 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006211 __ Cmp(out, cls);
6212 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00006213 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006214 __ Mov(out, 1);
6215 __ B(&done);
6216 break;
6217 }
6218
6219 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006220 // /* HeapReference<Class> */ out = obj->klass_
6221 GenerateReferenceLoadTwoRegisters(instruction,
6222 out_loc,
6223 obj_loc,
6224 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006225 maybe_temp_loc,
6226 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006227 // If the class is abstract, we eagerly fetch the super class of the
6228 // object to avoid doing a comparison we know will fail.
6229 vixl32::Label loop;
6230 __ Bind(&loop);
6231 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006232 GenerateReferenceLoadOneRegister(instruction,
6233 out_loc,
6234 super_offset,
6235 maybe_temp_loc,
6236 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006237 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006238 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006239 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006240 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006241 __ Mov(out, 1);
6242 if (zero.IsReferenced()) {
6243 __ B(&done);
6244 }
6245 break;
6246 }
6247
6248 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006249 // /* HeapReference<Class> */ out = obj->klass_
6250 GenerateReferenceLoadTwoRegisters(instruction,
6251 out_loc,
6252 obj_loc,
6253 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006254 maybe_temp_loc,
6255 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006256 // Walk over the class hierarchy to find a match.
6257 vixl32::Label loop, success;
6258 __ Bind(&loop);
6259 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006260 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006261 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006262 GenerateReferenceLoadOneRegister(instruction,
6263 out_loc,
6264 super_offset,
6265 maybe_temp_loc,
6266 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006267 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006268 // If `out` is null, we use it for the result, and jump to `done`.
6269 __ B(&done);
6270 __ Bind(&success);
6271 __ Mov(out, 1);
6272 if (zero.IsReferenced()) {
6273 __ B(&done);
6274 }
6275 break;
6276 }
6277
6278 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006279 // /* HeapReference<Class> */ out = obj->klass_
6280 GenerateReferenceLoadTwoRegisters(instruction,
6281 out_loc,
6282 obj_loc,
6283 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006284 maybe_temp_loc,
6285 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006286 // Do an exact check.
6287 vixl32::Label exact_check;
6288 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006289 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006290 // Otherwise, we need to check that the object's class is a non-primitive array.
6291 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006292 GenerateReferenceLoadOneRegister(instruction,
6293 out_loc,
6294 component_offset,
6295 maybe_temp_loc,
6296 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006297 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006298 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006299 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6300 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006301 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006302 __ Bind(&exact_check);
6303 __ Mov(out, 1);
6304 __ B(&done);
6305 break;
6306 }
6307
6308 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006309 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006310 // /* HeapReference<Class> */ out = obj->klass_
6311 GenerateReferenceLoadTwoRegisters(instruction,
6312 out_loc,
6313 obj_loc,
6314 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006315 maybe_temp_loc,
6316 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006317 __ Cmp(out, cls);
6318 DCHECK(locations->OnlyCallsOnSlowPath());
6319 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6320 /* is_fatal */ false);
6321 codegen_->AddSlowPath(slow_path);
6322 __ B(ne, slow_path->GetEntryLabel());
6323 __ Mov(out, 1);
6324 if (zero.IsReferenced()) {
6325 __ B(&done);
6326 }
6327 break;
6328 }
6329
6330 case TypeCheckKind::kUnresolvedCheck:
6331 case TypeCheckKind::kInterfaceCheck: {
6332 // Note that we indeed only call on slow path, but we always go
6333 // into the slow path for the unresolved and interface check
6334 // cases.
6335 //
6336 // We cannot directly call the InstanceofNonTrivial runtime
6337 // entry point without resorting to a type checking slow path
6338 // here (i.e. by calling InvokeRuntime directly), as it would
6339 // require to assign fixed registers for the inputs of this
6340 // HInstanceOf instruction (following the runtime calling
6341 // convention), which might be cluttered by the potential first
6342 // read barrier emission at the beginning of this method.
6343 //
6344 // TODO: Introduce a new runtime entry point taking the object
6345 // to test (instead of its class) as argument, and let it deal
6346 // with the read barrier issues. This will let us refactor this
6347 // case of the `switch` code as it was previously (with a direct
6348 // call to the runtime not using a type checking slow path).
6349 // This should also be beneficial for the other cases above.
6350 DCHECK(locations->OnlyCallsOnSlowPath());
6351 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6352 /* is_fatal */ false);
6353 codegen_->AddSlowPath(slow_path);
6354 __ B(slow_path->GetEntryLabel());
6355 if (zero.IsReferenced()) {
6356 __ B(&done);
6357 }
6358 break;
6359 }
6360 }
6361
6362 if (zero.IsReferenced()) {
6363 __ Bind(&zero);
6364 __ Mov(out, 0);
6365 }
6366
6367 if (done.IsReferenced()) {
6368 __ Bind(&done);
6369 }
6370
6371 if (slow_path != nullptr) {
6372 __ Bind(slow_path->GetExitLabel());
6373 }
6374}
6375
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006376void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6377 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6378 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6379
6380 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6381 switch (type_check_kind) {
6382 case TypeCheckKind::kExactCheck:
6383 case TypeCheckKind::kAbstractClassCheck:
6384 case TypeCheckKind::kClassHierarchyCheck:
6385 case TypeCheckKind::kArrayObjectCheck:
6386 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6387 LocationSummary::kCallOnSlowPath :
6388 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6389 break;
6390 case TypeCheckKind::kArrayCheck:
6391 case TypeCheckKind::kUnresolvedCheck:
6392 case TypeCheckKind::kInterfaceCheck:
6393 call_kind = LocationSummary::kCallOnSlowPath;
6394 break;
6395 }
6396
6397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6398 locations->SetInAt(0, Location::RequiresRegister());
6399 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006400 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006401}
6402
6403void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6404 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6405 LocationSummary* locations = instruction->GetLocations();
6406 Location obj_loc = locations->InAt(0);
6407 vixl32::Register obj = InputRegisterAt(instruction, 0);
6408 vixl32::Register cls = InputRegisterAt(instruction, 1);
6409 Location temp_loc = locations->GetTemp(0);
6410 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006411 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6412 DCHECK_LE(num_temps, 3u);
6413 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6414 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6415 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6416 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6417 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6418 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6419 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6420 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6421 const uint32_t object_array_data_offset =
6422 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006423
Artem Serov657022c2016-11-23 14:19:38 +00006424 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6425 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6426 // read barriers is done for performance and code size reasons.
6427 bool is_type_check_slow_path_fatal = false;
6428 if (!kEmitCompilerReadBarrier) {
6429 is_type_check_slow_path_fatal =
6430 (type_check_kind == TypeCheckKind::kExactCheck ||
6431 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6432 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6433 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6434 !instruction->CanThrowIntoCatchBlock();
6435 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006436 SlowPathCodeARMVIXL* type_check_slow_path =
6437 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6438 is_type_check_slow_path_fatal);
6439 codegen_->AddSlowPath(type_check_slow_path);
6440
6441 vixl32::Label done;
6442 // Avoid null check if we know obj is not null.
6443 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006444 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006445 }
6446
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006447 switch (type_check_kind) {
6448 case TypeCheckKind::kExactCheck:
6449 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006450 // /* HeapReference<Class> */ temp = obj->klass_
6451 GenerateReferenceLoadTwoRegisters(instruction,
6452 temp_loc,
6453 obj_loc,
6454 class_offset,
6455 maybe_temp2_loc,
6456 kWithoutReadBarrier);
6457
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006458 __ Cmp(temp, cls);
6459 // Jump to slow path for throwing the exception or doing a
6460 // more involved array check.
6461 __ B(ne, type_check_slow_path->GetEntryLabel());
6462 break;
6463 }
6464
6465 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006466 // /* HeapReference<Class> */ temp = obj->klass_
6467 GenerateReferenceLoadTwoRegisters(instruction,
6468 temp_loc,
6469 obj_loc,
6470 class_offset,
6471 maybe_temp2_loc,
6472 kWithoutReadBarrier);
6473
Artem Serovcfbe9132016-10-14 15:58:56 +01006474 // If the class is abstract, we eagerly fetch the super class of the
6475 // object to avoid doing a comparison we know will fail.
6476 vixl32::Label loop;
6477 __ Bind(&loop);
6478 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006479 GenerateReferenceLoadOneRegister(instruction,
6480 temp_loc,
6481 super_offset,
6482 maybe_temp2_loc,
6483 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006484
6485 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6486 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006487 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006488
6489 // Otherwise, compare the classes.
6490 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006491 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006492 break;
6493 }
6494
6495 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006496 // /* HeapReference<Class> */ temp = obj->klass_
6497 GenerateReferenceLoadTwoRegisters(instruction,
6498 temp_loc,
6499 obj_loc,
6500 class_offset,
6501 maybe_temp2_loc,
6502 kWithoutReadBarrier);
6503
Artem Serovcfbe9132016-10-14 15:58:56 +01006504 // Walk over the class hierarchy to find a match.
6505 vixl32::Label loop;
6506 __ Bind(&loop);
6507 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006508 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006509
6510 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006511 GenerateReferenceLoadOneRegister(instruction,
6512 temp_loc,
6513 super_offset,
6514 maybe_temp2_loc,
6515 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006516
6517 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6518 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006519 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006520 // Otherwise, jump to the beginning of the loop.
6521 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006522 break;
6523 }
6524
Artem Serovcfbe9132016-10-14 15:58:56 +01006525 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006526 // /* HeapReference<Class> */ temp = obj->klass_
6527 GenerateReferenceLoadTwoRegisters(instruction,
6528 temp_loc,
6529 obj_loc,
6530 class_offset,
6531 maybe_temp2_loc,
6532 kWithoutReadBarrier);
6533
Artem Serovcfbe9132016-10-14 15:58:56 +01006534 // Do an exact check.
6535 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006536 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006537
6538 // Otherwise, we need to check that the object's class is a non-primitive array.
6539 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006540 GenerateReferenceLoadOneRegister(instruction,
6541 temp_loc,
6542 component_offset,
6543 maybe_temp2_loc,
6544 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006545 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006546 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006547 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6548 // to further check that this component type is not a primitive type.
6549 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6550 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006551 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006552 break;
6553 }
6554
6555 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006556 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006557 // We cannot directly call the CheckCast runtime entry point
6558 // without resorting to a type checking slow path here (i.e. by
6559 // calling InvokeRuntime directly), as it would require to
6560 // assign fixed registers for the inputs of this HInstanceOf
6561 // instruction (following the runtime calling convention), which
6562 // might be cluttered by the potential first read barrier
6563 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006564
Artem Serovcfbe9132016-10-14 15:58:56 +01006565 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006566 break;
Artem Serov657022c2016-11-23 14:19:38 +00006567
6568 case TypeCheckKind::kInterfaceCheck: {
6569 // Avoid read barriers to improve performance of the fast path. We can not get false
6570 // positives by doing this.
6571 // /* HeapReference<Class> */ temp = obj->klass_
6572 GenerateReferenceLoadTwoRegisters(instruction,
6573 temp_loc,
6574 obj_loc,
6575 class_offset,
6576 maybe_temp2_loc,
6577 kWithoutReadBarrier);
6578
6579 // /* HeapReference<Class> */ temp = temp->iftable_
6580 GenerateReferenceLoadTwoRegisters(instruction,
6581 temp_loc,
6582 temp_loc,
6583 iftable_offset,
6584 maybe_temp2_loc,
6585 kWithoutReadBarrier);
6586 // Iftable is never null.
6587 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6588 // Loop through the iftable and check if any class matches.
6589 vixl32::Label start_loop;
6590 __ Bind(&start_loop);
6591 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6592 type_check_slow_path->GetEntryLabel());
6593 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6594 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6595 // Go to next interface.
6596 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6597 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6598 // Compare the classes and continue the loop if they do not match.
6599 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00006600 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00006601 break;
6602 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006603 }
6604 __ Bind(&done);
6605
6606 __ Bind(type_check_slow_path->GetExitLabel());
6607}
6608
Artem Serov551b28f2016-10-18 19:11:30 +01006609void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6610 LocationSummary* locations =
6611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6612 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6613 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6614}
6615
6616void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6617 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6618 instruction,
6619 instruction->GetDexPc());
6620 if (instruction->IsEnter()) {
6621 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6622 } else {
6623 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6624 }
6625}
6626
Artem Serov02109dd2016-09-23 17:17:54 +01006627void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6628 HandleBitwiseOperation(instruction, AND);
6629}
6630
6631void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6632 HandleBitwiseOperation(instruction, ORR);
6633}
6634
6635void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6636 HandleBitwiseOperation(instruction, EOR);
6637}
6638
6639void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6640 LocationSummary* locations =
6641 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6642 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6643 || instruction->GetResultType() == Primitive::kPrimLong);
6644 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6645 locations->SetInAt(0, Location::RequiresRegister());
6646 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6648}
6649
6650void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6651 HandleBitwiseOperation(instruction);
6652}
6653
6654void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6655 HandleBitwiseOperation(instruction);
6656}
6657
6658void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6659 HandleBitwiseOperation(instruction);
6660}
6661
Artem Serov2bbc9532016-10-21 11:51:50 +01006662void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6663 LocationSummary* locations =
6664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6665 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6666 || instruction->GetResultType() == Primitive::kPrimLong);
6667
6668 locations->SetInAt(0, Location::RequiresRegister());
6669 locations->SetInAt(1, Location::RequiresRegister());
6670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6671}
6672
6673void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6674 LocationSummary* locations = instruction->GetLocations();
6675 Location first = locations->InAt(0);
6676 Location second = locations->InAt(1);
6677 Location out = locations->Out();
6678
6679 if (instruction->GetResultType() == Primitive::kPrimInt) {
6680 vixl32::Register first_reg = RegisterFrom(first);
6681 vixl32::Register second_reg = RegisterFrom(second);
6682 vixl32::Register out_reg = RegisterFrom(out);
6683
6684 switch (instruction->GetOpKind()) {
6685 case HInstruction::kAnd:
6686 __ Bic(out_reg, first_reg, second_reg);
6687 break;
6688 case HInstruction::kOr:
6689 __ Orn(out_reg, first_reg, second_reg);
6690 break;
6691 // There is no EON on arm.
6692 case HInstruction::kXor:
6693 default:
6694 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6695 UNREACHABLE();
6696 }
6697 return;
6698
6699 } else {
6700 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6701 vixl32::Register first_low = LowRegisterFrom(first);
6702 vixl32::Register first_high = HighRegisterFrom(first);
6703 vixl32::Register second_low = LowRegisterFrom(second);
6704 vixl32::Register second_high = HighRegisterFrom(second);
6705 vixl32::Register out_low = LowRegisterFrom(out);
6706 vixl32::Register out_high = HighRegisterFrom(out);
6707
6708 switch (instruction->GetOpKind()) {
6709 case HInstruction::kAnd:
6710 __ Bic(out_low, first_low, second_low);
6711 __ Bic(out_high, first_high, second_high);
6712 break;
6713 case HInstruction::kOr:
6714 __ Orn(out_low, first_low, second_low);
6715 __ Orn(out_high, first_high, second_high);
6716 break;
6717 // There is no EON on arm.
6718 case HInstruction::kXor:
6719 default:
6720 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6721 UNREACHABLE();
6722 }
6723 }
6724}
6725
Artem Serov02109dd2016-09-23 17:17:54 +01006726// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6727void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
6728 vixl32::Register first,
6729 uint32_t value) {
6730 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6731 if (value == 0xffffffffu) {
6732 if (!out.Is(first)) {
6733 __ Mov(out, first);
6734 }
6735 return;
6736 }
6737 if (value == 0u) {
6738 __ Mov(out, 0);
6739 return;
6740 }
6741 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
6742 __ And(out, first, value);
6743 } else {
6744 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
6745 __ Bic(out, first, ~value);
6746 }
6747}
6748
6749// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6750void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
6751 vixl32::Register first,
6752 uint32_t value) {
6753 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6754 if (value == 0u) {
6755 if (!out.Is(first)) {
6756 __ Mov(out, first);
6757 }
6758 return;
6759 }
6760 if (value == 0xffffffffu) {
6761 __ Mvn(out, 0);
6762 return;
6763 }
6764 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
6765 __ Orr(out, first, value);
6766 } else {
6767 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
6768 __ Orn(out, first, ~value);
6769 }
6770}
6771
6772// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6773void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
6774 vixl32::Register first,
6775 uint32_t value) {
6776 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6777 if (value == 0u) {
6778 if (!out.Is(first)) {
6779 __ Mov(out, first);
6780 }
6781 return;
6782 }
6783 __ Eor(out, first, value);
6784}
6785
Anton Kirilovdda43962016-11-21 19:55:20 +00006786void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
6787 Location first,
6788 uint64_t value) {
6789 vixl32::Register out_low = LowRegisterFrom(out);
6790 vixl32::Register out_high = HighRegisterFrom(out);
6791 vixl32::Register first_low = LowRegisterFrom(first);
6792 vixl32::Register first_high = HighRegisterFrom(first);
6793 uint32_t value_low = Low32Bits(value);
6794 uint32_t value_high = High32Bits(value);
6795 if (value_low == 0u) {
6796 if (!out_low.Is(first_low)) {
6797 __ Mov(out_low, first_low);
6798 }
6799 __ Add(out_high, first_high, value_high);
6800 return;
6801 }
6802 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006803 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006804 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006805 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006806 __ Sbc(out_high, first_high, ~value_high);
6807 } else {
6808 LOG(FATAL) << "Unexpected constant " << value_high;
6809 UNREACHABLE();
6810 }
6811}
6812
Artem Serov02109dd2016-09-23 17:17:54 +01006813void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
6814 LocationSummary* locations = instruction->GetLocations();
6815 Location first = locations->InAt(0);
6816 Location second = locations->InAt(1);
6817 Location out = locations->Out();
6818
6819 if (second.IsConstant()) {
6820 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6821 uint32_t value_low = Low32Bits(value);
6822 if (instruction->GetResultType() == Primitive::kPrimInt) {
6823 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6824 vixl32::Register out_reg = OutputRegister(instruction);
6825 if (instruction->IsAnd()) {
6826 GenerateAndConst(out_reg, first_reg, value_low);
6827 } else if (instruction->IsOr()) {
6828 GenerateOrrConst(out_reg, first_reg, value_low);
6829 } else {
6830 DCHECK(instruction->IsXor());
6831 GenerateEorConst(out_reg, first_reg, value_low);
6832 }
6833 } else {
6834 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6835 uint32_t value_high = High32Bits(value);
6836 vixl32::Register first_low = LowRegisterFrom(first);
6837 vixl32::Register first_high = HighRegisterFrom(first);
6838 vixl32::Register out_low = LowRegisterFrom(out);
6839 vixl32::Register out_high = HighRegisterFrom(out);
6840 if (instruction->IsAnd()) {
6841 GenerateAndConst(out_low, first_low, value_low);
6842 GenerateAndConst(out_high, first_high, value_high);
6843 } else if (instruction->IsOr()) {
6844 GenerateOrrConst(out_low, first_low, value_low);
6845 GenerateOrrConst(out_high, first_high, value_high);
6846 } else {
6847 DCHECK(instruction->IsXor());
6848 GenerateEorConst(out_low, first_low, value_low);
6849 GenerateEorConst(out_high, first_high, value_high);
6850 }
6851 }
6852 return;
6853 }
6854
6855 if (instruction->GetResultType() == Primitive::kPrimInt) {
6856 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6857 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
6858 vixl32::Register out_reg = OutputRegister(instruction);
6859 if (instruction->IsAnd()) {
6860 __ And(out_reg, first_reg, second_reg);
6861 } else if (instruction->IsOr()) {
6862 __ Orr(out_reg, first_reg, second_reg);
6863 } else {
6864 DCHECK(instruction->IsXor());
6865 __ Eor(out_reg, first_reg, second_reg);
6866 }
6867 } else {
6868 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6869 vixl32::Register first_low = LowRegisterFrom(first);
6870 vixl32::Register first_high = HighRegisterFrom(first);
6871 vixl32::Register second_low = LowRegisterFrom(second);
6872 vixl32::Register second_high = HighRegisterFrom(second);
6873 vixl32::Register out_low = LowRegisterFrom(out);
6874 vixl32::Register out_high = HighRegisterFrom(out);
6875 if (instruction->IsAnd()) {
6876 __ And(out_low, first_low, second_low);
6877 __ And(out_high, first_high, second_high);
6878 } else if (instruction->IsOr()) {
6879 __ Orr(out_low, first_low, second_low);
6880 __ Orr(out_high, first_high, second_high);
6881 } else {
6882 DCHECK(instruction->IsXor());
6883 __ Eor(out_low, first_low, second_low);
6884 __ Eor(out_high, first_high, second_high);
6885 }
6886 }
6887}
6888
Artem Serovcfbe9132016-10-14 15:58:56 +01006889void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006890 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01006891 Location out,
6892 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006893 Location maybe_temp,
6894 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01006895 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006896 if (read_barrier_option == kWithReadBarrier) {
6897 CHECK(kEmitCompilerReadBarrier);
6898 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6899 if (kUseBakerReadBarrier) {
6900 // Load with fast path based Baker's read barrier.
6901 // /* HeapReference<Object> */ out = *(out + offset)
6902 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6903 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
6904 } else {
6905 // Load with slow path based read barrier.
6906 // Save the value of `out` into `maybe_temp` before overwriting it
6907 // in the following move operation, as we will need it for the
6908 // read barrier below.
6909 __ Mov(RegisterFrom(maybe_temp), out_reg);
6910 // /* HeapReference<Object> */ out = *(out + offset)
6911 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6912 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6913 }
Artem Serovcfbe9132016-10-14 15:58:56 +01006914 } else {
6915 // Plain load with no read barrier.
6916 // /* HeapReference<Object> */ out = *(out + offset)
6917 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6918 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6919 }
6920}
6921
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006922void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006923 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006924 Location out,
6925 Location obj,
6926 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006927 Location maybe_temp,
6928 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006929 vixl32::Register out_reg = RegisterFrom(out);
6930 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006931 if (read_barrier_option == kWithReadBarrier) {
6932 CHECK(kEmitCompilerReadBarrier);
6933 if (kUseBakerReadBarrier) {
6934 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6935 // Load with fast path based Baker's read barrier.
6936 // /* HeapReference<Object> */ out = *(obj + offset)
6937 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6938 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
6939 } else {
6940 // Load with slow path based read barrier.
6941 // /* HeapReference<Object> */ out = *(obj + offset)
6942 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6943 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6944 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006945 } else {
6946 // Plain load with no read barrier.
6947 // /* HeapReference<Object> */ out = *(obj + offset)
6948 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6949 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6950 }
6951}
6952
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006953void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006954 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006955 Location root,
6956 vixl32::Register obj,
6957 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00006958 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006959 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006960 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006961 DCHECK(kEmitCompilerReadBarrier);
6962 if (kUseBakerReadBarrier) {
6963 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6964 // Baker's read barrier are used:
6965 //
6966 // root = obj.field;
6967 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6968 // if (temp != null) {
6969 // root = temp(root)
6970 // }
6971
6972 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6973 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
6974 static_assert(
6975 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6976 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6977 "have different sizes.");
6978 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6979 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6980 "have different sizes.");
6981
6982 // Slow path marking the GC root `root`.
6983 Location temp = LocationFrom(lr);
6984 SlowPathCodeARMVIXL* slow_path =
6985 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
6986 instruction,
6987 root,
6988 /*entrypoint*/ temp);
6989 codegen_->AddSlowPath(slow_path);
6990
6991 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6992 const int32_t entry_point_offset =
6993 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6994 // Loading the entrypoint does not require a load acquire since it is only changed when
6995 // threads are suspended or running a checkpoint.
6996 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
6997 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6998 // checking GetIsGcMarking.
6999 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7000 __ Bind(slow_path->GetExitLabel());
7001 } else {
7002 // GC root loaded through a slow path for read barriers other
7003 // than Baker's.
7004 // /* GcRoot<mirror::Object>* */ root = obj + offset
7005 __ Add(root_reg, obj, offset);
7006 // /* mirror::Object* */ root = root->Read()
7007 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7008 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007009 } else {
7010 // Plain GC root load with no read barrier.
7011 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7012 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7013 // Note that GC roots are not affected by heap poisoning, thus we
7014 // do not have to unpoison `root_reg` here.
7015 }
7016}
7017
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007018void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7019 Location ref,
7020 vixl32::Register obj,
7021 uint32_t offset,
7022 Location temp,
7023 bool needs_null_check) {
7024 DCHECK(kEmitCompilerReadBarrier);
7025 DCHECK(kUseBakerReadBarrier);
7026
7027 // /* HeapReference<Object> */ ref = *(obj + offset)
7028 Location no_index = Location::NoLocation();
7029 ScaleFactor no_scale_factor = TIMES_1;
7030 GenerateReferenceLoadWithBakerReadBarrier(
7031 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007032}
7033
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007034void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7035 Location ref,
7036 vixl32::Register obj,
7037 uint32_t data_offset,
7038 Location index,
7039 Location temp,
7040 bool needs_null_check) {
7041 DCHECK(kEmitCompilerReadBarrier);
7042 DCHECK(kUseBakerReadBarrier);
7043
7044 static_assert(
7045 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7046 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7047 // /* HeapReference<Object> */ ref =
7048 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7049 ScaleFactor scale_factor = TIMES_4;
7050 GenerateReferenceLoadWithBakerReadBarrier(
7051 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007052}
7053
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007054void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7055 Location ref,
7056 vixl32::Register obj,
7057 uint32_t offset,
7058 Location index,
7059 ScaleFactor scale_factor,
7060 Location temp,
7061 bool needs_null_check,
7062 bool always_update_field,
7063 vixl32::Register* temp2) {
7064 DCHECK(kEmitCompilerReadBarrier);
7065 DCHECK(kUseBakerReadBarrier);
7066
7067 // In slow path based read barriers, the read barrier call is
7068 // inserted after the original load. However, in fast path based
7069 // Baker's read barriers, we need to perform the load of
7070 // mirror::Object::monitor_ *before* the original reference load.
7071 // This load-load ordering is required by the read barrier.
7072 // The fast path/slow path (for Baker's algorithm) should look like:
7073 //
7074 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7075 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7076 // HeapReference<Object> ref = *src; // Original reference load.
7077 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7078 // if (is_gray) {
7079 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7080 // }
7081 //
7082 // Note: the original implementation in ReadBarrier::Barrier is
7083 // slightly more complex as it performs additional checks that we do
7084 // not do here for performance reasons.
7085
7086 vixl32::Register ref_reg = RegisterFrom(ref);
7087 vixl32::Register temp_reg = RegisterFrom(temp);
7088 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7089
7090 // /* int32_t */ monitor = obj->monitor_
7091 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7092 if (needs_null_check) {
7093 MaybeRecordImplicitNullCheck(instruction);
7094 }
7095 // /* LockWord */ lock_word = LockWord(monitor)
7096 static_assert(sizeof(LockWord) == sizeof(int32_t),
7097 "art::LockWord and int32_t have different sizes.");
7098
7099 // Introduce a dependency on the lock_word including the rb_state,
7100 // which shall prevent load-load reordering without using
7101 // a memory barrier (which would be more expensive).
7102 // `obj` is unchanged by this operation, but its value now depends
7103 // on `temp_reg`.
7104 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7105
7106 // The actual reference load.
7107 if (index.IsValid()) {
7108 // Load types involving an "index": ArrayGet,
7109 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7110 // intrinsics.
7111 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7112 if (index.IsConstant()) {
7113 size_t computed_offset =
7114 (Int32ConstantFrom(index) << scale_factor) + offset;
7115 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7116 } else {
7117 // Handle the special case of the
7118 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7119 // intrinsics, which use a register pair as index ("long
7120 // offset"), of which only the low part contains data.
7121 vixl32::Register index_reg = index.IsRegisterPair()
7122 ? LowRegisterFrom(index)
7123 : RegisterFrom(index);
7124 UseScratchRegisterScope temps(GetVIXLAssembler());
7125 const vixl32::Register temp3 = temps.Acquire();
7126 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7127 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7128 }
7129 } else {
7130 // /* HeapReference<Object> */ ref = *(obj + offset)
7131 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7132 }
7133
7134 // Object* ref = ref_addr->AsMirrorPtr()
7135 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7136
7137 // Slow path marking the object `ref` when it is gray.
7138 SlowPathCodeARMVIXL* slow_path;
7139 if (always_update_field) {
7140 DCHECK(temp2 != nullptr);
7141 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7142 // of the form `obj + field_offset`, where `obj` is a register and
7143 // `field_offset` is a register pair (of which only the lower half
7144 // is used). Thus `offset` and `scale_factor` above are expected
7145 // to be null in this code path.
7146 DCHECK_EQ(offset, 0u);
7147 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7148 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
7149 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7150 } else {
7151 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
7152 }
7153 AddSlowPath(slow_path);
7154
7155 // if (rb_state == ReadBarrier::GrayState())
7156 // ref = ReadBarrier::Mark(ref);
7157 // Given the numeric representation, it's enough to check the low bit of the
7158 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7159 // which can be a 16-bit instruction unlike the TST immediate.
7160 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7161 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7162 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7163 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
7164 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007165}
7166
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007167void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7168 Location out,
7169 Location ref,
7170 Location obj,
7171 uint32_t offset,
7172 Location index) {
7173 DCHECK(kEmitCompilerReadBarrier);
7174
7175 // Insert a slow path based read barrier *after* the reference load.
7176 //
7177 // If heap poisoning is enabled, the unpoisoning of the loaded
7178 // reference will be carried out by the runtime within the slow
7179 // path.
7180 //
7181 // Note that `ref` currently does not get unpoisoned (when heap
7182 // poisoning is enabled), which is alright as the `ref` argument is
7183 // not used by the artReadBarrierSlow entry point.
7184 //
7185 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7186 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7187 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7188 AddSlowPath(slow_path);
7189
7190 __ B(slow_path->GetEntryLabel());
7191 __ Bind(slow_path->GetExitLabel());
7192}
7193
7194void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007195 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007196 Location ref,
7197 Location obj,
7198 uint32_t offset,
7199 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007200 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007201 // Baker's read barriers shall be handled by the fast path
7202 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007203 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007204 // If heap poisoning is enabled, unpoisoning will be taken care of
7205 // by the runtime within the slow path.
7206 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007207 } else if (kPoisonHeapReferences) {
7208 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7209 }
7210}
7211
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007212void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7213 Location out,
7214 Location root) {
7215 DCHECK(kEmitCompilerReadBarrier);
7216
7217 // Insert a slow path based read barrier *after* the GC root load.
7218 //
7219 // Note that GC roots are not affected by heap poisoning, so we do
7220 // not need to do anything special for this here.
7221 SlowPathCodeARMVIXL* slow_path =
7222 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7223 AddSlowPath(slow_path);
7224
7225 __ B(slow_path->GetEntryLabel());
7226 __ Bind(slow_path->GetExitLabel());
7227}
7228
Artem Serov02d37832016-10-25 15:25:33 +01007229// Check if the desired_dispatch_info is supported. If it is, return it,
7230// otherwise return a fall-back info that should be used instead.
7231HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007232 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007233 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007234 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7235 // We disable pc-relative load when there is an irreducible loop, as the optimization
7236 // is incompatible with it.
7237 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7238 // with irreducible loops.
7239 if (GetGraph()->HasIrreducibleLoops() &&
7240 (dispatch_info.method_load_kind ==
7241 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7242 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7243 }
7244
Artem Serovd4cc5b22016-11-04 11:19:09 +00007245 return dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007246}
7247
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007248vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7249 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7250 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7251 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7252 if (!invoke->GetLocations()->Intrinsified()) {
7253 return RegisterFrom(location);
7254 }
7255 // For intrinsics we allow any location, so it may be on the stack.
7256 if (!location.IsRegister()) {
7257 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7258 return temp;
7259 }
7260 // For register locations, check if the register was saved. If so, get it from the stack.
7261 // Note: There is a chance that the register was saved but not overwritten, so we could
7262 // save one load. However, since this is just an intrinsic slow path we prefer this
7263 // simple and more robust approach rather that trying to determine if that's the case.
7264 SlowPathCode* slow_path = GetCurrentSlowPath();
7265 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7266 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
7267 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7268 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7269 return temp;
7270 }
7271 return RegisterFrom(location);
7272}
7273
7274void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
7275 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007276 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007277 switch (invoke->GetMethodLoadKind()) {
7278 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7279 uint32_t offset =
7280 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7281 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007282 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7283 break;
7284 }
7285 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7286 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7287 break;
7288 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7289 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7290 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007291 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7292 HArmDexCacheArraysBase* base =
7293 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7294 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7295 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7296 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007297 break;
7298 }
7299 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7300 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7301 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007302 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007303 if (current_method.IsRegister()) {
7304 method_reg = RegisterFrom(current_method);
7305 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007306 DCHECK(invoke->GetLocations()->Intrinsified());
7307 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007308 method_reg = reg;
7309 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007310 }
7311 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7312 GetAssembler()->LoadFromOffset(
7313 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007314 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007315 method_reg,
7316 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7317 // temp = temp[index_in_cache];
7318 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7319 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7320 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007321 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007322 break;
7323 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007324 }
7325
Artem Serovd4cc5b22016-11-04 11:19:09 +00007326 switch (invoke->GetCodePtrLocation()) {
7327 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7328 __ Bl(GetFrameEntryLabel());
7329 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007330 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7331 // LR = callee_method->entry_point_from_quick_compiled_code_
7332 GetAssembler()->LoadFromOffset(
7333 kLoadWord,
7334 lr,
7335 RegisterFrom(callee_method),
7336 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007337 {
7338 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00007339 ExactAssemblyScope aas(GetVIXLAssembler(),
7340 vixl32::k16BitT32InstructionSizeInBytes,
7341 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007342 // LR()
7343 __ blx(lr);
7344 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007345 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007346 }
7347
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007348 DCHECK(!IsLeafMethod());
7349}
7350
7351void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7352 vixl32::Register temp = RegisterFrom(temp_location);
7353 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7354 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7355
7356 // Use the calling convention instead of the location of the receiver, as
7357 // intrinsics may have put the receiver in a different register. In the intrinsics
7358 // slow path, the arguments have been moved to the right place, so here we are
7359 // guaranteed that the receiver is the first register of the calling convention.
7360 InvokeDexCallingConventionARMVIXL calling_convention;
7361 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7362 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007363 {
7364 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007365 ExactAssemblyScope aas(GetVIXLAssembler(),
7366 vixl32::kMaxInstructionSizeInBytes,
7367 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007368 // /* HeapReference<Class> */ temp = receiver->klass_
7369 __ ldr(temp, MemOperand(receiver, class_offset));
7370 MaybeRecordImplicitNullCheck(invoke);
7371 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007372 // Instead of simply (possibly) unpoisoning `temp` here, we should
7373 // emit a read barrier for the previous class reference load.
7374 // However this is not required in practice, as this is an
7375 // intermediate/temporary reference and because the current
7376 // concurrent copying collector keeps the from-space memory
7377 // intact/accessible until the end of the marking phase (the
7378 // concurrent copying collector may not in the future).
7379 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7380
7381 // temp = temp->GetMethodAt(method_offset);
7382 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7383 kArmPointerSize).Int32Value();
7384 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7385 // LR = temp->GetEntryPoint();
7386 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7387 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007388 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7389 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7390 // that.
7391 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00007392 ExactAssemblyScope aas(GetVIXLAssembler(),
7393 vixl32::k16BitT32InstructionSizeInBytes,
7394 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007395 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007396}
7397
Artem Serovd4cc5b22016-11-04 11:19:09 +00007398CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007399 const DexFile& dex_file, dex::StringIndex string_index) {
7400 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007401}
7402
7403CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7404 const DexFile& dex_file, dex::TypeIndex type_index) {
7405 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7406}
7407
7408CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7409 const DexFile& dex_file, uint32_t element_offset) {
7410 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7411}
7412
7413CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7414 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7415 patches->emplace_back(dex_file, offset_or_index);
7416 return &patches->back();
7417}
7418
Artem Serovc5fcb442016-12-02 19:19:58 +00007419VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7420 const DexFile& dex_file,
7421 dex::StringIndex string_index) {
7422 return boot_image_string_patches_.GetOrCreate(
7423 StringReference(&dex_file, string_index),
7424 [this]() {
7425 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7426 });
7427}
7428
7429VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7430 const DexFile& dex_file,
7431 dex::TypeIndex type_index) {
7432 return boot_image_type_patches_.GetOrCreate(
7433 TypeReference(&dex_file, type_index),
7434 [this]() {
7435 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7436 });
7437}
7438
7439VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
7440 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7441 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7442 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7443}
7444
7445VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7446 return DeduplicateUint32Literal(address, &uint32_literals_);
7447}
7448
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007449VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
7450 const DexFile& dex_file,
7451 dex::StringIndex string_index,
7452 Handle<mirror::String> handle) {
7453 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7454 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007455 return jit_string_patches_.GetOrCreate(
7456 StringReference(&dex_file, string_index),
7457 [this]() {
7458 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7459 });
7460}
7461
7462VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7463 dex::TypeIndex type_index,
7464 uint64_t address) {
7465 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
7466 return jit_class_patches_.GetOrCreate(
7467 TypeReference(&dex_file, type_index),
7468 [this]() {
7469 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7470 });
7471}
7472
Artem Serovd4cc5b22016-11-04 11:19:09 +00007473template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7474inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7475 const ArenaDeque<PcRelativePatchInfo>& infos,
7476 ArenaVector<LinkerPatch>* linker_patches) {
7477 for (const PcRelativePatchInfo& info : infos) {
7478 const DexFile& dex_file = info.target_dex_file;
7479 size_t offset_or_index = info.offset_or_index;
7480 DCHECK(info.add_pc_label.IsBound());
7481 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7482 // Add MOVW patch.
7483 DCHECK(info.movw_label.IsBound());
7484 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7485 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7486 // Add MOVT patch.
7487 DCHECK(info.movt_label.IsBound());
7488 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7489 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7490 }
7491}
7492
7493void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7494 DCHECK(linker_patches->empty());
7495 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00007496 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007497 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007498 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007499 boot_image_type_patches_.size() +
7500 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
7501 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007502 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007503 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7504 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007505 for (const auto& entry : boot_image_string_patches_) {
7506 const StringReference& target_string = entry.first;
7507 VIXLUInt32Literal* literal = entry.second;
7508 DCHECK(literal->IsBound());
7509 uint32_t literal_offset = literal->GetLocation();
7510 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7511 target_string.dex_file,
7512 target_string.string_index.index_));
7513 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007514 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007515 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(pc_relative_type_patches_,
7516 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007517 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7518 linker_patches);
7519 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007520 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7521 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007522 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7523 linker_patches);
7524 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007525 for (const auto& entry : boot_image_type_patches_) {
7526 const TypeReference& target_type = entry.first;
7527 VIXLUInt32Literal* literal = entry.second;
7528 DCHECK(literal->IsBound());
7529 uint32_t literal_offset = literal->GetLocation();
7530 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7531 target_type.dex_file,
7532 target_type.type_index.index_));
7533 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007534 for (const auto& entry : boot_image_address_patches_) {
7535 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7536 VIXLUInt32Literal* literal = entry.second;
7537 DCHECK(literal->IsBound());
7538 uint32_t literal_offset = literal->GetLocation();
7539 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7540 }
7541}
7542
7543VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7544 uint32_t value,
7545 Uint32ToLiteralMap* map) {
7546 return map->GetOrCreate(
7547 value,
7548 [this, value]() {
7549 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7550 });
7551}
7552
7553VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7554 MethodReference target_method,
7555 MethodToLiteralMap* map) {
7556 return map->GetOrCreate(
7557 target_method,
7558 [this]() {
7559 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7560 });
7561}
7562
Artem Serov2bbc9532016-10-21 11:51:50 +01007563void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7564 LocationSummary* locations =
7565 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7566 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7567 Location::RequiresRegister());
7568 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7569 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7571}
7572
7573void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7574 vixl32::Register res = OutputRegister(instr);
7575 vixl32::Register accumulator =
7576 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7577 vixl32::Register mul_left =
7578 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7579 vixl32::Register mul_right =
7580 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7581
7582 if (instr->GetOpKind() == HInstruction::kAdd) {
7583 __ Mla(res, mul_left, mul_right, accumulator);
7584 } else {
7585 __ Mls(res, mul_left, mul_right, accumulator);
7586 }
7587}
7588
Artem Serov551b28f2016-10-18 19:11:30 +01007589void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7590 // Nothing to do, this should be removed during prepare for register allocator.
7591 LOG(FATAL) << "Unreachable";
7592}
7593
7594void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7595 // Nothing to do, this should be removed during prepare for register allocator.
7596 LOG(FATAL) << "Unreachable";
7597}
7598
7599// Simple implementation of packed switch - generate cascaded compare/jumps.
7600void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7601 LocationSummary* locations =
7602 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7603 locations->SetInAt(0, Location::RequiresRegister());
7604 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7605 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7606 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7607 if (switch_instr->GetStartValue() != 0) {
7608 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7609 }
7610 }
7611}
7612
7613// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7614void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7615 int32_t lower_bound = switch_instr->GetStartValue();
7616 uint32_t num_entries = switch_instr->GetNumEntries();
7617 LocationSummary* locations = switch_instr->GetLocations();
7618 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7619 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7620
7621 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7622 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7623 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007624 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007625 vixl32::Register temp_reg = temps.Acquire();
7626 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7627 // the immediate, because IP is used as the destination register. For the other
7628 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7629 // and they can be encoded in the instruction without making use of IP register.
7630 __ Adds(temp_reg, value_reg, -lower_bound);
7631
7632 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7633 // Jump to successors[0] if value == lower_bound.
7634 __ B(eq, codegen_->GetLabelOf(successors[0]));
7635 int32_t last_index = 0;
7636 for (; num_entries - last_index > 2; last_index += 2) {
7637 __ Adds(temp_reg, temp_reg, -2);
7638 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7639 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7640 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7641 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7642 }
7643 if (num_entries - last_index == 2) {
7644 // The last missing case_value.
7645 __ Cmp(temp_reg, 1);
7646 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7647 }
7648
7649 // And the default for any other value.
7650 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7651 __ B(codegen_->GetLabelOf(default_block));
7652 }
7653 } else {
7654 // Create a table lookup.
7655 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7656
7657 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7658
7659 // Remove the bias.
7660 vixl32::Register key_reg;
7661 if (lower_bound != 0) {
7662 key_reg = RegisterFrom(locations->GetTemp(1));
7663 __ Sub(key_reg, value_reg, lower_bound);
7664 } else {
7665 key_reg = value_reg;
7666 }
7667
7668 // Check whether the value is in the table, jump to default block if not.
7669 __ Cmp(key_reg, num_entries - 1);
7670 __ B(hi, codegen_->GetLabelOf(default_block));
7671
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007672 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007673 vixl32::Register jump_offset = temps.Acquire();
7674
7675 // Load jump offset from the table.
7676 __ Adr(table_base, jump_table->GetTableStartLabel());
7677 __ Ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
7678
7679 // Jump to target block by branching to table_base(pc related) + offset.
7680 vixl32::Register target_address = table_base;
7681 __ Add(target_address, table_base, jump_offset);
7682 __ Bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007683
7684 jump_table->EmitTable(codegen_);
Artem Serov551b28f2016-10-18 19:11:30 +01007685 }
7686}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007687void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7689 locations->SetOut(Location::RequiresRegister());
7690}
7691
7692void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7693 vixl32::Register base_reg = OutputRegister(base);
7694 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7695 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7696 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
7697}
Artem Serov551b28f2016-10-18 19:11:30 +01007698
Artem Serov02d37832016-10-25 15:25:33 +01007699// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007700void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7701 if (!trg.IsValid()) {
7702 DCHECK_EQ(type, Primitive::kPrimVoid);
7703 return;
7704 }
7705
7706 DCHECK_NE(type, Primitive::kPrimVoid);
7707
Artem Serovd4cc5b22016-11-04 11:19:09 +00007708 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007709 if (return_loc.Equals(trg)) {
7710 return;
7711 }
7712
7713 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7714 // with the last branch.
7715 if (type == Primitive::kPrimLong) {
7716 TODO_VIXL32(FATAL);
7717 } else if (type == Primitive::kPrimDouble) {
7718 TODO_VIXL32(FATAL);
7719 } else {
7720 // Let the parallel move resolver take care of all of this.
7721 HParallelMove parallel_move(GetGraph()->GetArena());
7722 parallel_move.AddMove(return_loc, trg, type, nullptr);
7723 GetMoveResolver()->EmitNativeCode(&parallel_move);
7724 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007725}
Scott Wakelingfe885462016-09-22 10:24:38 +01007726
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007727void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7728 LocationSummary* locations =
7729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7730 locations->SetInAt(0, Location::RequiresRegister());
7731 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01007732}
7733
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007734void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7735 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7736 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7737 instruction->GetIndex(), kArmPointerSize).SizeValue();
7738 GetAssembler()->LoadFromOffset(kLoadWord,
7739 OutputRegister(instruction),
7740 InputRegisterAt(instruction, 0),
7741 method_offset);
7742 } else {
7743 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7744 instruction->GetIndex(), kArmPointerSize));
7745 GetAssembler()->LoadFromOffset(kLoadWord,
7746 OutputRegister(instruction),
7747 InputRegisterAt(instruction, 0),
7748 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7749 GetAssembler()->LoadFromOffset(kLoadWord,
7750 OutputRegister(instruction),
7751 OutputRegister(instruction),
7752 method_offset);
7753 }
Artem Serov551b28f2016-10-18 19:11:30 +01007754}
7755
Artem Serovc5fcb442016-12-02 19:19:58 +00007756static void PatchJitRootUse(uint8_t* code,
7757 const uint8_t* roots_data,
7758 VIXLUInt32Literal* literal,
7759 uint64_t index_in_table) {
7760 DCHECK(literal->IsBound());
7761 uint32_t literal_offset = literal->GetLocation();
7762 uintptr_t address =
7763 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7764 uint8_t* data = code + literal_offset;
7765 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7766}
7767
7768void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7769 for (const auto& entry : jit_string_patches_) {
7770 const auto& it = jit_string_roots_.find(entry.first);
7771 DCHECK(it != jit_string_roots_.end());
7772 PatchJitRootUse(code, roots_data, entry.second, it->second);
7773 }
7774 for (const auto& entry : jit_class_patches_) {
7775 const auto& it = jit_class_roots_.find(entry.first);
7776 DCHECK(it != jit_class_roots_.end());
7777 PatchJitRootUse(code, roots_data, entry.second, it->second);
7778 }
7779}
7780
Artem Serovd4cc5b22016-11-04 11:19:09 +00007781void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
7782 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
7783 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00007784 ExactAssemblyScope aas(GetVIXLAssembler(),
7785 3 * vixl32::kMaxInstructionSizeInBytes,
7786 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007787 // TODO(VIXL): Think about using mov instead of movw.
7788 __ bind(&labels->movw_label);
7789 __ movw(out, /* placeholder */ 0u);
7790 __ bind(&labels->movt_label);
7791 __ movt(out, /* placeholder */ 0u);
7792 __ bind(&labels->add_pc_label);
7793 __ add(out, out, pc);
7794}
7795
Scott Wakelingfe885462016-09-22 10:24:38 +01007796#undef __
7797#undef QUICK_ENTRY_POINT
7798#undef TODO_VIXL32
7799
7800} // namespace arm
7801} // namespace art