blob: e943db733a706a55d901169eab3769ecb54a3d7f [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;
Donghui Bai426b49c2016-11-08 14:55:38 +080045using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010046using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010047using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000050using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010051using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000052using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000053using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010054using helpers::LocationFrom;
55using helpers::LowRegisterFrom;
56using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080057using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::OutputRegister;
59using helpers::OutputSRegister;
60using helpers::OutputVRegister;
61using helpers::RegisterFrom;
62using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000063using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010064
Artem Serov0fb37192016-12-06 18:13:40 +000065using vixl::ExactAssemblyScope;
66using vixl::CodeBufferCheckScope;
67
Scott Wakelingfe885462016-09-22 10:24:38 +010068using RegisterList = vixl32::RegisterList;
69
70static bool ExpectedPairLayout(Location location) {
71 // We expected this for both core and fpu register pairs.
72 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
73}
Artem Serovd4cc5b22016-11-04 11:19:09 +000074// Use a local definition to prevent copying mistakes.
75static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
76static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010077static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010079
80#ifdef __
81#error "ARM Codegen VIXL macro-assembler macro already defined."
82#endif
83
Scott Wakelingfe885462016-09-22 10:24:38 +010084// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
85#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
86#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
87
88// Marker that code is yet to be, and must, be implemented.
89#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
90
Scott Wakelinga7812ae2016-10-17 10:03:36 +010091// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
92// for each live D registers they treat two corresponding S registers as live ones.
93//
94// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
95// from a list of contiguous S registers a list of contiguous D registers (processing first/last
96// S registers corner cases) and save/restore this new list treating them as D registers.
97// - decreasing code size
98// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
99// restored and then used in regular non SlowPath code as D register.
100//
101// For the following example (v means the S register is live):
102// D names: | D0 | D1 | D2 | D4 | ...
103// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
104// Live? | | v | v | v | v | v | v | | ...
105//
106// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
107// as D registers.
108//
109// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
110// for lists of floating-point registers.
111static size_t SaveContiguousSRegisterList(size_t first,
112 size_t last,
113 CodeGenerator* codegen,
114 size_t stack_offset) {
115 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
116 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
117 DCHECK_LE(first, last);
118 if ((first == last) && (first == 0)) {
119 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
120 return stack_offset + kSRegSizeInBytes;
121 }
122 if (first % 2 == 1) {
123 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
124 stack_offset += kSRegSizeInBytes;
125 }
126
127 bool save_last = false;
128 if (last % 2 == 0) {
129 save_last = true;
130 --last;
131 }
132
133 if (first < last) {
134 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
135 DCHECK_EQ((last - first + 1) % 2, 0u);
136 size_t number_of_d_regs = (last - first + 1) / 2;
137
138 if (number_of_d_regs == 1) {
139 __ Vstr(d_reg, MemOperand(sp, stack_offset));
140 } else if (number_of_d_regs > 1) {
141 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
142 vixl32::Register base = sp;
143 if (stack_offset != 0) {
144 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000145 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100146 }
147 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
148 }
149 stack_offset += number_of_d_regs * kDRegSizeInBytes;
150 }
151
152 if (save_last) {
153 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
154 stack_offset += kSRegSizeInBytes;
155 }
156
157 return stack_offset;
158}
159
160static size_t RestoreContiguousSRegisterList(size_t first,
161 size_t last,
162 CodeGenerator* codegen,
163 size_t stack_offset) {
164 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
165 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
166 DCHECK_LE(first, last);
167 if ((first == last) && (first == 0)) {
168 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
169 return stack_offset + kSRegSizeInBytes;
170 }
171 if (first % 2 == 1) {
172 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
173 stack_offset += kSRegSizeInBytes;
174 }
175
176 bool restore_last = false;
177 if (last % 2 == 0) {
178 restore_last = true;
179 --last;
180 }
181
182 if (first < last) {
183 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
184 DCHECK_EQ((last - first + 1) % 2, 0u);
185 size_t number_of_d_regs = (last - first + 1) / 2;
186 if (number_of_d_regs == 1) {
187 __ Vldr(d_reg, MemOperand(sp, stack_offset));
188 } else if (number_of_d_regs > 1) {
189 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
190 vixl32::Register base = sp;
191 if (stack_offset != 0) {
192 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000193 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100194 }
195 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
196 }
197 stack_offset += number_of_d_regs * kDRegSizeInBytes;
198 }
199
200 if (restore_last) {
201 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
202 stack_offset += kSRegSizeInBytes;
203 }
204
205 return stack_offset;
206}
207
208void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
209 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
210 size_t orig_offset = stack_offset;
211
212 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
213 for (uint32_t i : LowToHighBits(core_spills)) {
214 // If the register holds an object, update the stack mask.
215 if (locations->RegisterContainsObject(i)) {
216 locations->SetStackBit(stack_offset / kVRegSize);
217 }
218 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
219 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
220 saved_core_stack_offsets_[i] = stack_offset;
221 stack_offset += kArmWordSize;
222 }
223
224 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
225 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
226
227 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
228 orig_offset = stack_offset;
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
231 saved_fpu_stack_offsets_[i] = stack_offset;
232 stack_offset += kArmWordSize;
233 }
234
235 stack_offset = orig_offset;
236 while (fp_spills != 0u) {
237 uint32_t begin = CTZ(fp_spills);
238 uint32_t tmp = fp_spills + (1u << begin);
239 fp_spills &= tmp; // Clear the contiguous range of 1s.
240 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
241 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
242 }
243 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
244}
245
246void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
247 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
248 size_t orig_offset = stack_offset;
249
250 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
251 for (uint32_t i : LowToHighBits(core_spills)) {
252 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
253 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
254 stack_offset += kArmWordSize;
255 }
256
257 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
258 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
259 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
260
261 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
262 while (fp_spills != 0u) {
263 uint32_t begin = CTZ(fp_spills);
264 uint32_t tmp = fp_spills + (1u << begin);
265 fp_spills &= tmp; // Clear the contiguous range of 1s.
266 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
267 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
268 }
269 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
270}
271
272class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
273 public:
274 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
278 __ Bind(GetEntryLabel());
279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
283 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
290 bool IsFatal() const OVERRIDE { return true; }
291
292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
293
294 private:
295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
296};
297
Scott Wakelingfe885462016-09-22 10:24:38 +0100298class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
299 public:
300 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
301 : SlowPathCodeARMVIXL(instruction) {}
302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100304 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100305 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100306 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100307 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
308 }
309
310 bool IsFatal() const OVERRIDE { return true; }
311
312 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
313
314 private:
315 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
316};
317
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100318class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
319 public:
320 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
321 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
325 __ Bind(GetEntryLabel());
326 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
327 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
328 if (successor_ == nullptr) {
329 __ B(GetReturnLabel());
330 } else {
331 __ B(arm_codegen->GetLabelOf(successor_));
332 }
333 }
334
335 vixl32::Label* GetReturnLabel() {
336 DCHECK(successor_ == nullptr);
337 return &return_label_;
338 }
339
340 HBasicBlock* GetSuccessor() const {
341 return successor_;
342 }
343
344 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
345
346 private:
347 // If not null, the block to branch to after the suspend check.
348 HBasicBlock* const successor_;
349
350 // If `successor_` is null, the label to branch to after the suspend check.
351 vixl32::Label return_label_;
352
353 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
354};
355
Scott Wakelingc34dba72016-10-03 10:14:44 +0100356class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
357 public:
358 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
359 : SlowPathCodeARMVIXL(instruction) {}
360
361 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
362 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
363 LocationSummary* locations = instruction_->GetLocations();
364
365 __ Bind(GetEntryLabel());
366 if (instruction_->CanThrowIntoCatchBlock()) {
367 // Live registers will be restored in the catch block if caught.
368 SaveLiveRegisters(codegen, instruction_->GetLocations());
369 }
370 // We're moving two locations to locations that could overlap, so we need a parallel
371 // move resolver.
372 InvokeRuntimeCallingConventionARMVIXL calling_convention;
373 codegen->EmitParallelMoves(
374 locations->InAt(0),
375 LocationFrom(calling_convention.GetRegisterAt(0)),
376 Primitive::kPrimInt,
377 locations->InAt(1),
378 LocationFrom(calling_convention.GetRegisterAt(1)),
379 Primitive::kPrimInt);
380 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
381 ? kQuickThrowStringBounds
382 : kQuickThrowArrayBounds;
383 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
384 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
385 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
386 }
387
388 bool IsFatal() const OVERRIDE { return true; }
389
390 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
391
392 private:
393 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
394};
395
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100396class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
397 public:
398 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000399 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100400 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
401 }
402
403 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000404 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000405 Location out = locations->Out();
406 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100407
408 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000413 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
414 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
415 bool is_load_class_bss_entry =
416 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
417 vixl32::Register entry_address;
418 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
419 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
420 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
421 // the kSaveEverything call.
422 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
423 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
424 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
425 if (temp_is_r0) {
426 __ Mov(entry_address, temp);
427 }
428 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000429 dex::TypeIndex type_index = cls_->GetTypeIndex();
430 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100431 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
432 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000433 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100434 if (do_clinit_) {
435 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
436 } else {
437 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
438 }
439
Vladimir Markoea4c1262017-02-06 19:59:33 +0000440 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
441 if (is_load_class_bss_entry) {
442 if (call_saves_everything_except_r0) {
443 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
444 __ Str(r0, MemOperand(entry_address));
445 } else {
446 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
447 UseScratchRegisterScope temps(
448 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
449 vixl32::Register temp = temps.Acquire();
450 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
451 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
452 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
453 __ Str(r0, MemOperand(temp));
454 }
455 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100456 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100457 if (out.IsValid()) {
458 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
459 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
460 }
461 RestoreLiveRegisters(codegen, locations);
462 __ B(GetExitLabel());
463 }
464
465 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
466
467 private:
468 // The class this slow path will load.
469 HLoadClass* const cls_;
470
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100471 // The dex PC of `at_`.
472 const uint32_t dex_pc_;
473
474 // Whether to initialize the class.
475 const bool do_clinit_;
476
477 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
478};
479
Artem Serovd4cc5b22016-11-04 11:19:09 +0000480class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
481 public:
482 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
483 : SlowPathCodeARMVIXL(instruction) {}
484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000486 DCHECK(instruction_->IsLoadString());
487 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000488 LocationSummary* locations = instruction_->GetLocations();
489 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
490 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000491 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000492 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000493 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
494
495 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, locations);
498
499 InvokeRuntimeCallingConventionARMVIXL calling_convention;
500 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000501 // the kSaveEverything call.
502 vixl32::Register entry_address;
503 if (call_saves_everything_except_r0) {
504 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
505 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
506 entry_address = temp_is_r0 ? out : temp;
507 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
508 if (temp_is_r0) {
509 __ Mov(entry_address, temp);
510 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000511 }
512
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000513 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000514 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
515 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
516
517 // Store the resolved String to the .bss entry.
518 if (call_saves_everything_except_r0) {
519 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
520 __ Str(r0, MemOperand(entry_address));
521 } else {
522 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000523 UseScratchRegisterScope temps(
524 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
525 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000526 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
527 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000528 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
529 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000530 }
531
532 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
533 RestoreLiveRegisters(codegen, locations);
534
535 __ B(GetExitLabel());
536 }
537
538 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
539
540 private:
541 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
542};
543
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100544class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
545 public:
546 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
547 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
548
549 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
550 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100551 DCHECK(instruction_->IsCheckCast()
552 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
553
554 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
555 __ Bind(GetEntryLabel());
556
557 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100558 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100559 }
560
561 // We're moving two locations to locations that could overlap, so we need a parallel
562 // move resolver.
563 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100564
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800565 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800566 LocationFrom(calling_convention.GetRegisterAt(0)),
567 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800568 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800569 LocationFrom(calling_convention.GetRegisterAt(1)),
570 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100571 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100572 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
573 instruction_,
574 instruction_->GetDexPc(),
575 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800576 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100577 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100578 } else {
579 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800580 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
581 instruction_,
582 instruction_->GetDexPc(),
583 this);
584 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100585 }
586
587 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100588 RestoreLiveRegisters(codegen, locations);
589 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100590 }
591 }
592
593 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
594
595 bool IsFatal() const OVERRIDE { return is_fatal_; }
596
597 private:
598 const bool is_fatal_;
599
600 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
601};
602
Scott Wakelingc34dba72016-10-03 10:14:44 +0100603class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
604 public:
605 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
606 : SlowPathCodeARMVIXL(instruction) {}
607
608 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
609 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
610 __ Bind(GetEntryLabel());
611 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
612 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
613 }
614
615 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
616
617 private:
618 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
619};
620
621class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
622 public:
623 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 __ Bind(GetEntryLabel());
628 SaveLiveRegisters(codegen, locations);
629
630 InvokeRuntimeCallingConventionARMVIXL calling_convention;
631 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
632 parallel_move.AddMove(
633 locations->InAt(0),
634 LocationFrom(calling_convention.GetRegisterAt(0)),
635 Primitive::kPrimNot,
636 nullptr);
637 parallel_move.AddMove(
638 locations->InAt(1),
639 LocationFrom(calling_convention.GetRegisterAt(1)),
640 Primitive::kPrimInt,
641 nullptr);
642 parallel_move.AddMove(
643 locations->InAt(2),
644 LocationFrom(calling_convention.GetRegisterAt(2)),
645 Primitive::kPrimNot,
646 nullptr);
647 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
648
649 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
650 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
651 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
652 RestoreLiveRegisters(codegen, locations);
653 __ B(GetExitLabel());
654 }
655
656 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
657
658 private:
659 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
660};
661
Roland Levillain54f869e2017-03-06 13:54:11 +0000662// Abstract base class for read barrier slow paths marking a reference
663// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000664//
Roland Levillain54f869e2017-03-06 13:54:11 +0000665// Argument `entrypoint` must be a register location holding the read
666// barrier marking runtime entry point to be invoked.
667class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
668 protected:
669 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000670 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
671 DCHECK(kEmitCompilerReadBarrier);
672 }
673
Roland Levillain54f869e2017-03-06 13:54:11 +0000674 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000675
Roland Levillain54f869e2017-03-06 13:54:11 +0000676 // Generate assembly code calling the read barrier marking runtime
677 // entry point (ReadBarrierMarkRegX).
678 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000679 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000680
Roland Levillain47b3ab22017-02-27 14:31:35 +0000681 // No need to save live registers; it's taken care of by the
682 // entrypoint. Also, there is no need to update the stack mask,
683 // as this runtime call will not trigger a garbage collection.
684 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
685 DCHECK(!ref_reg.Is(sp));
686 DCHECK(!ref_reg.Is(lr));
687 DCHECK(!ref_reg.Is(pc));
688 // IP is used internally by the ReadBarrierMarkRegX entry point
689 // as a temporary, it cannot be the entry point's input/output.
690 DCHECK(!ref_reg.Is(ip));
691 DCHECK(ref_reg.IsRegister()) << ref_reg;
692 // "Compact" slow path, saving two moves.
693 //
694 // Instead of using the standard runtime calling convention (input
695 // and output in R0):
696 //
697 // R0 <- ref
698 // R0 <- ReadBarrierMark(R0)
699 // ref <- R0
700 //
701 // we just use rX (the register containing `ref`) as input and output
702 // of a dedicated entrypoint:
703 //
704 // rX <- ReadBarrierMarkRegX(rX)
705 //
706 if (entrypoint_.IsValid()) {
707 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
708 __ Blx(RegisterFrom(entrypoint_));
709 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000710 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000711 int32_t entry_point_offset =
712 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
713 // This runtime call does not require a stack map.
714 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
715 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000716 }
717
Roland Levillain47b3ab22017-02-27 14:31:35 +0000718 // The location (register) of the marked object reference.
719 const Location ref_;
720
721 // The location of the entrypoint if already loaded.
722 const Location entrypoint_;
723
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 private:
725 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000726};
727
Scott Wakelingc34dba72016-10-03 10:14:44 +0100728// Slow path marking an object reference `ref` during a read
729// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000730// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000731//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100732// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000733// always be up-to-date, but `obj.field` may not; i.e., after the
734// flip, `ref` will be a to-space reference, but `obj.field` will
735// probably still be a from-space reference (unless it gets updated by
736// another thread, or if another thread installed another object
737// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000738//
739// If `entrypoint` is a valid location it is assumed to already be
740// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillain54f869e2017-03-06 13:54:11 +0000741// is when the decision to mark is based on whether the GC is marking.
742class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000743 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000744 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
745 Location ref,
746 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000748 DCHECK(kEmitCompilerReadBarrier);
749 }
750
Roland Levillain47b3ab22017-02-27 14:31:35 +0000751 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
752
753 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
754 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000755 DCHECK(locations->CanCall());
756 DCHECK(ref_.IsRegister()) << ref_;
757 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
758 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
759 << "Unexpected instruction in read barrier marking slow path: "
760 << instruction_->DebugName();
761
762 __ Bind(GetEntryLabel());
763 GenerateReadBarrierMarkRuntimeCall(codegen);
764 __ B(GetExitLabel());
765 }
766
767 private:
768 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
769};
770
771// Slow path loading `obj`'s lock word, loading a reference from
772// object `*(obj + offset + (index << scale_factor))` into `ref`, and
773// marking `ref` if `obj` is gray according to the lock word (Baker
774// read barrier). The field `obj.field` in the object `obj` holding
775// this reference does not get updated by this slow path after marking
776// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
777// below for that).
778//
779// This means that after the execution of this slow path, `ref` will
780// always be up-to-date, but `obj.field` may not; i.e., after the
781// flip, `ref` will be a to-space reference, but `obj.field` will
782// probably still be a from-space reference (unless it gets updated by
783// another thread, or if another thread installed another object
784// reference (different from `ref`) in `obj.field`).
785//
786// Argument `entrypoint` must be a register location holding the read
787// barrier marking runtime entry point to be invoked.
788class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
789 public:
790 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
791 Location ref,
792 vixl32::Register obj,
793 uint32_t offset,
794 Location index,
795 ScaleFactor scale_factor,
796 bool needs_null_check,
797 vixl32::Register temp,
798 Location entrypoint)
799 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
800 obj_(obj),
801 offset_(offset),
802 index_(index),
803 scale_factor_(scale_factor),
804 needs_null_check_(needs_null_check),
805 temp_(temp) {
806 DCHECK(kEmitCompilerReadBarrier);
807 DCHECK(kUseBakerReadBarrier);
808 }
809
Roland Levillain47b3ab22017-02-27 14:31:35 +0000810 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000812 }
813
814 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
815 LocationSummary* locations = instruction_->GetLocations();
816 vixl32::Register ref_reg = RegisterFrom(ref_);
817 DCHECK(locations->CanCall());
818 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000819 DCHECK(instruction_->IsInstanceFieldGet() ||
820 instruction_->IsStaticFieldGet() ||
821 instruction_->IsArrayGet() ||
822 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000823 instruction_->IsInstanceOf() ||
824 instruction_->IsCheckCast() ||
825 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
826 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
827 << "Unexpected instruction in read barrier marking slow path: "
828 << instruction_->DebugName();
829 // The read barrier instrumentation of object ArrayGet
830 // instructions does not support the HIntermediateAddress
831 // instruction.
832 DCHECK(!(instruction_->IsArrayGet() &&
833 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
834
Roland Levillain54f869e2017-03-06 13:54:11 +0000835 // Temporary register `temp_`, used to store the lock word, must
836 // not be IP, as we may use it to emit the reference load (in the
837 // call to GenerateRawReferenceLoad below), and we need the lock
838 // word to still be in `temp_` after the reference load.
839 DCHECK(!temp_.Is(ip));
840
Roland Levillain47b3ab22017-02-27 14:31:35 +0000841 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000842
843 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
844 // inserted after the original load. However, in fast path based
845 // Baker's read barriers, we need to perform the load of
846 // mirror::Object::monitor_ *before* the original reference load.
847 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000848 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000849 //
850 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
851 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
852 // HeapReference<mirror::Object> ref = *src; // Original reference load.
853 // bool is_gray = (rb_state == ReadBarrier::GrayState());
854 // if (is_gray) {
855 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
856 // }
857 //
858 // Note: the original implementation in ReadBarrier::Barrier is
859 // slightly more complex as it performs additional checks that we do
860 // not do here for performance reasons.
861
Roland Levillain47b3ab22017-02-27 14:31:35 +0000862 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000863
864 // /* int32_t */ monitor = obj->monitor_
865 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
866 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
867 if (needs_null_check_) {
868 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000869 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000870 // /* LockWord */ lock_word = LockWord(monitor)
871 static_assert(sizeof(LockWord) == sizeof(int32_t),
872 "art::LockWord and int32_t have different sizes.");
873
874 // Introduce a dependency on the lock_word including the rb_state,
875 // which shall prevent load-load reordering without using
876 // a memory barrier (which would be more expensive).
877 // `obj` is unchanged by this operation, but its value now depends
878 // on `temp`.
879 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
880
881 // The actual reference load.
882 // A possible implicit null check has already been handled above.
883 arm_codegen->GenerateRawReferenceLoad(
884 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
885
886 // Mark the object `ref` when `obj` is gray.
887 //
888 // if (rb_state == ReadBarrier::GrayState())
889 // ref = ReadBarrier::Mark(ref);
890 //
891 // Given the numeric representation, it's enough to check the low bit of the
892 // rb_state. We do that by shifting the bit out of the lock word with LSRS
893 // which can be a 16-bit instruction unlike the TST immediate.
894 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
895 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
896 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
897 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
898 GenerateReadBarrierMarkRuntimeCall(codegen);
899
Roland Levillain47b3ab22017-02-27 14:31:35 +0000900 __ B(GetExitLabel());
901 }
902
903 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000904 // The register containing the object holding the marked object reference field.
905 vixl32::Register obj_;
906 // The offset, index and scale factor to access the reference in `obj_`.
907 uint32_t offset_;
908 Location index_;
909 ScaleFactor scale_factor_;
910 // Is a null check required?
911 bool needs_null_check_;
912 // A temporary register used to hold the lock word of `obj_`.
913 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000914
Roland Levillain54f869e2017-03-06 13:54:11 +0000915 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000916};
917
Roland Levillain54f869e2017-03-06 13:54:11 +0000918// Slow path loading `obj`'s lock word, loading a reference from
919// object `*(obj + offset + (index << scale_factor))` into `ref`, and
920// marking `ref` if `obj` is gray according to the lock word (Baker
921// read barrier). If needed, this slow path also atomically updates
922// the field `obj.field` in the object `obj` holding this reference
923// after marking (contrary to
924// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
925// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000926//
927// This means that after the execution of this slow path, both `ref`
928// and `obj.field` will be up-to-date; i.e., after the flip, both will
929// hold the same to-space reference (unless another thread installed
930// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000931//
Roland Levillain54f869e2017-03-06 13:54:11 +0000932//
933// Argument `entrypoint` must be a register location holding the read
934// barrier marking runtime entry point to be invoked.
935class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
936 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000937 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000938 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
939 Location ref,
940 vixl32::Register obj,
941 uint32_t offset,
942 Location index,
943 ScaleFactor scale_factor,
944 bool needs_null_check,
945 vixl32::Register temp1,
946 vixl32::Register temp2,
947 Location entrypoint)
948 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000949 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000950 offset_(offset),
951 index_(index),
952 scale_factor_(scale_factor),
953 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000954 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000955 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000956 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000957 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000958 }
959
960 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000961 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000962 }
963
964 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
965 LocationSummary* locations = instruction_->GetLocations();
966 vixl32::Register ref_reg = RegisterFrom(ref_);
967 DCHECK(locations->CanCall());
968 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
970
971 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000972 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
973 << "Unexpected instruction in read barrier marking and field updating slow path: "
974 << instruction_->DebugName();
975 DCHECK(instruction_->GetLocations()->Intrinsified());
976 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000977 DCHECK_EQ(offset_, 0u);
978 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
979 Location field_offset = index_;
980 DCHECK(field_offset.IsRegisterPair()) << field_offset;
981
982 // Temporary register `temp1_`, used to store the lock word, must
983 // not be IP, as we may use it to emit the reference load (in the
984 // call to GenerateRawReferenceLoad below), and we need the lock
985 // word to still be in `temp1_` after the reference load.
986 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +0000987
988 __ Bind(GetEntryLabel());
989
Roland Levillainff487002017-03-07 16:50:01 +0000990 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
991 //
992 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
993 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
994 // HeapReference<mirror::Object> ref = *src; // Original reference load.
995 // bool is_gray = (rb_state == ReadBarrier::GrayState());
996 // if (is_gray) {
997 // old_ref = ref;
998 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
999 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1000 // }
1001
Roland Levillain54f869e2017-03-06 13:54:11 +00001002 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1003
1004 // /* int32_t */ monitor = obj->monitor_
1005 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1006 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1007 if (needs_null_check_) {
1008 codegen->MaybeRecordImplicitNullCheck(instruction_);
1009 }
1010 // /* LockWord */ lock_word = LockWord(monitor)
1011 static_assert(sizeof(LockWord) == sizeof(int32_t),
1012 "art::LockWord and int32_t have different sizes.");
1013
1014 // Introduce a dependency on the lock_word including the rb_state,
1015 // which shall prevent load-load reordering without using
1016 // a memory barrier (which would be more expensive).
1017 // `obj` is unchanged by this operation, but its value now depends
1018 // on `temp`.
1019 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1020
1021 // The actual reference load.
1022 // A possible implicit null check has already been handled above.
1023 arm_codegen->GenerateRawReferenceLoad(
1024 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1025
1026 // Mark the object `ref` when `obj` is gray.
1027 //
1028 // if (rb_state == ReadBarrier::GrayState())
1029 // ref = ReadBarrier::Mark(ref);
1030 //
1031 // Given the numeric representation, it's enough to check the low bit of the
1032 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1033 // which can be a 16-bit instruction unlike the TST immediate.
1034 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1035 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1036 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1037 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1038
1039 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 // Note that we cannot use IP to save the old reference, as IP is
1041 // used internally by the ReadBarrierMarkRegX entry point, and we
1042 // need the old reference after the call to that entry point.
1043 DCHECK(!temp1_.Is(ip));
1044 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001045
Roland Levillain54f869e2017-03-06 13:54:11 +00001046 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001047
1048 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001049 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001050 //
1051 // Note that this field could also hold a different object, if
1052 // another thread had concurrently changed it. In that case, the
1053 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1054 // (CAS) operation below would abort the CAS, leaving the field
1055 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001056 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001057 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001058
1059 // Update the the holder's field atomically. This may fail if
1060 // mutator updates before us, but it's OK. This is achieved
1061 // using a strong compare-and-set (CAS) operation with relaxed
1062 // memory synchronization ordering, where the expected value is
1063 // the old reference and the desired value is the new reference.
1064
1065 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1066 // Convenience aliases.
1067 vixl32::Register base = obj_;
1068 // The UnsafeCASObject intrinsic uses a register pair as field
1069 // offset ("long offset"), of which only the low part contains
1070 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001071 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001072 vixl32::Register expected = temp1_;
1073 vixl32::Register value = ref_reg;
1074 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1075 vixl32::Register tmp = temp2_; // Value in memory.
1076
1077 __ Add(tmp_ptr, base, offset);
1078
1079 if (kPoisonHeapReferences) {
1080 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1081 if (value.Is(expected)) {
1082 // Do not poison `value`, as it is the same register as
1083 // `expected`, which has just been poisoned.
1084 } else {
1085 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1086 }
1087 }
1088
1089 // do {
1090 // tmp = [r_ptr] - expected;
1091 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1092
1093 vixl32::Label loop_head, exit_loop;
1094 __ Bind(&loop_head);
1095
1096 __ Ldrex(tmp, MemOperand(tmp_ptr));
1097
1098 __ Subs(tmp, tmp, expected);
1099
1100 {
Artem Serov0fb37192016-12-06 18:13:40 +00001101 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1102 2 * kMaxInstructionSizeInBytes,
1103 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001104
1105 __ it(ne);
1106 __ clrex(ne);
1107 }
1108
Artem Serov517d9f62016-12-12 15:51:15 +00001109 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001110
1111 __ Strex(tmp, value, MemOperand(tmp_ptr));
1112 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001113 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001114
1115 __ Bind(&exit_loop);
1116
1117 if (kPoisonHeapReferences) {
1118 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1119 if (value.Is(expected)) {
1120 // Do not unpoison `value`, as it is the same register as
1121 // `expected`, which has just been unpoisoned.
1122 } else {
1123 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1124 }
1125 }
1126
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001127 __ B(GetExitLabel());
1128 }
1129
1130 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001131 // The register containing the object holding the marked object reference field.
1132 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001133 // The offset, index and scale factor to access the reference in `obj_`.
1134 uint32_t offset_;
1135 Location index_;
1136 ScaleFactor scale_factor_;
1137 // Is a null check required?
1138 bool needs_null_check_;
1139 // A temporary register used to hold the lock word of `obj_`; and
1140 // also to hold the original reference value, when the reference is
1141 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001142 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 // A temporary register used in the implementation of the CAS, to
1144 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001145 const vixl32::Register temp2_;
1146
Roland Levillain54f869e2017-03-06 13:54:11 +00001147 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001148};
1149
1150// Slow path generating a read barrier for a heap reference.
1151class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1152 public:
1153 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1154 Location out,
1155 Location ref,
1156 Location obj,
1157 uint32_t offset,
1158 Location index)
1159 : SlowPathCodeARMVIXL(instruction),
1160 out_(out),
1161 ref_(ref),
1162 obj_(obj),
1163 offset_(offset),
1164 index_(index) {
1165 DCHECK(kEmitCompilerReadBarrier);
1166 // If `obj` is equal to `out` or `ref`, it means the initial object
1167 // has been overwritten by (or after) the heap object reference load
1168 // to be instrumented, e.g.:
1169 //
1170 // __ LoadFromOffset(kLoadWord, out, out, offset);
1171 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1172 //
1173 // In that case, we have lost the information about the original
1174 // object, and the emitted read barrier cannot work properly.
1175 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1176 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1177 }
1178
1179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1180 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1181 LocationSummary* locations = instruction_->GetLocations();
1182 vixl32::Register reg_out = RegisterFrom(out_);
1183 DCHECK(locations->CanCall());
1184 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1185 DCHECK(instruction_->IsInstanceFieldGet() ||
1186 instruction_->IsStaticFieldGet() ||
1187 instruction_->IsArrayGet() ||
1188 instruction_->IsInstanceOf() ||
1189 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001190 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001191 << "Unexpected instruction in read barrier for heap reference slow path: "
1192 << instruction_->DebugName();
1193 // The read barrier instrumentation of object ArrayGet
1194 // instructions does not support the HIntermediateAddress
1195 // instruction.
1196 DCHECK(!(instruction_->IsArrayGet() &&
1197 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1198
1199 __ Bind(GetEntryLabel());
1200 SaveLiveRegisters(codegen, locations);
1201
1202 // We may have to change the index's value, but as `index_` is a
1203 // constant member (like other "inputs" of this slow path),
1204 // introduce a copy of it, `index`.
1205 Location index = index_;
1206 if (index_.IsValid()) {
1207 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1208 if (instruction_->IsArrayGet()) {
1209 // Compute the actual memory offset and store it in `index`.
1210 vixl32::Register index_reg = RegisterFrom(index_);
1211 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1212 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1213 // We are about to change the value of `index_reg` (see the
1214 // calls to art::arm::Thumb2Assembler::Lsl and
1215 // art::arm::Thumb2Assembler::AddConstant below), but it has
1216 // not been saved by the previous call to
1217 // art::SlowPathCode::SaveLiveRegisters, as it is a
1218 // callee-save register --
1219 // art::SlowPathCode::SaveLiveRegisters does not consider
1220 // callee-save registers, as it has been designed with the
1221 // assumption that callee-save registers are supposed to be
1222 // handled by the called function. So, as a callee-save
1223 // register, `index_reg` _would_ eventually be saved onto
1224 // the stack, but it would be too late: we would have
1225 // changed its value earlier. Therefore, we manually save
1226 // it here into another freely available register,
1227 // `free_reg`, chosen of course among the caller-save
1228 // registers (as a callee-save `free_reg` register would
1229 // exhibit the same problem).
1230 //
1231 // Note we could have requested a temporary register from
1232 // the register allocator instead; but we prefer not to, as
1233 // this is a slow path, and we know we can find a
1234 // caller-save register that is available.
1235 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1236 __ Mov(free_reg, index_reg);
1237 index_reg = free_reg;
1238 index = LocationFrom(index_reg);
1239 } else {
1240 // The initial register stored in `index_` has already been
1241 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1242 // (as it is not a callee-save register), so we can freely
1243 // use it.
1244 }
1245 // Shifting the index value contained in `index_reg` by the scale
1246 // factor (2) cannot overflow in practice, as the runtime is
1247 // unable to allocate object arrays with a size larger than
1248 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1249 __ Lsl(index_reg, index_reg, TIMES_4);
1250 static_assert(
1251 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1252 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1253 __ Add(index_reg, index_reg, offset_);
1254 } else {
1255 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1256 // intrinsics, `index_` is not shifted by a scale factor of 2
1257 // (as in the case of ArrayGet), as it is actually an offset
1258 // to an object field within an object.
1259 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1260 DCHECK(instruction_->GetLocations()->Intrinsified());
1261 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1262 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1263 << instruction_->AsInvoke()->GetIntrinsic();
1264 DCHECK_EQ(offset_, 0U);
1265 DCHECK(index_.IsRegisterPair());
1266 // UnsafeGet's offset location is a register pair, the low
1267 // part contains the correct offset.
1268 index = index_.ToLow();
1269 }
1270 }
1271
1272 // We're moving two or three locations to locations that could
1273 // overlap, so we need a parallel move resolver.
1274 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1275 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1276 parallel_move.AddMove(ref_,
1277 LocationFrom(calling_convention.GetRegisterAt(0)),
1278 Primitive::kPrimNot,
1279 nullptr);
1280 parallel_move.AddMove(obj_,
1281 LocationFrom(calling_convention.GetRegisterAt(1)),
1282 Primitive::kPrimNot,
1283 nullptr);
1284 if (index.IsValid()) {
1285 parallel_move.AddMove(index,
1286 LocationFrom(calling_convention.GetRegisterAt(2)),
1287 Primitive::kPrimInt,
1288 nullptr);
1289 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1290 } else {
1291 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1292 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1293 }
1294 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1295 CheckEntrypointTypes<
1296 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1297 arm_codegen->Move32(out_, LocationFrom(r0));
1298
1299 RestoreLiveRegisters(codegen, locations);
1300 __ B(GetExitLabel());
1301 }
1302
1303 const char* GetDescription() const OVERRIDE {
1304 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1305 }
1306
1307 private:
1308 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1309 uint32_t ref = RegisterFrom(ref_).GetCode();
1310 uint32_t obj = RegisterFrom(obj_).GetCode();
1311 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1312 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1313 return vixl32::Register(i);
1314 }
1315 }
1316 // We shall never fail to find a free caller-save register, as
1317 // there are more than two core caller-save registers on ARM
1318 // (meaning it is possible to find one which is different from
1319 // `ref` and `obj`).
1320 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1321 LOG(FATAL) << "Could not find a free caller-save register";
1322 UNREACHABLE();
1323 }
1324
1325 const Location out_;
1326 const Location ref_;
1327 const Location obj_;
1328 const uint32_t offset_;
1329 // An additional location containing an index to an array.
1330 // Only used for HArrayGet and the UnsafeGetObject &
1331 // UnsafeGetObjectVolatile intrinsics.
1332 const Location index_;
1333
1334 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1335};
1336
1337// Slow path generating a read barrier for a GC root.
1338class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1339 public:
1340 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1341 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1342 DCHECK(kEmitCompilerReadBarrier);
1343 }
1344
1345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1346 LocationSummary* locations = instruction_->GetLocations();
1347 vixl32::Register reg_out = RegisterFrom(out_);
1348 DCHECK(locations->CanCall());
1349 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1350 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1351 << "Unexpected instruction in read barrier for GC root slow path: "
1352 << instruction_->DebugName();
1353
1354 __ Bind(GetEntryLabel());
1355 SaveLiveRegisters(codegen, locations);
1356
1357 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1358 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1359 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1360 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1361 instruction_,
1362 instruction_->GetDexPc(),
1363 this);
1364 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1365 arm_codegen->Move32(out_, LocationFrom(r0));
1366
1367 RestoreLiveRegisters(codegen, locations);
1368 __ B(GetExitLabel());
1369 }
1370
1371 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1372
1373 private:
1374 const Location out_;
1375 const Location root_;
1376
1377 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1378};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001379
Scott Wakelingfe885462016-09-22 10:24:38 +01001380inline vixl32::Condition ARMCondition(IfCondition cond) {
1381 switch (cond) {
1382 case kCondEQ: return eq;
1383 case kCondNE: return ne;
1384 case kCondLT: return lt;
1385 case kCondLE: return le;
1386 case kCondGT: return gt;
1387 case kCondGE: return ge;
1388 case kCondB: return lo;
1389 case kCondBE: return ls;
1390 case kCondA: return hi;
1391 case kCondAE: return hs;
1392 }
1393 LOG(FATAL) << "Unreachable";
1394 UNREACHABLE();
1395}
1396
1397// Maps signed condition to unsigned condition.
1398inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1399 switch (cond) {
1400 case kCondEQ: return eq;
1401 case kCondNE: return ne;
1402 // Signed to unsigned.
1403 case kCondLT: return lo;
1404 case kCondLE: return ls;
1405 case kCondGT: return hi;
1406 case kCondGE: return hs;
1407 // Unsigned remain unchanged.
1408 case kCondB: return lo;
1409 case kCondBE: return ls;
1410 case kCondA: return hi;
1411 case kCondAE: return hs;
1412 }
1413 LOG(FATAL) << "Unreachable";
1414 UNREACHABLE();
1415}
1416
1417inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1418 // The ARM condition codes can express all the necessary branches, see the
1419 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1420 // There is no dex instruction or HIR that would need the missing conditions
1421 // "equal or unordered" or "not equal".
1422 switch (cond) {
1423 case kCondEQ: return eq;
1424 case kCondNE: return ne /* unordered */;
1425 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1426 case kCondLE: return gt_bias ? ls : le /* unordered */;
1427 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1428 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1429 default:
1430 LOG(FATAL) << "UNREACHABLE";
1431 UNREACHABLE();
1432 }
1433}
1434
Anton Kirilov74234da2017-01-13 14:42:47 +00001435inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1436 switch (op_kind) {
1437 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1438 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1439 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1440 default:
1441 LOG(FATAL) << "Unexpected op kind " << op_kind;
1442 UNREACHABLE();
1443 }
1444}
1445
Scott Wakelingfe885462016-09-22 10:24:38 +01001446void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1447 stream << vixl32::Register(reg);
1448}
1449
1450void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1451 stream << vixl32::SRegister(reg);
1452}
1453
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001454static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001455 uint32_t mask = 0;
1456 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1457 i <= regs.GetLastSRegister().GetCode();
1458 ++i) {
1459 mask |= (1 << i);
1460 }
1461 return mask;
1462}
1463
Artem Serovd4cc5b22016-11-04 11:19:09 +00001464// Saves the register in the stack. Returns the size taken on stack.
1465size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1466 uint32_t reg_id ATTRIBUTE_UNUSED) {
1467 TODO_VIXL32(FATAL);
1468 return 0;
1469}
1470
1471// Restores the register from the stack. Returns the size taken on stack.
1472size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1473 uint32_t reg_id ATTRIBUTE_UNUSED) {
1474 TODO_VIXL32(FATAL);
1475 return 0;
1476}
1477
1478size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1479 uint32_t reg_id ATTRIBUTE_UNUSED) {
1480 TODO_VIXL32(FATAL);
1481 return 0;
1482}
1483
1484size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1485 uint32_t reg_id ATTRIBUTE_UNUSED) {
1486 TODO_VIXL32(FATAL);
1487 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001488}
1489
Anton Kirilov74234da2017-01-13 14:42:47 +00001490static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1491 vixl32::Register out,
1492 vixl32::Register first,
1493 const Operand& second,
1494 CodeGeneratorARMVIXL* codegen) {
1495 if (second.IsImmediate() && second.GetImmediate() == 0) {
1496 const Operand in = kind == HInstruction::kAnd
1497 ? Operand(0)
1498 : Operand(first);
1499
1500 __ Mov(out, in);
1501 } else {
1502 switch (kind) {
1503 case HInstruction::kAdd:
1504 __ Add(out, first, second);
1505 break;
1506 case HInstruction::kAnd:
1507 __ And(out, first, second);
1508 break;
1509 case HInstruction::kOr:
1510 __ Orr(out, first, second);
1511 break;
1512 case HInstruction::kSub:
1513 __ Sub(out, first, second);
1514 break;
1515 case HInstruction::kXor:
1516 __ Eor(out, first, second);
1517 break;
1518 default:
1519 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1520 UNREACHABLE();
1521 }
1522 }
1523}
1524
1525static void GenerateDataProc(HInstruction::InstructionKind kind,
1526 const Location& out,
1527 const Location& first,
1528 const Operand& second_lo,
1529 const Operand& second_hi,
1530 CodeGeneratorARMVIXL* codegen) {
1531 const vixl32::Register first_hi = HighRegisterFrom(first);
1532 const vixl32::Register first_lo = LowRegisterFrom(first);
1533 const vixl32::Register out_hi = HighRegisterFrom(out);
1534 const vixl32::Register out_lo = LowRegisterFrom(out);
1535
1536 if (kind == HInstruction::kAdd) {
1537 __ Adds(out_lo, first_lo, second_lo);
1538 __ Adc(out_hi, first_hi, second_hi);
1539 } else if (kind == HInstruction::kSub) {
1540 __ Subs(out_lo, first_lo, second_lo);
1541 __ Sbc(out_hi, first_hi, second_hi);
1542 } else {
1543 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1544 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1545 }
1546}
1547
1548static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1549 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1550}
1551
1552static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1553 CodeGeneratorARMVIXL* codegen) {
1554 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1555 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1556
1557 const LocationSummary* const locations = instruction->GetLocations();
1558 const uint32_t shift_value = instruction->GetShiftAmount();
1559 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1560 const Location first = locations->InAt(0);
1561 const Location second = locations->InAt(1);
1562 const Location out = locations->Out();
1563 const vixl32::Register first_hi = HighRegisterFrom(first);
1564 const vixl32::Register first_lo = LowRegisterFrom(first);
1565 const vixl32::Register out_hi = HighRegisterFrom(out);
1566 const vixl32::Register out_lo = LowRegisterFrom(out);
1567 const vixl32::Register second_hi = HighRegisterFrom(second);
1568 const vixl32::Register second_lo = LowRegisterFrom(second);
1569 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1570
1571 if (shift_value >= 32) {
1572 if (shift == ShiftType::LSL) {
1573 GenerateDataProcInstruction(kind,
1574 out_hi,
1575 first_hi,
1576 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1577 codegen);
1578 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1579 } else if (shift == ShiftType::ASR) {
1580 GenerateDataProc(kind,
1581 out,
1582 first,
1583 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1584 Operand(second_hi, ShiftType::ASR, 31),
1585 codegen);
1586 } else {
1587 DCHECK_EQ(shift, ShiftType::LSR);
1588 GenerateDataProc(kind,
1589 out,
1590 first,
1591 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1592 0,
1593 codegen);
1594 }
1595 } else {
1596 DCHECK_GT(shift_value, 1U);
1597 DCHECK_LT(shift_value, 32U);
1598
1599 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1600
1601 if (shift == ShiftType::LSL) {
1602 // We are not doing this for HInstruction::kAdd because the output will require
1603 // Location::kOutputOverlap; not applicable to other cases.
1604 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1605 GenerateDataProcInstruction(kind,
1606 out_hi,
1607 first_hi,
1608 Operand(second_hi, ShiftType::LSL, shift_value),
1609 codegen);
1610 GenerateDataProcInstruction(kind,
1611 out_hi,
1612 out_hi,
1613 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1614 codegen);
1615 GenerateDataProcInstruction(kind,
1616 out_lo,
1617 first_lo,
1618 Operand(second_lo, ShiftType::LSL, shift_value),
1619 codegen);
1620 } else {
1621 const vixl32::Register temp = temps.Acquire();
1622
1623 __ Lsl(temp, second_hi, shift_value);
1624 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1625 GenerateDataProc(kind,
1626 out,
1627 first,
1628 Operand(second_lo, ShiftType::LSL, shift_value),
1629 temp,
1630 codegen);
1631 }
1632 } else {
1633 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1634
1635 // We are not doing this for HInstruction::kAdd because the output will require
1636 // Location::kOutputOverlap; not applicable to other cases.
1637 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1638 GenerateDataProcInstruction(kind,
1639 out_lo,
1640 first_lo,
1641 Operand(second_lo, ShiftType::LSR, shift_value),
1642 codegen);
1643 GenerateDataProcInstruction(kind,
1644 out_lo,
1645 out_lo,
1646 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1647 codegen);
1648 GenerateDataProcInstruction(kind,
1649 out_hi,
1650 first_hi,
1651 Operand(second_hi, shift, shift_value),
1652 codegen);
1653 } else {
1654 const vixl32::Register temp = temps.Acquire();
1655
1656 __ Lsr(temp, second_lo, shift_value);
1657 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1658 GenerateDataProc(kind,
1659 out,
1660 first,
1661 temp,
1662 Operand(second_hi, shift, shift_value),
1663 codegen);
1664 }
1665 }
1666 }
1667}
1668
Donghui Bai426b49c2016-11-08 14:55:38 +08001669static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1670 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1671 if (rhs_loc.IsConstant()) {
1672 // 0.0 is the only immediate that can be encoded directly in
1673 // a VCMP instruction.
1674 //
1675 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1676 // specify that in a floating-point comparison, positive zero
1677 // and negative zero are considered equal, so we can use the
1678 // literal 0.0 for both cases here.
1679 //
1680 // Note however that some methods (Float.equal, Float.compare,
1681 // Float.compareTo, Double.equal, Double.compare,
1682 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1683 // StrictMath.min) consider 0.0 to be (strictly) greater than
1684 // -0.0. So if we ever translate calls to these methods into a
1685 // HCompare instruction, we must handle the -0.0 case with
1686 // care here.
1687 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1688
1689 const Primitive::Type type = instruction->InputAt(0)->GetType();
1690
1691 if (type == Primitive::kPrimFloat) {
1692 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1693 } else {
1694 DCHECK_EQ(type, Primitive::kPrimDouble);
1695 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1696 }
1697 } else {
1698 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1699 }
1700}
1701
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001702static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1703 HCondition* condition,
1704 bool invert,
1705 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001706 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1707
1708 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001709 IfCondition cond = condition->GetCondition();
1710 IfCondition opposite = condition->GetOppositeCondition();
1711
1712 if (invert) {
1713 std::swap(cond, opposite);
1714 }
1715
1716 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001717 const Location left = locations->InAt(0);
1718 const Location right = locations->InAt(1);
1719
1720 DCHECK(right.IsConstant());
1721
1722 const vixl32::Register left_high = HighRegisterFrom(left);
1723 const vixl32::Register left_low = LowRegisterFrom(left);
1724 int64_t value = Int64ConstantFrom(right);
1725
1726 switch (cond) {
1727 case kCondEQ:
1728 case kCondNE:
1729 case kCondB:
1730 case kCondBE:
1731 case kCondA:
1732 case kCondAE: {
1733 __ Cmp(left_high, High32Bits(value));
1734
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001735 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001736 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1737 2 * vixl32::k16BitT32InstructionSizeInBytes,
1738 CodeBufferCheckScope::kExactSize);
1739
1740 __ it(eq);
1741 __ cmp(eq, left_low, Low32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001742 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001743 break;
1744 }
1745 case kCondLE:
1746 case kCondGT:
1747 // Trivially true or false.
1748 if (value == std::numeric_limits<int64_t>::max()) {
1749 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001750 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001751 break;
1752 }
1753
1754 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001755 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001756 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001757 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 } else {
1759 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001760 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001761 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001762 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001763 }
1764
1765 value++;
1766 FALLTHROUGH_INTENDED;
1767 case kCondGE:
1768 case kCondLT: {
1769 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1770
1771 __ Cmp(left_low, Low32Bits(value));
1772 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001773 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001774 break;
1775 }
1776 default:
1777 LOG(FATAL) << "Unreachable";
1778 UNREACHABLE();
1779 }
1780
1781 return ret;
1782}
1783
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001784static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1785 HCondition* condition,
1786 bool invert,
1787 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001788 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1789
1790 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001791 IfCondition cond = condition->GetCondition();
1792 IfCondition opposite = condition->GetOppositeCondition();
1793
1794 if (invert) {
1795 std::swap(cond, opposite);
1796 }
1797
1798 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001799 Location left = locations->InAt(0);
1800 Location right = locations->InAt(1);
1801
1802 DCHECK(right.IsRegisterPair());
1803
1804 switch (cond) {
1805 case kCondEQ:
1806 case kCondNE:
1807 case kCondB:
1808 case kCondBE:
1809 case kCondA:
1810 case kCondAE: {
1811 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1812
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001813 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001814 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1815 2 * vixl32::k16BitT32InstructionSizeInBytes,
1816 CodeBufferCheckScope::kExactSize);
1817
1818 __ it(eq);
1819 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001820 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001821 break;
1822 }
1823 case kCondLE:
1824 case kCondGT:
1825 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001826 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001827 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001828 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001829 } else {
1830 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001831 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001832 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001833 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001834 }
1835
1836 std::swap(left, right);
1837 FALLTHROUGH_INTENDED;
1838 case kCondGE:
1839 case kCondLT: {
1840 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1841
1842 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1843 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001844 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001845 break;
1846 }
1847 default:
1848 LOG(FATAL) << "Unreachable";
1849 UNREACHABLE();
1850 }
1851
1852 return ret;
1853}
1854
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001855static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1856 bool invert,
1857 CodeGeneratorARMVIXL* codegen) {
1858 const Primitive::Type type = condition->GetLeft()->GetType();
1859 IfCondition cond = condition->GetCondition();
1860 IfCondition opposite = condition->GetOppositeCondition();
1861 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001862
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001863 if (invert) {
1864 std::swap(cond, opposite);
1865 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001866
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001867 if (type == Primitive::kPrimLong) {
1868 ret = condition->GetLocations()->InAt(1).IsConstant()
1869 ? GenerateLongTestConstant(condition, invert, codegen)
1870 : GenerateLongTest(condition, invert, codegen);
1871 } else if (Primitive::IsFloatingPointType(type)) {
1872 GenerateVcmp(condition, codegen);
1873 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1874 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1875 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001876 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001877 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1878 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1879 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001880 }
1881
1882 return ret;
1883}
1884
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001885static bool CanGenerateTest(HCondition* condition, ArmVIXLAssembler* assembler) {
1886 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1887 const LocationSummary* const locations = condition->GetLocations();
1888 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001889
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001890 if (locations->InAt(1).IsConstant()) {
1891 const int64_t value = Int64ConstantFrom(locations->InAt(1));
Donghui Bai426b49c2016-11-08 14:55:38 +08001892
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001893 if (c < kCondLT || c > kCondGE) {
1894 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1895 // we check that the least significant half of the first input to be compared
1896 // is in a low register (the other half is read outside an IT block), and
1897 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1898 // encoding can be used.
1899 if (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001900 return false;
1901 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001902 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
1903 // the previous one, but are not strictly necessary.
1904 } else if (c == kCondLE || c == kCondGT) {
1905 if (value < std::numeric_limits<int64_t>::max() &&
1906 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
1907 return false;
1908 }
1909 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
1910 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001911 }
1912 }
1913 }
1914
1915 return true;
1916}
1917
1918static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1919 const Primitive::Type type = constant->GetType();
1920 bool ret = false;
1921
1922 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1923
1924 if (type == Primitive::kPrimLong) {
1925 const uint64_t value = Uint64ConstantFrom(constant);
1926
1927 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1928 } else {
1929 ret = IsUint<8>(Int32ConstantFrom(constant));
1930 }
1931
1932 return ret;
1933}
1934
1935static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1936 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1937
1938 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1939 return Location::ConstantLocation(constant->AsConstant());
1940 }
1941
1942 return Location::RequiresRegister();
1943}
1944
1945static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1946 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1947 // we check that we are not dealing with floating-point output (there is no
1948 // 16-bit VMOV encoding).
1949 if (!out.IsRegister() && !out.IsRegisterPair()) {
1950 return false;
1951 }
1952
1953 // For constants, we also check that the output is in one or two low registers,
1954 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1955 // MOV encoding can be used.
1956 if (src.IsConstant()) {
1957 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1958 return false;
1959 }
1960
1961 if (out.IsRegister()) {
1962 if (!RegisterFrom(out).IsLow()) {
1963 return false;
1964 }
1965 } else {
1966 DCHECK(out.IsRegisterPair());
1967
1968 if (!HighRegisterFrom(out).IsLow()) {
1969 return false;
1970 }
1971 }
1972 }
1973
1974 return true;
1975}
1976
Scott Wakelingfe885462016-09-22 10:24:38 +01001977#undef __
1978
Donghui Bai426b49c2016-11-08 14:55:38 +08001979vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1980 vixl32::Label* final_label) {
1981 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001982 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001983
1984 const HBasicBlock* const block = instruction->GetBlock();
1985 const HLoopInformation* const info = block->GetLoopInformation();
1986 HInstruction* const next = instruction->GetNext();
1987
1988 // Avoid a branch to a branch.
1989 if (next->IsGoto() && (info == nullptr ||
1990 !info->IsBackEdge(*block) ||
1991 !info->HasSuspendCheck())) {
1992 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1993 }
1994
1995 return final_label;
1996}
1997
Scott Wakelingfe885462016-09-22 10:24:38 +01001998CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1999 const ArmInstructionSetFeatures& isa_features,
2000 const CompilerOptions& compiler_options,
2001 OptimizingCompilerStats* stats)
2002 : CodeGenerator(graph,
2003 kNumberOfCoreRegisters,
2004 kNumberOfSRegisters,
2005 kNumberOfRegisterPairs,
2006 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002007 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002008 compiler_options,
2009 stats),
2010 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002011 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002012 location_builder_(graph, this),
2013 instruction_visitor_(graph, this),
2014 move_resolver_(graph->GetArena(), this),
2015 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002016 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002017 uint32_literals_(std::less<uint32_t>(),
2018 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002019 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002020 boot_image_string_patches_(StringReferenceValueComparator(),
2021 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002022 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002023 boot_image_type_patches_(TypeReferenceValueComparator(),
2024 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2025 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002026 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002027 jit_string_patches_(StringReferenceValueComparator(),
2028 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2029 jit_class_patches_(TypeReferenceValueComparator(),
2030 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002031 // Always save the LR register to mimic Quick.
2032 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002033 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2034 // S0-S31, which alias to D0-D15.
2035 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2036 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002037}
2038
Artem Serov551b28f2016-10-18 19:11:30 +01002039void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2040 uint32_t num_entries = switch_instr_->GetNumEntries();
2041 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2042
2043 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002044 // underlying code buffer and we have generated a jump table of the right size, using
2045 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002046 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2047 num_entries * sizeof(int32_t),
2048 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002049 // TODO(VIXL): Check that using lower case bind is fine here.
2050 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002051 for (uint32_t i = 0; i < num_entries; i++) {
2052 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2053 }
2054}
2055
2056void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2057 uint32_t num_entries = switch_instr_->GetNumEntries();
2058 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2059
Artem Serov551b28f2016-10-18 19:11:30 +01002060 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2061 for (uint32_t i = 0; i < num_entries; i++) {
2062 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2063 DCHECK(target_label->IsBound());
2064 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2065 // When doing BX to address we need to have lower bit set to 1 in T32.
2066 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2067 jump_offset++;
2068 }
2069 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2070 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002071
Scott Wakelingb77051e2016-11-21 19:46:00 +00002072 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002073 }
2074}
2075
Artem Serov09a940d2016-11-11 16:15:11 +00002076void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002077 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002078 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002079 }
2080}
2081
Andreas Gampeca620d72016-11-08 08:09:33 -08002082#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002083
2084void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002085 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002086 GetAssembler()->FinalizeCode();
2087 CodeGenerator::Finalize(allocator);
2088}
2089
2090void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002091 // Stack register, LR and PC are always reserved.
2092 blocked_core_registers_[SP] = true;
2093 blocked_core_registers_[LR] = true;
2094 blocked_core_registers_[PC] = true;
2095
2096 // Reserve thread register.
2097 blocked_core_registers_[TR] = true;
2098
2099 // Reserve temp register.
2100 blocked_core_registers_[IP] = true;
2101
2102 if (GetGraph()->IsDebuggable()) {
2103 // Stubs do not save callee-save floating point registers. If the graph
2104 // is debuggable, we need to deal with these registers differently. For
2105 // now, just block them.
2106 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2107 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2108 ++i) {
2109 blocked_fpu_registers_[i] = true;
2110 }
2111 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002112}
2113
Scott Wakelingfe885462016-09-22 10:24:38 +01002114InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2115 CodeGeneratorARMVIXL* codegen)
2116 : InstructionCodeGenerator(graph, codegen),
2117 assembler_(codegen->GetAssembler()),
2118 codegen_(codegen) {}
2119
2120void CodeGeneratorARMVIXL::ComputeSpillMask() {
2121 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2122 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2123 // There is no easy instruction to restore just the PC on thumb2. We spill and
2124 // restore another arbitrary register.
2125 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2126 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2127 // We use vpush and vpop for saving and restoring floating point registers, which take
2128 // a SRegister and the number of registers to save/restore after that SRegister. We
2129 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2130 // but in the range.
2131 if (fpu_spill_mask_ != 0) {
2132 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2133 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2134 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2135 fpu_spill_mask_ |= (1 << i);
2136 }
2137 }
2138}
2139
2140void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2141 bool skip_overflow_check =
2142 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2143 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2144 __ Bind(&frame_entry_label_);
2145
2146 if (HasEmptyFrame()) {
2147 return;
2148 }
2149
Scott Wakelingfe885462016-09-22 10:24:38 +01002150 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002151 UseScratchRegisterScope temps(GetVIXLAssembler());
2152 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002153 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002154 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002155 ExactAssemblyScope aas(GetVIXLAssembler(),
2156 vixl32::kMaxInstructionSizeInBytes,
2157 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002158 __ ldr(temp, MemOperand(temp));
2159 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002160 }
2161
2162 __ Push(RegisterList(core_spill_mask_));
2163 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2164 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2165 0,
2166 core_spill_mask_,
2167 kArmWordSize);
2168 if (fpu_spill_mask_ != 0) {
2169 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2170
2171 // Check that list is contiguous.
2172 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2173
2174 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2175 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002176 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002177 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002178
2179 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2180 UseScratchRegisterScope temps(GetVIXLAssembler());
2181 vixl32::Register temp = temps.Acquire();
2182 // Initialize should_deoptimize flag to 0.
2183 __ Mov(temp, 0);
2184 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2185 }
2186
Scott Wakelingfe885462016-09-22 10:24:38 +01002187 int adjust = GetFrameSize() - FrameEntrySpillSize();
2188 __ Sub(sp, sp, adjust);
2189 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002190
2191 // Save the current method if we need it. Note that we do not
2192 // do this in HCurrentMethod, as the instruction might have been removed
2193 // in the SSA graph.
2194 if (RequiresCurrentMethod()) {
2195 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2196 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002197}
2198
2199void CodeGeneratorARMVIXL::GenerateFrameExit() {
2200 if (HasEmptyFrame()) {
2201 __ Bx(lr);
2202 return;
2203 }
2204 GetAssembler()->cfi().RememberState();
2205 int adjust = GetFrameSize() - FrameEntrySpillSize();
2206 __ Add(sp, sp, adjust);
2207 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2208 if (fpu_spill_mask_ != 0) {
2209 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2210
2211 // Check that list is contiguous.
2212 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2213
2214 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2215 GetAssembler()->cfi().AdjustCFAOffset(
2216 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002217 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002218 }
2219 // Pop LR into PC to return.
2220 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2221 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2222 __ Pop(RegisterList(pop_mask));
2223 GetAssembler()->cfi().RestoreState();
2224 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2225}
2226
2227void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2228 __ Bind(GetLabelOf(block));
2229}
2230
Artem Serovd4cc5b22016-11-04 11:19:09 +00002231Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2232 switch (type) {
2233 case Primitive::kPrimBoolean:
2234 case Primitive::kPrimByte:
2235 case Primitive::kPrimChar:
2236 case Primitive::kPrimShort:
2237 case Primitive::kPrimInt:
2238 case Primitive::kPrimNot: {
2239 uint32_t index = gp_index_++;
2240 uint32_t stack_index = stack_index_++;
2241 if (index < calling_convention.GetNumberOfRegisters()) {
2242 return LocationFrom(calling_convention.GetRegisterAt(index));
2243 } else {
2244 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2245 }
2246 }
2247
2248 case Primitive::kPrimLong: {
2249 uint32_t index = gp_index_;
2250 uint32_t stack_index = stack_index_;
2251 gp_index_ += 2;
2252 stack_index_ += 2;
2253 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2254 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2255 // Skip R1, and use R2_R3 instead.
2256 gp_index_++;
2257 index++;
2258 }
2259 }
2260 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2261 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2262 calling_convention.GetRegisterAt(index + 1).GetCode());
2263
2264 return LocationFrom(calling_convention.GetRegisterAt(index),
2265 calling_convention.GetRegisterAt(index + 1));
2266 } else {
2267 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2268 }
2269 }
2270
2271 case Primitive::kPrimFloat: {
2272 uint32_t stack_index = stack_index_++;
2273 if (float_index_ % 2 == 0) {
2274 float_index_ = std::max(double_index_, float_index_);
2275 }
2276 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2277 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2278 } else {
2279 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2280 }
2281 }
2282
2283 case Primitive::kPrimDouble: {
2284 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2285 uint32_t stack_index = stack_index_;
2286 stack_index_ += 2;
2287 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2288 uint32_t index = double_index_;
2289 double_index_ += 2;
2290 Location result = LocationFrom(
2291 calling_convention.GetFpuRegisterAt(index),
2292 calling_convention.GetFpuRegisterAt(index + 1));
2293 DCHECK(ExpectedPairLayout(result));
2294 return result;
2295 } else {
2296 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2297 }
2298 }
2299
2300 case Primitive::kPrimVoid:
2301 LOG(FATAL) << "Unexpected parameter type " << type;
2302 break;
2303 }
2304 return Location::NoLocation();
2305}
2306
2307Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2308 switch (type) {
2309 case Primitive::kPrimBoolean:
2310 case Primitive::kPrimByte:
2311 case Primitive::kPrimChar:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
2314 case Primitive::kPrimNot: {
2315 return LocationFrom(r0);
2316 }
2317
2318 case Primitive::kPrimFloat: {
2319 return LocationFrom(s0);
2320 }
2321
2322 case Primitive::kPrimLong: {
2323 return LocationFrom(r0, r1);
2324 }
2325
2326 case Primitive::kPrimDouble: {
2327 return LocationFrom(s0, s1);
2328 }
2329
2330 case Primitive::kPrimVoid:
2331 return Location::NoLocation();
2332 }
2333
2334 UNREACHABLE();
2335}
2336
2337Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2338 return LocationFrom(kMethodRegister);
2339}
2340
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002341void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2342 if (source.Equals(destination)) {
2343 return;
2344 }
2345 if (destination.IsRegister()) {
2346 if (source.IsRegister()) {
2347 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2348 } else if (source.IsFpuRegister()) {
2349 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2350 } else {
2351 GetAssembler()->LoadFromOffset(kLoadWord,
2352 RegisterFrom(destination),
2353 sp,
2354 source.GetStackIndex());
2355 }
2356 } else if (destination.IsFpuRegister()) {
2357 if (source.IsRegister()) {
2358 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2359 } else if (source.IsFpuRegister()) {
2360 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2361 } else {
2362 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2363 }
2364 } else {
2365 DCHECK(destination.IsStackSlot()) << destination;
2366 if (source.IsRegister()) {
2367 GetAssembler()->StoreToOffset(kStoreWord,
2368 RegisterFrom(source),
2369 sp,
2370 destination.GetStackIndex());
2371 } else if (source.IsFpuRegister()) {
2372 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2373 } else {
2374 DCHECK(source.IsStackSlot()) << source;
2375 UseScratchRegisterScope temps(GetVIXLAssembler());
2376 vixl32::Register temp = temps.Acquire();
2377 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2378 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2379 }
2380 }
2381}
2382
Artem Serovcfbe9132016-10-14 15:58:56 +01002383void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2384 DCHECK(location.IsRegister());
2385 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002386}
2387
2388void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002389 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2390 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2391 HParallelMove move(GetGraph()->GetArena());
2392 move.AddMove(src, dst, dst_type, nullptr);
2393 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002394}
2395
Artem Serovcfbe9132016-10-14 15:58:56 +01002396void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2397 if (location.IsRegister()) {
2398 locations->AddTemp(location);
2399 } else if (location.IsRegisterPair()) {
2400 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2401 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2402 } else {
2403 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2404 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002405}
2406
2407void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2408 HInstruction* instruction,
2409 uint32_t dex_pc,
2410 SlowPathCode* slow_path) {
2411 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002412 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2413 // Ensure the pc position is recorded immediately after the `blx` instruction.
2414 // 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 +00002415 ExactAssemblyScope aas(GetVIXLAssembler(),
2416 vixl32::k16BitT32InstructionSizeInBytes,
2417 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002418 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002419 if (EntrypointRequiresStackMap(entrypoint)) {
2420 RecordPcInfo(instruction, dex_pc, slow_path);
2421 }
2422}
2423
2424void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2425 HInstruction* instruction,
2426 SlowPathCode* slow_path) {
2427 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002428 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002429 __ Blx(lr);
2430}
2431
Scott Wakelingfe885462016-09-22 10:24:38 +01002432void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2433 DCHECK(!successor->IsExitBlock());
2434 HBasicBlock* block = got->GetBlock();
2435 HInstruction* previous = got->GetPrevious();
2436 HLoopInformation* info = block->GetLoopInformation();
2437
2438 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2439 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2440 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2441 return;
2442 }
2443 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2444 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2445 }
2446 if (!codegen_->GoesToNextBlock(block, successor)) {
2447 __ B(codegen_->GetLabelOf(successor));
2448 }
2449}
2450
2451void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2452 got->SetLocations(nullptr);
2453}
2454
2455void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2456 HandleGoto(got, got->GetSuccessor());
2457}
2458
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002459void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2460 try_boundary->SetLocations(nullptr);
2461}
2462
2463void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2464 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2465 if (!successor->IsExitBlock()) {
2466 HandleGoto(try_boundary, successor);
2467 }
2468}
2469
Scott Wakelingfe885462016-09-22 10:24:38 +01002470void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2471 exit->SetLocations(nullptr);
2472}
2473
2474void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2475}
2476
Scott Wakelingfe885462016-09-22 10:24:38 +01002477void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2478 vixl32::Label* true_label,
2479 vixl32::Label* false_label) {
2480 LocationSummary* locations = cond->GetLocations();
2481 Location left = locations->InAt(0);
2482 Location right = locations->InAt(1);
2483 IfCondition if_cond = cond->GetCondition();
2484
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002485 vixl32::Register left_high = HighRegisterFrom(left);
2486 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002487 IfCondition true_high_cond = if_cond;
2488 IfCondition false_high_cond = cond->GetOppositeCondition();
2489 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2490
2491 // Set the conditions for the test, remembering that == needs to be
2492 // decided using the low words.
Scott Wakelingfe885462016-09-22 10:24:38 +01002493 switch (if_cond) {
2494 case kCondEQ:
2495 case kCondNE:
2496 // Nothing to do.
2497 break;
2498 case kCondLT:
2499 false_high_cond = kCondGT;
2500 break;
2501 case kCondLE:
2502 true_high_cond = kCondLT;
2503 break;
2504 case kCondGT:
2505 false_high_cond = kCondLT;
2506 break;
2507 case kCondGE:
2508 true_high_cond = kCondGT;
2509 break;
2510 case kCondB:
2511 false_high_cond = kCondA;
2512 break;
2513 case kCondBE:
2514 true_high_cond = kCondB;
2515 break;
2516 case kCondA:
2517 false_high_cond = kCondB;
2518 break;
2519 case kCondAE:
2520 true_high_cond = kCondA;
2521 break;
2522 }
2523 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002524 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002525 int32_t val_low = Low32Bits(value);
2526 int32_t val_high = High32Bits(value);
2527
2528 __ Cmp(left_high, val_high);
2529 if (if_cond == kCondNE) {
2530 __ B(ARMCondition(true_high_cond), true_label);
2531 } else if (if_cond == kCondEQ) {
2532 __ B(ARMCondition(false_high_cond), false_label);
2533 } else {
2534 __ B(ARMCondition(true_high_cond), true_label);
2535 __ B(ARMCondition(false_high_cond), false_label);
2536 }
2537 // Must be equal high, so compare the lows.
2538 __ Cmp(left_low, val_low);
2539 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002540 vixl32::Register right_high = HighRegisterFrom(right);
2541 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002542
2543 __ Cmp(left_high, right_high);
2544 if (if_cond == kCondNE) {
2545 __ B(ARMCondition(true_high_cond), true_label);
2546 } else if (if_cond == kCondEQ) {
2547 __ B(ARMCondition(false_high_cond), false_label);
2548 } else {
2549 __ B(ARMCondition(true_high_cond), true_label);
2550 __ B(ARMCondition(false_high_cond), false_label);
2551 }
2552 // Must be equal high, so compare the lows.
2553 __ Cmp(left_low, right_low);
2554 }
2555 // The last comparison might be unsigned.
2556 // TODO: optimize cases where this is always true/false
2557 __ B(final_condition, true_label);
2558}
2559
2560void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2561 vixl32::Label* true_target_in,
2562 vixl32::Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002563 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2564 vixl32::Label* non_fallthrough_target;
2565 bool invert;
2566
2567 if (true_target_in == nullptr) {
2568 DCHECK(false_target_in != nullptr);
2569 non_fallthrough_target = false_target_in;
2570 invert = true;
2571 } else {
2572 non_fallthrough_target = true_target_in;
2573 invert = false;
2574 }
2575
2576 const auto cond = GenerateTest(condition, invert, codegen_);
2577
2578 __ B(cond.first, non_fallthrough_target);
2579
2580 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2581 __ B(false_target_in);
2582 }
2583
2584 return;
2585 }
2586
Scott Wakelingfe885462016-09-22 10:24:38 +01002587 // Generated branching requires both targets to be explicit. If either of the
2588 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2589 vixl32::Label fallthrough;
2590 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2591 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2592
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002593 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
2594 GenerateLongComparesAndJumps(condition, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002595
2596 if (false_target != &fallthrough) {
2597 __ B(false_target);
2598 }
2599
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002600 if (fallthrough.IsReferenced()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002601 __ Bind(&fallthrough);
2602 }
2603}
2604
2605void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2606 size_t condition_input_index,
2607 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002608 vixl32::Label* false_target,
2609 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002610 HInstruction* cond = instruction->InputAt(condition_input_index);
2611
2612 if (true_target == nullptr && false_target == nullptr) {
2613 // Nothing to do. The code always falls through.
2614 return;
2615 } else if (cond->IsIntConstant()) {
2616 // Constant condition, statically compared against "true" (integer value 1).
2617 if (cond->AsIntConstant()->IsTrue()) {
2618 if (true_target != nullptr) {
2619 __ B(true_target);
2620 }
2621 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002622 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002623 if (false_target != nullptr) {
2624 __ B(false_target);
2625 }
2626 }
2627 return;
2628 }
2629
2630 // The following code generates these patterns:
2631 // (1) true_target == nullptr && false_target != nullptr
2632 // - opposite condition true => branch to false_target
2633 // (2) true_target != nullptr && false_target == nullptr
2634 // - condition true => branch to true_target
2635 // (3) true_target != nullptr && false_target != nullptr
2636 // - condition true => branch to true_target
2637 // - branch to false_target
2638 if (IsBooleanValueOrMaterializedCondition(cond)) {
2639 // Condition has been materialized, compare the output to 0.
2640 if (kIsDebugBuild) {
2641 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2642 DCHECK(cond_val.IsRegister());
2643 }
2644 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002645 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2646 false_target,
2647 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002648 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002649 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2650 true_target,
2651 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002652 }
2653 } else {
2654 // Condition has not been materialized. Use its inputs as the comparison and
2655 // its condition as the branch condition.
2656 HCondition* condition = cond->AsCondition();
2657
2658 // If this is a long or FP comparison that has been folded into
2659 // the HCondition, generate the comparison directly.
2660 Primitive::Type type = condition->InputAt(0)->GetType();
2661 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2662 GenerateCompareTestAndBranch(condition, true_target, false_target);
2663 return;
2664 }
2665
Donghui Bai426b49c2016-11-08 14:55:38 +08002666 vixl32::Label* non_fallthrough_target;
2667 vixl32::Condition arm_cond = vixl32::Condition::None();
2668 const vixl32::Register left = InputRegisterAt(cond, 0);
2669 const Operand right = InputOperandAt(cond, 1);
2670
Scott Wakelingfe885462016-09-22 10:24:38 +01002671 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002672 arm_cond = ARMCondition(condition->GetOppositeCondition());
2673 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002674 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002675 arm_cond = ARMCondition(condition->GetCondition());
2676 non_fallthrough_target = true_target;
2677 }
2678
2679 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2680 if (arm_cond.Is(eq)) {
2681 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2682 } else {
2683 DCHECK(arm_cond.Is(ne));
2684 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2685 }
2686 } else {
2687 __ Cmp(left, right);
2688 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002689 }
2690 }
2691
2692 // If neither branch falls through (case 3), the conditional branch to `true_target`
2693 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2694 if (true_target != nullptr && false_target != nullptr) {
2695 __ B(false_target);
2696 }
2697}
2698
2699void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2701 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2702 locations->SetInAt(0, Location::RequiresRegister());
2703 }
2704}
2705
2706void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2707 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2708 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002709 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2710 nullptr : codegen_->GetLabelOf(true_successor);
2711 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2712 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002713 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2714}
2715
Scott Wakelingc34dba72016-10-03 10:14:44 +01002716void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2717 LocationSummary* locations = new (GetGraph()->GetArena())
2718 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2719 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2720 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2721 locations->SetInAt(0, Location::RequiresRegister());
2722 }
2723}
2724
2725void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2726 SlowPathCodeARMVIXL* slow_path =
2727 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2728 GenerateTestAndBranch(deoptimize,
2729 /* condition_input_index */ 0,
2730 slow_path->GetEntryLabel(),
2731 /* false_target */ nullptr);
2732}
2733
Artem Serovd4cc5b22016-11-04 11:19:09 +00002734void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2735 LocationSummary* locations = new (GetGraph()->GetArena())
2736 LocationSummary(flag, LocationSummary::kNoCall);
2737 locations->SetOut(Location::RequiresRegister());
2738}
2739
2740void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2741 GetAssembler()->LoadFromOffset(kLoadWord,
2742 OutputRegister(flag),
2743 sp,
2744 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2745}
2746
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002747void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002749 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2750
2751 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002752 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002753 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002754 } else {
2755 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002756 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002757 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002758
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002759 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002760 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2761 // The code generator handles overlap with the values, but not with the condition.
2762 locations->SetOut(Location::SameAsFirstInput());
2763 } else if (is_floating_point) {
2764 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2765 } else {
2766 if (!locations->InAt(1).IsConstant()) {
2767 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2768 }
2769
2770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002771 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002772}
2773
2774void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002775 HInstruction* const condition = select->GetCondition();
2776 const LocationSummary* const locations = select->GetLocations();
2777 const Primitive::Type type = select->GetType();
2778 const Location first = locations->InAt(0);
2779 const Location out = locations->Out();
2780 const Location second = locations->InAt(1);
2781 Location src;
2782
2783 if (condition->IsIntConstant()) {
2784 if (condition->AsIntConstant()->IsFalse()) {
2785 src = first;
2786 } else {
2787 src = second;
2788 }
2789
2790 codegen_->MoveLocation(out, src, type);
2791 return;
2792 }
2793
2794 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002795 (IsBooleanValueOrMaterializedCondition(condition) ||
2796 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002797 bool invert = false;
2798
2799 if (out.Equals(second)) {
2800 src = first;
2801 invert = true;
2802 } else if (out.Equals(first)) {
2803 src = second;
2804 } else if (second.IsConstant()) {
2805 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2806 src = second;
2807 } else if (first.IsConstant()) {
2808 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2809 src = first;
2810 invert = true;
2811 } else {
2812 src = second;
2813 }
2814
2815 if (CanGenerateConditionalMove(out, src)) {
2816 if (!out.Equals(first) && !out.Equals(second)) {
2817 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2818 }
2819
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002820 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2821
2822 if (IsBooleanValueOrMaterializedCondition(condition)) {
2823 __ Cmp(InputRegisterAt(select, 2), 0);
2824 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2825 } else {
2826 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2827 }
2828
Donghui Bai426b49c2016-11-08 14:55:38 +08002829 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002830 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002831 ExactAssemblyScope guard(GetVIXLAssembler(),
2832 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2833 CodeBufferCheckScope::kExactSize);
2834
2835 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002836 __ it(cond.first);
2837 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002838 } else {
2839 DCHECK(out.IsRegisterPair());
2840
2841 Operand operand_high(0);
2842 Operand operand_low(0);
2843
2844 if (src.IsConstant()) {
2845 const int64_t value = Int64ConstantFrom(src);
2846
2847 operand_high = High32Bits(value);
2848 operand_low = Low32Bits(value);
2849 } else {
2850 DCHECK(src.IsRegisterPair());
2851 operand_high = HighRegisterFrom(src);
2852 operand_low = LowRegisterFrom(src);
2853 }
2854
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002855 __ it(cond.first);
2856 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2857 __ it(cond.first);
2858 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002859 }
2860
2861 return;
2862 }
2863 }
2864
2865 vixl32::Label* false_target = nullptr;
2866 vixl32::Label* true_target = nullptr;
2867 vixl32::Label select_end;
2868 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2869
2870 if (out.Equals(second)) {
2871 true_target = target;
2872 src = first;
2873 } else {
2874 false_target = target;
2875 src = second;
2876
2877 if (!out.Equals(first)) {
2878 codegen_->MoveLocation(out, first, type);
2879 }
2880 }
2881
2882 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2883 codegen_->MoveLocation(out, src, type);
2884
2885 if (select_end.IsReferenced()) {
2886 __ Bind(&select_end);
2887 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002888}
2889
Artem Serov551b28f2016-10-18 19:11:30 +01002890void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2891 new (GetGraph()->GetArena()) LocationSummary(info);
2892}
2893
2894void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2895 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2896}
2897
Scott Wakelingfe885462016-09-22 10:24:38 +01002898void CodeGeneratorARMVIXL::GenerateNop() {
2899 __ Nop();
2900}
2901
2902void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2903 LocationSummary* locations =
2904 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2905 // Handle the long/FP comparisons made in instruction simplification.
2906 switch (cond->InputAt(0)->GetType()) {
2907 case Primitive::kPrimLong:
2908 locations->SetInAt(0, Location::RequiresRegister());
2909 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2910 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01002912 }
2913 break;
2914
Scott Wakelingfe885462016-09-22 10:24:38 +01002915 case Primitive::kPrimFloat:
2916 case Primitive::kPrimDouble:
2917 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002918 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002919 if (!cond->IsEmittedAtUseSite()) {
2920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2921 }
2922 break;
2923
2924 default:
2925 locations->SetInAt(0, Location::RequiresRegister());
2926 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2927 if (!cond->IsEmittedAtUseSite()) {
2928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2929 }
2930 }
2931}
2932
2933void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2934 if (cond->IsEmittedAtUseSite()) {
2935 return;
2936 }
2937
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002938 const vixl32::Register out = OutputRegister(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002939
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002940 if (out.IsLow() && CanGenerateTest(cond, codegen_->GetAssembler())) {
2941 const auto condition = GenerateTest(cond, false, codegen_);
2942 // We use the scope because of the IT block that follows.
2943 ExactAssemblyScope guard(GetVIXLAssembler(),
2944 4 * vixl32::k16BitT32InstructionSizeInBytes,
2945 CodeBufferCheckScope::kExactSize);
2946
2947 __ it(condition.first);
2948 __ mov(condition.first, out, 1);
2949 __ it(condition.second);
2950 __ mov(condition.second, out, 0);
2951 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01002952 }
2953
2954 // Convert the jumps into the result.
2955 vixl32::Label done_label;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002956 vixl32::Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01002957
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002958 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2959 vixl32::Label true_label, false_label;
Scott Wakelingfe885462016-09-22 10:24:38 +01002960
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002961 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2962
2963 // False case: result = 0.
2964 __ Bind(&false_label);
2965 __ Mov(out, 0);
2966 __ B(final_label);
2967
2968 // True case: result = 1.
2969 __ Bind(&true_label);
2970 __ Mov(out, 1);
2971 } else {
2972 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
2973
2974 const auto condition = GenerateTest(cond, false, codegen_);
2975
2976 __ Mov(LeaveFlags, out, 0);
2977 __ B(condition.second, final_label, /* far_target */ false);
2978 __ Mov(out, 1);
2979 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002980
2981 if (done_label.IsReferenced()) {
2982 __ Bind(&done_label);
2983 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002984}
2985
2986void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2987 HandleCondition(comp);
2988}
2989
2990void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2991 HandleCondition(comp);
2992}
2993
2994void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2995 HandleCondition(comp);
2996}
2997
2998void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2999 HandleCondition(comp);
3000}
3001
3002void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3003 HandleCondition(comp);
3004}
3005
3006void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3007 HandleCondition(comp);
3008}
3009
3010void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3011 HandleCondition(comp);
3012}
3013
3014void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3015 HandleCondition(comp);
3016}
3017
3018void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3019 HandleCondition(comp);
3020}
3021
3022void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3023 HandleCondition(comp);
3024}
3025
3026void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3027 HandleCondition(comp);
3028}
3029
3030void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3031 HandleCondition(comp);
3032}
3033
3034void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3035 HandleCondition(comp);
3036}
3037
3038void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3039 HandleCondition(comp);
3040}
3041
3042void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3043 HandleCondition(comp);
3044}
3045
3046void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3047 HandleCondition(comp);
3048}
3049
3050void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3051 HandleCondition(comp);
3052}
3053
3054void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3055 HandleCondition(comp);
3056}
3057
3058void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3059 HandleCondition(comp);
3060}
3061
3062void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3063 HandleCondition(comp);
3064}
3065
3066void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3069 locations->SetOut(Location::ConstantLocation(constant));
3070}
3071
3072void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3073 // Will be generated at use site.
3074}
3075
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003076void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3077 LocationSummary* locations =
3078 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3079 locations->SetOut(Location::ConstantLocation(constant));
3080}
3081
3082void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3083 // Will be generated at use site.
3084}
3085
Scott Wakelingfe885462016-09-22 10:24:38 +01003086void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3087 LocationSummary* locations =
3088 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3089 locations->SetOut(Location::ConstantLocation(constant));
3090}
3091
3092void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3093 // Will be generated at use site.
3094}
3095
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003096void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3097 LocationSummary* locations =
3098 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3099 locations->SetOut(Location::ConstantLocation(constant));
3100}
3101
Scott Wakelingc34dba72016-10-03 10:14:44 +01003102void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3103 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003104 // Will be generated at use site.
3105}
3106
3107void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3108 LocationSummary* locations =
3109 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3110 locations->SetOut(Location::ConstantLocation(constant));
3111}
3112
Scott Wakelingc34dba72016-10-03 10:14:44 +01003113void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3114 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003115 // Will be generated at use site.
3116}
3117
Scott Wakelingfe885462016-09-22 10:24:38 +01003118void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3119 memory_barrier->SetLocations(nullptr);
3120}
3121
3122void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3123 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3124}
3125
3126void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3127 ret->SetLocations(nullptr);
3128}
3129
3130void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3131 codegen_->GenerateFrameExit();
3132}
3133
3134void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3135 LocationSummary* locations =
3136 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3137 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3138}
3139
3140void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3141 codegen_->GenerateFrameExit();
3142}
3143
Artem Serovcfbe9132016-10-14 15:58:56 +01003144void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3145 // The trampoline uses the same calling convention as dex calling conventions,
3146 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3147 // the method_idx.
3148 HandleInvoke(invoke);
3149}
3150
3151void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3152 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3153}
3154
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3156 // Explicit clinit checks triggered by static invokes must have been pruned by
3157 // art::PrepareForRegisterAllocation.
3158 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3159
Anton Kirilov5ec62182016-10-13 20:16:02 +01003160 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3161 if (intrinsic.TryDispatch(invoke)) {
3162 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3163 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3164 }
3165 return;
3166 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003167
3168 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003169
Artem Serovd4cc5b22016-11-04 11:19:09 +00003170 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3171 if (invoke->HasPcRelativeDexCache()) {
3172 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3173 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003174}
3175
Anton Kirilov5ec62182016-10-13 20:16:02 +01003176static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3177 if (invoke->GetLocations()->Intrinsified()) {
3178 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3179 intrinsic.Dispatch(invoke);
3180 return true;
3181 }
3182 return false;
3183}
3184
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003185void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3186 // Explicit clinit checks triggered by static invokes must have been pruned by
3187 // art::PrepareForRegisterAllocation.
3188 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3189
Anton Kirilov5ec62182016-10-13 20:16:02 +01003190 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3191 return;
3192 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003193
3194 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003195 codegen_->GenerateStaticOrDirectCall(
3196 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003197 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3198}
3199
3200void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003201 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003202 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3203}
3204
3205void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003206 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3207 if (intrinsic.TryDispatch(invoke)) {
3208 return;
3209 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003210
3211 HandleInvoke(invoke);
3212}
3213
3214void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3216 return;
3217 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003218
3219 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003220 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003221 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003222}
3223
Artem Serovcfbe9132016-10-14 15:58:56 +01003224void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3225 HandleInvoke(invoke);
3226 // Add the hidden argument.
3227 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3228}
3229
3230void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3231 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3232 LocationSummary* locations = invoke->GetLocations();
3233 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3234 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3235 Location receiver = locations->InAt(0);
3236 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3237
3238 DCHECK(!receiver.IsStackSlot());
3239
Alexandre Rames374ddf32016-11-04 10:40:49 +00003240 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3241 {
Artem Serov0fb37192016-12-06 18:13:40 +00003242 ExactAssemblyScope aas(GetVIXLAssembler(),
3243 vixl32::kMaxInstructionSizeInBytes,
3244 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003245 // /* HeapReference<Class> */ temp = receiver->klass_
3246 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3247 codegen_->MaybeRecordImplicitNullCheck(invoke);
3248 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003249 // Instead of simply (possibly) unpoisoning `temp` here, we should
3250 // emit a read barrier for the previous class reference load.
3251 // However this is not required in practice, as this is an
3252 // intermediate/temporary reference and because the current
3253 // concurrent copying collector keeps the from-space memory
3254 // intact/accessible until the end of the marking phase (the
3255 // concurrent copying collector may not in the future).
3256 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3257 GetAssembler()->LoadFromOffset(kLoadWord,
3258 temp,
3259 temp,
3260 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3261 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3262 invoke->GetImtIndex(), kArmPointerSize));
3263 // temp = temp->GetImtEntryAt(method_offset);
3264 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3265 uint32_t entry_point =
3266 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3267 // LR = temp->GetEntryPoint();
3268 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3269
3270 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3271 // instruction from clobbering it as they might use r12 as a scratch register.
3272 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003273
3274 {
3275 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3276 // so it checks if the application is using them (by passing them to the macro assembler
3277 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3278 // what is available, and is the opposite of the standard usage: Instead of requesting a
3279 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3280 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3281 // (to materialize the constant), since the destination register becomes available for such use
3282 // internally for the duration of the macro instruction.
3283 UseScratchRegisterScope temps(GetVIXLAssembler());
3284 temps.Exclude(hidden_reg);
3285 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3286 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003287 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003288 // Ensure the pc position is recorded immediately after the `blx` instruction.
3289 // 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 +00003290 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003291 vixl32::k16BitT32InstructionSizeInBytes,
3292 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003293 // LR();
3294 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003296 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003297 }
3298}
3299
Orion Hodsonac141392017-01-13 11:53:47 +00003300void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3301 HandleInvoke(invoke);
3302}
3303
3304void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3305 codegen_->GenerateInvokePolymorphicCall(invoke);
3306}
3307
Artem Serov02109dd2016-09-23 17:17:54 +01003308void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3309 LocationSummary* locations =
3310 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3311 switch (neg->GetResultType()) {
3312 case Primitive::kPrimInt: {
3313 locations->SetInAt(0, Location::RequiresRegister());
3314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3315 break;
3316 }
3317 case Primitive::kPrimLong: {
3318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3320 break;
3321 }
3322
3323 case Primitive::kPrimFloat:
3324 case Primitive::kPrimDouble:
3325 locations->SetInAt(0, Location::RequiresFpuRegister());
3326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3327 break;
3328
3329 default:
3330 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3331 }
3332}
3333
3334void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3335 LocationSummary* locations = neg->GetLocations();
3336 Location out = locations->Out();
3337 Location in = locations->InAt(0);
3338 switch (neg->GetResultType()) {
3339 case Primitive::kPrimInt:
3340 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3341 break;
3342
3343 case Primitive::kPrimLong:
3344 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3345 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3346 // We cannot emit an RSC (Reverse Subtract with Carry)
3347 // instruction here, as it does not exist in the Thumb-2
3348 // instruction set. We use the following approach
3349 // using SBC and SUB instead.
3350 //
3351 // out.hi = -C
3352 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3353 // out.hi = out.hi - in.hi
3354 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3355 break;
3356
3357 case Primitive::kPrimFloat:
3358 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003359 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003360 break;
3361
3362 default:
3363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3364 }
3365}
3366
Scott Wakelingfe885462016-09-22 10:24:38 +01003367void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3368 Primitive::Type result_type = conversion->GetResultType();
3369 Primitive::Type input_type = conversion->GetInputType();
3370 DCHECK_NE(result_type, input_type);
3371
3372 // The float-to-long, double-to-long and long-to-float type conversions
3373 // rely on a call to the runtime.
3374 LocationSummary::CallKind call_kind =
3375 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3376 && result_type == Primitive::kPrimLong)
3377 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3378 ? LocationSummary::kCallOnMainOnly
3379 : LocationSummary::kNoCall;
3380 LocationSummary* locations =
3381 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3382
3383 // The Java language does not allow treating boolean as an integral type but
3384 // our bit representation makes it safe.
3385
3386 switch (result_type) {
3387 case Primitive::kPrimByte:
3388 switch (input_type) {
3389 case Primitive::kPrimLong:
3390 // Type conversion from long to byte is a result of code transformations.
3391 case Primitive::kPrimBoolean:
3392 // Boolean input is a result of code transformations.
3393 case Primitive::kPrimShort:
3394 case Primitive::kPrimInt:
3395 case Primitive::kPrimChar:
3396 // Processing a Dex `int-to-byte' instruction.
3397 locations->SetInAt(0, Location::RequiresRegister());
3398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3399 break;
3400
3401 default:
3402 LOG(FATAL) << "Unexpected type conversion from " << input_type
3403 << " to " << result_type;
3404 }
3405 break;
3406
3407 case Primitive::kPrimShort:
3408 switch (input_type) {
3409 case Primitive::kPrimLong:
3410 // Type conversion from long to short is a result of code transformations.
3411 case Primitive::kPrimBoolean:
3412 // Boolean input is a result of code transformations.
3413 case Primitive::kPrimByte:
3414 case Primitive::kPrimInt:
3415 case Primitive::kPrimChar:
3416 // Processing a Dex `int-to-short' instruction.
3417 locations->SetInAt(0, Location::RequiresRegister());
3418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3419 break;
3420
3421 default:
3422 LOG(FATAL) << "Unexpected type conversion from " << input_type
3423 << " to " << result_type;
3424 }
3425 break;
3426
3427 case Primitive::kPrimInt:
3428 switch (input_type) {
3429 case Primitive::kPrimLong:
3430 // Processing a Dex `long-to-int' instruction.
3431 locations->SetInAt(0, Location::Any());
3432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3433 break;
3434
3435 case Primitive::kPrimFloat:
3436 // Processing a Dex `float-to-int' instruction.
3437 locations->SetInAt(0, Location::RequiresFpuRegister());
3438 locations->SetOut(Location::RequiresRegister());
3439 locations->AddTemp(Location::RequiresFpuRegister());
3440 break;
3441
3442 case Primitive::kPrimDouble:
3443 // Processing a Dex `double-to-int' instruction.
3444 locations->SetInAt(0, Location::RequiresFpuRegister());
3445 locations->SetOut(Location::RequiresRegister());
3446 locations->AddTemp(Location::RequiresFpuRegister());
3447 break;
3448
3449 default:
3450 LOG(FATAL) << "Unexpected type conversion from " << input_type
3451 << " to " << result_type;
3452 }
3453 break;
3454
3455 case Primitive::kPrimLong:
3456 switch (input_type) {
3457 case Primitive::kPrimBoolean:
3458 // Boolean input is a result of code transformations.
3459 case Primitive::kPrimByte:
3460 case Primitive::kPrimShort:
3461 case Primitive::kPrimInt:
3462 case Primitive::kPrimChar:
3463 // Processing a Dex `int-to-long' instruction.
3464 locations->SetInAt(0, Location::RequiresRegister());
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3466 break;
3467
3468 case Primitive::kPrimFloat: {
3469 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003470 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3471 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3472 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003473 break;
3474 }
3475
3476 case Primitive::kPrimDouble: {
3477 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003478 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3479 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3480 calling_convention.GetFpuRegisterAt(1)));
3481 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003482 break;
3483 }
3484
3485 default:
3486 LOG(FATAL) << "Unexpected type conversion from " << input_type
3487 << " to " << result_type;
3488 }
3489 break;
3490
3491 case Primitive::kPrimChar:
3492 switch (input_type) {
3493 case Primitive::kPrimLong:
3494 // Type conversion from long to char is a result of code transformations.
3495 case Primitive::kPrimBoolean:
3496 // Boolean input is a result of code transformations.
3497 case Primitive::kPrimByte:
3498 case Primitive::kPrimShort:
3499 case Primitive::kPrimInt:
3500 // Processing a Dex `int-to-char' instruction.
3501 locations->SetInAt(0, Location::RequiresRegister());
3502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3503 break;
3504
3505 default:
3506 LOG(FATAL) << "Unexpected type conversion from " << input_type
3507 << " to " << result_type;
3508 }
3509 break;
3510
3511 case Primitive::kPrimFloat:
3512 switch (input_type) {
3513 case Primitive::kPrimBoolean:
3514 // Boolean input is a result of code transformations.
3515 case Primitive::kPrimByte:
3516 case Primitive::kPrimShort:
3517 case Primitive::kPrimInt:
3518 case Primitive::kPrimChar:
3519 // Processing a Dex `int-to-float' instruction.
3520 locations->SetInAt(0, Location::RequiresRegister());
3521 locations->SetOut(Location::RequiresFpuRegister());
3522 break;
3523
3524 case Primitive::kPrimLong: {
3525 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003526 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3527 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3528 calling_convention.GetRegisterAt(1)));
3529 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003530 break;
3531 }
3532
3533 case Primitive::kPrimDouble:
3534 // Processing a Dex `double-to-float' instruction.
3535 locations->SetInAt(0, Location::RequiresFpuRegister());
3536 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3537 break;
3538
3539 default:
3540 LOG(FATAL) << "Unexpected type conversion from " << input_type
3541 << " to " << result_type;
3542 };
3543 break;
3544
3545 case Primitive::kPrimDouble:
3546 switch (input_type) {
3547 case Primitive::kPrimBoolean:
3548 // Boolean input is a result of code transformations.
3549 case Primitive::kPrimByte:
3550 case Primitive::kPrimShort:
3551 case Primitive::kPrimInt:
3552 case Primitive::kPrimChar:
3553 // Processing a Dex `int-to-double' instruction.
3554 locations->SetInAt(0, Location::RequiresRegister());
3555 locations->SetOut(Location::RequiresFpuRegister());
3556 break;
3557
3558 case Primitive::kPrimLong:
3559 // Processing a Dex `long-to-double' instruction.
3560 locations->SetInAt(0, Location::RequiresRegister());
3561 locations->SetOut(Location::RequiresFpuRegister());
3562 locations->AddTemp(Location::RequiresFpuRegister());
3563 locations->AddTemp(Location::RequiresFpuRegister());
3564 break;
3565
3566 case Primitive::kPrimFloat:
3567 // Processing a Dex `float-to-double' instruction.
3568 locations->SetInAt(0, Location::RequiresFpuRegister());
3569 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3570 break;
3571
3572 default:
3573 LOG(FATAL) << "Unexpected type conversion from " << input_type
3574 << " to " << result_type;
3575 };
3576 break;
3577
3578 default:
3579 LOG(FATAL) << "Unexpected type conversion from " << input_type
3580 << " to " << result_type;
3581 }
3582}
3583
3584void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3585 LocationSummary* locations = conversion->GetLocations();
3586 Location out = locations->Out();
3587 Location in = locations->InAt(0);
3588 Primitive::Type result_type = conversion->GetResultType();
3589 Primitive::Type input_type = conversion->GetInputType();
3590 DCHECK_NE(result_type, input_type);
3591 switch (result_type) {
3592 case Primitive::kPrimByte:
3593 switch (input_type) {
3594 case Primitive::kPrimLong:
3595 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003596 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003597 break;
3598 case Primitive::kPrimBoolean:
3599 // Boolean input is a result of code transformations.
3600 case Primitive::kPrimShort:
3601 case Primitive::kPrimInt:
3602 case Primitive::kPrimChar:
3603 // Processing a Dex `int-to-byte' instruction.
3604 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3605 break;
3606
3607 default:
3608 LOG(FATAL) << "Unexpected type conversion from " << input_type
3609 << " to " << result_type;
3610 }
3611 break;
3612
3613 case Primitive::kPrimShort:
3614 switch (input_type) {
3615 case Primitive::kPrimLong:
3616 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003617 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003618 break;
3619 case Primitive::kPrimBoolean:
3620 // Boolean input is a result of code transformations.
3621 case Primitive::kPrimByte:
3622 case Primitive::kPrimInt:
3623 case Primitive::kPrimChar:
3624 // Processing a Dex `int-to-short' instruction.
3625 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3626 break;
3627
3628 default:
3629 LOG(FATAL) << "Unexpected type conversion from " << input_type
3630 << " to " << result_type;
3631 }
3632 break;
3633
3634 case Primitive::kPrimInt:
3635 switch (input_type) {
3636 case Primitive::kPrimLong:
3637 // Processing a Dex `long-to-int' instruction.
3638 DCHECK(out.IsRegister());
3639 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003640 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003641 } else if (in.IsDoubleStackSlot()) {
3642 GetAssembler()->LoadFromOffset(kLoadWord,
3643 OutputRegister(conversion),
3644 sp,
3645 in.GetStackIndex());
3646 } else {
3647 DCHECK(in.IsConstant());
3648 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003649 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3650 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003651 }
3652 break;
3653
3654 case Primitive::kPrimFloat: {
3655 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003656 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003657 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003658 __ Vmov(OutputRegister(conversion), temp);
3659 break;
3660 }
3661
3662 case Primitive::kPrimDouble: {
3663 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003664 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003665 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003666 __ Vmov(OutputRegister(conversion), temp_s);
3667 break;
3668 }
3669
3670 default:
3671 LOG(FATAL) << "Unexpected type conversion from " << input_type
3672 << " to " << result_type;
3673 }
3674 break;
3675
3676 case Primitive::kPrimLong:
3677 switch (input_type) {
3678 case Primitive::kPrimBoolean:
3679 // Boolean input is a result of code transformations.
3680 case Primitive::kPrimByte:
3681 case Primitive::kPrimShort:
3682 case Primitive::kPrimInt:
3683 case Primitive::kPrimChar:
3684 // Processing a Dex `int-to-long' instruction.
3685 DCHECK(out.IsRegisterPair());
3686 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003687 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003688 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003689 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003690 break;
3691
3692 case Primitive::kPrimFloat:
3693 // Processing a Dex `float-to-long' instruction.
3694 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3695 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3696 break;
3697
3698 case Primitive::kPrimDouble:
3699 // Processing a Dex `double-to-long' instruction.
3700 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3701 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3702 break;
3703
3704 default:
3705 LOG(FATAL) << "Unexpected type conversion from " << input_type
3706 << " to " << result_type;
3707 }
3708 break;
3709
3710 case Primitive::kPrimChar:
3711 switch (input_type) {
3712 case Primitive::kPrimLong:
3713 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003714 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003715 break;
3716 case Primitive::kPrimBoolean:
3717 // Boolean input is a result of code transformations.
3718 case Primitive::kPrimByte:
3719 case Primitive::kPrimShort:
3720 case Primitive::kPrimInt:
3721 // Processing a Dex `int-to-char' instruction.
3722 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3723 break;
3724
3725 default:
3726 LOG(FATAL) << "Unexpected type conversion from " << input_type
3727 << " to " << result_type;
3728 }
3729 break;
3730
3731 case Primitive::kPrimFloat:
3732 switch (input_type) {
3733 case Primitive::kPrimBoolean:
3734 // Boolean input is a result of code transformations.
3735 case Primitive::kPrimByte:
3736 case Primitive::kPrimShort:
3737 case Primitive::kPrimInt:
3738 case Primitive::kPrimChar: {
3739 // Processing a Dex `int-to-float' instruction.
3740 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003741 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003742 break;
3743 }
3744
3745 case Primitive::kPrimLong:
3746 // Processing a Dex `long-to-float' instruction.
3747 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3748 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3749 break;
3750
3751 case Primitive::kPrimDouble:
3752 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003753 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003754 break;
3755
3756 default:
3757 LOG(FATAL) << "Unexpected type conversion from " << input_type
3758 << " to " << result_type;
3759 };
3760 break;
3761
3762 case Primitive::kPrimDouble:
3763 switch (input_type) {
3764 case Primitive::kPrimBoolean:
3765 // Boolean input is a result of code transformations.
3766 case Primitive::kPrimByte:
3767 case Primitive::kPrimShort:
3768 case Primitive::kPrimInt:
3769 case Primitive::kPrimChar: {
3770 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003771 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003772 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 break;
3774 }
3775
3776 case Primitive::kPrimLong: {
3777 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003778 vixl32::Register low = LowRegisterFrom(in);
3779 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003780 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003781 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003782 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003783 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003784 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003785
3786 // temp_d = int-to-double(high)
3787 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003788 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003789 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003790 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003791 // out_d = unsigned-to-double(low)
3792 __ Vmov(out_s, low);
3793 __ Vcvt(F64, U32, out_d, out_s);
3794 // out_d += temp_d * constant_d
3795 __ Vmla(F64, out_d, temp_d, constant_d);
3796 break;
3797 }
3798
3799 case Primitive::kPrimFloat:
3800 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003801 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003802 break;
3803
3804 default:
3805 LOG(FATAL) << "Unexpected type conversion from " << input_type
3806 << " to " << result_type;
3807 };
3808 break;
3809
3810 default:
3811 LOG(FATAL) << "Unexpected type conversion from " << input_type
3812 << " to " << result_type;
3813 }
3814}
3815
3816void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3817 LocationSummary* locations =
3818 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3819 switch (add->GetResultType()) {
3820 case Primitive::kPrimInt: {
3821 locations->SetInAt(0, Location::RequiresRegister());
3822 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3824 break;
3825 }
3826
Scott Wakelingfe885462016-09-22 10:24:38 +01003827 case Primitive::kPrimLong: {
3828 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003829 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3831 break;
3832 }
3833
3834 case Primitive::kPrimFloat:
3835 case Primitive::kPrimDouble: {
3836 locations->SetInAt(0, Location::RequiresFpuRegister());
3837 locations->SetInAt(1, Location::RequiresFpuRegister());
3838 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3839 break;
3840 }
3841
3842 default:
3843 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3844 }
3845}
3846
3847void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3848 LocationSummary* locations = add->GetLocations();
3849 Location out = locations->Out();
3850 Location first = locations->InAt(0);
3851 Location second = locations->InAt(1);
3852
3853 switch (add->GetResultType()) {
3854 case Primitive::kPrimInt: {
3855 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3856 }
3857 break;
3858
Scott Wakelingfe885462016-09-22 10:24:38 +01003859 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003860 if (second.IsConstant()) {
3861 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3862 GenerateAddLongConst(out, first, value);
3863 } else {
3864 DCHECK(second.IsRegisterPair());
3865 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3866 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3867 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 break;
3869 }
3870
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003871 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003872 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003873 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 break;
3875
3876 default:
3877 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3878 }
3879}
3880
3881void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3882 LocationSummary* locations =
3883 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3884 switch (sub->GetResultType()) {
3885 case Primitive::kPrimInt: {
3886 locations->SetInAt(0, Location::RequiresRegister());
3887 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3889 break;
3890 }
3891
Scott Wakelingfe885462016-09-22 10:24:38 +01003892 case Primitive::kPrimLong: {
3893 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003894 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3896 break;
3897 }
3898 case Primitive::kPrimFloat:
3899 case Primitive::kPrimDouble: {
3900 locations->SetInAt(0, Location::RequiresFpuRegister());
3901 locations->SetInAt(1, Location::RequiresFpuRegister());
3902 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3903 break;
3904 }
3905 default:
3906 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3907 }
3908}
3909
3910void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3911 LocationSummary* locations = sub->GetLocations();
3912 Location out = locations->Out();
3913 Location first = locations->InAt(0);
3914 Location second = locations->InAt(1);
3915 switch (sub->GetResultType()) {
3916 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003917 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003918 break;
3919 }
3920
Scott Wakelingfe885462016-09-22 10:24:38 +01003921 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003922 if (second.IsConstant()) {
3923 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3924 GenerateAddLongConst(out, first, -value);
3925 } else {
3926 DCHECK(second.IsRegisterPair());
3927 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3928 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3929 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003930 break;
3931 }
3932
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003933 case Primitive::kPrimFloat:
3934 case Primitive::kPrimDouble:
3935 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003936 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003937
3938 default:
3939 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3940 }
3941}
3942
3943void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3944 LocationSummary* locations =
3945 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3946 switch (mul->GetResultType()) {
3947 case Primitive::kPrimInt:
3948 case Primitive::kPrimLong: {
3949 locations->SetInAt(0, Location::RequiresRegister());
3950 locations->SetInAt(1, Location::RequiresRegister());
3951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3952 break;
3953 }
3954
3955 case Primitive::kPrimFloat:
3956 case Primitive::kPrimDouble: {
3957 locations->SetInAt(0, Location::RequiresFpuRegister());
3958 locations->SetInAt(1, Location::RequiresFpuRegister());
3959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3960 break;
3961 }
3962
3963 default:
3964 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3965 }
3966}
3967
3968void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3969 LocationSummary* locations = mul->GetLocations();
3970 Location out = locations->Out();
3971 Location first = locations->InAt(0);
3972 Location second = locations->InAt(1);
3973 switch (mul->GetResultType()) {
3974 case Primitive::kPrimInt: {
3975 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3976 break;
3977 }
3978 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003979 vixl32::Register out_hi = HighRegisterFrom(out);
3980 vixl32::Register out_lo = LowRegisterFrom(out);
3981 vixl32::Register in1_hi = HighRegisterFrom(first);
3982 vixl32::Register in1_lo = LowRegisterFrom(first);
3983 vixl32::Register in2_hi = HighRegisterFrom(second);
3984 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003985
3986 // Extra checks to protect caused by the existence of R1_R2.
3987 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3988 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003989 DCHECK(!out_hi.Is(in1_lo));
3990 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003991
3992 // input: in1 - 64 bits, in2 - 64 bits
3993 // output: out
3994 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3995 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3996 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3997
3998 UseScratchRegisterScope temps(GetVIXLAssembler());
3999 vixl32::Register temp = temps.Acquire();
4000 // temp <- in1.lo * in2.hi
4001 __ Mul(temp, in1_lo, in2_hi);
4002 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4003 __ Mla(out_hi, in1_hi, in2_lo, temp);
4004 // out.lo <- (in1.lo * in2.lo)[31:0];
4005 __ Umull(out_lo, temp, in1_lo, in2_lo);
4006 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004007 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004008 break;
4009 }
4010
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004011 case Primitive::kPrimFloat:
4012 case Primitive::kPrimDouble:
4013 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004014 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004015
4016 default:
4017 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4018 }
4019}
4020
Scott Wakelingfe885462016-09-22 10:24:38 +01004021void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4022 DCHECK(instruction->IsDiv() || instruction->IsRem());
4023 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4024
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004025 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 DCHECK(second.IsConstant());
4027
4028 vixl32::Register out = OutputRegister(instruction);
4029 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004030 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 DCHECK(imm == 1 || imm == -1);
4032
4033 if (instruction->IsRem()) {
4034 __ Mov(out, 0);
4035 } else {
4036 if (imm == 1) {
4037 __ Mov(out, dividend);
4038 } else {
4039 __ Rsb(out, dividend, 0);
4040 }
4041 }
4042}
4043
4044void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4045 DCHECK(instruction->IsDiv() || instruction->IsRem());
4046 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4047
4048 LocationSummary* locations = instruction->GetLocations();
4049 Location second = locations->InAt(1);
4050 DCHECK(second.IsConstant());
4051
4052 vixl32::Register out = OutputRegister(instruction);
4053 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004054 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004055 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4057 int ctz_imm = CTZ(abs_imm);
4058
4059 if (ctz_imm == 1) {
4060 __ Lsr(temp, dividend, 32 - ctz_imm);
4061 } else {
4062 __ Asr(temp, dividend, 31);
4063 __ Lsr(temp, temp, 32 - ctz_imm);
4064 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004065 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004066
4067 if (instruction->IsDiv()) {
4068 __ Asr(out, out, ctz_imm);
4069 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004070 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004071 }
4072 } else {
4073 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004074 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004075 }
4076}
4077
4078void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4079 DCHECK(instruction->IsDiv() || instruction->IsRem());
4080 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4081
4082 LocationSummary* locations = instruction->GetLocations();
4083 Location second = locations->InAt(1);
4084 DCHECK(second.IsConstant());
4085
4086 vixl32::Register out = OutputRegister(instruction);
4087 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004088 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4089 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004090 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004091
4092 int64_t magic;
4093 int shift;
4094 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4095
Anton Kirilovdda43962016-11-21 19:55:20 +00004096 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4097 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004098 __ Smull(temp2, temp1, dividend, temp1);
4099
4100 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004101 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004102 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004103 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004104 }
4105
4106 if (shift != 0) {
4107 __ Asr(temp1, temp1, shift);
4108 }
4109
4110 if (instruction->IsDiv()) {
4111 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4112 } else {
4113 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4114 // TODO: Strength reduction for mls.
4115 __ Mov(temp2, imm);
4116 __ Mls(out, temp1, temp2, dividend);
4117 }
4118}
4119
4120void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4121 HBinaryOperation* instruction) {
4122 DCHECK(instruction->IsDiv() || instruction->IsRem());
4123 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4124
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004125 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004126 DCHECK(second.IsConstant());
4127
Anton Kirilov644032c2016-12-06 17:51:43 +00004128 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004129 if (imm == 0) {
4130 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4131 } else if (imm == 1 || imm == -1) {
4132 DivRemOneOrMinusOne(instruction);
4133 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4134 DivRemByPowerOfTwo(instruction);
4135 } else {
4136 DCHECK(imm <= -2 || imm >= 2);
4137 GenerateDivRemWithAnyConstant(instruction);
4138 }
4139}
4140
4141void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4142 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4143 if (div->GetResultType() == Primitive::kPrimLong) {
4144 // pLdiv runtime call.
4145 call_kind = LocationSummary::kCallOnMainOnly;
4146 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4147 // sdiv will be replaced by other instruction sequence.
4148 } else if (div->GetResultType() == Primitive::kPrimInt &&
4149 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4150 // pIdivmod runtime call.
4151 call_kind = LocationSummary::kCallOnMainOnly;
4152 }
4153
4154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4155
4156 switch (div->GetResultType()) {
4157 case Primitive::kPrimInt: {
4158 if (div->InputAt(1)->IsConstant()) {
4159 locations->SetInAt(0, Location::RequiresRegister());
4160 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004162 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004163 if (value == 1 || value == 0 || value == -1) {
4164 // No temp register required.
4165 } else {
4166 locations->AddTemp(Location::RequiresRegister());
4167 if (!IsPowerOfTwo(AbsOrMin(value))) {
4168 locations->AddTemp(Location::RequiresRegister());
4169 }
4170 }
4171 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4172 locations->SetInAt(0, Location::RequiresRegister());
4173 locations->SetInAt(1, Location::RequiresRegister());
4174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4175 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004176 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4177 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4178 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004179 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004180 // we only need the former.
4181 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004182 }
4183 break;
4184 }
4185 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004186 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4187 locations->SetInAt(0, LocationFrom(
4188 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4189 locations->SetInAt(1, LocationFrom(
4190 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4191 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004192 break;
4193 }
4194 case Primitive::kPrimFloat:
4195 case Primitive::kPrimDouble: {
4196 locations->SetInAt(0, Location::RequiresFpuRegister());
4197 locations->SetInAt(1, Location::RequiresFpuRegister());
4198 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4199 break;
4200 }
4201
4202 default:
4203 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4204 }
4205}
4206
4207void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004208 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004209 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004210
4211 switch (div->GetResultType()) {
4212 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004213 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004214 GenerateDivRemConstantIntegral(div);
4215 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4216 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4217 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004218 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4219 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4220 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4221 DCHECK(r0.Is(OutputRegister(div)));
4222
4223 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4224 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004225 }
4226 break;
4227 }
4228
4229 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004230 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4231 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4232 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4233 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4234 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4235 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4236 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4237
4238 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4239 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004240 break;
4241 }
4242
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004243 case Primitive::kPrimFloat:
4244 case Primitive::kPrimDouble:
4245 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004246 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004247
4248 default:
4249 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4250 }
4251}
4252
Artem Serov551b28f2016-10-18 19:11:30 +01004253void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4254 Primitive::Type type = rem->GetResultType();
4255
4256 // Most remainders are implemented in the runtime.
4257 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4258 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4259 // sdiv will be replaced by other instruction sequence.
4260 call_kind = LocationSummary::kNoCall;
4261 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4262 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4263 // Have hardware divide instruction for int, do it with three instructions.
4264 call_kind = LocationSummary::kNoCall;
4265 }
4266
4267 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4268
4269 switch (type) {
4270 case Primitive::kPrimInt: {
4271 if (rem->InputAt(1)->IsConstant()) {
4272 locations->SetInAt(0, Location::RequiresRegister());
4273 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004275 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004276 if (value == 1 || value == 0 || value == -1) {
4277 // No temp register required.
4278 } else {
4279 locations->AddTemp(Location::RequiresRegister());
4280 if (!IsPowerOfTwo(AbsOrMin(value))) {
4281 locations->AddTemp(Location::RequiresRegister());
4282 }
4283 }
4284 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4285 locations->SetInAt(0, Location::RequiresRegister());
4286 locations->SetInAt(1, Location::RequiresRegister());
4287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4288 locations->AddTemp(Location::RequiresRegister());
4289 } else {
4290 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4291 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4292 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004293 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004294 // we only need the latter.
4295 locations->SetOut(LocationFrom(r1));
4296 }
4297 break;
4298 }
4299 case Primitive::kPrimLong: {
4300 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4301 locations->SetInAt(0, LocationFrom(
4302 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4303 locations->SetInAt(1, LocationFrom(
4304 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4305 // The runtime helper puts the output in R2,R3.
4306 locations->SetOut(LocationFrom(r2, r3));
4307 break;
4308 }
4309 case Primitive::kPrimFloat: {
4310 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4311 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4312 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4313 locations->SetOut(LocationFrom(s0));
4314 break;
4315 }
4316
4317 case Primitive::kPrimDouble: {
4318 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4319 locations->SetInAt(0, LocationFrom(
4320 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4321 locations->SetInAt(1, LocationFrom(
4322 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4323 locations->SetOut(LocationFrom(s0, s1));
4324 break;
4325 }
4326
4327 default:
4328 LOG(FATAL) << "Unexpected rem type " << type;
4329 }
4330}
4331
4332void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4333 LocationSummary* locations = rem->GetLocations();
4334 Location second = locations->InAt(1);
4335
4336 Primitive::Type type = rem->GetResultType();
4337 switch (type) {
4338 case Primitive::kPrimInt: {
4339 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4340 vixl32::Register out_reg = OutputRegister(rem);
4341 if (second.IsConstant()) {
4342 GenerateDivRemConstantIntegral(rem);
4343 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4344 vixl32::Register reg2 = RegisterFrom(second);
4345 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4346
4347 // temp = reg1 / reg2 (integer division)
4348 // dest = reg1 - temp * reg2
4349 __ Sdiv(temp, reg1, reg2);
4350 __ Mls(out_reg, temp, reg2, reg1);
4351 } else {
4352 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4353 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4354 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4355 DCHECK(out_reg.Is(r1));
4356
4357 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4358 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4359 }
4360 break;
4361 }
4362
4363 case Primitive::kPrimLong: {
4364 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4365 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4366 break;
4367 }
4368
4369 case Primitive::kPrimFloat: {
4370 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4371 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4372 break;
4373 }
4374
4375 case Primitive::kPrimDouble: {
4376 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4377 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4378 break;
4379 }
4380
4381 default:
4382 LOG(FATAL) << "Unexpected rem type " << type;
4383 }
4384}
4385
4386
Scott Wakelingfe885462016-09-22 10:24:38 +01004387void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004388 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004389 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004390}
4391
4392void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4393 DivZeroCheckSlowPathARMVIXL* slow_path =
4394 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4395 codegen_->AddSlowPath(slow_path);
4396
4397 LocationSummary* locations = instruction->GetLocations();
4398 Location value = locations->InAt(0);
4399
4400 switch (instruction->GetType()) {
4401 case Primitive::kPrimBoolean:
4402 case Primitive::kPrimByte:
4403 case Primitive::kPrimChar:
4404 case Primitive::kPrimShort:
4405 case Primitive::kPrimInt: {
4406 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004407 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004408 } else {
4409 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004410 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004411 __ B(slow_path->GetEntryLabel());
4412 }
4413 }
4414 break;
4415 }
4416 case Primitive::kPrimLong: {
4417 if (value.IsRegisterPair()) {
4418 UseScratchRegisterScope temps(GetVIXLAssembler());
4419 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004420 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004421 __ B(eq, slow_path->GetEntryLabel());
4422 } else {
4423 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004424 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004425 __ B(slow_path->GetEntryLabel());
4426 }
4427 }
4428 break;
4429 }
4430 default:
4431 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4432 }
4433}
4434
Artem Serov02109dd2016-09-23 17:17:54 +01004435void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4436 LocationSummary* locations = ror->GetLocations();
4437 vixl32::Register in = InputRegisterAt(ror, 0);
4438 Location rhs = locations->InAt(1);
4439 vixl32::Register out = OutputRegister(ror);
4440
4441 if (rhs.IsConstant()) {
4442 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4443 // so map all rotations to a +ve. equivalent in that range.
4444 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4445 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4446 if (rot) {
4447 // Rotate, mapping left rotations to right equivalents if necessary.
4448 // (e.g. left by 2 bits == right by 30.)
4449 __ Ror(out, in, rot);
4450 } else if (!out.Is(in)) {
4451 __ Mov(out, in);
4452 }
4453 } else {
4454 __ Ror(out, in, RegisterFrom(rhs));
4455 }
4456}
4457
4458// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4459// rotates by swapping input regs (effectively rotating by the first 32-bits of
4460// a larger rotation) or flipping direction (thus treating larger right/left
4461// rotations as sub-word sized rotations in the other direction) as appropriate.
4462void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4463 LocationSummary* locations = ror->GetLocations();
4464 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4465 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4466 Location rhs = locations->InAt(1);
4467 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4468 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4469
4470 if (rhs.IsConstant()) {
4471 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4472 // Map all rotations to +ve. equivalents on the interval [0,63].
4473 rot &= kMaxLongShiftDistance;
4474 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4475 // logic below to a simple pair of binary orr.
4476 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4477 if (rot >= kArmBitsPerWord) {
4478 rot -= kArmBitsPerWord;
4479 std::swap(in_reg_hi, in_reg_lo);
4480 }
4481 // Rotate, or mov to out for zero or word size rotations.
4482 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004483 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004484 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004485 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004486 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4487 } else {
4488 __ Mov(out_reg_lo, in_reg_lo);
4489 __ Mov(out_reg_hi, in_reg_hi);
4490 }
4491 } else {
4492 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4493 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4494 vixl32::Label end;
4495 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004496 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004497
4498 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4499 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004500 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004501 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004502
4503 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4504 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4505 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4506 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4507 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4508 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4509 __ Lsr(shift_left, in_reg_hi, shift_right);
4510 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004511 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004512
4513 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4514 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4515 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4516 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4517 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4518 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4519 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4520 __ Lsl(shift_right, in_reg_hi, shift_left);
4521 __ Add(out_reg_lo, out_reg_lo, shift_right);
4522
Anton Kirilov6f644202017-02-27 18:29:45 +00004523 if (end.IsReferenced()) {
4524 __ Bind(&end);
4525 }
Artem Serov02109dd2016-09-23 17:17:54 +01004526 }
4527}
4528
4529void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4530 LocationSummary* locations =
4531 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4532 switch (ror->GetResultType()) {
4533 case Primitive::kPrimInt: {
4534 locations->SetInAt(0, Location::RequiresRegister());
4535 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4537 break;
4538 }
4539 case Primitive::kPrimLong: {
4540 locations->SetInAt(0, Location::RequiresRegister());
4541 if (ror->InputAt(1)->IsConstant()) {
4542 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4543 } else {
4544 locations->SetInAt(1, Location::RequiresRegister());
4545 locations->AddTemp(Location::RequiresRegister());
4546 locations->AddTemp(Location::RequiresRegister());
4547 }
4548 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4549 break;
4550 }
4551 default:
4552 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4553 }
4554}
4555
4556void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4557 Primitive::Type type = ror->GetResultType();
4558 switch (type) {
4559 case Primitive::kPrimInt: {
4560 HandleIntegerRotate(ror);
4561 break;
4562 }
4563 case Primitive::kPrimLong: {
4564 HandleLongRotate(ror);
4565 break;
4566 }
4567 default:
4568 LOG(FATAL) << "Unexpected operation type " << type;
4569 UNREACHABLE();
4570 }
4571}
4572
Artem Serov02d37832016-10-25 15:25:33 +01004573void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4574 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4575
4576 LocationSummary* locations =
4577 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4578
4579 switch (op->GetResultType()) {
4580 case Primitive::kPrimInt: {
4581 locations->SetInAt(0, Location::RequiresRegister());
4582 if (op->InputAt(1)->IsConstant()) {
4583 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4585 } else {
4586 locations->SetInAt(1, Location::RequiresRegister());
4587 // Make the output overlap, as it will be used to hold the masked
4588 // second input.
4589 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4590 }
4591 break;
4592 }
4593 case Primitive::kPrimLong: {
4594 locations->SetInAt(0, Location::RequiresRegister());
4595 if (op->InputAt(1)->IsConstant()) {
4596 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4597 // For simplicity, use kOutputOverlap even though we only require that low registers
4598 // don't clash with high registers which the register allocator currently guarantees.
4599 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4600 } else {
4601 locations->SetInAt(1, Location::RequiresRegister());
4602 locations->AddTemp(Location::RequiresRegister());
4603 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4604 }
4605 break;
4606 }
4607 default:
4608 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4609 }
4610}
4611
4612void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4613 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4614
4615 LocationSummary* locations = op->GetLocations();
4616 Location out = locations->Out();
4617 Location first = locations->InAt(0);
4618 Location second = locations->InAt(1);
4619
4620 Primitive::Type type = op->GetResultType();
4621 switch (type) {
4622 case Primitive::kPrimInt: {
4623 vixl32::Register out_reg = OutputRegister(op);
4624 vixl32::Register first_reg = InputRegisterAt(op, 0);
4625 if (second.IsRegister()) {
4626 vixl32::Register second_reg = RegisterFrom(second);
4627 // ARM doesn't mask the shift count so we need to do it ourselves.
4628 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4629 if (op->IsShl()) {
4630 __ Lsl(out_reg, first_reg, out_reg);
4631 } else if (op->IsShr()) {
4632 __ Asr(out_reg, first_reg, out_reg);
4633 } else {
4634 __ Lsr(out_reg, first_reg, out_reg);
4635 }
4636 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004637 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004638 uint32_t shift_value = cst & kMaxIntShiftDistance;
4639 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4640 __ Mov(out_reg, first_reg);
4641 } else if (op->IsShl()) {
4642 __ Lsl(out_reg, first_reg, shift_value);
4643 } else if (op->IsShr()) {
4644 __ Asr(out_reg, first_reg, shift_value);
4645 } else {
4646 __ Lsr(out_reg, first_reg, shift_value);
4647 }
4648 }
4649 break;
4650 }
4651 case Primitive::kPrimLong: {
4652 vixl32::Register o_h = HighRegisterFrom(out);
4653 vixl32::Register o_l = LowRegisterFrom(out);
4654
4655 vixl32::Register high = HighRegisterFrom(first);
4656 vixl32::Register low = LowRegisterFrom(first);
4657
4658 if (second.IsRegister()) {
4659 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4660
4661 vixl32::Register second_reg = RegisterFrom(second);
4662
4663 if (op->IsShl()) {
4664 __ And(o_l, second_reg, kMaxLongShiftDistance);
4665 // Shift the high part
4666 __ Lsl(o_h, high, o_l);
4667 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004668 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004669 __ Lsr(temp, low, temp);
4670 __ Orr(o_h, o_h, temp);
4671 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004672 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004673 {
Artem Serov0fb37192016-12-06 18:13:40 +00004674 ExactAssemblyScope guard(GetVIXLAssembler(),
4675 2 * vixl32::kMaxInstructionSizeInBytes,
4676 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004677 __ it(pl);
4678 __ lsl(pl, o_h, low, temp);
4679 }
4680 // Shift the low part
4681 __ Lsl(o_l, low, o_l);
4682 } else if (op->IsShr()) {
4683 __ And(o_h, second_reg, kMaxLongShiftDistance);
4684 // Shift the low part
4685 __ Lsr(o_l, low, o_h);
4686 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004687 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004688 __ Lsl(temp, high, temp);
4689 __ Orr(o_l, o_l, temp);
4690 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004691 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004692 {
Artem Serov0fb37192016-12-06 18:13:40 +00004693 ExactAssemblyScope guard(GetVIXLAssembler(),
4694 2 * vixl32::kMaxInstructionSizeInBytes,
4695 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004696 __ it(pl);
4697 __ asr(pl, o_l, high, temp);
4698 }
4699 // Shift the high part
4700 __ Asr(o_h, high, o_h);
4701 } else {
4702 __ And(o_h, second_reg, kMaxLongShiftDistance);
4703 // same as Shr except we use `Lsr`s and not `Asr`s
4704 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004705 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004706 __ Lsl(temp, high, temp);
4707 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004708 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004709 {
Artem Serov0fb37192016-12-06 18:13:40 +00004710 ExactAssemblyScope guard(GetVIXLAssembler(),
4711 2 * vixl32::kMaxInstructionSizeInBytes,
4712 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004713 __ it(pl);
4714 __ lsr(pl, o_l, high, temp);
4715 }
4716 __ Lsr(o_h, high, o_h);
4717 }
4718 } else {
4719 // Register allocator doesn't create partial overlap.
4720 DCHECK(!o_l.Is(high));
4721 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004722 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004723 uint32_t shift_value = cst & kMaxLongShiftDistance;
4724 if (shift_value > 32) {
4725 if (op->IsShl()) {
4726 __ Lsl(o_h, low, shift_value - 32);
4727 __ Mov(o_l, 0);
4728 } else if (op->IsShr()) {
4729 __ Asr(o_l, high, shift_value - 32);
4730 __ Asr(o_h, high, 31);
4731 } else {
4732 __ Lsr(o_l, high, shift_value - 32);
4733 __ Mov(o_h, 0);
4734 }
4735 } else if (shift_value == 32) {
4736 if (op->IsShl()) {
4737 __ Mov(o_h, low);
4738 __ Mov(o_l, 0);
4739 } else if (op->IsShr()) {
4740 __ Mov(o_l, high);
4741 __ Asr(o_h, high, 31);
4742 } else {
4743 __ Mov(o_l, high);
4744 __ Mov(o_h, 0);
4745 }
4746 } else if (shift_value == 1) {
4747 if (op->IsShl()) {
4748 __ Lsls(o_l, low, 1);
4749 __ Adc(o_h, high, high);
4750 } else if (op->IsShr()) {
4751 __ Asrs(o_h, high, 1);
4752 __ Rrx(o_l, low);
4753 } else {
4754 __ Lsrs(o_h, high, 1);
4755 __ Rrx(o_l, low);
4756 }
4757 } else {
4758 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4759 if (op->IsShl()) {
4760 __ Lsl(o_h, high, shift_value);
4761 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4762 __ Lsl(o_l, low, shift_value);
4763 } else if (op->IsShr()) {
4764 __ Lsr(o_l, low, shift_value);
4765 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4766 __ Asr(o_h, high, shift_value);
4767 } else {
4768 __ Lsr(o_l, low, shift_value);
4769 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4770 __ Lsr(o_h, high, shift_value);
4771 }
4772 }
4773 }
4774 break;
4775 }
4776 default:
4777 LOG(FATAL) << "Unexpected operation type " << type;
4778 UNREACHABLE();
4779 }
4780}
4781
4782void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4783 HandleShift(shl);
4784}
4785
4786void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4787 HandleShift(shl);
4788}
4789
4790void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4791 HandleShift(shr);
4792}
4793
4794void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4795 HandleShift(shr);
4796}
4797
4798void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4799 HandleShift(ushr);
4800}
4801
4802void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4803 HandleShift(ushr);
4804}
4805
4806void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4807 LocationSummary* locations =
4808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4809 if (instruction->IsStringAlloc()) {
4810 locations->AddTemp(LocationFrom(kMethodRegister));
4811 } else {
4812 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4813 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004814 }
4815 locations->SetOut(LocationFrom(r0));
4816}
4817
4818void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4819 // Note: if heap poisoning is enabled, the entry point takes cares
4820 // of poisoning the reference.
4821 if (instruction->IsStringAlloc()) {
4822 // String is allocated through StringFactory. Call NewEmptyString entry point.
4823 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4824 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4825 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4826 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004827 // 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 +00004828 ExactAssemblyScope aas(GetVIXLAssembler(),
4829 vixl32::k16BitT32InstructionSizeInBytes,
4830 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004831 __ blx(lr);
4832 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4833 } else {
4834 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004835 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004836 }
4837}
4838
4839void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4840 LocationSummary* locations =
4841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4842 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004843 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004844 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4845 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004846}
4847
4848void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004849 // Note: if heap poisoning is enabled, the entry point takes cares
4850 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004851 QuickEntrypointEnum entrypoint =
4852 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4853 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004854 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004855 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004856}
4857
4858void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4859 LocationSummary* locations =
4860 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4861 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4862 if (location.IsStackSlot()) {
4863 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4864 } else if (location.IsDoubleStackSlot()) {
4865 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4866 }
4867 locations->SetOut(location);
4868}
4869
4870void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4871 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4872 // Nothing to do, the parameter is already at its location.
4873}
4874
4875void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4878 locations->SetOut(LocationFrom(kMethodRegister));
4879}
4880
4881void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4882 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4883 // Nothing to do, the method is already at its location.
4884}
4885
4886void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4887 LocationSummary* locations =
4888 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4889 locations->SetInAt(0, Location::RequiresRegister());
4890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4891}
4892
4893void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4894 LocationSummary* locations = not_->GetLocations();
4895 Location out = locations->Out();
4896 Location in = locations->InAt(0);
4897 switch (not_->GetResultType()) {
4898 case Primitive::kPrimInt:
4899 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4900 break;
4901
4902 case Primitive::kPrimLong:
4903 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4904 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4905 break;
4906
4907 default:
4908 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4909 }
4910}
4911
Scott Wakelingc34dba72016-10-03 10:14:44 +01004912void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4913 LocationSummary* locations =
4914 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4915 locations->SetInAt(0, Location::RequiresRegister());
4916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4917}
4918
4919void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4920 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4921}
4922
Artem Serov02d37832016-10-25 15:25:33 +01004923void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4924 LocationSummary* locations =
4925 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4926 switch (compare->InputAt(0)->GetType()) {
4927 case Primitive::kPrimBoolean:
4928 case Primitive::kPrimByte:
4929 case Primitive::kPrimShort:
4930 case Primitive::kPrimChar:
4931 case Primitive::kPrimInt:
4932 case Primitive::kPrimLong: {
4933 locations->SetInAt(0, Location::RequiresRegister());
4934 locations->SetInAt(1, Location::RequiresRegister());
4935 // Output overlaps because it is written before doing the low comparison.
4936 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4937 break;
4938 }
4939 case Primitive::kPrimFloat:
4940 case Primitive::kPrimDouble: {
4941 locations->SetInAt(0, Location::RequiresFpuRegister());
4942 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4943 locations->SetOut(Location::RequiresRegister());
4944 break;
4945 }
4946 default:
4947 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4948 }
4949}
4950
4951void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4952 LocationSummary* locations = compare->GetLocations();
4953 vixl32::Register out = OutputRegister(compare);
4954 Location left = locations->InAt(0);
4955 Location right = locations->InAt(1);
4956
4957 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004958 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01004959 Primitive::Type type = compare->InputAt(0)->GetType();
4960 vixl32::Condition less_cond = vixl32::Condition(kNone);
4961 switch (type) {
4962 case Primitive::kPrimBoolean:
4963 case Primitive::kPrimByte:
4964 case Primitive::kPrimShort:
4965 case Primitive::kPrimChar:
4966 case Primitive::kPrimInt: {
4967 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4968 __ Mov(out, 0);
4969 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4970 less_cond = lt;
4971 break;
4972 }
4973 case Primitive::kPrimLong: {
4974 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004975 __ B(lt, &less, /* far_target */ false);
4976 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004977 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4978 __ Mov(out, 0);
4979 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4980 less_cond = lo;
4981 break;
4982 }
4983 case Primitive::kPrimFloat:
4984 case Primitive::kPrimDouble: {
4985 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004986 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01004987 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4988 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4989 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4990 break;
4991 }
4992 default:
4993 LOG(FATAL) << "Unexpected compare type " << type;
4994 UNREACHABLE();
4995 }
4996
Anton Kirilov6f644202017-02-27 18:29:45 +00004997 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00004998 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004999
5000 __ Bind(&greater);
5001 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005002 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005003
5004 __ Bind(&less);
5005 __ Mov(out, -1);
5006
Anton Kirilov6f644202017-02-27 18:29:45 +00005007 if (done.IsReferenced()) {
5008 __ Bind(&done);
5009 }
Artem Serov02d37832016-10-25 15:25:33 +01005010}
5011
5012void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5013 LocationSummary* locations =
5014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5015 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5016 locations->SetInAt(i, Location::Any());
5017 }
5018 locations->SetOut(Location::Any());
5019}
5020
5021void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5022 LOG(FATAL) << "Unreachable";
5023}
5024
5025void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5026 // TODO (ported from quick): revisit ARM barrier kinds.
5027 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5028 switch (kind) {
5029 case MemBarrierKind::kAnyStore:
5030 case MemBarrierKind::kLoadAny:
5031 case MemBarrierKind::kAnyAny: {
5032 flavor = DmbOptions::ISH;
5033 break;
5034 }
5035 case MemBarrierKind::kStoreStore: {
5036 flavor = DmbOptions::ISHST;
5037 break;
5038 }
5039 default:
5040 LOG(FATAL) << "Unexpected memory barrier " << kind;
5041 }
5042 __ Dmb(flavor);
5043}
5044
5045void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5046 uint32_t offset,
5047 vixl32::Register out_lo,
5048 vixl32::Register out_hi) {
5049 UseScratchRegisterScope temps(GetVIXLAssembler());
5050 if (offset != 0) {
5051 vixl32::Register temp = temps.Acquire();
5052 __ Add(temp, addr, offset);
5053 addr = temp;
5054 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005055 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005056}
5057
5058void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5059 uint32_t offset,
5060 vixl32::Register value_lo,
5061 vixl32::Register value_hi,
5062 vixl32::Register temp1,
5063 vixl32::Register temp2,
5064 HInstruction* instruction) {
5065 UseScratchRegisterScope temps(GetVIXLAssembler());
5066 vixl32::Label fail;
5067 if (offset != 0) {
5068 vixl32::Register temp = temps.Acquire();
5069 __ Add(temp, addr, offset);
5070 addr = temp;
5071 }
5072 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005073 {
5074 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005075 ExactAssemblyScope aas(GetVIXLAssembler(),
5076 vixl32::kMaxInstructionSizeInBytes,
5077 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005078 // We need a load followed by store. (The address used in a STREX instruction must
5079 // be the same as the address in the most recently executed LDREX instruction.)
5080 __ ldrexd(temp1, temp2, MemOperand(addr));
5081 codegen_->MaybeRecordImplicitNullCheck(instruction);
5082 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005083 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005084 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005085}
Artem Serov02109dd2016-09-23 17:17:54 +01005086
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005087void LocationsBuilderARMVIXL::HandleFieldSet(
5088 HInstruction* instruction, const FieldInfo& field_info) {
5089 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5090
5091 LocationSummary* locations =
5092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5093 locations->SetInAt(0, Location::RequiresRegister());
5094
5095 Primitive::Type field_type = field_info.GetFieldType();
5096 if (Primitive::IsFloatingPointType(field_type)) {
5097 locations->SetInAt(1, Location::RequiresFpuRegister());
5098 } else {
5099 locations->SetInAt(1, Location::RequiresRegister());
5100 }
5101
5102 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5103 bool generate_volatile = field_info.IsVolatile()
5104 && is_wide
5105 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5106 bool needs_write_barrier =
5107 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5108 // Temporary registers for the write barrier.
5109 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5110 if (needs_write_barrier) {
5111 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5112 locations->AddTemp(Location::RequiresRegister());
5113 } else if (generate_volatile) {
5114 // ARM encoding have some additional constraints for ldrexd/strexd:
5115 // - registers need to be consecutive
5116 // - the first register should be even but not R14.
5117 // We don't test for ARM yet, and the assertion makes sure that we
5118 // revisit this if we ever enable ARM encoding.
5119 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5120
5121 locations->AddTemp(Location::RequiresRegister());
5122 locations->AddTemp(Location::RequiresRegister());
5123 if (field_type == Primitive::kPrimDouble) {
5124 // For doubles we need two more registers to copy the value.
5125 locations->AddTemp(LocationFrom(r2));
5126 locations->AddTemp(LocationFrom(r3));
5127 }
5128 }
5129}
5130
5131void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5132 const FieldInfo& field_info,
5133 bool value_can_be_null) {
5134 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5135
5136 LocationSummary* locations = instruction->GetLocations();
5137 vixl32::Register base = InputRegisterAt(instruction, 0);
5138 Location value = locations->InAt(1);
5139
5140 bool is_volatile = field_info.IsVolatile();
5141 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5142 Primitive::Type field_type = field_info.GetFieldType();
5143 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5144 bool needs_write_barrier =
5145 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5146
5147 if (is_volatile) {
5148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5149 }
5150
5151 switch (field_type) {
5152 case Primitive::kPrimBoolean:
5153 case Primitive::kPrimByte: {
5154 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5155 break;
5156 }
5157
5158 case Primitive::kPrimShort:
5159 case Primitive::kPrimChar: {
5160 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5161 break;
5162 }
5163
5164 case Primitive::kPrimInt:
5165 case Primitive::kPrimNot: {
5166 if (kPoisonHeapReferences && needs_write_barrier) {
5167 // Note that in the case where `value` is a null reference,
5168 // we do not enter this block, as a null reference does not
5169 // need poisoning.
5170 DCHECK_EQ(field_type, Primitive::kPrimNot);
5171 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5172 __ Mov(temp, RegisterFrom(value));
5173 GetAssembler()->PoisonHeapReference(temp);
5174 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5175 } else {
5176 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5177 }
5178 break;
5179 }
5180
5181 case Primitive::kPrimLong: {
5182 if (is_volatile && !atomic_ldrd_strd) {
5183 GenerateWideAtomicStore(base,
5184 offset,
5185 LowRegisterFrom(value),
5186 HighRegisterFrom(value),
5187 RegisterFrom(locations->GetTemp(0)),
5188 RegisterFrom(locations->GetTemp(1)),
5189 instruction);
5190 } else {
5191 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5192 codegen_->MaybeRecordImplicitNullCheck(instruction);
5193 }
5194 break;
5195 }
5196
5197 case Primitive::kPrimFloat: {
5198 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5199 break;
5200 }
5201
5202 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005203 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005204 if (is_volatile && !atomic_ldrd_strd) {
5205 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5206 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5207
5208 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5209
5210 GenerateWideAtomicStore(base,
5211 offset,
5212 value_reg_lo,
5213 value_reg_hi,
5214 RegisterFrom(locations->GetTemp(2)),
5215 RegisterFrom(locations->GetTemp(3)),
5216 instruction);
5217 } else {
5218 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5219 codegen_->MaybeRecordImplicitNullCheck(instruction);
5220 }
5221 break;
5222 }
5223
5224 case Primitive::kPrimVoid:
5225 LOG(FATAL) << "Unreachable type " << field_type;
5226 UNREACHABLE();
5227 }
5228
5229 // Longs and doubles are handled in the switch.
5230 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005231 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5232 // should use a scope and the assembler to emit the store instruction to guarantee that we
5233 // record the pc at the correct position. But the `Assembler` does not automatically handle
5234 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5235 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005236 codegen_->MaybeRecordImplicitNullCheck(instruction);
5237 }
5238
5239 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5240 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5241 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5242 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5243 }
5244
5245 if (is_volatile) {
5246 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5247 }
5248}
5249
Artem Serov02d37832016-10-25 15:25:33 +01005250void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5251 const FieldInfo& field_info) {
5252 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5253
5254 bool object_field_get_with_read_barrier =
5255 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5256 LocationSummary* locations =
5257 new (GetGraph()->GetArena()) LocationSummary(instruction,
5258 object_field_get_with_read_barrier ?
5259 LocationSummary::kCallOnSlowPath :
5260 LocationSummary::kNoCall);
5261 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5262 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5263 }
5264 locations->SetInAt(0, Location::RequiresRegister());
5265
5266 bool volatile_for_double = field_info.IsVolatile()
5267 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5268 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5269 // The output overlaps in case of volatile long: we don't want the
5270 // code generated by GenerateWideAtomicLoad to overwrite the
5271 // object's location. Likewise, in the case of an object field get
5272 // with read barriers enabled, we do not want the load to overwrite
5273 // the object's location, as we need it to emit the read barrier.
5274 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5275 object_field_get_with_read_barrier;
5276
5277 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5278 locations->SetOut(Location::RequiresFpuRegister());
5279 } else {
5280 locations->SetOut(Location::RequiresRegister(),
5281 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5282 }
5283 if (volatile_for_double) {
5284 // ARM encoding have some additional constraints for ldrexd/strexd:
5285 // - registers need to be consecutive
5286 // - the first register should be even but not R14.
5287 // We don't test for ARM yet, and the assertion makes sure that we
5288 // revisit this if we ever enable ARM encoding.
5289 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5290 locations->AddTemp(Location::RequiresRegister());
5291 locations->AddTemp(Location::RequiresRegister());
5292 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5293 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005294 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01005295 locations->AddTemp(Location::RequiresRegister());
5296 }
5297}
5298
5299Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5300 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5301 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5302 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5303 return Location::ConstantLocation(input->AsConstant());
5304 } else {
5305 return Location::RequiresFpuRegister();
5306 }
5307}
5308
Artem Serov02109dd2016-09-23 17:17:54 +01005309Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5310 Opcode opcode) {
5311 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5312 if (constant->IsConstant() &&
5313 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5314 return Location::ConstantLocation(constant->AsConstant());
5315 }
5316 return Location::RequiresRegister();
5317}
5318
5319bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5320 Opcode opcode) {
5321 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5322 if (Primitive::Is64BitType(input_cst->GetType())) {
5323 Opcode high_opcode = opcode;
5324 SetCc low_set_cc = kCcDontCare;
5325 switch (opcode) {
5326 case SUB:
5327 // Flip the operation to an ADD.
5328 value = -value;
5329 opcode = ADD;
5330 FALLTHROUGH_INTENDED;
5331 case ADD:
5332 if (Low32Bits(value) == 0u) {
5333 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5334 }
5335 high_opcode = ADC;
5336 low_set_cc = kCcSet;
5337 break;
5338 default:
5339 break;
5340 }
5341 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5342 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5343 } else {
5344 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5345 }
5346}
5347
5348// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5349// enabled.
5350bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5351 Opcode opcode,
5352 SetCc set_cc) {
5353 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5354 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5355 return true;
5356 }
5357 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005358 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005359 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005360 case AND: neg_opcode = BIC; neg_value = ~value; break;
5361 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5362 case ADD: neg_opcode = SUB; neg_value = -value; break;
5363 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5364 case SUB: neg_opcode = ADD; neg_value = -value; break;
5365 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5366 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005367 default:
5368 return false;
5369 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005370
5371 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5372 return true;
5373 }
5374
5375 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005376}
5377
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005378void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5379 const FieldInfo& field_info) {
5380 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5381
5382 LocationSummary* locations = instruction->GetLocations();
5383 vixl32::Register base = InputRegisterAt(instruction, 0);
5384 Location out = locations->Out();
5385 bool is_volatile = field_info.IsVolatile();
5386 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5387 Primitive::Type field_type = field_info.GetFieldType();
5388 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5389
5390 switch (field_type) {
5391 case Primitive::kPrimBoolean:
5392 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5393 break;
5394
5395 case Primitive::kPrimByte:
5396 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5397 break;
5398
5399 case Primitive::kPrimShort:
5400 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5401 break;
5402
5403 case Primitive::kPrimChar:
5404 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5405 break;
5406
5407 case Primitive::kPrimInt:
5408 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5409 break;
5410
5411 case Primitive::kPrimNot: {
5412 // /* HeapReference<Object> */ out = *(base + offset)
5413 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005414 Location temp_loc = locations->GetTemp(0);
5415 // Note that a potential implicit null check is handled in this
5416 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5417 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5418 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5419 if (is_volatile) {
5420 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5421 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005422 } else {
5423 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005424 codegen_->MaybeRecordImplicitNullCheck(instruction);
5425 if (is_volatile) {
5426 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5427 }
5428 // If read barriers are enabled, emit read barriers other than
5429 // Baker's using a slow path (and also unpoison the loaded
5430 // reference, if heap poisoning is enabled).
5431 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5432 }
5433 break;
5434 }
5435
5436 case Primitive::kPrimLong:
5437 if (is_volatile && !atomic_ldrd_strd) {
5438 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5439 } else {
5440 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5441 }
5442 break;
5443
5444 case Primitive::kPrimFloat:
5445 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5446 break;
5447
5448 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005449 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005450 if (is_volatile && !atomic_ldrd_strd) {
5451 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5452 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5453 GenerateWideAtomicLoad(base, offset, lo, hi);
5454 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5455 // scope.
5456 codegen_->MaybeRecordImplicitNullCheck(instruction);
5457 __ Vmov(out_dreg, lo, hi);
5458 } else {
5459 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005460 codegen_->MaybeRecordImplicitNullCheck(instruction);
5461 }
5462 break;
5463 }
5464
5465 case Primitive::kPrimVoid:
5466 LOG(FATAL) << "Unreachable type " << field_type;
5467 UNREACHABLE();
5468 }
5469
5470 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5471 // Potential implicit null checks, in the case of reference or
5472 // double fields, are handled in the previous switch statement.
5473 } else {
5474 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005475 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5476 // should use a scope and the assembler to emit the load instruction to guarantee that we
5477 // record the pc at the correct position. But the `Assembler` does not automatically handle
5478 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5479 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005480 codegen_->MaybeRecordImplicitNullCheck(instruction);
5481 }
5482
5483 if (is_volatile) {
5484 if (field_type == Primitive::kPrimNot) {
5485 // Memory barriers, in the case of references, are also handled
5486 // in the previous switch statement.
5487 } else {
5488 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5489 }
5490 }
5491}
5492
5493void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5494 HandleFieldSet(instruction, instruction->GetFieldInfo());
5495}
5496
5497void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5498 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5499}
5500
5501void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5502 HandleFieldGet(instruction, instruction->GetFieldInfo());
5503}
5504
5505void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5506 HandleFieldGet(instruction, instruction->GetFieldInfo());
5507}
5508
5509void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5510 HandleFieldGet(instruction, instruction->GetFieldInfo());
5511}
5512
5513void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5514 HandleFieldGet(instruction, instruction->GetFieldInfo());
5515}
5516
Scott Wakelingc34dba72016-10-03 10:14:44 +01005517void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5518 HandleFieldSet(instruction, instruction->GetFieldInfo());
5519}
5520
5521void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5522 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5523}
5524
Artem Serovcfbe9132016-10-14 15:58:56 +01005525void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5526 HUnresolvedInstanceFieldGet* instruction) {
5527 FieldAccessCallingConventionARMVIXL calling_convention;
5528 codegen_->CreateUnresolvedFieldLocationSummary(
5529 instruction, instruction->GetFieldType(), calling_convention);
5530}
5531
5532void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5533 HUnresolvedInstanceFieldGet* instruction) {
5534 FieldAccessCallingConventionARMVIXL calling_convention;
5535 codegen_->GenerateUnresolvedFieldAccess(instruction,
5536 instruction->GetFieldType(),
5537 instruction->GetFieldIndex(),
5538 instruction->GetDexPc(),
5539 calling_convention);
5540}
5541
5542void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5543 HUnresolvedInstanceFieldSet* instruction) {
5544 FieldAccessCallingConventionARMVIXL calling_convention;
5545 codegen_->CreateUnresolvedFieldLocationSummary(
5546 instruction, instruction->GetFieldType(), calling_convention);
5547}
5548
5549void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5550 HUnresolvedInstanceFieldSet* instruction) {
5551 FieldAccessCallingConventionARMVIXL calling_convention;
5552 codegen_->GenerateUnresolvedFieldAccess(instruction,
5553 instruction->GetFieldType(),
5554 instruction->GetFieldIndex(),
5555 instruction->GetDexPc(),
5556 calling_convention);
5557}
5558
5559void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5560 HUnresolvedStaticFieldGet* instruction) {
5561 FieldAccessCallingConventionARMVIXL calling_convention;
5562 codegen_->CreateUnresolvedFieldLocationSummary(
5563 instruction, instruction->GetFieldType(), calling_convention);
5564}
5565
5566void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5567 HUnresolvedStaticFieldGet* instruction) {
5568 FieldAccessCallingConventionARMVIXL calling_convention;
5569 codegen_->GenerateUnresolvedFieldAccess(instruction,
5570 instruction->GetFieldType(),
5571 instruction->GetFieldIndex(),
5572 instruction->GetDexPc(),
5573 calling_convention);
5574}
5575
5576void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5577 HUnresolvedStaticFieldSet* instruction) {
5578 FieldAccessCallingConventionARMVIXL calling_convention;
5579 codegen_->CreateUnresolvedFieldLocationSummary(
5580 instruction, instruction->GetFieldType(), calling_convention);
5581}
5582
5583void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5584 HUnresolvedStaticFieldSet* instruction) {
5585 FieldAccessCallingConventionARMVIXL calling_convention;
5586 codegen_->GenerateUnresolvedFieldAccess(instruction,
5587 instruction->GetFieldType(),
5588 instruction->GetFieldIndex(),
5589 instruction->GetDexPc(),
5590 calling_convention);
5591}
5592
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005593void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005594 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005595 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005596}
5597
5598void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5599 if (CanMoveNullCheckToUser(instruction)) {
5600 return;
5601 }
5602
5603 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005604 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005605 ExactAssemblyScope aas(GetVIXLAssembler(),
5606 vixl32::kMaxInstructionSizeInBytes,
5607 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005608 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5609 RecordPcInfo(instruction, instruction->GetDexPc());
5610}
5611
5612void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5613 NullCheckSlowPathARMVIXL* slow_path =
5614 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5615 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005616 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005617}
5618
5619void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5620 codegen_->GenerateNullCheck(instruction);
5621}
5622
Scott Wakelingc34dba72016-10-03 10:14:44 +01005623static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5624 switch (type) {
5625 case Primitive::kPrimNot:
5626 return kLoadWord;
5627 case Primitive::kPrimBoolean:
5628 return kLoadUnsignedByte;
5629 case Primitive::kPrimByte:
5630 return kLoadSignedByte;
5631 case Primitive::kPrimChar:
5632 return kLoadUnsignedHalfword;
5633 case Primitive::kPrimShort:
5634 return kLoadSignedHalfword;
5635 case Primitive::kPrimInt:
5636 return kLoadWord;
5637 case Primitive::kPrimLong:
5638 return kLoadWordPair;
5639 case Primitive::kPrimFloat:
5640 return kLoadSWord;
5641 case Primitive::kPrimDouble:
5642 return kLoadDWord;
5643 default:
5644 LOG(FATAL) << "Unreachable type " << type;
5645 UNREACHABLE();
5646 }
5647}
5648
5649static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5650 switch (type) {
5651 case Primitive::kPrimNot:
5652 return kStoreWord;
5653 case Primitive::kPrimBoolean:
5654 case Primitive::kPrimByte:
5655 return kStoreByte;
5656 case Primitive::kPrimChar:
5657 case Primitive::kPrimShort:
5658 return kStoreHalfword;
5659 case Primitive::kPrimInt:
5660 return kStoreWord;
5661 case Primitive::kPrimLong:
5662 return kStoreWordPair;
5663 case Primitive::kPrimFloat:
5664 return kStoreSWord;
5665 case Primitive::kPrimDouble:
5666 return kStoreDWord;
5667 default:
5668 LOG(FATAL) << "Unreachable type " << type;
5669 UNREACHABLE();
5670 }
5671}
5672
5673void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5674 Location out_loc,
5675 vixl32::Register base,
5676 vixl32::Register reg_index,
5677 vixl32::Condition cond) {
5678 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5679 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5680
5681 switch (type) {
5682 case Primitive::kPrimByte:
5683 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5684 break;
5685 case Primitive::kPrimBoolean:
5686 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5687 break;
5688 case Primitive::kPrimShort:
5689 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5690 break;
5691 case Primitive::kPrimChar:
5692 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5693 break;
5694 case Primitive::kPrimNot:
5695 case Primitive::kPrimInt:
5696 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5697 break;
5698 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5699 case Primitive::kPrimLong:
5700 case Primitive::kPrimFloat:
5701 case Primitive::kPrimDouble:
5702 default:
5703 LOG(FATAL) << "Unreachable type " << type;
5704 UNREACHABLE();
5705 }
5706}
5707
5708void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5709 Location loc,
5710 vixl32::Register base,
5711 vixl32::Register reg_index,
5712 vixl32::Condition cond) {
5713 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5714 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5715
5716 switch (type) {
5717 case Primitive::kPrimByte:
5718 case Primitive::kPrimBoolean:
5719 __ Strb(cond, RegisterFrom(loc), mem_address);
5720 break;
5721 case Primitive::kPrimShort:
5722 case Primitive::kPrimChar:
5723 __ Strh(cond, RegisterFrom(loc), mem_address);
5724 break;
5725 case Primitive::kPrimNot:
5726 case Primitive::kPrimInt:
5727 __ Str(cond, RegisterFrom(loc), mem_address);
5728 break;
5729 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5730 case Primitive::kPrimLong:
5731 case Primitive::kPrimFloat:
5732 case Primitive::kPrimDouble:
5733 default:
5734 LOG(FATAL) << "Unreachable type " << type;
5735 UNREACHABLE();
5736 }
5737}
5738
5739void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5740 bool object_array_get_with_read_barrier =
5741 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5742 LocationSummary* locations =
5743 new (GetGraph()->GetArena()) LocationSummary(instruction,
5744 object_array_get_with_read_barrier ?
5745 LocationSummary::kCallOnSlowPath :
5746 LocationSummary::kNoCall);
5747 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005748 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005749 }
5750 locations->SetInAt(0, Location::RequiresRegister());
5751 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5752 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5753 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5754 } else {
5755 // The output overlaps in the case of an object array get with
5756 // read barriers enabled: we do not want the move to overwrite the
5757 // array's location, as we need it to emit the read barrier.
5758 locations->SetOut(
5759 Location::RequiresRegister(),
5760 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5761 }
5762 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005763 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005764 // Also need for String compression feature.
5765 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5766 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005767 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005768 }
5769}
5770
5771void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005772 LocationSummary* locations = instruction->GetLocations();
5773 Location obj_loc = locations->InAt(0);
5774 vixl32::Register obj = InputRegisterAt(instruction, 0);
5775 Location index = locations->InAt(1);
5776 Location out_loc = locations->Out();
5777 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5778 Primitive::Type type = instruction->GetType();
5779 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5780 instruction->IsStringCharAt();
5781 HInstruction* array_instr = instruction->GetArray();
5782 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005783
5784 switch (type) {
5785 case Primitive::kPrimBoolean:
5786 case Primitive::kPrimByte:
5787 case Primitive::kPrimShort:
5788 case Primitive::kPrimChar:
5789 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005790 vixl32::Register length;
5791 if (maybe_compressed_char_at) {
5792 length = RegisterFrom(locations->GetTemp(0));
5793 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5794 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5795 codegen_->MaybeRecordImplicitNullCheck(instruction);
5796 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005797 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005798 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005799 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005800 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005801 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005802 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5803 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5804 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005805 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005806 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5807 RegisterFrom(out_loc),
5808 obj,
5809 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005810 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005811 __ Bind(&uncompressed_load);
5812 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5813 RegisterFrom(out_loc),
5814 obj,
5815 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005816 if (done.IsReferenced()) {
5817 __ Bind(&done);
5818 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005819 } else {
5820 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5821
5822 LoadOperandType load_type = GetLoadOperandType(type);
5823 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5824 }
5825 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005826 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005827 vixl32::Register temp = temps.Acquire();
5828
5829 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005830 // We do not need to compute the intermediate address from the array: the
5831 // input instruction has done it already. See the comment in
5832 // `TryExtractArrayAccessAddress()`.
5833 if (kIsDebugBuild) {
5834 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005835 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005836 }
5837 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005838 } else {
5839 __ Add(temp, obj, data_offset);
5840 }
5841 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005842 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005843 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005844 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5845 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5846 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005847 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005848 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005849 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005850 __ Bind(&uncompressed_load);
5851 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005852 if (done.IsReferenced()) {
5853 __ Bind(&done);
5854 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005855 } else {
5856 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5857 }
5858 }
5859 break;
5860 }
5861
5862 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005863 // The read barrier instrumentation of object ArrayGet
5864 // instructions does not support the HIntermediateAddress
5865 // instruction.
5866 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5867
Scott Wakelingc34dba72016-10-03 10:14:44 +01005868 static_assert(
5869 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5870 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5871 // /* HeapReference<Object> */ out =
5872 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5873 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005874 Location temp = locations->GetTemp(0);
5875 // Note that a potential implicit null check is handled in this
5876 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5877 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5878 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005879 } else {
5880 vixl32::Register out = OutputRegister(instruction);
5881 if (index.IsConstant()) {
5882 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005883 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005884 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005885 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5886 // we should use a scope and the assembler to emit the load instruction to guarantee that
5887 // we record the pc at the correct position. But the `Assembler` does not automatically
5888 // handle unencodable offsets. Practically, everything is fine because the helper and
5889 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005890 codegen_->MaybeRecordImplicitNullCheck(instruction);
5891 // If read barriers are enabled, emit read barriers other than
5892 // Baker's using a slow path (and also unpoison the loaded
5893 // reference, if heap poisoning is enabled).
5894 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5895 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005896 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005897 vixl32::Register temp = temps.Acquire();
5898
5899 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005900 // We do not need to compute the intermediate address from the array: the
5901 // input instruction has done it already. See the comment in
5902 // `TryExtractArrayAccessAddress()`.
5903 if (kIsDebugBuild) {
5904 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005905 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005906 }
5907 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005908 } else {
5909 __ Add(temp, obj, data_offset);
5910 }
5911 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005912 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005913 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5914 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5915 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005916 codegen_->MaybeRecordImplicitNullCheck(instruction);
5917 // If read barriers are enabled, emit read barriers other than
5918 // Baker's using a slow path (and also unpoison the loaded
5919 // reference, if heap poisoning is enabled).
5920 codegen_->MaybeGenerateReadBarrierSlow(
5921 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5922 }
5923 }
5924 break;
5925 }
5926
5927 case Primitive::kPrimLong: {
5928 if (index.IsConstant()) {
5929 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005930 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005931 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5932 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005933 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005934 vixl32::Register temp = temps.Acquire();
5935 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5936 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5937 }
5938 break;
5939 }
5940
5941 case Primitive::kPrimFloat: {
5942 vixl32::SRegister out = SRegisterFrom(out_loc);
5943 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005944 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005945 GetAssembler()->LoadSFromOffset(out, obj, offset);
5946 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005947 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005948 vixl32::Register temp = temps.Acquire();
5949 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5950 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5951 }
5952 break;
5953 }
5954
5955 case Primitive::kPrimDouble: {
5956 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005957 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005958 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5959 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005960 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005961 vixl32::Register temp = temps.Acquire();
5962 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5963 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5964 }
5965 break;
5966 }
5967
5968 case Primitive::kPrimVoid:
5969 LOG(FATAL) << "Unreachable type " << type;
5970 UNREACHABLE();
5971 }
5972
5973 if (type == Primitive::kPrimNot) {
5974 // Potential implicit null checks, in the case of reference
5975 // arrays, are handled in the previous switch statement.
5976 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005977 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5978 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005979 codegen_->MaybeRecordImplicitNullCheck(instruction);
5980 }
5981}
5982
5983void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5984 Primitive::Type value_type = instruction->GetComponentType();
5985
5986 bool needs_write_barrier =
5987 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5988 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5989
5990 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5991 instruction,
5992 may_need_runtime_call_for_type_check ?
5993 LocationSummary::kCallOnSlowPath :
5994 LocationSummary::kNoCall);
5995
5996 locations->SetInAt(0, Location::RequiresRegister());
5997 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5998 if (Primitive::IsFloatingPointType(value_type)) {
5999 locations->SetInAt(2, Location::RequiresFpuRegister());
6000 } else {
6001 locations->SetInAt(2, Location::RequiresRegister());
6002 }
6003 if (needs_write_barrier) {
6004 // Temporary registers for the write barrier.
6005 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6006 locations->AddTemp(Location::RequiresRegister());
6007 }
6008}
6009
6010void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006011 LocationSummary* locations = instruction->GetLocations();
6012 vixl32::Register array = InputRegisterAt(instruction, 0);
6013 Location index = locations->InAt(1);
6014 Primitive::Type value_type = instruction->GetComponentType();
6015 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6016 bool needs_write_barrier =
6017 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6018 uint32_t data_offset =
6019 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6020 Location value_loc = locations->InAt(2);
6021 HInstruction* array_instr = instruction->GetArray();
6022 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006023
6024 switch (value_type) {
6025 case Primitive::kPrimBoolean:
6026 case Primitive::kPrimByte:
6027 case Primitive::kPrimShort:
6028 case Primitive::kPrimChar:
6029 case Primitive::kPrimInt: {
6030 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006031 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006032 uint32_t full_offset =
6033 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6034 StoreOperandType store_type = GetStoreOperandType(value_type);
6035 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6036 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006037 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006038 vixl32::Register temp = temps.Acquire();
6039
6040 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006041 // We do not need to compute the intermediate address from the array: the
6042 // input instruction has done it already. See the comment in
6043 // `TryExtractArrayAccessAddress()`.
6044 if (kIsDebugBuild) {
6045 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006046 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006047 }
6048 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006049 } else {
6050 __ Add(temp, array, data_offset);
6051 }
6052 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6053 }
6054 break;
6055 }
6056
6057 case Primitive::kPrimNot: {
6058 vixl32::Register value = RegisterFrom(value_loc);
6059 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6060 // See the comment in instruction_simplifier_shared.cc.
6061 DCHECK(!has_intermediate_address);
6062
6063 if (instruction->InputAt(2)->IsNullConstant()) {
6064 // Just setting null.
6065 if (index.IsConstant()) {
6066 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006067 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006068 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6069 } else {
6070 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006071 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006072 vixl32::Register temp = temps.Acquire();
6073 __ Add(temp, array, data_offset);
6074 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6075 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006076 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6077 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006078 codegen_->MaybeRecordImplicitNullCheck(instruction);
6079 DCHECK(!needs_write_barrier);
6080 DCHECK(!may_need_runtime_call_for_type_check);
6081 break;
6082 }
6083
6084 DCHECK(needs_write_barrier);
6085 Location temp1_loc = locations->GetTemp(0);
6086 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6087 Location temp2_loc = locations->GetTemp(1);
6088 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6089 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6090 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6091 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6092 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006093 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006094 SlowPathCodeARMVIXL* slow_path = nullptr;
6095
6096 if (may_need_runtime_call_for_type_check) {
6097 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6098 codegen_->AddSlowPath(slow_path);
6099 if (instruction->GetValueCanBeNull()) {
6100 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006101 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006102 if (index.IsConstant()) {
6103 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006104 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006105 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6106 } else {
6107 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006108 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006109 vixl32::Register temp = temps.Acquire();
6110 __ Add(temp, array, data_offset);
6111 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6112 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006113 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6114 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006115 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006116 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006117 __ Bind(&non_zero);
6118 }
6119
6120 // Note that when read barriers are enabled, the type checks
6121 // are performed without read barriers. This is fine, even in
6122 // the case where a class object is in the from-space after
6123 // the flip, as a comparison involving such a type would not
6124 // produce a false positive; it may of course produce a false
6125 // negative, in which case we would take the ArraySet slow
6126 // path.
6127
Alexandre Rames374ddf32016-11-04 10:40:49 +00006128 {
6129 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006130 ExactAssemblyScope aas(GetVIXLAssembler(),
6131 vixl32::kMaxInstructionSizeInBytes,
6132 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006133 // /* HeapReference<Class> */ temp1 = array->klass_
6134 __ ldr(temp1, MemOperand(array, class_offset));
6135 codegen_->MaybeRecordImplicitNullCheck(instruction);
6136 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006137 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6138
6139 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6140 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6141 // /* HeapReference<Class> */ temp2 = value->klass_
6142 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6143 // If heap poisoning is enabled, no need to unpoison `temp1`
6144 // nor `temp2`, as we are comparing two poisoned references.
6145 __ Cmp(temp1, temp2);
6146
6147 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6148 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006149 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006150 // If heap poisoning is enabled, the `temp1` reference has
6151 // not been unpoisoned yet; unpoison it now.
6152 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6153
6154 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6155 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6156 // If heap poisoning is enabled, no need to unpoison
6157 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006158 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006159 __ Bind(&do_put);
6160 } else {
6161 __ B(ne, slow_path->GetEntryLabel());
6162 }
6163 }
6164
6165 vixl32::Register source = value;
6166 if (kPoisonHeapReferences) {
6167 // Note that in the case where `value` is a null reference,
6168 // we do not enter this block, as a null reference does not
6169 // need poisoning.
6170 DCHECK_EQ(value_type, Primitive::kPrimNot);
6171 __ Mov(temp1, value);
6172 GetAssembler()->PoisonHeapReference(temp1);
6173 source = temp1;
6174 }
6175
6176 if (index.IsConstant()) {
6177 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006178 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006179 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6180 } else {
6181 DCHECK(index.IsRegister()) << index;
6182
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006183 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006184 vixl32::Register temp = temps.Acquire();
6185 __ Add(temp, array, data_offset);
6186 codegen_->StoreToShiftedRegOffset(value_type,
6187 LocationFrom(source),
6188 temp,
6189 RegisterFrom(index));
6190 }
6191
6192 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006193 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6194 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006195 codegen_->MaybeRecordImplicitNullCheck(instruction);
6196 }
6197
6198 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6199
6200 if (done.IsReferenced()) {
6201 __ Bind(&done);
6202 }
6203
6204 if (slow_path != nullptr) {
6205 __ Bind(slow_path->GetExitLabel());
6206 }
6207
6208 break;
6209 }
6210
6211 case Primitive::kPrimLong: {
6212 Location value = locations->InAt(2);
6213 if (index.IsConstant()) {
6214 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006215 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006216 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6217 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006218 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006219 vixl32::Register temp = temps.Acquire();
6220 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6221 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6222 }
6223 break;
6224 }
6225
6226 case Primitive::kPrimFloat: {
6227 Location value = locations->InAt(2);
6228 DCHECK(value.IsFpuRegister());
6229 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006230 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006231 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6232 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006233 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 vixl32::Register temp = temps.Acquire();
6235 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6236 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6237 }
6238 break;
6239 }
6240
6241 case Primitive::kPrimDouble: {
6242 Location value = locations->InAt(2);
6243 DCHECK(value.IsFpuRegisterPair());
6244 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006245 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006246 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6247 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006248 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006249 vixl32::Register temp = temps.Acquire();
6250 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6251 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6252 }
6253 break;
6254 }
6255
6256 case Primitive::kPrimVoid:
6257 LOG(FATAL) << "Unreachable type " << value_type;
6258 UNREACHABLE();
6259 }
6260
6261 // Objects are handled in the switch.
6262 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006263 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6264 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006265 codegen_->MaybeRecordImplicitNullCheck(instruction);
6266 }
6267}
6268
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006269void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6270 LocationSummary* locations =
6271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6272 locations->SetInAt(0, Location::RequiresRegister());
6273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6274}
6275
6276void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6277 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6278 vixl32::Register obj = InputRegisterAt(instruction, 0);
6279 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006280 {
Artem Serov0fb37192016-12-06 18:13:40 +00006281 ExactAssemblyScope aas(GetVIXLAssembler(),
6282 vixl32::kMaxInstructionSizeInBytes,
6283 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006284 __ ldr(out, MemOperand(obj, offset));
6285 codegen_->MaybeRecordImplicitNullCheck(instruction);
6286 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006287 // Mask out compression flag from String's array length.
6288 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006289 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006290 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006291}
6292
Artem Serov2bbc9532016-10-21 11:51:50 +01006293void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006294 LocationSummary* locations =
6295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6296
6297 locations->SetInAt(0, Location::RequiresRegister());
6298 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6300}
6301
6302void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6303 vixl32::Register out = OutputRegister(instruction);
6304 vixl32::Register first = InputRegisterAt(instruction, 0);
6305 Location second = instruction->GetLocations()->InAt(1);
6306
Artem Serov2bbc9532016-10-21 11:51:50 +01006307 if (second.IsRegister()) {
6308 __ Add(out, first, RegisterFrom(second));
6309 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006310 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006311 }
6312}
6313
Scott Wakelingc34dba72016-10-03 10:14:44 +01006314void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6315 RegisterSet caller_saves = RegisterSet::Empty();
6316 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6317 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6318 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6319 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006320
6321 HInstruction* index = instruction->InputAt(0);
6322 HInstruction* length = instruction->InputAt(1);
6323 // If both index and length are constants we can statically check the bounds. But if at least one
6324 // of them is not encodable ArmEncodableConstantOrRegister will create
6325 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6326 // locations.
6327 bool both_const = index->IsConstant() && length->IsConstant();
6328 locations->SetInAt(0, both_const
6329 ? Location::ConstantLocation(index->AsConstant())
6330 : ArmEncodableConstantOrRegister(index, CMP));
6331 locations->SetInAt(1, both_const
6332 ? Location::ConstantLocation(length->AsConstant())
6333 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006334}
6335
6336void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006337 LocationSummary* locations = instruction->GetLocations();
6338 Location index_loc = locations->InAt(0);
6339 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006340
Artem Serov2dd053d2017-03-08 14:54:06 +00006341 if (length_loc.IsConstant()) {
6342 int32_t length = Int32ConstantFrom(length_loc);
6343 if (index_loc.IsConstant()) {
6344 // BCE will remove the bounds check if we are guaranteed to pass.
6345 int32_t index = Int32ConstantFrom(index_loc);
6346 if (index < 0 || index >= length) {
6347 SlowPathCodeARMVIXL* slow_path =
6348 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6349 codegen_->AddSlowPath(slow_path);
6350 __ B(slow_path->GetEntryLabel());
6351 } else {
6352 // Some optimization after BCE may have generated this, and we should not
6353 // generate a bounds check if it is a valid range.
6354 }
6355 return;
6356 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006357
Artem Serov2dd053d2017-03-08 14:54:06 +00006358 SlowPathCodeARMVIXL* slow_path =
6359 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6360 __ Cmp(RegisterFrom(index_loc), length);
6361 codegen_->AddSlowPath(slow_path);
6362 __ B(hs, slow_path->GetEntryLabel());
6363 } else {
6364 SlowPathCodeARMVIXL* slow_path =
6365 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6366 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6367 codegen_->AddSlowPath(slow_path);
6368 __ B(ls, slow_path->GetEntryLabel());
6369 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006370}
6371
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006372void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6373 vixl32::Register card,
6374 vixl32::Register object,
6375 vixl32::Register value,
6376 bool can_be_null) {
6377 vixl32::Label is_null;
6378 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006379 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006380 }
6381 GetAssembler()->LoadFromOffset(
6382 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006383 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006384 __ Strb(card, MemOperand(card, temp));
6385 if (can_be_null) {
6386 __ Bind(&is_null);
6387 }
6388}
6389
Scott Wakelingfe885462016-09-22 10:24:38 +01006390void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6391 LOG(FATAL) << "Unreachable";
6392}
6393
6394void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6395 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6396}
6397
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006398void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006399 LocationSummary* locations =
6400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6401 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006402}
6403
6404void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6405 HBasicBlock* block = instruction->GetBlock();
6406 if (block->GetLoopInformation() != nullptr) {
6407 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6408 // The back edge will generate the suspend check.
6409 return;
6410 }
6411 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6412 // The goto will generate the suspend check.
6413 return;
6414 }
6415 GenerateSuspendCheck(instruction, nullptr);
6416}
6417
6418void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6419 HBasicBlock* successor) {
6420 SuspendCheckSlowPathARMVIXL* slow_path =
6421 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6422 if (slow_path == nullptr) {
6423 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6424 instruction->SetSlowPath(slow_path);
6425 codegen_->AddSlowPath(slow_path);
6426 if (successor != nullptr) {
6427 DCHECK(successor->IsLoopHeader());
6428 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6429 }
6430 } else {
6431 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6432 }
6433
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006434 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006435 vixl32::Register temp = temps.Acquire();
6436 GetAssembler()->LoadFromOffset(
6437 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6438 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006439 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006440 __ Bind(slow_path->GetReturnLabel());
6441 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006442 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006443 __ B(slow_path->GetEntryLabel());
6444 }
6445}
6446
Scott Wakelingfe885462016-09-22 10:24:38 +01006447ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6448 return codegen_->GetAssembler();
6449}
6450
6451void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006452 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006453 MoveOperands* move = moves_[index];
6454 Location source = move->GetSource();
6455 Location destination = move->GetDestination();
6456
6457 if (source.IsRegister()) {
6458 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006459 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006460 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006461 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006462 } else {
6463 DCHECK(destination.IsStackSlot());
6464 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006465 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006466 sp,
6467 destination.GetStackIndex());
6468 }
6469 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006470 if (destination.IsRegister()) {
6471 GetAssembler()->LoadFromOffset(kLoadWord,
6472 RegisterFrom(destination),
6473 sp,
6474 source.GetStackIndex());
6475 } else if (destination.IsFpuRegister()) {
6476 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6477 } else {
6478 DCHECK(destination.IsStackSlot());
6479 vixl32::Register temp = temps.Acquire();
6480 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6481 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6482 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006483 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006484 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006486 } else if (destination.IsFpuRegister()) {
6487 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6488 } else {
6489 DCHECK(destination.IsStackSlot());
6490 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6491 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006492 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006493 if (destination.IsDoubleStackSlot()) {
6494 vixl32::DRegister temp = temps.AcquireD();
6495 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6496 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6497 } else if (destination.IsRegisterPair()) {
6498 DCHECK(ExpectedPairLayout(destination));
6499 GetAssembler()->LoadFromOffset(
6500 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6501 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006502 DCHECK(destination.IsFpuRegisterPair()) << destination;
6503 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006504 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006505 } else if (source.IsRegisterPair()) {
6506 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006507 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6508 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006509 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006511 } else {
6512 DCHECK(destination.IsDoubleStackSlot()) << destination;
6513 DCHECK(ExpectedPairLayout(source));
6514 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006515 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006516 sp,
6517 destination.GetStackIndex());
6518 }
6519 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006520 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006521 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006522 } else if (destination.IsFpuRegisterPair()) {
6523 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6524 } else {
6525 DCHECK(destination.IsDoubleStackSlot()) << destination;
6526 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6527 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006528 } else {
6529 DCHECK(source.IsConstant()) << source;
6530 HConstant* constant = source.GetConstant();
6531 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6532 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6533 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006534 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006535 } else {
6536 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006537 vixl32::Register temp = temps.Acquire();
6538 __ Mov(temp, value);
6539 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6540 }
6541 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006542 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006543 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006544 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6545 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006546 } else {
6547 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006548 vixl32::Register temp = temps.Acquire();
6549 __ Mov(temp, Low32Bits(value));
6550 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6551 __ Mov(temp, High32Bits(value));
6552 GetAssembler()->StoreToOffset(kStoreWord,
6553 temp,
6554 sp,
6555 destination.GetHighStackIndex(kArmWordSize));
6556 }
6557 } else if (constant->IsDoubleConstant()) {
6558 double value = constant->AsDoubleConstant()->GetValue();
6559 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006560 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006561 } else {
6562 DCHECK(destination.IsDoubleStackSlot()) << destination;
6563 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006564 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006565 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006566 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006567 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006568 GetAssembler()->StoreToOffset(kStoreWord,
6569 temp,
6570 sp,
6571 destination.GetHighStackIndex(kArmWordSize));
6572 }
6573 } else {
6574 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6575 float value = constant->AsFloatConstant()->GetValue();
6576 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006577 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006578 } else {
6579 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006580 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006581 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006582 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6583 }
6584 }
6585 }
6586}
6587
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006588void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6589 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6590 vixl32::Register temp = temps.Acquire();
6591 __ Mov(temp, reg);
6592 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6593 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006594}
6595
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006596void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6597 // TODO(VIXL32): Double check the performance of this implementation.
6598 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006599 vixl32::Register temp1 = temps.Acquire();
6600 ScratchRegisterScope ensure_scratch(
6601 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6602 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006603
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006604 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6605 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6606 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6607 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6608 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006609}
6610
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006611void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6612 MoveOperands* move = moves_[index];
6613 Location source = move->GetSource();
6614 Location destination = move->GetDestination();
6615 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6616
6617 if (source.IsRegister() && destination.IsRegister()) {
6618 vixl32::Register temp = temps.Acquire();
6619 DCHECK(!RegisterFrom(source).Is(temp));
6620 DCHECK(!RegisterFrom(destination).Is(temp));
6621 __ Mov(temp, RegisterFrom(destination));
6622 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6623 __ Mov(RegisterFrom(source), temp);
6624 } else if (source.IsRegister() && destination.IsStackSlot()) {
6625 Exchange(RegisterFrom(source), destination.GetStackIndex());
6626 } else if (source.IsStackSlot() && destination.IsRegister()) {
6627 Exchange(RegisterFrom(destination), source.GetStackIndex());
6628 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006629 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006630 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006631 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006632 __ Vmov(temp, SRegisterFrom(source));
6633 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6634 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006635 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6636 vixl32::DRegister temp = temps.AcquireD();
6637 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6638 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6639 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6640 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6641 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6642 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6643 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6644 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6645 vixl32::DRegister temp = temps.AcquireD();
6646 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6647 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6648 GetAssembler()->StoreDToOffset(temp, sp, mem);
6649 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006650 vixl32::DRegister first = DRegisterFrom(source);
6651 vixl32::DRegister second = DRegisterFrom(destination);
6652 vixl32::DRegister temp = temps.AcquireD();
6653 __ Vmov(temp, first);
6654 __ Vmov(first, second);
6655 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006656 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006657 vixl32::DRegister reg = source.IsFpuRegisterPair()
6658 ? DRegisterFrom(source)
6659 : DRegisterFrom(destination);
6660 int mem = source.IsFpuRegisterPair()
6661 ? destination.GetStackIndex()
6662 : source.GetStackIndex();
6663 vixl32::DRegister temp = temps.AcquireD();
6664 __ Vmov(temp, reg);
6665 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6666 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006667 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006668 vixl32::SRegister reg = source.IsFpuRegister()
6669 ? SRegisterFrom(source)
6670 : SRegisterFrom(destination);
6671 int mem = source.IsFpuRegister()
6672 ? destination.GetStackIndex()
6673 : source.GetStackIndex();
6674 vixl32::Register temp = temps.Acquire();
6675 __ Vmov(temp, reg);
6676 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6677 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006678 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6679 vixl32::DRegister temp1 = temps.AcquireD();
6680 vixl32::DRegister temp2 = temps.AcquireD();
6681 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6682 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6683 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6684 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6685 } else {
6686 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6687 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006688}
6689
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006690void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6691 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006692}
6693
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006694void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6695 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006696}
6697
Artem Serov02d37832016-10-25 15:25:33 +01006698HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006699 HLoadClass::LoadKind desired_class_load_kind) {
6700 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006701 case HLoadClass::LoadKind::kInvalid:
6702 LOG(FATAL) << "UNREACHABLE";
6703 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006704 case HLoadClass::LoadKind::kReferrersClass:
6705 break;
6706 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006707 DCHECK(!GetCompilerOptions().GetCompilePic());
6708 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006709 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6710 DCHECK(GetCompilerOptions().GetCompilePic());
6711 break;
6712 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006713 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006714 case HLoadClass::LoadKind::kBssEntry:
6715 DCHECK(!Runtime::Current()->UseJitCompilation());
6716 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006717 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006718 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006719 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006720 case HLoadClass::LoadKind::kDexCacheViaMethod:
6721 break;
6722 }
6723 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006724}
6725
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006726void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006727 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6728 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006729 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006730 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006731 cls,
6732 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006733 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006734 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006735 return;
6736 }
Vladimir Marko41559982017-01-06 14:04:23 +00006737 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006738
Artem Serovd4cc5b22016-11-04 11:19:09 +00006739 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6740 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006741 ? LocationSummary::kCallOnSlowPath
6742 : LocationSummary::kNoCall;
6743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006744 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006745 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006746 }
6747
Vladimir Marko41559982017-01-06 14:04:23 +00006748 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006749 locations->SetInAt(0, Location::RequiresRegister());
6750 }
6751 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006752 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6753 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6754 // Rely on the type resolution or initialization and marking to save everything we need.
6755 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6756 // to the custom calling convention) or by marking, so we request a different temp.
6757 locations->AddTemp(Location::RequiresRegister());
6758 RegisterSet caller_saves = RegisterSet::Empty();
6759 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6760 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6761 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6762 // that the the kPrimNot result register is the same as the first argument register.
6763 locations->SetCustomSlowPathCallerSaves(caller_saves);
6764 } else {
6765 // For non-Baker read barrier we have a temp-clobbering call.
6766 }
6767 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006768}
6769
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006770// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6771// move.
6772void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006773 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6774 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6775 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006776 return;
6777 }
Vladimir Marko41559982017-01-06 14:04:23 +00006778 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006779
Vladimir Marko41559982017-01-06 14:04:23 +00006780 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006781 Location out_loc = locations->Out();
6782 vixl32::Register out = OutputRegister(cls);
6783
Artem Serovd4cc5b22016-11-04 11:19:09 +00006784 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6785 ? kWithoutReadBarrier
6786 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006787 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006788 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006789 case HLoadClass::LoadKind::kReferrersClass: {
6790 DCHECK(!cls->CanCallRuntime());
6791 DCHECK(!cls->MustGenerateClinitCheck());
6792 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6793 vixl32::Register current_method = InputRegisterAt(cls, 0);
6794 GenerateGcRootFieldLoad(cls,
6795 out_loc,
6796 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006797 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006798 read_barrier_option);
6799 break;
6800 }
6801 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006802 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006803 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6804 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6805 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006806 break;
6807 }
6808 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006809 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006810 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6811 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6812 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6813 codegen_->EmitMovwMovtPlaceholder(labels, out);
6814 break;
6815 }
6816 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006817 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006818 uint32_t address = dchecked_integral_cast<uint32_t>(
6819 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6820 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006821 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006822 break;
6823 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006824 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006825 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6826 ? RegisterFrom(locations->GetTemp(0))
6827 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006828 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006829 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006830 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6831 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006832 generate_null_check = true;
6833 break;
6834 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006835 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006836 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6837 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006838 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006839 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006840 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006841 break;
6842 }
Vladimir Marko41559982017-01-06 14:04:23 +00006843 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006844 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006845 LOG(FATAL) << "UNREACHABLE";
6846 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006847 }
6848
6849 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6850 DCHECK(cls->CanCallRuntime());
6851 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6852 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6853 codegen_->AddSlowPath(slow_path);
6854 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006855 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006856 }
6857 if (cls->MustGenerateClinitCheck()) {
6858 GenerateClassInitializationCheck(slow_path, out);
6859 } else {
6860 __ Bind(slow_path->GetExitLabel());
6861 }
6862 }
6863}
6864
Artem Serov02d37832016-10-25 15:25:33 +01006865void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6866 LocationSummary* locations =
6867 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6868 locations->SetInAt(0, Location::RequiresRegister());
6869 if (check->HasUses()) {
6870 locations->SetOut(Location::SameAsFirstInput());
6871 }
6872}
6873
6874void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6875 // We assume the class is not null.
6876 LoadClassSlowPathARMVIXL* slow_path =
6877 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6878 check,
6879 check->GetDexPc(),
6880 /* do_clinit */ true);
6881 codegen_->AddSlowPath(slow_path);
6882 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6883}
6884
6885void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6886 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6887 UseScratchRegisterScope temps(GetVIXLAssembler());
6888 vixl32::Register temp = temps.Acquire();
6889 GetAssembler()->LoadFromOffset(kLoadWord,
6890 temp,
6891 class_reg,
6892 mirror::Class::StatusOffset().Int32Value());
6893 __ Cmp(temp, mirror::Class::kStatusInitialized);
6894 __ B(lt, slow_path->GetEntryLabel());
6895 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6896 // properly. Therefore, we do a memory fence.
6897 __ Dmb(ISH);
6898 __ Bind(slow_path->GetExitLabel());
6899}
6900
Artem Serov02d37832016-10-25 15:25:33 +01006901HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006902 HLoadString::LoadKind desired_string_load_kind) {
6903 switch (desired_string_load_kind) {
6904 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006905 DCHECK(!GetCompilerOptions().GetCompilePic());
6906 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006907 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6908 DCHECK(GetCompilerOptions().GetCompilePic());
6909 break;
6910 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006911 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006912 case HLoadString::LoadKind::kBssEntry:
6913 DCHECK(!Runtime::Current()->UseJitCompilation());
6914 break;
6915 case HLoadString::LoadKind::kJitTableAddress:
6916 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006917 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006918 case HLoadString::LoadKind::kDexCacheViaMethod:
6919 break;
6920 }
6921 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006922}
6923
6924void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006925 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006926 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006927 HLoadString::LoadKind load_kind = load->GetLoadKind();
6928 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006929 locations->SetOut(LocationFrom(r0));
6930 } else {
6931 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006932 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6933 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006934 // Rely on the pResolveString and marking to save everything we need, including temps.
6935 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6936 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006937 locations->AddTemp(Location::RequiresRegister());
6938 RegisterSet caller_saves = RegisterSet::Empty();
6939 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6940 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6941 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6942 // that the the kPrimNot result register is the same as the first argument register.
6943 locations->SetCustomSlowPathCallerSaves(caller_saves);
6944 } else {
6945 // For non-Baker read barrier we have a temp-clobbering call.
6946 }
6947 }
Artem Serov02d37832016-10-25 15:25:33 +01006948 }
6949}
6950
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006951// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6952// move.
6953void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006954 LocationSummary* locations = load->GetLocations();
6955 Location out_loc = locations->Out();
6956 vixl32::Register out = OutputRegister(load);
6957 HLoadString::LoadKind load_kind = load->GetLoadKind();
6958
6959 switch (load_kind) {
6960 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006961 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6962 load->GetStringIndex()));
6963 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006964 }
6965 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6966 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6967 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006968 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006969 codegen_->EmitMovwMovtPlaceholder(labels, out);
6970 return; // No dex cache slow path.
6971 }
6972 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006973 uint32_t address = dchecked_integral_cast<uint32_t>(
6974 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6975 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006976 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6977 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006978 }
6979 case HLoadString::LoadKind::kBssEntry: {
6980 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006981 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6982 ? RegisterFrom(locations->GetTemp(0))
6983 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006984 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006985 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006986 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6987 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6988 LoadStringSlowPathARMVIXL* slow_path =
6989 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6990 codegen_->AddSlowPath(slow_path);
6991 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6992 __ Bind(slow_path->GetExitLabel());
6993 return;
6994 }
6995 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006996 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006997 load->GetStringIndex(),
6998 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006999 // /* GcRoot<mirror::String> */ out = *out
7000 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7001 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007002 }
7003 default:
7004 break;
7005 }
Artem Serov02d37832016-10-25 15:25:33 +01007006
7007 // TODO: Re-add the compiler code to do string dex cache lookup again.
7008 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
7009 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007010 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007011 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7012 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
7013}
7014
7015static int32_t GetExceptionTlsOffset() {
7016 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7017}
7018
7019void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7020 LocationSummary* locations =
7021 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7022 locations->SetOut(Location::RequiresRegister());
7023}
7024
7025void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7026 vixl32::Register out = OutputRegister(load);
7027 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7028}
7029
7030
7031void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7032 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7033}
7034
7035void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7036 UseScratchRegisterScope temps(GetVIXLAssembler());
7037 vixl32::Register temp = temps.Acquire();
7038 __ Mov(temp, 0);
7039 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7040}
7041
7042void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7043 LocationSummary* locations =
7044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7045 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7046 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7047}
7048
7049void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7050 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7051 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7052}
7053
Artem Serov657022c2016-11-23 14:19:38 +00007054// Temp is used for read barrier.
7055static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7056 if (kEmitCompilerReadBarrier &&
7057 (kUseBakerReadBarrier ||
7058 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7059 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7060 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7061 return 1;
7062 }
7063 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007064}
7065
Artem Serov657022c2016-11-23 14:19:38 +00007066// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7067// interface pointer, one for loading the current interface.
7068// The other checks have one temp for loading the object's class.
7069static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7070 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7071 return 3;
7072 }
7073 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7074}
Artem Serovcfbe9132016-10-14 15:58:56 +01007075
7076void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7077 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7078 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7079 bool baker_read_barrier_slow_path = false;
7080 switch (type_check_kind) {
7081 case TypeCheckKind::kExactCheck:
7082 case TypeCheckKind::kAbstractClassCheck:
7083 case TypeCheckKind::kClassHierarchyCheck:
7084 case TypeCheckKind::kArrayObjectCheck:
7085 call_kind =
7086 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7087 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7088 break;
7089 case TypeCheckKind::kArrayCheck:
7090 case TypeCheckKind::kUnresolvedCheck:
7091 case TypeCheckKind::kInterfaceCheck:
7092 call_kind = LocationSummary::kCallOnSlowPath;
7093 break;
7094 }
7095
7096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7097 if (baker_read_barrier_slow_path) {
7098 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7099 }
7100 locations->SetInAt(0, Location::RequiresRegister());
7101 locations->SetInAt(1, Location::RequiresRegister());
7102 // The "out" register is used as a temporary, so it overlaps with the inputs.
7103 // Note that TypeCheckSlowPathARM uses this register too.
7104 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007105 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007106}
7107
7108void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7109 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7110 LocationSummary* locations = instruction->GetLocations();
7111 Location obj_loc = locations->InAt(0);
7112 vixl32::Register obj = InputRegisterAt(instruction, 0);
7113 vixl32::Register cls = InputRegisterAt(instruction, 1);
7114 Location out_loc = locations->Out();
7115 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007116 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7117 DCHECK_LE(num_temps, 1u);
7118 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007119 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7120 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7121 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7122 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007123 vixl32::Label done;
7124 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007125 SlowPathCodeARMVIXL* slow_path = nullptr;
7126
7127 // Return 0 if `obj` is null.
7128 // avoid null check if we know obj is not null.
7129 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007130 DCHECK(!out.Is(obj));
7131 __ Mov(out, 0);
7132 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007133 }
7134
Artem Serovcfbe9132016-10-14 15:58:56 +01007135 switch (type_check_kind) {
7136 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007137 // /* HeapReference<Class> */ out = obj->klass_
7138 GenerateReferenceLoadTwoRegisters(instruction,
7139 out_loc,
7140 obj_loc,
7141 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007142 maybe_temp_loc,
7143 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007144 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007145 __ Cmp(out, cls);
7146 // We speculatively set the result to false without changing the condition
7147 // flags, which allows us to avoid some branching later.
7148 __ Mov(LeaveFlags, out, 0);
7149
7150 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7151 // we check that the output is in a low register, so that a 16-bit MOV
7152 // encoding can be used.
7153 if (out.IsLow()) {
7154 // We use the scope because of the IT block that follows.
7155 ExactAssemblyScope guard(GetVIXLAssembler(),
7156 2 * vixl32::k16BitT32InstructionSizeInBytes,
7157 CodeBufferCheckScope::kExactSize);
7158
7159 __ it(eq);
7160 __ mov(eq, out, 1);
7161 } else {
7162 __ B(ne, final_label, /* far_target */ false);
7163 __ Mov(out, 1);
7164 }
7165
Artem Serovcfbe9132016-10-14 15:58:56 +01007166 break;
7167 }
7168
7169 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007170 // /* HeapReference<Class> */ out = obj->klass_
7171 GenerateReferenceLoadTwoRegisters(instruction,
7172 out_loc,
7173 obj_loc,
7174 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007175 maybe_temp_loc,
7176 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007177 // If the class is abstract, we eagerly fetch the super class of the
7178 // object to avoid doing a comparison we know will fail.
7179 vixl32::Label loop;
7180 __ Bind(&loop);
7181 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007182 GenerateReferenceLoadOneRegister(instruction,
7183 out_loc,
7184 super_offset,
7185 maybe_temp_loc,
7186 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007187 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007188 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007189 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007190 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007191 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007192 break;
7193 }
7194
7195 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007196 // /* HeapReference<Class> */ out = obj->klass_
7197 GenerateReferenceLoadTwoRegisters(instruction,
7198 out_loc,
7199 obj_loc,
7200 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007201 maybe_temp_loc,
7202 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007203 // Walk over the class hierarchy to find a match.
7204 vixl32::Label loop, success;
7205 __ Bind(&loop);
7206 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007207 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007208 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007209 GenerateReferenceLoadOneRegister(instruction,
7210 out_loc,
7211 super_offset,
7212 maybe_temp_loc,
7213 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007214 // This is essentially a null check, but it sets the condition flags to the
7215 // proper value for the code that follows the loop, i.e. not `eq`.
7216 __ Cmp(out, 1);
7217 __ B(hs, &loop, /* far_target */ false);
7218
7219 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7220 // we check that the output is in a low register, so that a 16-bit MOV
7221 // encoding can be used.
7222 if (out.IsLow()) {
7223 // If `out` is null, we use it for the result, and the condition flags
7224 // have already been set to `ne`, so the IT block that comes afterwards
7225 // (and which handles the successful case) turns into a NOP (instead of
7226 // overwriting `out`).
7227 __ Bind(&success);
7228
7229 // We use the scope because of the IT block that follows.
7230 ExactAssemblyScope guard(GetVIXLAssembler(),
7231 2 * vixl32::k16BitT32InstructionSizeInBytes,
7232 CodeBufferCheckScope::kExactSize);
7233
7234 // There is only one branch to the `success` label (which is bound to this
7235 // IT block), and it has the same condition, `eq`, so in that case the MOV
7236 // is executed.
7237 __ it(eq);
7238 __ mov(eq, out, 1);
7239 } else {
7240 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007241 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007242 __ Bind(&success);
7243 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007244 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007245
Artem Serovcfbe9132016-10-14 15:58:56 +01007246 break;
7247 }
7248
7249 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007250 // /* HeapReference<Class> */ out = obj->klass_
7251 GenerateReferenceLoadTwoRegisters(instruction,
7252 out_loc,
7253 obj_loc,
7254 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007255 maybe_temp_loc,
7256 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007257 // Do an exact check.
7258 vixl32::Label exact_check;
7259 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007260 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007261 // Otherwise, we need to check that the object's class is a non-primitive array.
7262 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007263 GenerateReferenceLoadOneRegister(instruction,
7264 out_loc,
7265 component_offset,
7266 maybe_temp_loc,
7267 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007268 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007269 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007270 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7271 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007272 __ Cmp(out, 0);
7273 // We speculatively set the result to false without changing the condition
7274 // flags, which allows us to avoid some branching later.
7275 __ Mov(LeaveFlags, out, 0);
7276
7277 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7278 // we check that the output is in a low register, so that a 16-bit MOV
7279 // encoding can be used.
7280 if (out.IsLow()) {
7281 __ Bind(&exact_check);
7282
7283 // We use the scope because of the IT block that follows.
7284 ExactAssemblyScope guard(GetVIXLAssembler(),
7285 2 * vixl32::k16BitT32InstructionSizeInBytes,
7286 CodeBufferCheckScope::kExactSize);
7287
7288 __ it(eq);
7289 __ mov(eq, out, 1);
7290 } else {
7291 __ B(ne, final_label, /* far_target */ false);
7292 __ Bind(&exact_check);
7293 __ Mov(out, 1);
7294 }
7295
Artem Serovcfbe9132016-10-14 15:58:56 +01007296 break;
7297 }
7298
7299 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007300 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007301 // /* HeapReference<Class> */ out = obj->klass_
7302 GenerateReferenceLoadTwoRegisters(instruction,
7303 out_loc,
7304 obj_loc,
7305 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007306 maybe_temp_loc,
7307 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007308 __ Cmp(out, cls);
7309 DCHECK(locations->OnlyCallsOnSlowPath());
7310 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7311 /* is_fatal */ false);
7312 codegen_->AddSlowPath(slow_path);
7313 __ B(ne, slow_path->GetEntryLabel());
7314 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007315 break;
7316 }
7317
7318 case TypeCheckKind::kUnresolvedCheck:
7319 case TypeCheckKind::kInterfaceCheck: {
7320 // Note that we indeed only call on slow path, but we always go
7321 // into the slow path for the unresolved and interface check
7322 // cases.
7323 //
7324 // We cannot directly call the InstanceofNonTrivial runtime
7325 // entry point without resorting to a type checking slow path
7326 // here (i.e. by calling InvokeRuntime directly), as it would
7327 // require to assign fixed registers for the inputs of this
7328 // HInstanceOf instruction (following the runtime calling
7329 // convention), which might be cluttered by the potential first
7330 // read barrier emission at the beginning of this method.
7331 //
7332 // TODO: Introduce a new runtime entry point taking the object
7333 // to test (instead of its class) as argument, and let it deal
7334 // with the read barrier issues. This will let us refactor this
7335 // case of the `switch` code as it was previously (with a direct
7336 // call to the runtime not using a type checking slow path).
7337 // This should also be beneficial for the other cases above.
7338 DCHECK(locations->OnlyCallsOnSlowPath());
7339 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7340 /* is_fatal */ false);
7341 codegen_->AddSlowPath(slow_path);
7342 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007343 break;
7344 }
7345 }
7346
Artem Serovcfbe9132016-10-14 15:58:56 +01007347 if (done.IsReferenced()) {
7348 __ Bind(&done);
7349 }
7350
7351 if (slow_path != nullptr) {
7352 __ Bind(slow_path->GetExitLabel());
7353 }
7354}
7355
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007356void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7357 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7358 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7359
7360 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7361 switch (type_check_kind) {
7362 case TypeCheckKind::kExactCheck:
7363 case TypeCheckKind::kAbstractClassCheck:
7364 case TypeCheckKind::kClassHierarchyCheck:
7365 case TypeCheckKind::kArrayObjectCheck:
7366 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7367 LocationSummary::kCallOnSlowPath :
7368 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7369 break;
7370 case TypeCheckKind::kArrayCheck:
7371 case TypeCheckKind::kUnresolvedCheck:
7372 case TypeCheckKind::kInterfaceCheck:
7373 call_kind = LocationSummary::kCallOnSlowPath;
7374 break;
7375 }
7376
7377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7378 locations->SetInAt(0, Location::RequiresRegister());
7379 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007380 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007381}
7382
7383void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7384 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7385 LocationSummary* locations = instruction->GetLocations();
7386 Location obj_loc = locations->InAt(0);
7387 vixl32::Register obj = InputRegisterAt(instruction, 0);
7388 vixl32::Register cls = InputRegisterAt(instruction, 1);
7389 Location temp_loc = locations->GetTemp(0);
7390 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007391 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7392 DCHECK_LE(num_temps, 3u);
7393 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7394 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7395 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7396 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7397 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7398 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7399 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7400 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7401 const uint32_t object_array_data_offset =
7402 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007403
Artem Serov657022c2016-11-23 14:19:38 +00007404 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7405 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7406 // read barriers is done for performance and code size reasons.
7407 bool is_type_check_slow_path_fatal = false;
7408 if (!kEmitCompilerReadBarrier) {
7409 is_type_check_slow_path_fatal =
7410 (type_check_kind == TypeCheckKind::kExactCheck ||
7411 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7412 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7413 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7414 !instruction->CanThrowIntoCatchBlock();
7415 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007416 SlowPathCodeARMVIXL* type_check_slow_path =
7417 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7418 is_type_check_slow_path_fatal);
7419 codegen_->AddSlowPath(type_check_slow_path);
7420
7421 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007422 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007423 // Avoid null check if we know obj is not null.
7424 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007425 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007426 }
7427
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007428 switch (type_check_kind) {
7429 case TypeCheckKind::kExactCheck:
7430 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007431 // /* HeapReference<Class> */ temp = obj->klass_
7432 GenerateReferenceLoadTwoRegisters(instruction,
7433 temp_loc,
7434 obj_loc,
7435 class_offset,
7436 maybe_temp2_loc,
7437 kWithoutReadBarrier);
7438
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007439 __ Cmp(temp, cls);
7440 // Jump to slow path for throwing the exception or doing a
7441 // more involved array check.
7442 __ B(ne, type_check_slow_path->GetEntryLabel());
7443 break;
7444 }
7445
7446 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007447 // /* HeapReference<Class> */ temp = obj->klass_
7448 GenerateReferenceLoadTwoRegisters(instruction,
7449 temp_loc,
7450 obj_loc,
7451 class_offset,
7452 maybe_temp2_loc,
7453 kWithoutReadBarrier);
7454
Artem Serovcfbe9132016-10-14 15:58:56 +01007455 // If the class is abstract, we eagerly fetch the super class of the
7456 // object to avoid doing a comparison we know will fail.
7457 vixl32::Label loop;
7458 __ Bind(&loop);
7459 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007460 GenerateReferenceLoadOneRegister(instruction,
7461 temp_loc,
7462 super_offset,
7463 maybe_temp2_loc,
7464 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007465
7466 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7467 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007468 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007469
7470 // Otherwise, compare the classes.
7471 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007472 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007473 break;
7474 }
7475
7476 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007477 // /* HeapReference<Class> */ temp = obj->klass_
7478 GenerateReferenceLoadTwoRegisters(instruction,
7479 temp_loc,
7480 obj_loc,
7481 class_offset,
7482 maybe_temp2_loc,
7483 kWithoutReadBarrier);
7484
Artem Serovcfbe9132016-10-14 15:58:56 +01007485 // Walk over the class hierarchy to find a match.
7486 vixl32::Label loop;
7487 __ Bind(&loop);
7488 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007489 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007490
7491 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007492 GenerateReferenceLoadOneRegister(instruction,
7493 temp_loc,
7494 super_offset,
7495 maybe_temp2_loc,
7496 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007497
7498 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7499 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007500 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007501 // Otherwise, jump to the beginning of the loop.
7502 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007503 break;
7504 }
7505
Artem Serovcfbe9132016-10-14 15:58:56 +01007506 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007507 // /* HeapReference<Class> */ temp = obj->klass_
7508 GenerateReferenceLoadTwoRegisters(instruction,
7509 temp_loc,
7510 obj_loc,
7511 class_offset,
7512 maybe_temp2_loc,
7513 kWithoutReadBarrier);
7514
Artem Serovcfbe9132016-10-14 15:58:56 +01007515 // Do an exact check.
7516 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007517 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007518
7519 // Otherwise, we need to check that the object's class is a non-primitive array.
7520 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007521 GenerateReferenceLoadOneRegister(instruction,
7522 temp_loc,
7523 component_offset,
7524 maybe_temp2_loc,
7525 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007526 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007527 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007528 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7529 // to further check that this component type is not a primitive type.
7530 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7531 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007532 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007533 break;
7534 }
7535
7536 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007537 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007538 // We cannot directly call the CheckCast runtime entry point
7539 // without resorting to a type checking slow path here (i.e. by
7540 // calling InvokeRuntime directly), as it would require to
7541 // assign fixed registers for the inputs of this HInstanceOf
7542 // instruction (following the runtime calling convention), which
7543 // might be cluttered by the potential first read barrier
7544 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007545
Artem Serovcfbe9132016-10-14 15:58:56 +01007546 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007547 break;
Artem Serov657022c2016-11-23 14:19:38 +00007548
7549 case TypeCheckKind::kInterfaceCheck: {
7550 // Avoid read barriers to improve performance of the fast path. We can not get false
7551 // positives by doing this.
7552 // /* HeapReference<Class> */ temp = obj->klass_
7553 GenerateReferenceLoadTwoRegisters(instruction,
7554 temp_loc,
7555 obj_loc,
7556 class_offset,
7557 maybe_temp2_loc,
7558 kWithoutReadBarrier);
7559
7560 // /* HeapReference<Class> */ temp = temp->iftable_
7561 GenerateReferenceLoadTwoRegisters(instruction,
7562 temp_loc,
7563 temp_loc,
7564 iftable_offset,
7565 maybe_temp2_loc,
7566 kWithoutReadBarrier);
7567 // Iftable is never null.
7568 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7569 // Loop through the iftable and check if any class matches.
7570 vixl32::Label start_loop;
7571 __ Bind(&start_loop);
7572 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7573 type_check_slow_path->GetEntryLabel());
7574 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7575 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7576 // Go to next interface.
7577 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7578 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7579 // Compare the classes and continue the loop if they do not match.
7580 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007581 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007582 break;
7583 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007584 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007585 if (done.IsReferenced()) {
7586 __ Bind(&done);
7587 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007588
7589 __ Bind(type_check_slow_path->GetExitLabel());
7590}
7591
Artem Serov551b28f2016-10-18 19:11:30 +01007592void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7593 LocationSummary* locations =
7594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7595 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7596 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7597}
7598
7599void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7600 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7601 instruction,
7602 instruction->GetDexPc());
7603 if (instruction->IsEnter()) {
7604 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7605 } else {
7606 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7607 }
7608}
7609
Artem Serov02109dd2016-09-23 17:17:54 +01007610void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7611 HandleBitwiseOperation(instruction, AND);
7612}
7613
7614void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7615 HandleBitwiseOperation(instruction, ORR);
7616}
7617
7618void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7619 HandleBitwiseOperation(instruction, EOR);
7620}
7621
7622void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7623 LocationSummary* locations =
7624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7625 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7626 || instruction->GetResultType() == Primitive::kPrimLong);
7627 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7628 locations->SetInAt(0, Location::RequiresRegister());
7629 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7631}
7632
7633void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7634 HandleBitwiseOperation(instruction);
7635}
7636
7637void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7638 HandleBitwiseOperation(instruction);
7639}
7640
7641void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7642 HandleBitwiseOperation(instruction);
7643}
7644
Artem Serov2bbc9532016-10-21 11:51:50 +01007645void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7646 LocationSummary* locations =
7647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7648 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7649 || instruction->GetResultType() == Primitive::kPrimLong);
7650
7651 locations->SetInAt(0, Location::RequiresRegister());
7652 locations->SetInAt(1, Location::RequiresRegister());
7653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7654}
7655
7656void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7657 LocationSummary* locations = instruction->GetLocations();
7658 Location first = locations->InAt(0);
7659 Location second = locations->InAt(1);
7660 Location out = locations->Out();
7661
7662 if (instruction->GetResultType() == Primitive::kPrimInt) {
7663 vixl32::Register first_reg = RegisterFrom(first);
7664 vixl32::Register second_reg = RegisterFrom(second);
7665 vixl32::Register out_reg = RegisterFrom(out);
7666
7667 switch (instruction->GetOpKind()) {
7668 case HInstruction::kAnd:
7669 __ Bic(out_reg, first_reg, second_reg);
7670 break;
7671 case HInstruction::kOr:
7672 __ Orn(out_reg, first_reg, second_reg);
7673 break;
7674 // There is no EON on arm.
7675 case HInstruction::kXor:
7676 default:
7677 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7678 UNREACHABLE();
7679 }
7680 return;
7681
7682 } else {
7683 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7684 vixl32::Register first_low = LowRegisterFrom(first);
7685 vixl32::Register first_high = HighRegisterFrom(first);
7686 vixl32::Register second_low = LowRegisterFrom(second);
7687 vixl32::Register second_high = HighRegisterFrom(second);
7688 vixl32::Register out_low = LowRegisterFrom(out);
7689 vixl32::Register out_high = HighRegisterFrom(out);
7690
7691 switch (instruction->GetOpKind()) {
7692 case HInstruction::kAnd:
7693 __ Bic(out_low, first_low, second_low);
7694 __ Bic(out_high, first_high, second_high);
7695 break;
7696 case HInstruction::kOr:
7697 __ Orn(out_low, first_low, second_low);
7698 __ Orn(out_high, first_high, second_high);
7699 break;
7700 // There is no EON on arm.
7701 case HInstruction::kXor:
7702 default:
7703 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7704 UNREACHABLE();
7705 }
7706 }
7707}
7708
Anton Kirilov74234da2017-01-13 14:42:47 +00007709void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7710 HDataProcWithShifterOp* instruction) {
7711 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7712 instruction->GetType() == Primitive::kPrimLong);
7713 LocationSummary* locations =
7714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7715 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7716 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7717
7718 locations->SetInAt(0, Location::RequiresRegister());
7719 locations->SetInAt(1, Location::RequiresRegister());
7720 locations->SetOut(Location::RequiresRegister(),
7721 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7722}
7723
7724void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7725 HDataProcWithShifterOp* instruction) {
7726 const LocationSummary* const locations = instruction->GetLocations();
7727 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7728 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7729
7730 if (instruction->GetType() == Primitive::kPrimInt) {
7731 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7732
7733 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7734 ? LowRegisterFrom(locations->InAt(1))
7735 : InputRegisterAt(instruction, 1);
7736
7737 GenerateDataProcInstruction(kind,
7738 OutputRegister(instruction),
7739 InputRegisterAt(instruction, 0),
7740 Operand(second,
7741 ShiftFromOpKind(op_kind),
7742 instruction->GetShiftAmount()),
7743 codegen_);
7744 } else {
7745 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7746
7747 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7748 const vixl32::Register second = InputRegisterAt(instruction, 1);
7749
7750 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7751 GenerateDataProc(kind,
7752 locations->Out(),
7753 locations->InAt(0),
7754 second,
7755 Operand(second, ShiftType::ASR, 31),
7756 codegen_);
7757 } else {
7758 GenerateLongDataProc(instruction, codegen_);
7759 }
7760 }
7761}
7762
Artem Serov02109dd2016-09-23 17:17:54 +01007763// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7764void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7765 vixl32::Register first,
7766 uint32_t value) {
7767 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7768 if (value == 0xffffffffu) {
7769 if (!out.Is(first)) {
7770 __ Mov(out, first);
7771 }
7772 return;
7773 }
7774 if (value == 0u) {
7775 __ Mov(out, 0);
7776 return;
7777 }
7778 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007779 __ And(out, first, value);
7780 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
7781 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01007782 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007783 DCHECK(IsPowerOfTwo(value + 1));
7784 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01007785 }
7786}
7787
7788// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7789void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7790 vixl32::Register first,
7791 uint32_t value) {
7792 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7793 if (value == 0u) {
7794 if (!out.Is(first)) {
7795 __ Mov(out, first);
7796 }
7797 return;
7798 }
7799 if (value == 0xffffffffu) {
7800 __ Mvn(out, 0);
7801 return;
7802 }
7803 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7804 __ Orr(out, first, value);
7805 } else {
7806 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7807 __ Orn(out, first, ~value);
7808 }
7809}
7810
7811// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7812void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7813 vixl32::Register first,
7814 uint32_t value) {
7815 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7816 if (value == 0u) {
7817 if (!out.Is(first)) {
7818 __ Mov(out, first);
7819 }
7820 return;
7821 }
7822 __ Eor(out, first, value);
7823}
7824
Anton Kirilovdda43962016-11-21 19:55:20 +00007825void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7826 Location first,
7827 uint64_t value) {
7828 vixl32::Register out_low = LowRegisterFrom(out);
7829 vixl32::Register out_high = HighRegisterFrom(out);
7830 vixl32::Register first_low = LowRegisterFrom(first);
7831 vixl32::Register first_high = HighRegisterFrom(first);
7832 uint32_t value_low = Low32Bits(value);
7833 uint32_t value_high = High32Bits(value);
7834 if (value_low == 0u) {
7835 if (!out_low.Is(first_low)) {
7836 __ Mov(out_low, first_low);
7837 }
7838 __ Add(out_high, first_high, value_high);
7839 return;
7840 }
7841 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007842 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007843 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007844 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007845 __ Sbc(out_high, first_high, ~value_high);
7846 } else {
7847 LOG(FATAL) << "Unexpected constant " << value_high;
7848 UNREACHABLE();
7849 }
7850}
7851
Artem Serov02109dd2016-09-23 17:17:54 +01007852void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7853 LocationSummary* locations = instruction->GetLocations();
7854 Location first = locations->InAt(0);
7855 Location second = locations->InAt(1);
7856 Location out = locations->Out();
7857
7858 if (second.IsConstant()) {
7859 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7860 uint32_t value_low = Low32Bits(value);
7861 if (instruction->GetResultType() == Primitive::kPrimInt) {
7862 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7863 vixl32::Register out_reg = OutputRegister(instruction);
7864 if (instruction->IsAnd()) {
7865 GenerateAndConst(out_reg, first_reg, value_low);
7866 } else if (instruction->IsOr()) {
7867 GenerateOrrConst(out_reg, first_reg, value_low);
7868 } else {
7869 DCHECK(instruction->IsXor());
7870 GenerateEorConst(out_reg, first_reg, value_low);
7871 }
7872 } else {
7873 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7874 uint32_t value_high = High32Bits(value);
7875 vixl32::Register first_low = LowRegisterFrom(first);
7876 vixl32::Register first_high = HighRegisterFrom(first);
7877 vixl32::Register out_low = LowRegisterFrom(out);
7878 vixl32::Register out_high = HighRegisterFrom(out);
7879 if (instruction->IsAnd()) {
7880 GenerateAndConst(out_low, first_low, value_low);
7881 GenerateAndConst(out_high, first_high, value_high);
7882 } else if (instruction->IsOr()) {
7883 GenerateOrrConst(out_low, first_low, value_low);
7884 GenerateOrrConst(out_high, first_high, value_high);
7885 } else {
7886 DCHECK(instruction->IsXor());
7887 GenerateEorConst(out_low, first_low, value_low);
7888 GenerateEorConst(out_high, first_high, value_high);
7889 }
7890 }
7891 return;
7892 }
7893
7894 if (instruction->GetResultType() == Primitive::kPrimInt) {
7895 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7896 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7897 vixl32::Register out_reg = OutputRegister(instruction);
7898 if (instruction->IsAnd()) {
7899 __ And(out_reg, first_reg, second_reg);
7900 } else if (instruction->IsOr()) {
7901 __ Orr(out_reg, first_reg, second_reg);
7902 } else {
7903 DCHECK(instruction->IsXor());
7904 __ Eor(out_reg, first_reg, second_reg);
7905 }
7906 } else {
7907 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7908 vixl32::Register first_low = LowRegisterFrom(first);
7909 vixl32::Register first_high = HighRegisterFrom(first);
7910 vixl32::Register second_low = LowRegisterFrom(second);
7911 vixl32::Register second_high = HighRegisterFrom(second);
7912 vixl32::Register out_low = LowRegisterFrom(out);
7913 vixl32::Register out_high = HighRegisterFrom(out);
7914 if (instruction->IsAnd()) {
7915 __ And(out_low, first_low, second_low);
7916 __ And(out_high, first_high, second_high);
7917 } else if (instruction->IsOr()) {
7918 __ Orr(out_low, first_low, second_low);
7919 __ Orr(out_high, first_high, second_high);
7920 } else {
7921 DCHECK(instruction->IsXor());
7922 __ Eor(out_low, first_low, second_low);
7923 __ Eor(out_high, first_high, second_high);
7924 }
7925 }
7926}
7927
Artem Serovcfbe9132016-10-14 15:58:56 +01007928void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007929 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007930 Location out,
7931 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007932 Location maybe_temp,
7933 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007934 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007935 if (read_barrier_option == kWithReadBarrier) {
7936 CHECK(kEmitCompilerReadBarrier);
7937 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7938 if (kUseBakerReadBarrier) {
7939 // Load with fast path based Baker's read barrier.
7940 // /* HeapReference<Object> */ out = *(out + offset)
7941 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7942 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7943 } else {
7944 // Load with slow path based read barrier.
7945 // Save the value of `out` into `maybe_temp` before overwriting it
7946 // in the following move operation, as we will need it for the
7947 // read barrier below.
7948 __ Mov(RegisterFrom(maybe_temp), out_reg);
7949 // /* HeapReference<Object> */ out = *(out + offset)
7950 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7951 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7952 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007953 } else {
7954 // Plain load with no read barrier.
7955 // /* HeapReference<Object> */ out = *(out + offset)
7956 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7957 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7958 }
7959}
7960
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007961void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007962 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007963 Location out,
7964 Location obj,
7965 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007966 Location maybe_temp,
7967 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007968 vixl32::Register out_reg = RegisterFrom(out);
7969 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007970 if (read_barrier_option == kWithReadBarrier) {
7971 CHECK(kEmitCompilerReadBarrier);
7972 if (kUseBakerReadBarrier) {
7973 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7974 // Load with fast path based Baker's read barrier.
7975 // /* HeapReference<Object> */ out = *(obj + offset)
7976 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7977 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7978 } else {
7979 // Load with slow path based read barrier.
7980 // /* HeapReference<Object> */ out = *(obj + offset)
7981 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7982 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7983 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007984 } else {
7985 // Plain load with no read barrier.
7986 // /* HeapReference<Object> */ out = *(obj + offset)
7987 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7988 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7989 }
7990}
7991
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007992void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007993 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007994 Location root,
7995 vixl32::Register obj,
7996 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007997 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007998 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007999 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008000 DCHECK(kEmitCompilerReadBarrier);
8001 if (kUseBakerReadBarrier) {
8002 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008003 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008004 //
Roland Levillainba650a42017-03-06 13:52:32 +00008005 // Note that we do not actually check the value of
8006 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8007 // root or not. Instead, we load into `temp` the read barrier
8008 // mark entry point corresponding to register `root`. If `temp`
8009 // is null, it means that `GetIsGcMarking()` is false, and vice
8010 // versa.
8011 //
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008012 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008013 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8014 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8015 // // Slow path.
8016 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008017 // }
8018
Roland Levillainba650a42017-03-06 13:52:32 +00008019 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8020 Location temp = LocationFrom(lr);
8021 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
8022 instruction, root, /* entrypoint */ temp);
8023 codegen_->AddSlowPath(slow_path);
8024
8025 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8026 const int32_t entry_point_offset =
8027 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8028 // Loading the entrypoint does not require a load acquire since it is only changed when
8029 // threads are suspended or running a checkpoint.
8030 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
8031
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008032 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8033 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8034 static_assert(
8035 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8036 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8037 "have different sizes.");
8038 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8039 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8040 "have different sizes.");
8041
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008042 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8043 // checking GetIsGcMarking.
8044 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
8045 __ Bind(slow_path->GetExitLabel());
8046 } else {
8047 // GC root loaded through a slow path for read barriers other
8048 // than Baker's.
8049 // /* GcRoot<mirror::Object>* */ root = obj + offset
8050 __ Add(root_reg, obj, offset);
8051 // /* mirror::Object* */ root = root->Read()
8052 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8053 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008054 } else {
8055 // Plain GC root load with no read barrier.
8056 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8057 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8058 // Note that GC roots are not affected by heap poisoning, thus we
8059 // do not have to unpoison `root_reg` here.
8060 }
8061}
8062
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008063void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8064 Location ref,
8065 vixl32::Register obj,
8066 uint32_t offset,
8067 Location temp,
8068 bool needs_null_check) {
8069 DCHECK(kEmitCompilerReadBarrier);
8070 DCHECK(kUseBakerReadBarrier);
8071
8072 // /* HeapReference<Object> */ ref = *(obj + offset)
8073 Location no_index = Location::NoLocation();
8074 ScaleFactor no_scale_factor = TIMES_1;
8075 GenerateReferenceLoadWithBakerReadBarrier(
8076 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008077}
8078
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008079void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8080 Location ref,
8081 vixl32::Register obj,
8082 uint32_t data_offset,
8083 Location index,
8084 Location temp,
8085 bool needs_null_check) {
8086 DCHECK(kEmitCompilerReadBarrier);
8087 DCHECK(kUseBakerReadBarrier);
8088
8089 static_assert(
8090 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8091 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
8092 // /* HeapReference<Object> */ ref =
8093 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8094 ScaleFactor scale_factor = TIMES_4;
8095 GenerateReferenceLoadWithBakerReadBarrier(
8096 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008097}
8098
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008099void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8100 Location ref,
8101 vixl32::Register obj,
8102 uint32_t offset,
8103 Location index,
8104 ScaleFactor scale_factor,
8105 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008106 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008107 DCHECK(kEmitCompilerReadBarrier);
8108 DCHECK(kUseBakerReadBarrier);
8109
Roland Levillain54f869e2017-03-06 13:54:11 +00008110 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8111 // whether we need to enter the slow path to mark the reference.
8112 // Then, in the slow path, check the gray bit in the lock word of
8113 // the reference's holder (`obj`) to decide whether to mark `ref` or
8114 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008115 //
Roland Levillainba650a42017-03-06 13:52:32 +00008116 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008117 // instead, we load into `temp2` the read barrier mark entry point
8118 // corresponding to register `ref`. If `temp2` is null, it means
8119 // that `GetIsGcMarking()` is false, and vice versa.
8120 //
8121 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8122 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8123 // // Slow path.
8124 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8125 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8126 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8127 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8128 // if (is_gray) {
8129 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8130 // }
8131 // } else {
8132 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8133 // }
8134
8135 vixl32::Register temp_reg = RegisterFrom(temp);
8136
8137 // Slow path marking the object `ref` when the GC is marking. The
8138 // entrypoint will already be loaded in `temp2`.
8139 Location temp2 = LocationFrom(lr);
8140 SlowPathCodeARMVIXL* slow_path =
8141 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8142 instruction,
8143 ref,
8144 obj,
8145 offset,
8146 index,
8147 scale_factor,
8148 needs_null_check,
8149 temp_reg,
8150 /* entrypoint */ temp2);
8151 AddSlowPath(slow_path);
8152
8153 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8154 const int32_t entry_point_offset =
8155 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8156 // Loading the entrypoint does not require a load acquire since it is only changed when
8157 // threads are suspended or running a checkpoint.
8158 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp2), tr, entry_point_offset);
8159 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8160 // checking GetIsGcMarking.
8161 __ CompareAndBranchIfNonZero(RegisterFrom(temp2), slow_path->GetEntryLabel());
8162 // Fast path: the GC is not marking: just load the reference.
8163 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8164 __ Bind(slow_path->GetExitLabel());
8165}
8166
8167void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8168 Location ref,
8169 vixl32::Register obj,
8170 Location field_offset,
8171 Location temp,
8172 bool needs_null_check,
8173 vixl32::Register temp2) {
8174 DCHECK(kEmitCompilerReadBarrier);
8175 DCHECK(kUseBakerReadBarrier);
8176
8177 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8178 // whether we need to enter the slow path to update the reference
8179 // field within `obj`. Then, in the slow path, check the gray bit
8180 // in the lock word of the reference's holder (`obj`) to decide
8181 // whether to mark `ref` and update the field or not.
8182 //
8183 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008184 // instead, we load into `temp3` the read barrier mark entry point
8185 // corresponding to register `ref`. If `temp3` is null, it means
8186 // that `GetIsGcMarking()` is false, and vice versa.
8187 //
8188 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008189 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8190 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008191 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8192 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008193 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008194 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8195 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008196 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008197 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008198 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008199 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008200 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008201
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008202 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008203
Roland Levillainff487002017-03-07 16:50:01 +00008204 // Slow path updating the object reference at address `obj + field_offset`
8205 // when the GC is marking. The entrypoint will already be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008206 Location temp3 = LocationFrom(lr);
Roland Levillainff487002017-03-07 16:50:01 +00008207 SlowPathCodeARMVIXL* slow_path =
8208 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8209 instruction,
8210 ref,
8211 obj,
8212 /* offset */ 0u,
8213 /* index */ field_offset,
8214 /* scale_factor */ ScaleFactor::TIMES_1,
8215 needs_null_check,
8216 temp_reg,
8217 temp2,
8218 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008219 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008220
Roland Levillainba650a42017-03-06 13:52:32 +00008221 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8222 const int32_t entry_point_offset =
8223 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8224 // Loading the entrypoint does not require a load acquire since it is only changed when
8225 // threads are suspended or running a checkpoint.
8226 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008227 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8228 // checking GetIsGcMarking.
8229 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008230 // Fast path: the GC is not marking: nothing to do (the field is
8231 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008232 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008233}
Scott Wakelingfe885462016-09-22 10:24:38 +01008234
Roland Levillainba650a42017-03-06 13:52:32 +00008235void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8236 Location ref,
8237 vixl::aarch32::Register obj,
8238 uint32_t offset,
8239 Location index,
8240 ScaleFactor scale_factor,
8241 bool needs_null_check) {
8242 Primitive::Type type = Primitive::kPrimNot;
8243 vixl32::Register ref_reg = RegisterFrom(ref, type);
8244
8245 // If needed, vixl::EmissionCheckScope guards are used to ensure
8246 // that no pools are emitted between the load (macro) instruction
8247 // and MaybeRecordImplicitNullCheck.
8248
Scott Wakelingfe885462016-09-22 10:24:38 +01008249 if (index.IsValid()) {
8250 // Load types involving an "index": ArrayGet,
8251 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8252 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008253 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008254 if (index.IsConstant()) {
8255 size_t computed_offset =
8256 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008257 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008258 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008259 if (needs_null_check) {
8260 MaybeRecordImplicitNullCheck(instruction);
8261 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008262 } else {
8263 // Handle the special case of the
8264 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8265 // intrinsics, which use a register pair as index ("long
8266 // offset"), of which only the low part contains data.
8267 vixl32::Register index_reg = index.IsRegisterPair()
8268 ? LowRegisterFrom(index)
8269 : RegisterFrom(index);
8270 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008271 vixl32::Register temp = temps.Acquire();
8272 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8273 {
8274 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8275 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8276 if (needs_null_check) {
8277 MaybeRecordImplicitNullCheck(instruction);
8278 }
8279 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008280 }
8281 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008282 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8283 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008284 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008285 if (needs_null_check) {
8286 MaybeRecordImplicitNullCheck(instruction);
8287 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008288 }
8289
Roland Levillain844e6532016-11-03 16:09:47 +00008290 // Object* ref = ref_addr->AsMirrorPtr()
8291 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008292}
8293
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008294void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8295 Location out,
8296 Location ref,
8297 Location obj,
8298 uint32_t offset,
8299 Location index) {
8300 DCHECK(kEmitCompilerReadBarrier);
8301
8302 // Insert a slow path based read barrier *after* the reference load.
8303 //
8304 // If heap poisoning is enabled, the unpoisoning of the loaded
8305 // reference will be carried out by the runtime within the slow
8306 // path.
8307 //
8308 // Note that `ref` currently does not get unpoisoned (when heap
8309 // poisoning is enabled), which is alright as the `ref` argument is
8310 // not used by the artReadBarrierSlow entry point.
8311 //
8312 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8313 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8314 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8315 AddSlowPath(slow_path);
8316
8317 __ B(slow_path->GetEntryLabel());
8318 __ Bind(slow_path->GetExitLabel());
8319}
8320
8321void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008322 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008323 Location ref,
8324 Location obj,
8325 uint32_t offset,
8326 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008327 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008328 // Baker's read barriers shall be handled by the fast path
8329 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008330 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008331 // If heap poisoning is enabled, unpoisoning will be taken care of
8332 // by the runtime within the slow path.
8333 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008334 } else if (kPoisonHeapReferences) {
8335 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8336 }
8337}
8338
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008339void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8340 Location out,
8341 Location root) {
8342 DCHECK(kEmitCompilerReadBarrier);
8343
8344 // Insert a slow path based read barrier *after* the GC root load.
8345 //
8346 // Note that GC roots are not affected by heap poisoning, so we do
8347 // not need to do anything special for this here.
8348 SlowPathCodeARMVIXL* slow_path =
8349 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8350 AddSlowPath(slow_path);
8351
8352 __ B(slow_path->GetEntryLabel());
8353 __ Bind(slow_path->GetExitLabel());
8354}
8355
Artem Serov02d37832016-10-25 15:25:33 +01008356// Check if the desired_dispatch_info is supported. If it is, return it,
8357// otherwise return a fall-back info that should be used instead.
8358HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008359 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008360 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008361 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008362}
8363
Scott Wakelingfe885462016-09-22 10:24:38 +01008364vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8365 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8366 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8367 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8368 if (!invoke->GetLocations()->Intrinsified()) {
8369 return RegisterFrom(location);
8370 }
8371 // For intrinsics we allow any location, so it may be on the stack.
8372 if (!location.IsRegister()) {
8373 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8374 return temp;
8375 }
8376 // For register locations, check if the register was saved. If so, get it from the stack.
8377 // Note: There is a chance that the register was saved but not overwritten, so we could
8378 // save one load. However, since this is just an intrinsic slow path we prefer this
8379 // simple and more robust approach rather that trying to determine if that's the case.
8380 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008381 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008382 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8383 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8384 return temp;
8385 }
8386 return RegisterFrom(location);
8387}
8388
TatWai Chongd8c052a2016-11-02 16:12:48 +08008389Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01008390 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008391 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008392 switch (invoke->GetMethodLoadKind()) {
8393 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8394 uint32_t offset =
8395 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8396 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008397 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8398 break;
8399 }
8400 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8401 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8402 break;
8403 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8404 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8405 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008406 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8407 HArmDexCacheArraysBase* base =
8408 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8409 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
8410 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8411 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008412 break;
8413 }
8414 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
8415 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8416 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008417 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008418 if (current_method.IsRegister()) {
8419 method_reg = RegisterFrom(current_method);
8420 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008421 DCHECK(invoke->GetLocations()->Intrinsified());
8422 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008423 method_reg = reg;
8424 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008425 }
8426 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8427 GetAssembler()->LoadFromOffset(
8428 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008429 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01008430 method_reg,
8431 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
8432 // temp = temp[index_in_cache];
8433 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8434 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8435 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008436 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01008437 break;
8438 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008439 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008440 return callee_method;
8441}
8442
8443void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8444 Location temp) {
8445 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008446
Artem Serovd4cc5b22016-11-04 11:19:09 +00008447 switch (invoke->GetCodePtrLocation()) {
8448 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8449 __ Bl(GetFrameEntryLabel());
8450 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008451 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8452 // LR = callee_method->entry_point_from_quick_compiled_code_
8453 GetAssembler()->LoadFromOffset(
8454 kLoadWord,
8455 lr,
8456 RegisterFrom(callee_method),
8457 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008458 {
8459 // 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 +00008460 ExactAssemblyScope aas(GetVIXLAssembler(),
8461 vixl32::k16BitT32InstructionSizeInBytes,
8462 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008463 // LR()
8464 __ blx(lr);
8465 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008466 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008467 }
8468
Scott Wakelingfe885462016-09-22 10:24:38 +01008469 DCHECK(!IsLeafMethod());
8470}
8471
8472void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8473 vixl32::Register temp = RegisterFrom(temp_location);
8474 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8475 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8476
8477 // Use the calling convention instead of the location of the receiver, as
8478 // intrinsics may have put the receiver in a different register. In the intrinsics
8479 // slow path, the arguments have been moved to the right place, so here we are
8480 // guaranteed that the receiver is the first register of the calling convention.
8481 InvokeDexCallingConventionARMVIXL calling_convention;
8482 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8483 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008484 {
8485 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008486 ExactAssemblyScope aas(GetVIXLAssembler(),
8487 vixl32::kMaxInstructionSizeInBytes,
8488 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008489 // /* HeapReference<Class> */ temp = receiver->klass_
8490 __ ldr(temp, MemOperand(receiver, class_offset));
8491 MaybeRecordImplicitNullCheck(invoke);
8492 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008493 // Instead of simply (possibly) unpoisoning `temp` here, we should
8494 // emit a read barrier for the previous class reference load.
8495 // However this is not required in practice, as this is an
8496 // intermediate/temporary reference and because the current
8497 // concurrent copying collector keeps the from-space memory
8498 // intact/accessible until the end of the marking phase (the
8499 // concurrent copying collector may not in the future).
8500 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8501
8502 // temp = temp->GetMethodAt(method_offset);
8503 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8504 kArmPointerSize).Int32Value();
8505 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8506 // LR = temp->GetEntryPoint();
8507 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8508 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008509 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8510 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8511 // that.
8512 // 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 +00008513 ExactAssemblyScope aas(GetVIXLAssembler(),
8514 vixl32::k16BitT32InstructionSizeInBytes,
8515 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008516 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01008517}
8518
Artem Serovd4cc5b22016-11-04 11:19:09 +00008519CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008520 const DexFile& dex_file, dex::StringIndex string_index) {
8521 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008522}
8523
8524CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8525 const DexFile& dex_file, dex::TypeIndex type_index) {
8526 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8527}
8528
Vladimir Marko1998cd02017-01-13 13:02:58 +00008529CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8530 const DexFile& dex_file, dex::TypeIndex type_index) {
8531 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8532}
8533
Artem Serovd4cc5b22016-11-04 11:19:09 +00008534CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8535 const DexFile& dex_file, uint32_t element_offset) {
8536 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8537}
8538
8539CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8540 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8541 patches->emplace_back(dex_file, offset_or_index);
8542 return &patches->back();
8543}
8544
Artem Serovc5fcb442016-12-02 19:19:58 +00008545VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
8546 const DexFile& dex_file,
8547 dex::StringIndex string_index) {
8548 return boot_image_string_patches_.GetOrCreate(
8549 StringReference(&dex_file, string_index),
8550 [this]() {
8551 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8552 });
8553}
8554
8555VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
8556 const DexFile& dex_file,
8557 dex::TypeIndex type_index) {
8558 return boot_image_type_patches_.GetOrCreate(
8559 TypeReference(&dex_file, type_index),
8560 [this]() {
8561 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8562 });
8563}
8564
8565VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008566 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008567}
8568
8569VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
8570 return DeduplicateUint32Literal(address, &uint32_literals_);
8571}
8572
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008573VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8574 const DexFile& dex_file,
8575 dex::StringIndex string_index,
8576 Handle<mirror::String> handle) {
8577 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8578 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008579 return jit_string_patches_.GetOrCreate(
8580 StringReference(&dex_file, string_index),
8581 [this]() {
8582 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8583 });
8584}
8585
8586VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8587 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008588 Handle<mirror::Class> handle) {
8589 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8590 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008591 return jit_class_patches_.GetOrCreate(
8592 TypeReference(&dex_file, type_index),
8593 [this]() {
8594 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8595 });
8596}
8597
Artem Serovd4cc5b22016-11-04 11:19:09 +00008598template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8599inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8600 const ArenaDeque<PcRelativePatchInfo>& infos,
8601 ArenaVector<LinkerPatch>* linker_patches) {
8602 for (const PcRelativePatchInfo& info : infos) {
8603 const DexFile& dex_file = info.target_dex_file;
8604 size_t offset_or_index = info.offset_or_index;
8605 DCHECK(info.add_pc_label.IsBound());
8606 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8607 // Add MOVW patch.
8608 DCHECK(info.movw_label.IsBound());
8609 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8610 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8611 // Add MOVT patch.
8612 DCHECK(info.movt_label.IsBound());
8613 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8614 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8615 }
8616}
8617
8618void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8619 DCHECK(linker_patches->empty());
8620 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008621 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008622 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00008623 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008624 boot_image_type_patches_.size() +
8625 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00008626 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008627 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008628 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8629 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008630 for (const auto& entry : boot_image_string_patches_) {
8631 const StringReference& target_string = entry.first;
8632 VIXLUInt32Literal* literal = entry.second;
8633 DCHECK(literal->IsBound());
8634 uint32_t literal_offset = literal->GetLocation();
8635 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8636 target_string.dex_file,
8637 target_string.string_index.index_));
8638 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008639 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008640 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008641 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8642 linker_patches);
8643 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008644 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8645 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008646 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8647 linker_patches);
8648 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008649 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8650 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008651 for (const auto& entry : boot_image_type_patches_) {
8652 const TypeReference& target_type = entry.first;
8653 VIXLUInt32Literal* literal = entry.second;
8654 DCHECK(literal->IsBound());
8655 uint32_t literal_offset = literal->GetLocation();
8656 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8657 target_type.dex_file,
8658 target_type.type_index.index_));
8659 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008660 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00008661}
8662
8663VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
8664 uint32_t value,
8665 Uint32ToLiteralMap* map) {
8666 return map->GetOrCreate(
8667 value,
8668 [this, value]() {
8669 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
8670 });
8671}
8672
8673VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
8674 MethodReference target_method,
8675 MethodToLiteralMap* map) {
8676 return map->GetOrCreate(
8677 target_method,
8678 [this]() {
8679 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8680 });
8681}
8682
Artem Serov2bbc9532016-10-21 11:51:50 +01008683void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8684 LocationSummary* locations =
8685 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8686 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8687 Location::RequiresRegister());
8688 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8689 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8691}
8692
8693void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8694 vixl32::Register res = OutputRegister(instr);
8695 vixl32::Register accumulator =
8696 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
8697 vixl32::Register mul_left =
8698 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
8699 vixl32::Register mul_right =
8700 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
8701
8702 if (instr->GetOpKind() == HInstruction::kAdd) {
8703 __ Mla(res, mul_left, mul_right, accumulator);
8704 } else {
8705 __ Mls(res, mul_left, mul_right, accumulator);
8706 }
8707}
8708
Artem Serov551b28f2016-10-18 19:11:30 +01008709void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8710 // Nothing to do, this should be removed during prepare for register allocator.
8711 LOG(FATAL) << "Unreachable";
8712}
8713
8714void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8715 // Nothing to do, this should be removed during prepare for register allocator.
8716 LOG(FATAL) << "Unreachable";
8717}
8718
8719// Simple implementation of packed switch - generate cascaded compare/jumps.
8720void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8721 LocationSummary* locations =
8722 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8723 locations->SetInAt(0, Location::RequiresRegister());
8724 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
8725 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8726 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8727 if (switch_instr->GetStartValue() != 0) {
8728 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8729 }
8730 }
8731}
8732
8733// TODO(VIXL): Investigate and reach the parity with old arm codegen.
8734void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8735 int32_t lower_bound = switch_instr->GetStartValue();
8736 uint32_t num_entries = switch_instr->GetNumEntries();
8737 LocationSummary* locations = switch_instr->GetLocations();
8738 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
8739 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8740
8741 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
8742 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8743 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008744 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008745 vixl32::Register temp_reg = temps.Acquire();
8746 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8747 // the immediate, because IP is used as the destination register. For the other
8748 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8749 // and they can be encoded in the instruction without making use of IP register.
8750 __ Adds(temp_reg, value_reg, -lower_bound);
8751
8752 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8753 // Jump to successors[0] if value == lower_bound.
8754 __ B(eq, codegen_->GetLabelOf(successors[0]));
8755 int32_t last_index = 0;
8756 for (; num_entries - last_index > 2; last_index += 2) {
8757 __ Adds(temp_reg, temp_reg, -2);
8758 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8759 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
8760 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8761 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
8762 }
8763 if (num_entries - last_index == 2) {
8764 // The last missing case_value.
8765 __ Cmp(temp_reg, 1);
8766 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
8767 }
8768
8769 // And the default for any other value.
8770 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8771 __ B(codegen_->GetLabelOf(default_block));
8772 }
8773 } else {
8774 // Create a table lookup.
8775 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
8776
8777 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
8778
8779 // Remove the bias.
8780 vixl32::Register key_reg;
8781 if (lower_bound != 0) {
8782 key_reg = RegisterFrom(locations->GetTemp(1));
8783 __ Sub(key_reg, value_reg, lower_bound);
8784 } else {
8785 key_reg = value_reg;
8786 }
8787
8788 // Check whether the value is in the table, jump to default block if not.
8789 __ Cmp(key_reg, num_entries - 1);
8790 __ B(hi, codegen_->GetLabelOf(default_block));
8791
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008792 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008793 vixl32::Register jump_offset = temps.Acquire();
8794
8795 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00008796 {
8797 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
8798 ExactAssemblyScope aas(GetVIXLAssembler(),
8799 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
8800 CodeBufferCheckScope::kMaximumSize);
8801 __ adr(table_base, jump_table->GetTableStartLabel());
8802 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01008803
Scott Wakeling86e9d262017-01-18 15:59:24 +00008804 // Jump to target block by branching to table_base(pc related) + offset.
8805 vixl32::Register target_address = table_base;
8806 __ add(target_address, table_base, jump_offset);
8807 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00008808
Scott Wakeling86e9d262017-01-18 15:59:24 +00008809 jump_table->EmitTable(codegen_);
8810 }
Artem Serov551b28f2016-10-18 19:11:30 +01008811 }
8812}
Artem Serovd4cc5b22016-11-04 11:19:09 +00008813void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8815 locations->SetOut(Location::RequiresRegister());
8816}
8817
8818void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8819 vixl32::Register base_reg = OutputRegister(base);
8820 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8821 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
8822 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
8823}
Artem Serov551b28f2016-10-18 19:11:30 +01008824
Artem Serov02d37832016-10-25 15:25:33 +01008825// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008826void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8827 if (!trg.IsValid()) {
8828 DCHECK_EQ(type, Primitive::kPrimVoid);
8829 return;
8830 }
8831
8832 DCHECK_NE(type, Primitive::kPrimVoid);
8833
Artem Serovd4cc5b22016-11-04 11:19:09 +00008834 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008835 if (return_loc.Equals(trg)) {
8836 return;
8837 }
8838
8839 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8840 // with the last branch.
8841 if (type == Primitive::kPrimLong) {
8842 TODO_VIXL32(FATAL);
8843 } else if (type == Primitive::kPrimDouble) {
8844 TODO_VIXL32(FATAL);
8845 } else {
8846 // Let the parallel move resolver take care of all of this.
8847 HParallelMove parallel_move(GetGraph()->GetArena());
8848 parallel_move.AddMove(return_loc, trg, type, nullptr);
8849 GetMoveResolver()->EmitNativeCode(&parallel_move);
8850 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008851}
8852
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008853void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8854 LocationSummary* locations =
8855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8856 locations->SetInAt(0, Location::RequiresRegister());
8857 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008858}
8859
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008860void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8861 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8862 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8863 instruction->GetIndex(), kArmPointerSize).SizeValue();
8864 GetAssembler()->LoadFromOffset(kLoadWord,
8865 OutputRegister(instruction),
8866 InputRegisterAt(instruction, 0),
8867 method_offset);
8868 } else {
8869 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8870 instruction->GetIndex(), kArmPointerSize));
8871 GetAssembler()->LoadFromOffset(kLoadWord,
8872 OutputRegister(instruction),
8873 InputRegisterAt(instruction, 0),
8874 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8875 GetAssembler()->LoadFromOffset(kLoadWord,
8876 OutputRegister(instruction),
8877 OutputRegister(instruction),
8878 method_offset);
8879 }
Artem Serov551b28f2016-10-18 19:11:30 +01008880}
8881
Artem Serovc5fcb442016-12-02 19:19:58 +00008882static void PatchJitRootUse(uint8_t* code,
8883 const uint8_t* roots_data,
8884 VIXLUInt32Literal* literal,
8885 uint64_t index_in_table) {
8886 DCHECK(literal->IsBound());
8887 uint32_t literal_offset = literal->GetLocation();
8888 uintptr_t address =
8889 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8890 uint8_t* data = code + literal_offset;
8891 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8892}
8893
8894void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8895 for (const auto& entry : jit_string_patches_) {
8896 const auto& it = jit_string_roots_.find(entry.first);
8897 DCHECK(it != jit_string_roots_.end());
8898 PatchJitRootUse(code, roots_data, entry.second, it->second);
8899 }
8900 for (const auto& entry : jit_class_patches_) {
8901 const auto& it = jit_class_roots_.find(entry.first);
8902 DCHECK(it != jit_class_roots_.end());
8903 PatchJitRootUse(code, roots_data, entry.second, it->second);
8904 }
8905}
8906
Artem Serovd4cc5b22016-11-04 11:19:09 +00008907void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8908 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8909 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008910 ExactAssemblyScope aas(GetVIXLAssembler(),
8911 3 * vixl32::kMaxInstructionSizeInBytes,
8912 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008913 // TODO(VIXL): Think about using mov instead of movw.
8914 __ bind(&labels->movw_label);
8915 __ movw(out, /* placeholder */ 0u);
8916 __ bind(&labels->movt_label);
8917 __ movt(out, /* placeholder */ 0u);
8918 __ bind(&labels->add_pc_label);
8919 __ add(out, out, pc);
8920}
8921
Scott Wakelingfe885462016-09-22 10:24:38 +01008922#undef __
8923#undef QUICK_ENTRY_POINT
8924#undef TODO_VIXL32
8925
8926} // namespace arm
8927} // namespace art