blob: 2d2d8109a345fed3a5d6c1664348624209d29e4a [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.
848 // The fast path/slow path (for Baker's algorithm) should look like:
849 //
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 Levillain54f869e2017-03-06 13:54:11 +0000990 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
991
992 // /* int32_t */ monitor = obj->monitor_
993 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
994 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
995 if (needs_null_check_) {
996 codegen->MaybeRecordImplicitNullCheck(instruction_);
997 }
998 // /* LockWord */ lock_word = LockWord(monitor)
999 static_assert(sizeof(LockWord) == sizeof(int32_t),
1000 "art::LockWord and int32_t have different sizes.");
1001
1002 // Introduce a dependency on the lock_word including the rb_state,
1003 // which shall prevent load-load reordering without using
1004 // a memory barrier (which would be more expensive).
1005 // `obj` is unchanged by this operation, but its value now depends
1006 // on `temp`.
1007 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1008
1009 // The actual reference load.
1010 // A possible implicit null check has already been handled above.
1011 arm_codegen->GenerateRawReferenceLoad(
1012 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1013
1014 // Mark the object `ref` when `obj` is gray.
1015 //
1016 // if (rb_state == ReadBarrier::GrayState())
1017 // ref = ReadBarrier::Mark(ref);
1018 //
1019 // Given the numeric representation, it's enough to check the low bit of the
1020 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1021 // which can be a 16-bit instruction unlike the TST immediate.
1022 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1023 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1024 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1025 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1026
1027 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001028 // Note that we cannot use IP to save the old reference, as IP is
1029 // used internally by the ReadBarrierMarkRegX entry point, and we
1030 // need the old reference after the call to that entry point.
1031 DCHECK(!temp1_.Is(ip));
1032 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001033
Roland Levillain54f869e2017-03-06 13:54:11 +00001034 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001035
1036 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001037 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001038 //
1039 // Note that this field could also hold a different object, if
1040 // another thread had concurrently changed it. In that case, the
1041 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1042 // (CAS) operation below would abort the CAS, leaving the field
1043 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001044 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001045 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001046
1047 // Update the the holder's field atomically. This may fail if
1048 // mutator updates before us, but it's OK. This is achieved
1049 // using a strong compare-and-set (CAS) operation with relaxed
1050 // memory synchronization ordering, where the expected value is
1051 // the old reference and the desired value is the new reference.
1052
1053 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1054 // Convenience aliases.
1055 vixl32::Register base = obj_;
1056 // The UnsafeCASObject intrinsic uses a register pair as field
1057 // offset ("long offset"), of which only the low part contains
1058 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001059 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001060 vixl32::Register expected = temp1_;
1061 vixl32::Register value = ref_reg;
1062 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1063 vixl32::Register tmp = temp2_; // Value in memory.
1064
1065 __ Add(tmp_ptr, base, offset);
1066
1067 if (kPoisonHeapReferences) {
1068 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1069 if (value.Is(expected)) {
1070 // Do not poison `value`, as it is the same register as
1071 // `expected`, which has just been poisoned.
1072 } else {
1073 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1074 }
1075 }
1076
1077 // do {
1078 // tmp = [r_ptr] - expected;
1079 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1080
1081 vixl32::Label loop_head, exit_loop;
1082 __ Bind(&loop_head);
1083
1084 __ Ldrex(tmp, MemOperand(tmp_ptr));
1085
1086 __ Subs(tmp, tmp, expected);
1087
1088 {
Artem Serov0fb37192016-12-06 18:13:40 +00001089 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1090 2 * kMaxInstructionSizeInBytes,
1091 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001092
1093 __ it(ne);
1094 __ clrex(ne);
1095 }
1096
Artem Serov517d9f62016-12-12 15:51:15 +00001097 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001098
1099 __ Strex(tmp, value, MemOperand(tmp_ptr));
1100 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001101 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001102
1103 __ Bind(&exit_loop);
1104
1105 if (kPoisonHeapReferences) {
1106 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1107 if (value.Is(expected)) {
1108 // Do not unpoison `value`, as it is the same register as
1109 // `expected`, which has just been unpoisoned.
1110 } else {
1111 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1112 }
1113 }
1114
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001115 __ B(GetExitLabel());
1116 }
1117
1118 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001119 // The register containing the object holding the marked object reference field.
1120 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001121 // The offset, index and scale factor to access the reference in `obj_`.
1122 uint32_t offset_;
1123 Location index_;
1124 ScaleFactor scale_factor_;
1125 // Is a null check required?
1126 bool needs_null_check_;
1127 // A temporary register used to hold the lock word of `obj_`; and
1128 // also to hold the original reference value, when the reference is
1129 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001130 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001131 // A temporary register used in the implementation of the CAS, to
1132 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001133 const vixl32::Register temp2_;
1134
Roland Levillain54f869e2017-03-06 13:54:11 +00001135 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001136};
1137
1138// Slow path generating a read barrier for a heap reference.
1139class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1140 public:
1141 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1142 Location out,
1143 Location ref,
1144 Location obj,
1145 uint32_t offset,
1146 Location index)
1147 : SlowPathCodeARMVIXL(instruction),
1148 out_(out),
1149 ref_(ref),
1150 obj_(obj),
1151 offset_(offset),
1152 index_(index) {
1153 DCHECK(kEmitCompilerReadBarrier);
1154 // If `obj` is equal to `out` or `ref`, it means the initial object
1155 // has been overwritten by (or after) the heap object reference load
1156 // to be instrumented, e.g.:
1157 //
1158 // __ LoadFromOffset(kLoadWord, out, out, offset);
1159 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1160 //
1161 // In that case, we have lost the information about the original
1162 // object, and the emitted read barrier cannot work properly.
1163 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1164 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1165 }
1166
1167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1168 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1169 LocationSummary* locations = instruction_->GetLocations();
1170 vixl32::Register reg_out = RegisterFrom(out_);
1171 DCHECK(locations->CanCall());
1172 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1173 DCHECK(instruction_->IsInstanceFieldGet() ||
1174 instruction_->IsStaticFieldGet() ||
1175 instruction_->IsArrayGet() ||
1176 instruction_->IsInstanceOf() ||
1177 instruction_->IsCheckCast() ||
1178 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
1179 << "Unexpected instruction in read barrier for heap reference slow path: "
1180 << instruction_->DebugName();
1181 // The read barrier instrumentation of object ArrayGet
1182 // instructions does not support the HIntermediateAddress
1183 // instruction.
1184 DCHECK(!(instruction_->IsArrayGet() &&
1185 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1186
1187 __ Bind(GetEntryLabel());
1188 SaveLiveRegisters(codegen, locations);
1189
1190 // We may have to change the index's value, but as `index_` is a
1191 // constant member (like other "inputs" of this slow path),
1192 // introduce a copy of it, `index`.
1193 Location index = index_;
1194 if (index_.IsValid()) {
1195 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1196 if (instruction_->IsArrayGet()) {
1197 // Compute the actual memory offset and store it in `index`.
1198 vixl32::Register index_reg = RegisterFrom(index_);
1199 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1200 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1201 // We are about to change the value of `index_reg` (see the
1202 // calls to art::arm::Thumb2Assembler::Lsl and
1203 // art::arm::Thumb2Assembler::AddConstant below), but it has
1204 // not been saved by the previous call to
1205 // art::SlowPathCode::SaveLiveRegisters, as it is a
1206 // callee-save register --
1207 // art::SlowPathCode::SaveLiveRegisters does not consider
1208 // callee-save registers, as it has been designed with the
1209 // assumption that callee-save registers are supposed to be
1210 // handled by the called function. So, as a callee-save
1211 // register, `index_reg` _would_ eventually be saved onto
1212 // the stack, but it would be too late: we would have
1213 // changed its value earlier. Therefore, we manually save
1214 // it here into another freely available register,
1215 // `free_reg`, chosen of course among the caller-save
1216 // registers (as a callee-save `free_reg` register would
1217 // exhibit the same problem).
1218 //
1219 // Note we could have requested a temporary register from
1220 // the register allocator instead; but we prefer not to, as
1221 // this is a slow path, and we know we can find a
1222 // caller-save register that is available.
1223 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1224 __ Mov(free_reg, index_reg);
1225 index_reg = free_reg;
1226 index = LocationFrom(index_reg);
1227 } else {
1228 // The initial register stored in `index_` has already been
1229 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1230 // (as it is not a callee-save register), so we can freely
1231 // use it.
1232 }
1233 // Shifting the index value contained in `index_reg` by the scale
1234 // factor (2) cannot overflow in practice, as the runtime is
1235 // unable to allocate object arrays with a size larger than
1236 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1237 __ Lsl(index_reg, index_reg, TIMES_4);
1238 static_assert(
1239 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1240 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1241 __ Add(index_reg, index_reg, offset_);
1242 } else {
1243 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1244 // intrinsics, `index_` is not shifted by a scale factor of 2
1245 // (as in the case of ArrayGet), as it is actually an offset
1246 // to an object field within an object.
1247 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1248 DCHECK(instruction_->GetLocations()->Intrinsified());
1249 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1250 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1251 << instruction_->AsInvoke()->GetIntrinsic();
1252 DCHECK_EQ(offset_, 0U);
1253 DCHECK(index_.IsRegisterPair());
1254 // UnsafeGet's offset location is a register pair, the low
1255 // part contains the correct offset.
1256 index = index_.ToLow();
1257 }
1258 }
1259
1260 // We're moving two or three locations to locations that could
1261 // overlap, so we need a parallel move resolver.
1262 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1263 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1264 parallel_move.AddMove(ref_,
1265 LocationFrom(calling_convention.GetRegisterAt(0)),
1266 Primitive::kPrimNot,
1267 nullptr);
1268 parallel_move.AddMove(obj_,
1269 LocationFrom(calling_convention.GetRegisterAt(1)),
1270 Primitive::kPrimNot,
1271 nullptr);
1272 if (index.IsValid()) {
1273 parallel_move.AddMove(index,
1274 LocationFrom(calling_convention.GetRegisterAt(2)),
1275 Primitive::kPrimInt,
1276 nullptr);
1277 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1278 } else {
1279 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1280 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1281 }
1282 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1283 CheckEntrypointTypes<
1284 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1285 arm_codegen->Move32(out_, LocationFrom(r0));
1286
1287 RestoreLiveRegisters(codegen, locations);
1288 __ B(GetExitLabel());
1289 }
1290
1291 const char* GetDescription() const OVERRIDE {
1292 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1293 }
1294
1295 private:
1296 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1297 uint32_t ref = RegisterFrom(ref_).GetCode();
1298 uint32_t obj = RegisterFrom(obj_).GetCode();
1299 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1300 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1301 return vixl32::Register(i);
1302 }
1303 }
1304 // We shall never fail to find a free caller-save register, as
1305 // there are more than two core caller-save registers on ARM
1306 // (meaning it is possible to find one which is different from
1307 // `ref` and `obj`).
1308 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1309 LOG(FATAL) << "Could not find a free caller-save register";
1310 UNREACHABLE();
1311 }
1312
1313 const Location out_;
1314 const Location ref_;
1315 const Location obj_;
1316 const uint32_t offset_;
1317 // An additional location containing an index to an array.
1318 // Only used for HArrayGet and the UnsafeGetObject &
1319 // UnsafeGetObjectVolatile intrinsics.
1320 const Location index_;
1321
1322 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1323};
1324
1325// Slow path generating a read barrier for a GC root.
1326class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1327 public:
1328 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1329 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1330 DCHECK(kEmitCompilerReadBarrier);
1331 }
1332
1333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1334 LocationSummary* locations = instruction_->GetLocations();
1335 vixl32::Register reg_out = RegisterFrom(out_);
1336 DCHECK(locations->CanCall());
1337 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1338 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1339 << "Unexpected instruction in read barrier for GC root slow path: "
1340 << instruction_->DebugName();
1341
1342 __ Bind(GetEntryLabel());
1343 SaveLiveRegisters(codegen, locations);
1344
1345 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1346 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1347 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1348 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1349 instruction_,
1350 instruction_->GetDexPc(),
1351 this);
1352 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1353 arm_codegen->Move32(out_, LocationFrom(r0));
1354
1355 RestoreLiveRegisters(codegen, locations);
1356 __ B(GetExitLabel());
1357 }
1358
1359 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1360
1361 private:
1362 const Location out_;
1363 const Location root_;
1364
1365 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1366};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001367
Scott Wakelingfe885462016-09-22 10:24:38 +01001368inline vixl32::Condition ARMCondition(IfCondition cond) {
1369 switch (cond) {
1370 case kCondEQ: return eq;
1371 case kCondNE: return ne;
1372 case kCondLT: return lt;
1373 case kCondLE: return le;
1374 case kCondGT: return gt;
1375 case kCondGE: return ge;
1376 case kCondB: return lo;
1377 case kCondBE: return ls;
1378 case kCondA: return hi;
1379 case kCondAE: return hs;
1380 }
1381 LOG(FATAL) << "Unreachable";
1382 UNREACHABLE();
1383}
1384
1385// Maps signed condition to unsigned condition.
1386inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1387 switch (cond) {
1388 case kCondEQ: return eq;
1389 case kCondNE: return ne;
1390 // Signed to unsigned.
1391 case kCondLT: return lo;
1392 case kCondLE: return ls;
1393 case kCondGT: return hi;
1394 case kCondGE: return hs;
1395 // Unsigned remain unchanged.
1396 case kCondB: return lo;
1397 case kCondBE: return ls;
1398 case kCondA: return hi;
1399 case kCondAE: return hs;
1400 }
1401 LOG(FATAL) << "Unreachable";
1402 UNREACHABLE();
1403}
1404
1405inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1406 // The ARM condition codes can express all the necessary branches, see the
1407 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1408 // There is no dex instruction or HIR that would need the missing conditions
1409 // "equal or unordered" or "not equal".
1410 switch (cond) {
1411 case kCondEQ: return eq;
1412 case kCondNE: return ne /* unordered */;
1413 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1414 case kCondLE: return gt_bias ? ls : le /* unordered */;
1415 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1416 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1417 default:
1418 LOG(FATAL) << "UNREACHABLE";
1419 UNREACHABLE();
1420 }
1421}
1422
Anton Kirilov74234da2017-01-13 14:42:47 +00001423inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1424 switch (op_kind) {
1425 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1426 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1427 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1428 default:
1429 LOG(FATAL) << "Unexpected op kind " << op_kind;
1430 UNREACHABLE();
1431 }
1432}
1433
Scott Wakelingfe885462016-09-22 10:24:38 +01001434void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1435 stream << vixl32::Register(reg);
1436}
1437
1438void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1439 stream << vixl32::SRegister(reg);
1440}
1441
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001442static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001443 uint32_t mask = 0;
1444 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1445 i <= regs.GetLastSRegister().GetCode();
1446 ++i) {
1447 mask |= (1 << i);
1448 }
1449 return mask;
1450}
1451
Artem Serovd4cc5b22016-11-04 11:19:09 +00001452// Saves the register in the stack. Returns the size taken on stack.
1453size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1454 uint32_t reg_id ATTRIBUTE_UNUSED) {
1455 TODO_VIXL32(FATAL);
1456 return 0;
1457}
1458
1459// Restores the register from the stack. Returns the size taken on stack.
1460size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1461 uint32_t reg_id ATTRIBUTE_UNUSED) {
1462 TODO_VIXL32(FATAL);
1463 return 0;
1464}
1465
1466size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1467 uint32_t reg_id ATTRIBUTE_UNUSED) {
1468 TODO_VIXL32(FATAL);
1469 return 0;
1470}
1471
1472size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1473 uint32_t reg_id ATTRIBUTE_UNUSED) {
1474 TODO_VIXL32(FATAL);
1475 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001476}
1477
Anton Kirilov74234da2017-01-13 14:42:47 +00001478static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1479 vixl32::Register out,
1480 vixl32::Register first,
1481 const Operand& second,
1482 CodeGeneratorARMVIXL* codegen) {
1483 if (second.IsImmediate() && second.GetImmediate() == 0) {
1484 const Operand in = kind == HInstruction::kAnd
1485 ? Operand(0)
1486 : Operand(first);
1487
1488 __ Mov(out, in);
1489 } else {
1490 switch (kind) {
1491 case HInstruction::kAdd:
1492 __ Add(out, first, second);
1493 break;
1494 case HInstruction::kAnd:
1495 __ And(out, first, second);
1496 break;
1497 case HInstruction::kOr:
1498 __ Orr(out, first, second);
1499 break;
1500 case HInstruction::kSub:
1501 __ Sub(out, first, second);
1502 break;
1503 case HInstruction::kXor:
1504 __ Eor(out, first, second);
1505 break;
1506 default:
1507 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1508 UNREACHABLE();
1509 }
1510 }
1511}
1512
1513static void GenerateDataProc(HInstruction::InstructionKind kind,
1514 const Location& out,
1515 const Location& first,
1516 const Operand& second_lo,
1517 const Operand& second_hi,
1518 CodeGeneratorARMVIXL* codegen) {
1519 const vixl32::Register first_hi = HighRegisterFrom(first);
1520 const vixl32::Register first_lo = LowRegisterFrom(first);
1521 const vixl32::Register out_hi = HighRegisterFrom(out);
1522 const vixl32::Register out_lo = LowRegisterFrom(out);
1523
1524 if (kind == HInstruction::kAdd) {
1525 __ Adds(out_lo, first_lo, second_lo);
1526 __ Adc(out_hi, first_hi, second_hi);
1527 } else if (kind == HInstruction::kSub) {
1528 __ Subs(out_lo, first_lo, second_lo);
1529 __ Sbc(out_hi, first_hi, second_hi);
1530 } else {
1531 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1532 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1533 }
1534}
1535
1536static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1537 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1538}
1539
1540static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1541 CodeGeneratorARMVIXL* codegen) {
1542 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1543 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1544
1545 const LocationSummary* const locations = instruction->GetLocations();
1546 const uint32_t shift_value = instruction->GetShiftAmount();
1547 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1548 const Location first = locations->InAt(0);
1549 const Location second = locations->InAt(1);
1550 const Location out = locations->Out();
1551 const vixl32::Register first_hi = HighRegisterFrom(first);
1552 const vixl32::Register first_lo = LowRegisterFrom(first);
1553 const vixl32::Register out_hi = HighRegisterFrom(out);
1554 const vixl32::Register out_lo = LowRegisterFrom(out);
1555 const vixl32::Register second_hi = HighRegisterFrom(second);
1556 const vixl32::Register second_lo = LowRegisterFrom(second);
1557 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1558
1559 if (shift_value >= 32) {
1560 if (shift == ShiftType::LSL) {
1561 GenerateDataProcInstruction(kind,
1562 out_hi,
1563 first_hi,
1564 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1565 codegen);
1566 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1567 } else if (shift == ShiftType::ASR) {
1568 GenerateDataProc(kind,
1569 out,
1570 first,
1571 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1572 Operand(second_hi, ShiftType::ASR, 31),
1573 codegen);
1574 } else {
1575 DCHECK_EQ(shift, ShiftType::LSR);
1576 GenerateDataProc(kind,
1577 out,
1578 first,
1579 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1580 0,
1581 codegen);
1582 }
1583 } else {
1584 DCHECK_GT(shift_value, 1U);
1585 DCHECK_LT(shift_value, 32U);
1586
1587 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1588
1589 if (shift == ShiftType::LSL) {
1590 // We are not doing this for HInstruction::kAdd because the output will require
1591 // Location::kOutputOverlap; not applicable to other cases.
1592 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1593 GenerateDataProcInstruction(kind,
1594 out_hi,
1595 first_hi,
1596 Operand(second_hi, ShiftType::LSL, shift_value),
1597 codegen);
1598 GenerateDataProcInstruction(kind,
1599 out_hi,
1600 out_hi,
1601 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1602 codegen);
1603 GenerateDataProcInstruction(kind,
1604 out_lo,
1605 first_lo,
1606 Operand(second_lo, ShiftType::LSL, shift_value),
1607 codegen);
1608 } else {
1609 const vixl32::Register temp = temps.Acquire();
1610
1611 __ Lsl(temp, second_hi, shift_value);
1612 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1613 GenerateDataProc(kind,
1614 out,
1615 first,
1616 Operand(second_lo, ShiftType::LSL, shift_value),
1617 temp,
1618 codegen);
1619 }
1620 } else {
1621 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1622
1623 // We are not doing this for HInstruction::kAdd because the output will require
1624 // Location::kOutputOverlap; not applicable to other cases.
1625 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1626 GenerateDataProcInstruction(kind,
1627 out_lo,
1628 first_lo,
1629 Operand(second_lo, ShiftType::LSR, shift_value),
1630 codegen);
1631 GenerateDataProcInstruction(kind,
1632 out_lo,
1633 out_lo,
1634 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1635 codegen);
1636 GenerateDataProcInstruction(kind,
1637 out_hi,
1638 first_hi,
1639 Operand(second_hi, shift, shift_value),
1640 codegen);
1641 } else {
1642 const vixl32::Register temp = temps.Acquire();
1643
1644 __ Lsr(temp, second_lo, shift_value);
1645 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1646 GenerateDataProc(kind,
1647 out,
1648 first,
1649 temp,
1650 Operand(second_hi, shift, shift_value),
1651 codegen);
1652 }
1653 }
1654 }
1655}
1656
Donghui Bai426b49c2016-11-08 14:55:38 +08001657static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1658 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1659 if (rhs_loc.IsConstant()) {
1660 // 0.0 is the only immediate that can be encoded directly in
1661 // a VCMP instruction.
1662 //
1663 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1664 // specify that in a floating-point comparison, positive zero
1665 // and negative zero are considered equal, so we can use the
1666 // literal 0.0 for both cases here.
1667 //
1668 // Note however that some methods (Float.equal, Float.compare,
1669 // Float.compareTo, Double.equal, Double.compare,
1670 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1671 // StrictMath.min) consider 0.0 to be (strictly) greater than
1672 // -0.0. So if we ever translate calls to these methods into a
1673 // HCompare instruction, we must handle the -0.0 case with
1674 // care here.
1675 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1676
1677 const Primitive::Type type = instruction->InputAt(0)->GetType();
1678
1679 if (type == Primitive::kPrimFloat) {
1680 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1681 } else {
1682 DCHECK_EQ(type, Primitive::kPrimDouble);
1683 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1684 }
1685 } else {
1686 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1687 }
1688}
1689
1690static vixl32::Condition GenerateLongTestConstant(HCondition* condition,
1691 bool invert,
1692 CodeGeneratorARMVIXL* codegen) {
1693 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1694
1695 const LocationSummary* const locations = condition->GetLocations();
1696 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1697 vixl32::Condition ret = eq;
1698 const Location left = locations->InAt(0);
1699 const Location right = locations->InAt(1);
1700
1701 DCHECK(right.IsConstant());
1702
1703 const vixl32::Register left_high = HighRegisterFrom(left);
1704 const vixl32::Register left_low = LowRegisterFrom(left);
1705 int64_t value = Int64ConstantFrom(right);
1706
1707 switch (cond) {
1708 case kCondEQ:
1709 case kCondNE:
1710 case kCondB:
1711 case kCondBE:
1712 case kCondA:
1713 case kCondAE: {
1714 __ Cmp(left_high, High32Bits(value));
1715
1716 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1717 2 * vixl32::k16BitT32InstructionSizeInBytes,
1718 CodeBufferCheckScope::kExactSize);
1719
1720 __ it(eq);
1721 __ cmp(eq, left_low, Low32Bits(value));
1722 ret = ARMUnsignedCondition(cond);
1723 break;
1724 }
1725 case kCondLE:
1726 case kCondGT:
1727 // Trivially true or false.
1728 if (value == std::numeric_limits<int64_t>::max()) {
1729 __ Cmp(left_low, left_low);
1730 ret = cond == kCondLE ? eq : ne;
1731 break;
1732 }
1733
1734 if (cond == kCondLE) {
1735 cond = kCondLT;
1736 } else {
1737 DCHECK_EQ(cond, kCondGT);
1738 cond = kCondGE;
1739 }
1740
1741 value++;
1742 FALLTHROUGH_INTENDED;
1743 case kCondGE:
1744 case kCondLT: {
1745 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1746
1747 __ Cmp(left_low, Low32Bits(value));
1748 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
1749 ret = ARMCondition(cond);
1750 break;
1751 }
1752 default:
1753 LOG(FATAL) << "Unreachable";
1754 UNREACHABLE();
1755 }
1756
1757 return ret;
1758}
1759
1760static vixl32::Condition GenerateLongTest(HCondition* condition,
1761 bool invert,
1762 CodeGeneratorARMVIXL* codegen) {
1763 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1764
1765 const LocationSummary* const locations = condition->GetLocations();
1766 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1767 vixl32::Condition ret = eq;
1768 Location left = locations->InAt(0);
1769 Location right = locations->InAt(1);
1770
1771 DCHECK(right.IsRegisterPair());
1772
1773 switch (cond) {
1774 case kCondEQ:
1775 case kCondNE:
1776 case kCondB:
1777 case kCondBE:
1778 case kCondA:
1779 case kCondAE: {
1780 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1781
1782 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1783 2 * vixl32::k16BitT32InstructionSizeInBytes,
1784 CodeBufferCheckScope::kExactSize);
1785
1786 __ it(eq);
1787 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
1788 ret = ARMUnsignedCondition(cond);
1789 break;
1790 }
1791 case kCondLE:
1792 case kCondGT:
1793 if (cond == kCondLE) {
1794 cond = kCondGE;
1795 } else {
1796 DCHECK_EQ(cond, kCondGT);
1797 cond = kCondLT;
1798 }
1799
1800 std::swap(left, right);
1801 FALLTHROUGH_INTENDED;
1802 case kCondGE:
1803 case kCondLT: {
1804 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1805
1806 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1807 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
1808 ret = ARMCondition(cond);
1809 break;
1810 }
1811 default:
1812 LOG(FATAL) << "Unreachable";
1813 UNREACHABLE();
1814 }
1815
1816 return ret;
1817}
1818
1819static vixl32::Condition GenerateTest(HInstruction* instruction,
1820 Location loc,
1821 bool invert,
1822 CodeGeneratorARMVIXL* codegen) {
1823 DCHECK(!instruction->IsConstant());
1824
1825 vixl32::Condition ret = invert ? eq : ne;
1826
1827 if (IsBooleanValueOrMaterializedCondition(instruction)) {
1828 __ Cmp(RegisterFrom(loc), 0);
1829 } else {
1830 HCondition* const condition = instruction->AsCondition();
1831 const Primitive::Type type = condition->GetLeft()->GetType();
1832 const IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1833
1834 if (type == Primitive::kPrimLong) {
1835 ret = condition->GetLocations()->InAt(1).IsConstant()
1836 ? GenerateLongTestConstant(condition, invert, codegen)
1837 : GenerateLongTest(condition, invert, codegen);
1838 } else if (Primitive::IsFloatingPointType(type)) {
1839 GenerateVcmp(condition, codegen);
1840 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1841 ret = ARMFPCondition(cond, condition->IsGtBias());
1842 } else {
1843 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1844 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1845 ret = ARMCondition(cond);
1846 }
1847 }
1848
1849 return ret;
1850}
1851
1852static bool CanGenerateTest(HInstruction* condition, ArmVIXLAssembler* assembler) {
1853 if (!IsBooleanValueOrMaterializedCondition(condition)) {
1854 const HCondition* const cond = condition->AsCondition();
1855
1856 if (cond->GetLeft()->GetType() == Primitive::kPrimLong) {
1857 const LocationSummary* const locations = cond->GetLocations();
1858 const IfCondition c = cond->GetCondition();
1859
1860 if (locations->InAt(1).IsConstant()) {
1861 const int64_t value = Int64ConstantFrom(locations->InAt(1));
1862
1863 if (c < kCondLT || c > kCondGE) {
1864 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1865 // we check that the least significant half of the first input to be compared
1866 // is in a low register (the other half is read outside an IT block), and
1867 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1868 // encoding can be used.
1869 if (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value))) {
1870 return false;
1871 }
1872 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
1873 // the previous one, but are not strictly necessary.
1874 } else if (c == kCondLE || c == kCondGT) {
1875 if (value < std::numeric_limits<int64_t>::max() &&
1876 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
1877 return false;
1878 }
1879 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
1880 return false;
1881 }
1882 }
1883 }
1884 }
1885
1886 return true;
1887}
1888
1889static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1890 const Primitive::Type type = constant->GetType();
1891 bool ret = false;
1892
1893 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1894
1895 if (type == Primitive::kPrimLong) {
1896 const uint64_t value = Uint64ConstantFrom(constant);
1897
1898 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1899 } else {
1900 ret = IsUint<8>(Int32ConstantFrom(constant));
1901 }
1902
1903 return ret;
1904}
1905
1906static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1907 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1908
1909 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1910 return Location::ConstantLocation(constant->AsConstant());
1911 }
1912
1913 return Location::RequiresRegister();
1914}
1915
1916static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1917 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1918 // we check that we are not dealing with floating-point output (there is no
1919 // 16-bit VMOV encoding).
1920 if (!out.IsRegister() && !out.IsRegisterPair()) {
1921 return false;
1922 }
1923
1924 // For constants, we also check that the output is in one or two low registers,
1925 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1926 // MOV encoding can be used.
1927 if (src.IsConstant()) {
1928 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1929 return false;
1930 }
1931
1932 if (out.IsRegister()) {
1933 if (!RegisterFrom(out).IsLow()) {
1934 return false;
1935 }
1936 } else {
1937 DCHECK(out.IsRegisterPair());
1938
1939 if (!HighRegisterFrom(out).IsLow()) {
1940 return false;
1941 }
1942 }
1943 }
1944
1945 return true;
1946}
1947
Scott Wakelingfe885462016-09-22 10:24:38 +01001948#undef __
1949
Donghui Bai426b49c2016-11-08 14:55:38 +08001950vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1951 vixl32::Label* final_label) {
1952 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001953 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001954
1955 const HBasicBlock* const block = instruction->GetBlock();
1956 const HLoopInformation* const info = block->GetLoopInformation();
1957 HInstruction* const next = instruction->GetNext();
1958
1959 // Avoid a branch to a branch.
1960 if (next->IsGoto() && (info == nullptr ||
1961 !info->IsBackEdge(*block) ||
1962 !info->HasSuspendCheck())) {
1963 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1964 }
1965
1966 return final_label;
1967}
1968
Scott Wakelingfe885462016-09-22 10:24:38 +01001969CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1970 const ArmInstructionSetFeatures& isa_features,
1971 const CompilerOptions& compiler_options,
1972 OptimizingCompilerStats* stats)
1973 : CodeGenerator(graph,
1974 kNumberOfCoreRegisters,
1975 kNumberOfSRegisters,
1976 kNumberOfRegisterPairs,
1977 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001978 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001979 compiler_options,
1980 stats),
1981 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001982 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001983 location_builder_(graph, this),
1984 instruction_visitor_(graph, this),
1985 move_resolver_(graph->GetArena(), this),
1986 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001987 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001988 uint32_literals_(std::less<uint32_t>(),
1989 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001990 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001991 boot_image_string_patches_(StringReferenceValueComparator(),
1992 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001993 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001994 boot_image_type_patches_(TypeReferenceValueComparator(),
1995 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1996 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001997 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001998 jit_string_patches_(StringReferenceValueComparator(),
1999 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2000 jit_class_patches_(TypeReferenceValueComparator(),
2001 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002002 // Always save the LR register to mimic Quick.
2003 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002004 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2005 // S0-S31, which alias to D0-D15.
2006 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2007 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002008}
2009
Artem Serov551b28f2016-10-18 19:11:30 +01002010void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2011 uint32_t num_entries = switch_instr_->GetNumEntries();
2012 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2013
2014 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002015 // underlying code buffer and we have generated a jump table of the right size, using
2016 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002017 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2018 num_entries * sizeof(int32_t),
2019 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002020 // TODO(VIXL): Check that using lower case bind is fine here.
2021 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002022 for (uint32_t i = 0; i < num_entries; i++) {
2023 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2024 }
2025}
2026
2027void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2028 uint32_t num_entries = switch_instr_->GetNumEntries();
2029 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2030
Artem Serov551b28f2016-10-18 19:11:30 +01002031 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2032 for (uint32_t i = 0; i < num_entries; i++) {
2033 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2034 DCHECK(target_label->IsBound());
2035 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2036 // When doing BX to address we need to have lower bit set to 1 in T32.
2037 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2038 jump_offset++;
2039 }
2040 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2041 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002042
Scott Wakelingb77051e2016-11-21 19:46:00 +00002043 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002044 }
2045}
2046
Artem Serov09a940d2016-11-11 16:15:11 +00002047void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002048 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002049 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002050 }
2051}
2052
Andreas Gampeca620d72016-11-08 08:09:33 -08002053#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002054
2055void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002056 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002057 GetAssembler()->FinalizeCode();
2058 CodeGenerator::Finalize(allocator);
2059}
2060
2061void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002062 // Stack register, LR and PC are always reserved.
2063 blocked_core_registers_[SP] = true;
2064 blocked_core_registers_[LR] = true;
2065 blocked_core_registers_[PC] = true;
2066
2067 // Reserve thread register.
2068 blocked_core_registers_[TR] = true;
2069
2070 // Reserve temp register.
2071 blocked_core_registers_[IP] = true;
2072
2073 if (GetGraph()->IsDebuggable()) {
2074 // Stubs do not save callee-save floating point registers. If the graph
2075 // is debuggable, we need to deal with these registers differently. For
2076 // now, just block them.
2077 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2078 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2079 ++i) {
2080 blocked_fpu_registers_[i] = true;
2081 }
2082 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002083}
2084
Scott Wakelingfe885462016-09-22 10:24:38 +01002085InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2086 CodeGeneratorARMVIXL* codegen)
2087 : InstructionCodeGenerator(graph, codegen),
2088 assembler_(codegen->GetAssembler()),
2089 codegen_(codegen) {}
2090
2091void CodeGeneratorARMVIXL::ComputeSpillMask() {
2092 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2093 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2094 // There is no easy instruction to restore just the PC on thumb2. We spill and
2095 // restore another arbitrary register.
2096 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2097 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2098 // We use vpush and vpop for saving and restoring floating point registers, which take
2099 // a SRegister and the number of registers to save/restore after that SRegister. We
2100 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2101 // but in the range.
2102 if (fpu_spill_mask_ != 0) {
2103 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2104 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2105 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2106 fpu_spill_mask_ |= (1 << i);
2107 }
2108 }
2109}
2110
2111void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2112 bool skip_overflow_check =
2113 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2114 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2115 __ Bind(&frame_entry_label_);
2116
2117 if (HasEmptyFrame()) {
2118 return;
2119 }
2120
Scott Wakelingfe885462016-09-22 10:24:38 +01002121 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002122 UseScratchRegisterScope temps(GetVIXLAssembler());
2123 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002124 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002125 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002126 ExactAssemblyScope aas(GetVIXLAssembler(),
2127 vixl32::kMaxInstructionSizeInBytes,
2128 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002129 __ ldr(temp, MemOperand(temp));
2130 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002131 }
2132
2133 __ Push(RegisterList(core_spill_mask_));
2134 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2135 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2136 0,
2137 core_spill_mask_,
2138 kArmWordSize);
2139 if (fpu_spill_mask_ != 0) {
2140 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2141
2142 // Check that list is contiguous.
2143 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2144
2145 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2146 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002147 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002148 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002149
2150 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2151 UseScratchRegisterScope temps(GetVIXLAssembler());
2152 vixl32::Register temp = temps.Acquire();
2153 // Initialize should_deoptimize flag to 0.
2154 __ Mov(temp, 0);
2155 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2156 }
2157
Scott Wakelingfe885462016-09-22 10:24:38 +01002158 int adjust = GetFrameSize() - FrameEntrySpillSize();
2159 __ Sub(sp, sp, adjust);
2160 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002161
2162 // Save the current method if we need it. Note that we do not
2163 // do this in HCurrentMethod, as the instruction might have been removed
2164 // in the SSA graph.
2165 if (RequiresCurrentMethod()) {
2166 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2167 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002168}
2169
2170void CodeGeneratorARMVIXL::GenerateFrameExit() {
2171 if (HasEmptyFrame()) {
2172 __ Bx(lr);
2173 return;
2174 }
2175 GetAssembler()->cfi().RememberState();
2176 int adjust = GetFrameSize() - FrameEntrySpillSize();
2177 __ Add(sp, sp, adjust);
2178 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2179 if (fpu_spill_mask_ != 0) {
2180 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2181
2182 // Check that list is contiguous.
2183 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2184
2185 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2186 GetAssembler()->cfi().AdjustCFAOffset(
2187 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002188 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002189 }
2190 // Pop LR into PC to return.
2191 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2192 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2193 __ Pop(RegisterList(pop_mask));
2194 GetAssembler()->cfi().RestoreState();
2195 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2196}
2197
2198void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2199 __ Bind(GetLabelOf(block));
2200}
2201
Artem Serovd4cc5b22016-11-04 11:19:09 +00002202Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2203 switch (type) {
2204 case Primitive::kPrimBoolean:
2205 case Primitive::kPrimByte:
2206 case Primitive::kPrimChar:
2207 case Primitive::kPrimShort:
2208 case Primitive::kPrimInt:
2209 case Primitive::kPrimNot: {
2210 uint32_t index = gp_index_++;
2211 uint32_t stack_index = stack_index_++;
2212 if (index < calling_convention.GetNumberOfRegisters()) {
2213 return LocationFrom(calling_convention.GetRegisterAt(index));
2214 } else {
2215 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2216 }
2217 }
2218
2219 case Primitive::kPrimLong: {
2220 uint32_t index = gp_index_;
2221 uint32_t stack_index = stack_index_;
2222 gp_index_ += 2;
2223 stack_index_ += 2;
2224 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2225 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2226 // Skip R1, and use R2_R3 instead.
2227 gp_index_++;
2228 index++;
2229 }
2230 }
2231 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2232 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2233 calling_convention.GetRegisterAt(index + 1).GetCode());
2234
2235 return LocationFrom(calling_convention.GetRegisterAt(index),
2236 calling_convention.GetRegisterAt(index + 1));
2237 } else {
2238 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2239 }
2240 }
2241
2242 case Primitive::kPrimFloat: {
2243 uint32_t stack_index = stack_index_++;
2244 if (float_index_ % 2 == 0) {
2245 float_index_ = std::max(double_index_, float_index_);
2246 }
2247 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2248 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2249 } else {
2250 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2251 }
2252 }
2253
2254 case Primitive::kPrimDouble: {
2255 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2256 uint32_t stack_index = stack_index_;
2257 stack_index_ += 2;
2258 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2259 uint32_t index = double_index_;
2260 double_index_ += 2;
2261 Location result = LocationFrom(
2262 calling_convention.GetFpuRegisterAt(index),
2263 calling_convention.GetFpuRegisterAt(index + 1));
2264 DCHECK(ExpectedPairLayout(result));
2265 return result;
2266 } else {
2267 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2268 }
2269 }
2270
2271 case Primitive::kPrimVoid:
2272 LOG(FATAL) << "Unexpected parameter type " << type;
2273 break;
2274 }
2275 return Location::NoLocation();
2276}
2277
2278Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2279 switch (type) {
2280 case Primitive::kPrimBoolean:
2281 case Primitive::kPrimByte:
2282 case Primitive::kPrimChar:
2283 case Primitive::kPrimShort:
2284 case Primitive::kPrimInt:
2285 case Primitive::kPrimNot: {
2286 return LocationFrom(r0);
2287 }
2288
2289 case Primitive::kPrimFloat: {
2290 return LocationFrom(s0);
2291 }
2292
2293 case Primitive::kPrimLong: {
2294 return LocationFrom(r0, r1);
2295 }
2296
2297 case Primitive::kPrimDouble: {
2298 return LocationFrom(s0, s1);
2299 }
2300
2301 case Primitive::kPrimVoid:
2302 return Location::NoLocation();
2303 }
2304
2305 UNREACHABLE();
2306}
2307
2308Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2309 return LocationFrom(kMethodRegister);
2310}
2311
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002312void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2313 if (source.Equals(destination)) {
2314 return;
2315 }
2316 if (destination.IsRegister()) {
2317 if (source.IsRegister()) {
2318 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2319 } else if (source.IsFpuRegister()) {
2320 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2321 } else {
2322 GetAssembler()->LoadFromOffset(kLoadWord,
2323 RegisterFrom(destination),
2324 sp,
2325 source.GetStackIndex());
2326 }
2327 } else if (destination.IsFpuRegister()) {
2328 if (source.IsRegister()) {
2329 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2330 } else if (source.IsFpuRegister()) {
2331 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2332 } else {
2333 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2334 }
2335 } else {
2336 DCHECK(destination.IsStackSlot()) << destination;
2337 if (source.IsRegister()) {
2338 GetAssembler()->StoreToOffset(kStoreWord,
2339 RegisterFrom(source),
2340 sp,
2341 destination.GetStackIndex());
2342 } else if (source.IsFpuRegister()) {
2343 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2344 } else {
2345 DCHECK(source.IsStackSlot()) << source;
2346 UseScratchRegisterScope temps(GetVIXLAssembler());
2347 vixl32::Register temp = temps.Acquire();
2348 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2349 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2350 }
2351 }
2352}
2353
Artem Serovcfbe9132016-10-14 15:58:56 +01002354void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2355 DCHECK(location.IsRegister());
2356 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002357}
2358
2359void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002360 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2361 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2362 HParallelMove move(GetGraph()->GetArena());
2363 move.AddMove(src, dst, dst_type, nullptr);
2364 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002365}
2366
Artem Serovcfbe9132016-10-14 15:58:56 +01002367void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2368 if (location.IsRegister()) {
2369 locations->AddTemp(location);
2370 } else if (location.IsRegisterPair()) {
2371 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2372 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2373 } else {
2374 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2375 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002376}
2377
2378void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2379 HInstruction* instruction,
2380 uint32_t dex_pc,
2381 SlowPathCode* slow_path) {
2382 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002383 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2384 // Ensure the pc position is recorded immediately after the `blx` instruction.
2385 // 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 +00002386 ExactAssemblyScope aas(GetVIXLAssembler(),
2387 vixl32::k16BitT32InstructionSizeInBytes,
2388 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002389 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002390 if (EntrypointRequiresStackMap(entrypoint)) {
2391 RecordPcInfo(instruction, dex_pc, slow_path);
2392 }
2393}
2394
2395void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2396 HInstruction* instruction,
2397 SlowPathCode* slow_path) {
2398 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002399 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002400 __ Blx(lr);
2401}
2402
Scott Wakelingfe885462016-09-22 10:24:38 +01002403void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2404 DCHECK(!successor->IsExitBlock());
2405 HBasicBlock* block = got->GetBlock();
2406 HInstruction* previous = got->GetPrevious();
2407 HLoopInformation* info = block->GetLoopInformation();
2408
2409 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2410 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2411 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2412 return;
2413 }
2414 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2415 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2416 }
2417 if (!codegen_->GoesToNextBlock(block, successor)) {
2418 __ B(codegen_->GetLabelOf(successor));
2419 }
2420}
2421
2422void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2423 got->SetLocations(nullptr);
2424}
2425
2426void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2427 HandleGoto(got, got->GetSuccessor());
2428}
2429
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002430void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2431 try_boundary->SetLocations(nullptr);
2432}
2433
2434void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2435 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2436 if (!successor->IsExitBlock()) {
2437 HandleGoto(try_boundary, successor);
2438 }
2439}
2440
Scott Wakelingfe885462016-09-22 10:24:38 +01002441void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2442 exit->SetLocations(nullptr);
2443}
2444
2445void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2446}
2447
Scott Wakelingfe885462016-09-22 10:24:38 +01002448void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
2449 vixl32::Label* true_label,
2450 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
2451 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
2452 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2453 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
2454}
2455
2456void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2457 vixl32::Label* true_label,
2458 vixl32::Label* false_label) {
2459 LocationSummary* locations = cond->GetLocations();
2460 Location left = locations->InAt(0);
2461 Location right = locations->InAt(1);
2462 IfCondition if_cond = cond->GetCondition();
2463
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002464 vixl32::Register left_high = HighRegisterFrom(left);
2465 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002466 IfCondition true_high_cond = if_cond;
2467 IfCondition false_high_cond = cond->GetOppositeCondition();
2468 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2469
2470 // Set the conditions for the test, remembering that == needs to be
2471 // decided using the low words.
2472 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
2473 switch (if_cond) {
2474 case kCondEQ:
2475 case kCondNE:
2476 // Nothing to do.
2477 break;
2478 case kCondLT:
2479 false_high_cond = kCondGT;
2480 break;
2481 case kCondLE:
2482 true_high_cond = kCondLT;
2483 break;
2484 case kCondGT:
2485 false_high_cond = kCondLT;
2486 break;
2487 case kCondGE:
2488 true_high_cond = kCondGT;
2489 break;
2490 case kCondB:
2491 false_high_cond = kCondA;
2492 break;
2493 case kCondBE:
2494 true_high_cond = kCondB;
2495 break;
2496 case kCondA:
2497 false_high_cond = kCondB;
2498 break;
2499 case kCondAE:
2500 true_high_cond = kCondA;
2501 break;
2502 }
2503 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002504 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002505 int32_t val_low = Low32Bits(value);
2506 int32_t val_high = High32Bits(value);
2507
2508 __ Cmp(left_high, val_high);
2509 if (if_cond == kCondNE) {
2510 __ B(ARMCondition(true_high_cond), true_label);
2511 } else if (if_cond == kCondEQ) {
2512 __ B(ARMCondition(false_high_cond), false_label);
2513 } else {
2514 __ B(ARMCondition(true_high_cond), true_label);
2515 __ B(ARMCondition(false_high_cond), false_label);
2516 }
2517 // Must be equal high, so compare the lows.
2518 __ Cmp(left_low, val_low);
2519 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002520 vixl32::Register right_high = HighRegisterFrom(right);
2521 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002522
2523 __ Cmp(left_high, right_high);
2524 if (if_cond == kCondNE) {
2525 __ B(ARMCondition(true_high_cond), true_label);
2526 } else if (if_cond == kCondEQ) {
2527 __ B(ARMCondition(false_high_cond), false_label);
2528 } else {
2529 __ B(ARMCondition(true_high_cond), true_label);
2530 __ B(ARMCondition(false_high_cond), false_label);
2531 }
2532 // Must be equal high, so compare the lows.
2533 __ Cmp(left_low, right_low);
2534 }
2535 // The last comparison might be unsigned.
2536 // TODO: optimize cases where this is always true/false
2537 __ B(final_condition, true_label);
2538}
2539
2540void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2541 vixl32::Label* true_target_in,
2542 vixl32::Label* false_target_in) {
2543 // Generated branching requires both targets to be explicit. If either of the
2544 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2545 vixl32::Label fallthrough;
2546 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2547 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2548
2549 Primitive::Type type = condition->InputAt(0)->GetType();
2550 switch (type) {
2551 case Primitive::kPrimLong:
2552 GenerateLongComparesAndJumps(condition, true_target, false_target);
2553 break;
2554 case Primitive::kPrimFloat:
2555 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002556 GenerateVcmp(condition, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002557 GenerateFPJumps(condition, true_target, false_target);
2558 break;
2559 default:
2560 LOG(FATAL) << "Unexpected compare type " << type;
2561 }
2562
2563 if (false_target != &fallthrough) {
2564 __ B(false_target);
2565 }
2566
2567 if (true_target_in == nullptr || false_target_in == nullptr) {
2568 __ Bind(&fallthrough);
2569 }
2570}
2571
2572void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2573 size_t condition_input_index,
2574 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002575 vixl32::Label* false_target,
2576 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002577 HInstruction* cond = instruction->InputAt(condition_input_index);
2578
2579 if (true_target == nullptr && false_target == nullptr) {
2580 // Nothing to do. The code always falls through.
2581 return;
2582 } else if (cond->IsIntConstant()) {
2583 // Constant condition, statically compared against "true" (integer value 1).
2584 if (cond->AsIntConstant()->IsTrue()) {
2585 if (true_target != nullptr) {
2586 __ B(true_target);
2587 }
2588 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002589 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002590 if (false_target != nullptr) {
2591 __ B(false_target);
2592 }
2593 }
2594 return;
2595 }
2596
2597 // The following code generates these patterns:
2598 // (1) true_target == nullptr && false_target != nullptr
2599 // - opposite condition true => branch to false_target
2600 // (2) true_target != nullptr && false_target == nullptr
2601 // - condition true => branch to true_target
2602 // (3) true_target != nullptr && false_target != nullptr
2603 // - condition true => branch to true_target
2604 // - branch to false_target
2605 if (IsBooleanValueOrMaterializedCondition(cond)) {
2606 // Condition has been materialized, compare the output to 0.
2607 if (kIsDebugBuild) {
2608 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2609 DCHECK(cond_val.IsRegister());
2610 }
2611 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002612 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2613 false_target,
2614 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002615 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002616 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2617 true_target,
2618 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002619 }
2620 } else {
2621 // Condition has not been materialized. Use its inputs as the comparison and
2622 // its condition as the branch condition.
2623 HCondition* condition = cond->AsCondition();
2624
2625 // If this is a long or FP comparison that has been folded into
2626 // the HCondition, generate the comparison directly.
2627 Primitive::Type type = condition->InputAt(0)->GetType();
2628 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2629 GenerateCompareTestAndBranch(condition, true_target, false_target);
2630 return;
2631 }
2632
Donghui Bai426b49c2016-11-08 14:55:38 +08002633 vixl32::Label* non_fallthrough_target;
2634 vixl32::Condition arm_cond = vixl32::Condition::None();
2635 const vixl32::Register left = InputRegisterAt(cond, 0);
2636 const Operand right = InputOperandAt(cond, 1);
2637
Scott Wakelingfe885462016-09-22 10:24:38 +01002638 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002639 arm_cond = ARMCondition(condition->GetOppositeCondition());
2640 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002641 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002642 arm_cond = ARMCondition(condition->GetCondition());
2643 non_fallthrough_target = true_target;
2644 }
2645
2646 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2647 if (arm_cond.Is(eq)) {
2648 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2649 } else {
2650 DCHECK(arm_cond.Is(ne));
2651 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2652 }
2653 } else {
2654 __ Cmp(left, right);
2655 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002656 }
2657 }
2658
2659 // If neither branch falls through (case 3), the conditional branch to `true_target`
2660 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2661 if (true_target != nullptr && false_target != nullptr) {
2662 __ B(false_target);
2663 }
2664}
2665
2666void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2668 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2669 locations->SetInAt(0, Location::RequiresRegister());
2670 }
2671}
2672
2673void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2674 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2675 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002676 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2677 nullptr : codegen_->GetLabelOf(true_successor);
2678 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2679 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002680 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2681}
2682
Scott Wakelingc34dba72016-10-03 10:14:44 +01002683void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2684 LocationSummary* locations = new (GetGraph()->GetArena())
2685 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2686 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2687 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2688 locations->SetInAt(0, Location::RequiresRegister());
2689 }
2690}
2691
2692void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2693 SlowPathCodeARMVIXL* slow_path =
2694 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2695 GenerateTestAndBranch(deoptimize,
2696 /* condition_input_index */ 0,
2697 slow_path->GetEntryLabel(),
2698 /* false_target */ nullptr);
2699}
2700
Artem Serovd4cc5b22016-11-04 11:19:09 +00002701void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2702 LocationSummary* locations = new (GetGraph()->GetArena())
2703 LocationSummary(flag, LocationSummary::kNoCall);
2704 locations->SetOut(Location::RequiresRegister());
2705}
2706
2707void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2708 GetAssembler()->LoadFromOffset(kLoadWord,
2709 OutputRegister(flag),
2710 sp,
2711 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2712}
2713
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002714void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002716 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2717
2718 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002719 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002720 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002721 } else {
2722 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002723 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002724 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002725
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002726 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002727 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2728 // The code generator handles overlap with the values, but not with the condition.
2729 locations->SetOut(Location::SameAsFirstInput());
2730 } else if (is_floating_point) {
2731 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2732 } else {
2733 if (!locations->InAt(1).IsConstant()) {
2734 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2735 }
2736
2737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002738 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002739}
2740
2741void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002742 HInstruction* const condition = select->GetCondition();
2743 const LocationSummary* const locations = select->GetLocations();
2744 const Primitive::Type type = select->GetType();
2745 const Location first = locations->InAt(0);
2746 const Location out = locations->Out();
2747 const Location second = locations->InAt(1);
2748 Location src;
2749
2750 if (condition->IsIntConstant()) {
2751 if (condition->AsIntConstant()->IsFalse()) {
2752 src = first;
2753 } else {
2754 src = second;
2755 }
2756
2757 codegen_->MoveLocation(out, src, type);
2758 return;
2759 }
2760
2761 if (!Primitive::IsFloatingPointType(type) &&
2762 CanGenerateTest(condition, codegen_->GetAssembler())) {
2763 bool invert = false;
2764
2765 if (out.Equals(second)) {
2766 src = first;
2767 invert = true;
2768 } else if (out.Equals(first)) {
2769 src = second;
2770 } else if (second.IsConstant()) {
2771 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2772 src = second;
2773 } else if (first.IsConstant()) {
2774 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2775 src = first;
2776 invert = true;
2777 } else {
2778 src = second;
2779 }
2780
2781 if (CanGenerateConditionalMove(out, src)) {
2782 if (!out.Equals(first) && !out.Equals(second)) {
2783 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2784 }
2785
2786 const vixl32::Condition cond = GenerateTest(condition, locations->InAt(2), invert, codegen_);
2787 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
2788 ExactAssemblyScope guard(GetVIXLAssembler(),
2789 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2790 CodeBufferCheckScope::kExactSize);
2791
2792 if (out.IsRegister()) {
2793 __ it(cond);
2794 __ mov(cond, RegisterFrom(out), OperandFrom(src, type));
2795 } else {
2796 DCHECK(out.IsRegisterPair());
2797
2798 Operand operand_high(0);
2799 Operand operand_low(0);
2800
2801 if (src.IsConstant()) {
2802 const int64_t value = Int64ConstantFrom(src);
2803
2804 operand_high = High32Bits(value);
2805 operand_low = Low32Bits(value);
2806 } else {
2807 DCHECK(src.IsRegisterPair());
2808 operand_high = HighRegisterFrom(src);
2809 operand_low = LowRegisterFrom(src);
2810 }
2811
2812 __ it(cond);
2813 __ mov(cond, LowRegisterFrom(out), operand_low);
2814 __ it(cond);
2815 __ mov(cond, HighRegisterFrom(out), operand_high);
2816 }
2817
2818 return;
2819 }
2820 }
2821
2822 vixl32::Label* false_target = nullptr;
2823 vixl32::Label* true_target = nullptr;
2824 vixl32::Label select_end;
2825 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2826
2827 if (out.Equals(second)) {
2828 true_target = target;
2829 src = first;
2830 } else {
2831 false_target = target;
2832 src = second;
2833
2834 if (!out.Equals(first)) {
2835 codegen_->MoveLocation(out, first, type);
2836 }
2837 }
2838
2839 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2840 codegen_->MoveLocation(out, src, type);
2841
2842 if (select_end.IsReferenced()) {
2843 __ Bind(&select_end);
2844 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002845}
2846
Artem Serov551b28f2016-10-18 19:11:30 +01002847void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2848 new (GetGraph()->GetArena()) LocationSummary(info);
2849}
2850
2851void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2852 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2853}
2854
Scott Wakelingfe885462016-09-22 10:24:38 +01002855void CodeGeneratorARMVIXL::GenerateNop() {
2856 __ Nop();
2857}
2858
2859void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2860 LocationSummary* locations =
2861 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2862 // Handle the long/FP comparisons made in instruction simplification.
2863 switch (cond->InputAt(0)->GetType()) {
2864 case Primitive::kPrimLong:
2865 locations->SetInAt(0, Location::RequiresRegister());
2866 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2867 if (!cond->IsEmittedAtUseSite()) {
2868 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2869 }
2870 break;
2871
Scott Wakelingfe885462016-09-22 10:24:38 +01002872 case Primitive::kPrimFloat:
2873 case Primitive::kPrimDouble:
2874 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002875 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002876 if (!cond->IsEmittedAtUseSite()) {
2877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2878 }
2879 break;
2880
2881 default:
2882 locations->SetInAt(0, Location::RequiresRegister());
2883 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2884 if (!cond->IsEmittedAtUseSite()) {
2885 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2886 }
2887 }
2888}
2889
2890void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2891 if (cond->IsEmittedAtUseSite()) {
2892 return;
2893 }
2894
Artem Serov657022c2016-11-23 14:19:38 +00002895 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002896 vixl32::Register out = OutputRegister(cond);
2897 vixl32::Label true_label, false_label;
2898
2899 switch (cond->InputAt(0)->GetType()) {
2900 default: {
2901 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002902 if (right.IsRegister()) {
2903 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2904 } else {
2905 DCHECK(right.IsConstant());
2906 __ Cmp(InputRegisterAt(cond, 0),
2907 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2908 }
Artem Serov0fb37192016-12-06 18:13:40 +00002909 ExactAssemblyScope aas(GetVIXLAssembler(),
2910 3 * vixl32::kMaxInstructionSizeInBytes,
2911 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002912 __ ite(ARMCondition(cond->GetCondition()));
2913 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2914 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002915 return;
2916 }
2917 case Primitive::kPrimLong:
2918 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2919 break;
2920 case Primitive::kPrimFloat:
2921 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002922 GenerateVcmp(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002923 GenerateFPJumps(cond, &true_label, &false_label);
2924 break;
2925 }
2926
2927 // Convert the jumps into the result.
2928 vixl32::Label done_label;
Anton Kirilov6f644202017-02-27 18:29:45 +00002929 vixl32::Label* final_label = codegen_->GetFinalLabel(cond, &done_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01002930
2931 // False case: result = 0.
2932 __ Bind(&false_label);
2933 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00002934 __ B(final_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01002935
2936 // True case: result = 1.
2937 __ Bind(&true_label);
2938 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00002939
2940 if (done_label.IsReferenced()) {
2941 __ Bind(&done_label);
2942 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002943}
2944
2945void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2946 HandleCondition(comp);
2947}
2948
2949void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2950 HandleCondition(comp);
2951}
2952
2953void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2954 HandleCondition(comp);
2955}
2956
2957void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2958 HandleCondition(comp);
2959}
2960
2961void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2962 HandleCondition(comp);
2963}
2964
2965void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2966 HandleCondition(comp);
2967}
2968
2969void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2970 HandleCondition(comp);
2971}
2972
2973void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2974 HandleCondition(comp);
2975}
2976
2977void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2978 HandleCondition(comp);
2979}
2980
2981void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2982 HandleCondition(comp);
2983}
2984
2985void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2986 HandleCondition(comp);
2987}
2988
2989void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2990 HandleCondition(comp);
2991}
2992
2993void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2994 HandleCondition(comp);
2995}
2996
2997void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2998 HandleCondition(comp);
2999}
3000
3001void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3002 HandleCondition(comp);
3003}
3004
3005void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3006 HandleCondition(comp);
3007}
3008
3009void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3010 HandleCondition(comp);
3011}
3012
3013void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3014 HandleCondition(comp);
3015}
3016
3017void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3018 HandleCondition(comp);
3019}
3020
3021void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3022 HandleCondition(comp);
3023}
3024
3025void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3026 LocationSummary* locations =
3027 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3028 locations->SetOut(Location::ConstantLocation(constant));
3029}
3030
3031void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3032 // Will be generated at use site.
3033}
3034
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003035void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3036 LocationSummary* locations =
3037 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3038 locations->SetOut(Location::ConstantLocation(constant));
3039}
3040
3041void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3042 // Will be generated at use site.
3043}
3044
Scott Wakelingfe885462016-09-22 10:24:38 +01003045void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3046 LocationSummary* locations =
3047 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3048 locations->SetOut(Location::ConstantLocation(constant));
3049}
3050
3051void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3052 // Will be generated at use site.
3053}
3054
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003055void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3058 locations->SetOut(Location::ConstantLocation(constant));
3059}
3060
Scott Wakelingc34dba72016-10-03 10:14:44 +01003061void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3062 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003063 // Will be generated at use site.
3064}
3065
3066void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3069 locations->SetOut(Location::ConstantLocation(constant));
3070}
3071
Scott Wakelingc34dba72016-10-03 10:14:44 +01003072void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3073 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003074 // Will be generated at use site.
3075}
3076
Scott Wakelingfe885462016-09-22 10:24:38 +01003077void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3078 memory_barrier->SetLocations(nullptr);
3079}
3080
3081void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3082 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3083}
3084
3085void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3086 ret->SetLocations(nullptr);
3087}
3088
3089void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3090 codegen_->GenerateFrameExit();
3091}
3092
3093void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3094 LocationSummary* locations =
3095 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3096 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3097}
3098
3099void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3100 codegen_->GenerateFrameExit();
3101}
3102
Artem Serovcfbe9132016-10-14 15:58:56 +01003103void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3104 // The trampoline uses the same calling convention as dex calling conventions,
3105 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3106 // the method_idx.
3107 HandleInvoke(invoke);
3108}
3109
3110void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3111 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3112}
3113
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003114void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3115 // Explicit clinit checks triggered by static invokes must have been pruned by
3116 // art::PrepareForRegisterAllocation.
3117 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3118
Anton Kirilov5ec62182016-10-13 20:16:02 +01003119 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3120 if (intrinsic.TryDispatch(invoke)) {
3121 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3122 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3123 }
3124 return;
3125 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003126
3127 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003128
Artem Serovd4cc5b22016-11-04 11:19:09 +00003129 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3130 if (invoke->HasPcRelativeDexCache()) {
3131 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3132 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003133}
3134
Anton Kirilov5ec62182016-10-13 20:16:02 +01003135static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3136 if (invoke->GetLocations()->Intrinsified()) {
3137 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3138 intrinsic.Dispatch(invoke);
3139 return true;
3140 }
3141 return false;
3142}
3143
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003144void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3145 // Explicit clinit checks triggered by static invokes must have been pruned by
3146 // art::PrepareForRegisterAllocation.
3147 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3148
Anton Kirilov5ec62182016-10-13 20:16:02 +01003149 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3150 return;
3151 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003152
3153 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003154 codegen_->GenerateStaticOrDirectCall(
3155 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003156 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3157}
3158
3159void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003160 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003161 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3162}
3163
3164void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003165 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3166 if (intrinsic.TryDispatch(invoke)) {
3167 return;
3168 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003169
3170 HandleInvoke(invoke);
3171}
3172
3173void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003174 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3175 return;
3176 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003177
3178 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003179 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003180 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003181}
3182
Artem Serovcfbe9132016-10-14 15:58:56 +01003183void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3184 HandleInvoke(invoke);
3185 // Add the hidden argument.
3186 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3187}
3188
3189void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3190 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3191 LocationSummary* locations = invoke->GetLocations();
3192 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3193 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3194 Location receiver = locations->InAt(0);
3195 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3196
3197 DCHECK(!receiver.IsStackSlot());
3198
Alexandre Rames374ddf32016-11-04 10:40:49 +00003199 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3200 {
Artem Serov0fb37192016-12-06 18:13:40 +00003201 ExactAssemblyScope aas(GetVIXLAssembler(),
3202 vixl32::kMaxInstructionSizeInBytes,
3203 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003204 // /* HeapReference<Class> */ temp = receiver->klass_
3205 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3206 codegen_->MaybeRecordImplicitNullCheck(invoke);
3207 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003208 // Instead of simply (possibly) unpoisoning `temp` here, we should
3209 // emit a read barrier for the previous class reference load.
3210 // However this is not required in practice, as this is an
3211 // intermediate/temporary reference and because the current
3212 // concurrent copying collector keeps the from-space memory
3213 // intact/accessible until the end of the marking phase (the
3214 // concurrent copying collector may not in the future).
3215 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3216 GetAssembler()->LoadFromOffset(kLoadWord,
3217 temp,
3218 temp,
3219 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3220 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3221 invoke->GetImtIndex(), kArmPointerSize));
3222 // temp = temp->GetImtEntryAt(method_offset);
3223 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3224 uint32_t entry_point =
3225 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3226 // LR = temp->GetEntryPoint();
3227 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3228
3229 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3230 // instruction from clobbering it as they might use r12 as a scratch register.
3231 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003232
3233 {
3234 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3235 // so it checks if the application is using them (by passing them to the macro assembler
3236 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3237 // what is available, and is the opposite of the standard usage: Instead of requesting a
3238 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3239 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3240 // (to materialize the constant), since the destination register becomes available for such use
3241 // internally for the duration of the macro instruction.
3242 UseScratchRegisterScope temps(GetVIXLAssembler());
3243 temps.Exclude(hidden_reg);
3244 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3245 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003246 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003247 // Ensure the pc position is recorded immediately after the `blx` instruction.
3248 // 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 +00003249 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003250 vixl32::k16BitT32InstructionSizeInBytes,
3251 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003252 // LR();
3253 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003254 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003255 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003256 }
3257}
3258
Orion Hodsonac141392017-01-13 11:53:47 +00003259void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3260 HandleInvoke(invoke);
3261}
3262
3263void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3264 codegen_->GenerateInvokePolymorphicCall(invoke);
3265}
3266
Artem Serov02109dd2016-09-23 17:17:54 +01003267void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3268 LocationSummary* locations =
3269 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3270 switch (neg->GetResultType()) {
3271 case Primitive::kPrimInt: {
3272 locations->SetInAt(0, Location::RequiresRegister());
3273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3274 break;
3275 }
3276 case Primitive::kPrimLong: {
3277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3279 break;
3280 }
3281
3282 case Primitive::kPrimFloat:
3283 case Primitive::kPrimDouble:
3284 locations->SetInAt(0, Location::RequiresFpuRegister());
3285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3286 break;
3287
3288 default:
3289 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3290 }
3291}
3292
3293void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3294 LocationSummary* locations = neg->GetLocations();
3295 Location out = locations->Out();
3296 Location in = locations->InAt(0);
3297 switch (neg->GetResultType()) {
3298 case Primitive::kPrimInt:
3299 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3300 break;
3301
3302 case Primitive::kPrimLong:
3303 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3304 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3305 // We cannot emit an RSC (Reverse Subtract with Carry)
3306 // instruction here, as it does not exist in the Thumb-2
3307 // instruction set. We use the following approach
3308 // using SBC and SUB instead.
3309 //
3310 // out.hi = -C
3311 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3312 // out.hi = out.hi - in.hi
3313 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3314 break;
3315
3316 case Primitive::kPrimFloat:
3317 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003318 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003319 break;
3320
3321 default:
3322 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3323 }
3324}
3325
Scott Wakelingfe885462016-09-22 10:24:38 +01003326void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3327 Primitive::Type result_type = conversion->GetResultType();
3328 Primitive::Type input_type = conversion->GetInputType();
3329 DCHECK_NE(result_type, input_type);
3330
3331 // The float-to-long, double-to-long and long-to-float type conversions
3332 // rely on a call to the runtime.
3333 LocationSummary::CallKind call_kind =
3334 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3335 && result_type == Primitive::kPrimLong)
3336 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3337 ? LocationSummary::kCallOnMainOnly
3338 : LocationSummary::kNoCall;
3339 LocationSummary* locations =
3340 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3341
3342 // The Java language does not allow treating boolean as an integral type but
3343 // our bit representation makes it safe.
3344
3345 switch (result_type) {
3346 case Primitive::kPrimByte:
3347 switch (input_type) {
3348 case Primitive::kPrimLong:
3349 // Type conversion from long to byte is a result of code transformations.
3350 case Primitive::kPrimBoolean:
3351 // Boolean input is a result of code transformations.
3352 case Primitive::kPrimShort:
3353 case Primitive::kPrimInt:
3354 case Primitive::kPrimChar:
3355 // Processing a Dex `int-to-byte' instruction.
3356 locations->SetInAt(0, Location::RequiresRegister());
3357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3358 break;
3359
3360 default:
3361 LOG(FATAL) << "Unexpected type conversion from " << input_type
3362 << " to " << result_type;
3363 }
3364 break;
3365
3366 case Primitive::kPrimShort:
3367 switch (input_type) {
3368 case Primitive::kPrimLong:
3369 // Type conversion from long to short is a result of code transformations.
3370 case Primitive::kPrimBoolean:
3371 // Boolean input is a result of code transformations.
3372 case Primitive::kPrimByte:
3373 case Primitive::kPrimInt:
3374 case Primitive::kPrimChar:
3375 // Processing a Dex `int-to-short' instruction.
3376 locations->SetInAt(0, Location::RequiresRegister());
3377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3378 break;
3379
3380 default:
3381 LOG(FATAL) << "Unexpected type conversion from " << input_type
3382 << " to " << result_type;
3383 }
3384 break;
3385
3386 case Primitive::kPrimInt:
3387 switch (input_type) {
3388 case Primitive::kPrimLong:
3389 // Processing a Dex `long-to-int' instruction.
3390 locations->SetInAt(0, Location::Any());
3391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3392 break;
3393
3394 case Primitive::kPrimFloat:
3395 // Processing a Dex `float-to-int' instruction.
3396 locations->SetInAt(0, Location::RequiresFpuRegister());
3397 locations->SetOut(Location::RequiresRegister());
3398 locations->AddTemp(Location::RequiresFpuRegister());
3399 break;
3400
3401 case Primitive::kPrimDouble:
3402 // Processing a Dex `double-to-int' instruction.
3403 locations->SetInAt(0, Location::RequiresFpuRegister());
3404 locations->SetOut(Location::RequiresRegister());
3405 locations->AddTemp(Location::RequiresFpuRegister());
3406 break;
3407
3408 default:
3409 LOG(FATAL) << "Unexpected type conversion from " << input_type
3410 << " to " << result_type;
3411 }
3412 break;
3413
3414 case Primitive::kPrimLong:
3415 switch (input_type) {
3416 case Primitive::kPrimBoolean:
3417 // Boolean input is a result of code transformations.
3418 case Primitive::kPrimByte:
3419 case Primitive::kPrimShort:
3420 case Primitive::kPrimInt:
3421 case Primitive::kPrimChar:
3422 // Processing a Dex `int-to-long' instruction.
3423 locations->SetInAt(0, Location::RequiresRegister());
3424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3425 break;
3426
3427 case Primitive::kPrimFloat: {
3428 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003429 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3430 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3431 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003432 break;
3433 }
3434
3435 case Primitive::kPrimDouble: {
3436 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003437 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3438 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3439 calling_convention.GetFpuRegisterAt(1)));
3440 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003441 break;
3442 }
3443
3444 default:
3445 LOG(FATAL) << "Unexpected type conversion from " << input_type
3446 << " to " << result_type;
3447 }
3448 break;
3449
3450 case Primitive::kPrimChar:
3451 switch (input_type) {
3452 case Primitive::kPrimLong:
3453 // Type conversion from long to char is a result of code transformations.
3454 case Primitive::kPrimBoolean:
3455 // Boolean input is a result of code transformations.
3456 case Primitive::kPrimByte:
3457 case Primitive::kPrimShort:
3458 case Primitive::kPrimInt:
3459 // Processing a Dex `int-to-char' instruction.
3460 locations->SetInAt(0, Location::RequiresRegister());
3461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3462 break;
3463
3464 default:
3465 LOG(FATAL) << "Unexpected type conversion from " << input_type
3466 << " to " << result_type;
3467 }
3468 break;
3469
3470 case Primitive::kPrimFloat:
3471 switch (input_type) {
3472 case Primitive::kPrimBoolean:
3473 // Boolean input is a result of code transformations.
3474 case Primitive::kPrimByte:
3475 case Primitive::kPrimShort:
3476 case Primitive::kPrimInt:
3477 case Primitive::kPrimChar:
3478 // Processing a Dex `int-to-float' instruction.
3479 locations->SetInAt(0, Location::RequiresRegister());
3480 locations->SetOut(Location::RequiresFpuRegister());
3481 break;
3482
3483 case Primitive::kPrimLong: {
3484 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003485 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3486 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3487 calling_convention.GetRegisterAt(1)));
3488 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003489 break;
3490 }
3491
3492 case Primitive::kPrimDouble:
3493 // Processing a Dex `double-to-float' instruction.
3494 locations->SetInAt(0, Location::RequiresFpuRegister());
3495 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3496 break;
3497
3498 default:
3499 LOG(FATAL) << "Unexpected type conversion from " << input_type
3500 << " to " << result_type;
3501 };
3502 break;
3503
3504 case Primitive::kPrimDouble:
3505 switch (input_type) {
3506 case Primitive::kPrimBoolean:
3507 // Boolean input is a result of code transformations.
3508 case Primitive::kPrimByte:
3509 case Primitive::kPrimShort:
3510 case Primitive::kPrimInt:
3511 case Primitive::kPrimChar:
3512 // Processing a Dex `int-to-double' instruction.
3513 locations->SetInAt(0, Location::RequiresRegister());
3514 locations->SetOut(Location::RequiresFpuRegister());
3515 break;
3516
3517 case Primitive::kPrimLong:
3518 // Processing a Dex `long-to-double' instruction.
3519 locations->SetInAt(0, Location::RequiresRegister());
3520 locations->SetOut(Location::RequiresFpuRegister());
3521 locations->AddTemp(Location::RequiresFpuRegister());
3522 locations->AddTemp(Location::RequiresFpuRegister());
3523 break;
3524
3525 case Primitive::kPrimFloat:
3526 // Processing a Dex `float-to-double' instruction.
3527 locations->SetInAt(0, Location::RequiresFpuRegister());
3528 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3529 break;
3530
3531 default:
3532 LOG(FATAL) << "Unexpected type conversion from " << input_type
3533 << " to " << result_type;
3534 };
3535 break;
3536
3537 default:
3538 LOG(FATAL) << "Unexpected type conversion from " << input_type
3539 << " to " << result_type;
3540 }
3541}
3542
3543void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3544 LocationSummary* locations = conversion->GetLocations();
3545 Location out = locations->Out();
3546 Location in = locations->InAt(0);
3547 Primitive::Type result_type = conversion->GetResultType();
3548 Primitive::Type input_type = conversion->GetInputType();
3549 DCHECK_NE(result_type, input_type);
3550 switch (result_type) {
3551 case Primitive::kPrimByte:
3552 switch (input_type) {
3553 case Primitive::kPrimLong:
3554 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003555 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003556 break;
3557 case Primitive::kPrimBoolean:
3558 // Boolean input is a result of code transformations.
3559 case Primitive::kPrimShort:
3560 case Primitive::kPrimInt:
3561 case Primitive::kPrimChar:
3562 // Processing a Dex `int-to-byte' instruction.
3563 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3564 break;
3565
3566 default:
3567 LOG(FATAL) << "Unexpected type conversion from " << input_type
3568 << " to " << result_type;
3569 }
3570 break;
3571
3572 case Primitive::kPrimShort:
3573 switch (input_type) {
3574 case Primitive::kPrimLong:
3575 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003576 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003577 break;
3578 case Primitive::kPrimBoolean:
3579 // Boolean input is a result of code transformations.
3580 case Primitive::kPrimByte:
3581 case Primitive::kPrimInt:
3582 case Primitive::kPrimChar:
3583 // Processing a Dex `int-to-short' instruction.
3584 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3585 break;
3586
3587 default:
3588 LOG(FATAL) << "Unexpected type conversion from " << input_type
3589 << " to " << result_type;
3590 }
3591 break;
3592
3593 case Primitive::kPrimInt:
3594 switch (input_type) {
3595 case Primitive::kPrimLong:
3596 // Processing a Dex `long-to-int' instruction.
3597 DCHECK(out.IsRegister());
3598 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003599 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003600 } else if (in.IsDoubleStackSlot()) {
3601 GetAssembler()->LoadFromOffset(kLoadWord,
3602 OutputRegister(conversion),
3603 sp,
3604 in.GetStackIndex());
3605 } else {
3606 DCHECK(in.IsConstant());
3607 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00003608 int32_t value = Int32ConstantFrom(in);
3609 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003610 }
3611 break;
3612
3613 case Primitive::kPrimFloat: {
3614 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003615 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003616 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003617 __ Vmov(OutputRegister(conversion), temp);
3618 break;
3619 }
3620
3621 case Primitive::kPrimDouble: {
3622 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003623 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003624 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003625 __ Vmov(OutputRegister(conversion), temp_s);
3626 break;
3627 }
3628
3629 default:
3630 LOG(FATAL) << "Unexpected type conversion from " << input_type
3631 << " to " << result_type;
3632 }
3633 break;
3634
3635 case Primitive::kPrimLong:
3636 switch (input_type) {
3637 case Primitive::kPrimBoolean:
3638 // Boolean input is a result of code transformations.
3639 case Primitive::kPrimByte:
3640 case Primitive::kPrimShort:
3641 case Primitive::kPrimInt:
3642 case Primitive::kPrimChar:
3643 // Processing a Dex `int-to-long' instruction.
3644 DCHECK(out.IsRegisterPair());
3645 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003646 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003647 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003648 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003649 break;
3650
3651 case Primitive::kPrimFloat:
3652 // Processing a Dex `float-to-long' instruction.
3653 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3654 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3655 break;
3656
3657 case Primitive::kPrimDouble:
3658 // Processing a Dex `double-to-long' instruction.
3659 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3660 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3661 break;
3662
3663 default:
3664 LOG(FATAL) << "Unexpected type conversion from " << input_type
3665 << " to " << result_type;
3666 }
3667 break;
3668
3669 case Primitive::kPrimChar:
3670 switch (input_type) {
3671 case Primitive::kPrimLong:
3672 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003673 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003674 break;
3675 case Primitive::kPrimBoolean:
3676 // Boolean input is a result of code transformations.
3677 case Primitive::kPrimByte:
3678 case Primitive::kPrimShort:
3679 case Primitive::kPrimInt:
3680 // Processing a Dex `int-to-char' instruction.
3681 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3682 break;
3683
3684 default:
3685 LOG(FATAL) << "Unexpected type conversion from " << input_type
3686 << " to " << result_type;
3687 }
3688 break;
3689
3690 case Primitive::kPrimFloat:
3691 switch (input_type) {
3692 case Primitive::kPrimBoolean:
3693 // Boolean input is a result of code transformations.
3694 case Primitive::kPrimByte:
3695 case Primitive::kPrimShort:
3696 case Primitive::kPrimInt:
3697 case Primitive::kPrimChar: {
3698 // Processing a Dex `int-to-float' instruction.
3699 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003700 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003701 break;
3702 }
3703
3704 case Primitive::kPrimLong:
3705 // Processing a Dex `long-to-float' instruction.
3706 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3707 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3708 break;
3709
3710 case Primitive::kPrimDouble:
3711 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003712 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003713 break;
3714
3715 default:
3716 LOG(FATAL) << "Unexpected type conversion from " << input_type
3717 << " to " << result_type;
3718 };
3719 break;
3720
3721 case Primitive::kPrimDouble:
3722 switch (input_type) {
3723 case Primitive::kPrimBoolean:
3724 // Boolean input is a result of code transformations.
3725 case Primitive::kPrimByte:
3726 case Primitive::kPrimShort:
3727 case Primitive::kPrimInt:
3728 case Primitive::kPrimChar: {
3729 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003730 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003731 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003732 break;
3733 }
3734
3735 case Primitive::kPrimLong: {
3736 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003737 vixl32::Register low = LowRegisterFrom(in);
3738 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003739 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003740 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003741 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003742 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003743 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003744
3745 // temp_d = int-to-double(high)
3746 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003747 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003748 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003749 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003750 // out_d = unsigned-to-double(low)
3751 __ Vmov(out_s, low);
3752 __ Vcvt(F64, U32, out_d, out_s);
3753 // out_d += temp_d * constant_d
3754 __ Vmla(F64, out_d, temp_d, constant_d);
3755 break;
3756 }
3757
3758 case Primitive::kPrimFloat:
3759 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003760 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003761 break;
3762
3763 default:
3764 LOG(FATAL) << "Unexpected type conversion from " << input_type
3765 << " to " << result_type;
3766 };
3767 break;
3768
3769 default:
3770 LOG(FATAL) << "Unexpected type conversion from " << input_type
3771 << " to " << result_type;
3772 }
3773}
3774
3775void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3776 LocationSummary* locations =
3777 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3778 switch (add->GetResultType()) {
3779 case Primitive::kPrimInt: {
3780 locations->SetInAt(0, Location::RequiresRegister());
3781 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3783 break;
3784 }
3785
Scott Wakelingfe885462016-09-22 10:24:38 +01003786 case Primitive::kPrimLong: {
3787 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003788 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3790 break;
3791 }
3792
3793 case Primitive::kPrimFloat:
3794 case Primitive::kPrimDouble: {
3795 locations->SetInAt(0, Location::RequiresFpuRegister());
3796 locations->SetInAt(1, Location::RequiresFpuRegister());
3797 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3798 break;
3799 }
3800
3801 default:
3802 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3803 }
3804}
3805
3806void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3807 LocationSummary* locations = add->GetLocations();
3808 Location out = locations->Out();
3809 Location first = locations->InAt(0);
3810 Location second = locations->InAt(1);
3811
3812 switch (add->GetResultType()) {
3813 case Primitive::kPrimInt: {
3814 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3815 }
3816 break;
3817
Scott Wakelingfe885462016-09-22 10:24:38 +01003818 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003819 if (second.IsConstant()) {
3820 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3821 GenerateAddLongConst(out, first, value);
3822 } else {
3823 DCHECK(second.IsRegisterPair());
3824 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3825 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3826 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003827 break;
3828 }
3829
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003830 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003831 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003832 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003833 break;
3834
3835 default:
3836 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3837 }
3838}
3839
3840void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3841 LocationSummary* locations =
3842 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3843 switch (sub->GetResultType()) {
3844 case Primitive::kPrimInt: {
3845 locations->SetInAt(0, Location::RequiresRegister());
3846 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3848 break;
3849 }
3850
Scott Wakelingfe885462016-09-22 10:24:38 +01003851 case Primitive::kPrimLong: {
3852 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003853 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3855 break;
3856 }
3857 case Primitive::kPrimFloat:
3858 case Primitive::kPrimDouble: {
3859 locations->SetInAt(0, Location::RequiresFpuRegister());
3860 locations->SetInAt(1, Location::RequiresFpuRegister());
3861 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3862 break;
3863 }
3864 default:
3865 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3866 }
3867}
3868
3869void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3870 LocationSummary* locations = sub->GetLocations();
3871 Location out = locations->Out();
3872 Location first = locations->InAt(0);
3873 Location second = locations->InAt(1);
3874 switch (sub->GetResultType()) {
3875 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003876 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003877 break;
3878 }
3879
Scott Wakelingfe885462016-09-22 10:24:38 +01003880 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003881 if (second.IsConstant()) {
3882 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3883 GenerateAddLongConst(out, first, -value);
3884 } else {
3885 DCHECK(second.IsRegisterPair());
3886 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3887 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3888 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003889 break;
3890 }
3891
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003892 case Primitive::kPrimFloat:
3893 case Primitive::kPrimDouble:
3894 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003895 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003896
3897 default:
3898 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3899 }
3900}
3901
3902void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3903 LocationSummary* locations =
3904 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3905 switch (mul->GetResultType()) {
3906 case Primitive::kPrimInt:
3907 case Primitive::kPrimLong: {
3908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetInAt(1, Location::RequiresRegister());
3910 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3911 break;
3912 }
3913
3914 case Primitive::kPrimFloat:
3915 case Primitive::kPrimDouble: {
3916 locations->SetInAt(0, Location::RequiresFpuRegister());
3917 locations->SetInAt(1, Location::RequiresFpuRegister());
3918 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3919 break;
3920 }
3921
3922 default:
3923 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3924 }
3925}
3926
3927void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3928 LocationSummary* locations = mul->GetLocations();
3929 Location out = locations->Out();
3930 Location first = locations->InAt(0);
3931 Location second = locations->InAt(1);
3932 switch (mul->GetResultType()) {
3933 case Primitive::kPrimInt: {
3934 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3935 break;
3936 }
3937 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003938 vixl32::Register out_hi = HighRegisterFrom(out);
3939 vixl32::Register out_lo = LowRegisterFrom(out);
3940 vixl32::Register in1_hi = HighRegisterFrom(first);
3941 vixl32::Register in1_lo = LowRegisterFrom(first);
3942 vixl32::Register in2_hi = HighRegisterFrom(second);
3943 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003944
3945 // Extra checks to protect caused by the existence of R1_R2.
3946 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3947 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003948 DCHECK(!out_hi.Is(in1_lo));
3949 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003950
3951 // input: in1 - 64 bits, in2 - 64 bits
3952 // output: out
3953 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3954 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3955 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3956
3957 UseScratchRegisterScope temps(GetVIXLAssembler());
3958 vixl32::Register temp = temps.Acquire();
3959 // temp <- in1.lo * in2.hi
3960 __ Mul(temp, in1_lo, in2_hi);
3961 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3962 __ Mla(out_hi, in1_hi, in2_lo, temp);
3963 // out.lo <- (in1.lo * in2.lo)[31:0];
3964 __ Umull(out_lo, temp, in1_lo, in2_lo);
3965 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003966 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003967 break;
3968 }
3969
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003970 case Primitive::kPrimFloat:
3971 case Primitive::kPrimDouble:
3972 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003973 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003974
3975 default:
3976 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3977 }
3978}
3979
Scott Wakelingfe885462016-09-22 10:24:38 +01003980void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3981 DCHECK(instruction->IsDiv() || instruction->IsRem());
3982 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3983
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003984 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003985 DCHECK(second.IsConstant());
3986
3987 vixl32::Register out = OutputRegister(instruction);
3988 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003989 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003990 DCHECK(imm == 1 || imm == -1);
3991
3992 if (instruction->IsRem()) {
3993 __ Mov(out, 0);
3994 } else {
3995 if (imm == 1) {
3996 __ Mov(out, dividend);
3997 } else {
3998 __ Rsb(out, dividend, 0);
3999 }
4000 }
4001}
4002
4003void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4004 DCHECK(instruction->IsDiv() || instruction->IsRem());
4005 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4006
4007 LocationSummary* locations = instruction->GetLocations();
4008 Location second = locations->InAt(1);
4009 DCHECK(second.IsConstant());
4010
4011 vixl32::Register out = OutputRegister(instruction);
4012 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004013 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004014 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004015 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4016 int ctz_imm = CTZ(abs_imm);
4017
4018 if (ctz_imm == 1) {
4019 __ Lsr(temp, dividend, 32 - ctz_imm);
4020 } else {
4021 __ Asr(temp, dividend, 31);
4022 __ Lsr(temp, temp, 32 - ctz_imm);
4023 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004024 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004025
4026 if (instruction->IsDiv()) {
4027 __ Asr(out, out, ctz_imm);
4028 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004029 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004030 }
4031 } else {
4032 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004033 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004034 }
4035}
4036
4037void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4038 DCHECK(instruction->IsDiv() || instruction->IsRem());
4039 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4040
4041 LocationSummary* locations = instruction->GetLocations();
4042 Location second = locations->InAt(1);
4043 DCHECK(second.IsConstant());
4044
4045 vixl32::Register out = OutputRegister(instruction);
4046 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004047 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4048 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004049 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004050
4051 int64_t magic;
4052 int shift;
4053 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4054
Anton Kirilovdda43962016-11-21 19:55:20 +00004055 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4056 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004057 __ Smull(temp2, temp1, dividend, temp1);
4058
4059 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004060 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004061 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004062 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004063 }
4064
4065 if (shift != 0) {
4066 __ Asr(temp1, temp1, shift);
4067 }
4068
4069 if (instruction->IsDiv()) {
4070 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4071 } else {
4072 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4073 // TODO: Strength reduction for mls.
4074 __ Mov(temp2, imm);
4075 __ Mls(out, temp1, temp2, dividend);
4076 }
4077}
4078
4079void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4080 HBinaryOperation* instruction) {
4081 DCHECK(instruction->IsDiv() || instruction->IsRem());
4082 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4083
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004084 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004085 DCHECK(second.IsConstant());
4086
Anton Kirilov644032c2016-12-06 17:51:43 +00004087 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004088 if (imm == 0) {
4089 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4090 } else if (imm == 1 || imm == -1) {
4091 DivRemOneOrMinusOne(instruction);
4092 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4093 DivRemByPowerOfTwo(instruction);
4094 } else {
4095 DCHECK(imm <= -2 || imm >= 2);
4096 GenerateDivRemWithAnyConstant(instruction);
4097 }
4098}
4099
4100void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4101 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4102 if (div->GetResultType() == Primitive::kPrimLong) {
4103 // pLdiv runtime call.
4104 call_kind = LocationSummary::kCallOnMainOnly;
4105 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4106 // sdiv will be replaced by other instruction sequence.
4107 } else if (div->GetResultType() == Primitive::kPrimInt &&
4108 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4109 // pIdivmod runtime call.
4110 call_kind = LocationSummary::kCallOnMainOnly;
4111 }
4112
4113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4114
4115 switch (div->GetResultType()) {
4116 case Primitive::kPrimInt: {
4117 if (div->InputAt(1)->IsConstant()) {
4118 locations->SetInAt(0, Location::RequiresRegister());
4119 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004121 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004122 if (value == 1 || value == 0 || value == -1) {
4123 // No temp register required.
4124 } else {
4125 locations->AddTemp(Location::RequiresRegister());
4126 if (!IsPowerOfTwo(AbsOrMin(value))) {
4127 locations->AddTemp(Location::RequiresRegister());
4128 }
4129 }
4130 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4131 locations->SetInAt(0, Location::RequiresRegister());
4132 locations->SetInAt(1, Location::RequiresRegister());
4133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4134 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004135 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4136 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4137 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004138 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004139 // we only need the former.
4140 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004141 }
4142 break;
4143 }
4144 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004145 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4146 locations->SetInAt(0, LocationFrom(
4147 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4148 locations->SetInAt(1, LocationFrom(
4149 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4150 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004151 break;
4152 }
4153 case Primitive::kPrimFloat:
4154 case Primitive::kPrimDouble: {
4155 locations->SetInAt(0, Location::RequiresFpuRegister());
4156 locations->SetInAt(1, Location::RequiresFpuRegister());
4157 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4158 break;
4159 }
4160
4161 default:
4162 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4163 }
4164}
4165
4166void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004167 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004168 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004169
4170 switch (div->GetResultType()) {
4171 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004172 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004173 GenerateDivRemConstantIntegral(div);
4174 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4175 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4176 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004177 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4178 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4179 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4180 DCHECK(r0.Is(OutputRegister(div)));
4181
4182 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4183 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004184 }
4185 break;
4186 }
4187
4188 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004189 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4190 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4191 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4192 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4193 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4194 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4195 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4196
4197 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4198 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004199 break;
4200 }
4201
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004202 case Primitive::kPrimFloat:
4203 case Primitive::kPrimDouble:
4204 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004205 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004206
4207 default:
4208 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4209 }
4210}
4211
Artem Serov551b28f2016-10-18 19:11:30 +01004212void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4213 Primitive::Type type = rem->GetResultType();
4214
4215 // Most remainders are implemented in the runtime.
4216 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4217 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4218 // sdiv will be replaced by other instruction sequence.
4219 call_kind = LocationSummary::kNoCall;
4220 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4221 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4222 // Have hardware divide instruction for int, do it with three instructions.
4223 call_kind = LocationSummary::kNoCall;
4224 }
4225
4226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4227
4228 switch (type) {
4229 case Primitive::kPrimInt: {
4230 if (rem->InputAt(1)->IsConstant()) {
4231 locations->SetInAt(0, Location::RequiresRegister());
4232 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004234 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004235 if (value == 1 || value == 0 || value == -1) {
4236 // No temp register required.
4237 } else {
4238 locations->AddTemp(Location::RequiresRegister());
4239 if (!IsPowerOfTwo(AbsOrMin(value))) {
4240 locations->AddTemp(Location::RequiresRegister());
4241 }
4242 }
4243 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4244 locations->SetInAt(0, Location::RequiresRegister());
4245 locations->SetInAt(1, Location::RequiresRegister());
4246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4247 locations->AddTemp(Location::RequiresRegister());
4248 } else {
4249 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4250 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4251 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004252 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004253 // we only need the latter.
4254 locations->SetOut(LocationFrom(r1));
4255 }
4256 break;
4257 }
4258 case Primitive::kPrimLong: {
4259 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4260 locations->SetInAt(0, LocationFrom(
4261 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4262 locations->SetInAt(1, LocationFrom(
4263 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4264 // The runtime helper puts the output in R2,R3.
4265 locations->SetOut(LocationFrom(r2, r3));
4266 break;
4267 }
4268 case Primitive::kPrimFloat: {
4269 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4270 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4271 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4272 locations->SetOut(LocationFrom(s0));
4273 break;
4274 }
4275
4276 case Primitive::kPrimDouble: {
4277 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4278 locations->SetInAt(0, LocationFrom(
4279 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4280 locations->SetInAt(1, LocationFrom(
4281 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4282 locations->SetOut(LocationFrom(s0, s1));
4283 break;
4284 }
4285
4286 default:
4287 LOG(FATAL) << "Unexpected rem type " << type;
4288 }
4289}
4290
4291void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4292 LocationSummary* locations = rem->GetLocations();
4293 Location second = locations->InAt(1);
4294
4295 Primitive::Type type = rem->GetResultType();
4296 switch (type) {
4297 case Primitive::kPrimInt: {
4298 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4299 vixl32::Register out_reg = OutputRegister(rem);
4300 if (second.IsConstant()) {
4301 GenerateDivRemConstantIntegral(rem);
4302 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4303 vixl32::Register reg2 = RegisterFrom(second);
4304 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4305
4306 // temp = reg1 / reg2 (integer division)
4307 // dest = reg1 - temp * reg2
4308 __ Sdiv(temp, reg1, reg2);
4309 __ Mls(out_reg, temp, reg2, reg1);
4310 } else {
4311 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4312 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4313 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4314 DCHECK(out_reg.Is(r1));
4315
4316 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4317 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4318 }
4319 break;
4320 }
4321
4322 case Primitive::kPrimLong: {
4323 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4324 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4325 break;
4326 }
4327
4328 case Primitive::kPrimFloat: {
4329 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4330 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4331 break;
4332 }
4333
4334 case Primitive::kPrimDouble: {
4335 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4336 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4337 break;
4338 }
4339
4340 default:
4341 LOG(FATAL) << "Unexpected rem type " << type;
4342 }
4343}
4344
4345
Scott Wakelingfe885462016-09-22 10:24:38 +01004346void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004347 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004348 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004349}
4350
4351void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4352 DivZeroCheckSlowPathARMVIXL* slow_path =
4353 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4354 codegen_->AddSlowPath(slow_path);
4355
4356 LocationSummary* locations = instruction->GetLocations();
4357 Location value = locations->InAt(0);
4358
4359 switch (instruction->GetType()) {
4360 case Primitive::kPrimBoolean:
4361 case Primitive::kPrimByte:
4362 case Primitive::kPrimChar:
4363 case Primitive::kPrimShort:
4364 case Primitive::kPrimInt: {
4365 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004366 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004367 } else {
4368 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004369 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004370 __ B(slow_path->GetEntryLabel());
4371 }
4372 }
4373 break;
4374 }
4375 case Primitive::kPrimLong: {
4376 if (value.IsRegisterPair()) {
4377 UseScratchRegisterScope temps(GetVIXLAssembler());
4378 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004379 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004380 __ B(eq, slow_path->GetEntryLabel());
4381 } else {
4382 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004383 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004384 __ B(slow_path->GetEntryLabel());
4385 }
4386 }
4387 break;
4388 }
4389 default:
4390 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4391 }
4392}
4393
Artem Serov02109dd2016-09-23 17:17:54 +01004394void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4395 LocationSummary* locations = ror->GetLocations();
4396 vixl32::Register in = InputRegisterAt(ror, 0);
4397 Location rhs = locations->InAt(1);
4398 vixl32::Register out = OutputRegister(ror);
4399
4400 if (rhs.IsConstant()) {
4401 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4402 // so map all rotations to a +ve. equivalent in that range.
4403 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4404 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4405 if (rot) {
4406 // Rotate, mapping left rotations to right equivalents if necessary.
4407 // (e.g. left by 2 bits == right by 30.)
4408 __ Ror(out, in, rot);
4409 } else if (!out.Is(in)) {
4410 __ Mov(out, in);
4411 }
4412 } else {
4413 __ Ror(out, in, RegisterFrom(rhs));
4414 }
4415}
4416
4417// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4418// rotates by swapping input regs (effectively rotating by the first 32-bits of
4419// a larger rotation) or flipping direction (thus treating larger right/left
4420// rotations as sub-word sized rotations in the other direction) as appropriate.
4421void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4422 LocationSummary* locations = ror->GetLocations();
4423 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4424 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4425 Location rhs = locations->InAt(1);
4426 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4427 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4428
4429 if (rhs.IsConstant()) {
4430 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4431 // Map all rotations to +ve. equivalents on the interval [0,63].
4432 rot &= kMaxLongShiftDistance;
4433 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4434 // logic below to a simple pair of binary orr.
4435 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4436 if (rot >= kArmBitsPerWord) {
4437 rot -= kArmBitsPerWord;
4438 std::swap(in_reg_hi, in_reg_lo);
4439 }
4440 // Rotate, or mov to out for zero or word size rotations.
4441 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004442 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004443 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004444 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004445 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4446 } else {
4447 __ Mov(out_reg_lo, in_reg_lo);
4448 __ Mov(out_reg_hi, in_reg_hi);
4449 }
4450 } else {
4451 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4452 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4453 vixl32::Label end;
4454 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004455 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004456
4457 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4458 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004459 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004460 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004461
4462 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4463 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4464 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4465 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4466 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4467 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4468 __ Lsr(shift_left, in_reg_hi, shift_right);
4469 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004470 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004471
4472 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4473 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4474 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4475 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4476 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4477 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4478 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4479 __ Lsl(shift_right, in_reg_hi, shift_left);
4480 __ Add(out_reg_lo, out_reg_lo, shift_right);
4481
Anton Kirilov6f644202017-02-27 18:29:45 +00004482 if (end.IsReferenced()) {
4483 __ Bind(&end);
4484 }
Artem Serov02109dd2016-09-23 17:17:54 +01004485 }
4486}
4487
4488void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4489 LocationSummary* locations =
4490 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4491 switch (ror->GetResultType()) {
4492 case Primitive::kPrimInt: {
4493 locations->SetInAt(0, Location::RequiresRegister());
4494 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4496 break;
4497 }
4498 case Primitive::kPrimLong: {
4499 locations->SetInAt(0, Location::RequiresRegister());
4500 if (ror->InputAt(1)->IsConstant()) {
4501 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4502 } else {
4503 locations->SetInAt(1, Location::RequiresRegister());
4504 locations->AddTemp(Location::RequiresRegister());
4505 locations->AddTemp(Location::RequiresRegister());
4506 }
4507 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4508 break;
4509 }
4510 default:
4511 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4512 }
4513}
4514
4515void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4516 Primitive::Type type = ror->GetResultType();
4517 switch (type) {
4518 case Primitive::kPrimInt: {
4519 HandleIntegerRotate(ror);
4520 break;
4521 }
4522 case Primitive::kPrimLong: {
4523 HandleLongRotate(ror);
4524 break;
4525 }
4526 default:
4527 LOG(FATAL) << "Unexpected operation type " << type;
4528 UNREACHABLE();
4529 }
4530}
4531
Artem Serov02d37832016-10-25 15:25:33 +01004532void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4533 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4534
4535 LocationSummary* locations =
4536 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4537
4538 switch (op->GetResultType()) {
4539 case Primitive::kPrimInt: {
4540 locations->SetInAt(0, Location::RequiresRegister());
4541 if (op->InputAt(1)->IsConstant()) {
4542 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4544 } else {
4545 locations->SetInAt(1, Location::RequiresRegister());
4546 // Make the output overlap, as it will be used to hold the masked
4547 // second input.
4548 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4549 }
4550 break;
4551 }
4552 case Primitive::kPrimLong: {
4553 locations->SetInAt(0, Location::RequiresRegister());
4554 if (op->InputAt(1)->IsConstant()) {
4555 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4556 // For simplicity, use kOutputOverlap even though we only require that low registers
4557 // don't clash with high registers which the register allocator currently guarantees.
4558 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4559 } else {
4560 locations->SetInAt(1, Location::RequiresRegister());
4561 locations->AddTemp(Location::RequiresRegister());
4562 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4563 }
4564 break;
4565 }
4566 default:
4567 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4568 }
4569}
4570
4571void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4572 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4573
4574 LocationSummary* locations = op->GetLocations();
4575 Location out = locations->Out();
4576 Location first = locations->InAt(0);
4577 Location second = locations->InAt(1);
4578
4579 Primitive::Type type = op->GetResultType();
4580 switch (type) {
4581 case Primitive::kPrimInt: {
4582 vixl32::Register out_reg = OutputRegister(op);
4583 vixl32::Register first_reg = InputRegisterAt(op, 0);
4584 if (second.IsRegister()) {
4585 vixl32::Register second_reg = RegisterFrom(second);
4586 // ARM doesn't mask the shift count so we need to do it ourselves.
4587 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4588 if (op->IsShl()) {
4589 __ Lsl(out_reg, first_reg, out_reg);
4590 } else if (op->IsShr()) {
4591 __ Asr(out_reg, first_reg, out_reg);
4592 } else {
4593 __ Lsr(out_reg, first_reg, out_reg);
4594 }
4595 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004596 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004597 uint32_t shift_value = cst & kMaxIntShiftDistance;
4598 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4599 __ Mov(out_reg, first_reg);
4600 } else if (op->IsShl()) {
4601 __ Lsl(out_reg, first_reg, shift_value);
4602 } else if (op->IsShr()) {
4603 __ Asr(out_reg, first_reg, shift_value);
4604 } else {
4605 __ Lsr(out_reg, first_reg, shift_value);
4606 }
4607 }
4608 break;
4609 }
4610 case Primitive::kPrimLong: {
4611 vixl32::Register o_h = HighRegisterFrom(out);
4612 vixl32::Register o_l = LowRegisterFrom(out);
4613
4614 vixl32::Register high = HighRegisterFrom(first);
4615 vixl32::Register low = LowRegisterFrom(first);
4616
4617 if (second.IsRegister()) {
4618 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4619
4620 vixl32::Register second_reg = RegisterFrom(second);
4621
4622 if (op->IsShl()) {
4623 __ And(o_l, second_reg, kMaxLongShiftDistance);
4624 // Shift the high part
4625 __ Lsl(o_h, high, o_l);
4626 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004627 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004628 __ Lsr(temp, low, temp);
4629 __ Orr(o_h, o_h, temp);
4630 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004631 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004632 {
Artem Serov0fb37192016-12-06 18:13:40 +00004633 ExactAssemblyScope guard(GetVIXLAssembler(),
4634 2 * vixl32::kMaxInstructionSizeInBytes,
4635 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004636 __ it(pl);
4637 __ lsl(pl, o_h, low, temp);
4638 }
4639 // Shift the low part
4640 __ Lsl(o_l, low, o_l);
4641 } else if (op->IsShr()) {
4642 __ And(o_h, second_reg, kMaxLongShiftDistance);
4643 // Shift the low part
4644 __ Lsr(o_l, low, o_h);
4645 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004646 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004647 __ Lsl(temp, high, temp);
4648 __ Orr(o_l, o_l, temp);
4649 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004650 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004651 {
Artem Serov0fb37192016-12-06 18:13:40 +00004652 ExactAssemblyScope guard(GetVIXLAssembler(),
4653 2 * vixl32::kMaxInstructionSizeInBytes,
4654 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004655 __ it(pl);
4656 __ asr(pl, o_l, high, temp);
4657 }
4658 // Shift the high part
4659 __ Asr(o_h, high, o_h);
4660 } else {
4661 __ And(o_h, second_reg, kMaxLongShiftDistance);
4662 // same as Shr except we use `Lsr`s and not `Asr`s
4663 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004664 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004665 __ Lsl(temp, high, temp);
4666 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004667 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004668 {
Artem Serov0fb37192016-12-06 18:13:40 +00004669 ExactAssemblyScope guard(GetVIXLAssembler(),
4670 2 * vixl32::kMaxInstructionSizeInBytes,
4671 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004672 __ it(pl);
4673 __ lsr(pl, o_l, high, temp);
4674 }
4675 __ Lsr(o_h, high, o_h);
4676 }
4677 } else {
4678 // Register allocator doesn't create partial overlap.
4679 DCHECK(!o_l.Is(high));
4680 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004681 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004682 uint32_t shift_value = cst & kMaxLongShiftDistance;
4683 if (shift_value > 32) {
4684 if (op->IsShl()) {
4685 __ Lsl(o_h, low, shift_value - 32);
4686 __ Mov(o_l, 0);
4687 } else if (op->IsShr()) {
4688 __ Asr(o_l, high, shift_value - 32);
4689 __ Asr(o_h, high, 31);
4690 } else {
4691 __ Lsr(o_l, high, shift_value - 32);
4692 __ Mov(o_h, 0);
4693 }
4694 } else if (shift_value == 32) {
4695 if (op->IsShl()) {
4696 __ Mov(o_h, low);
4697 __ Mov(o_l, 0);
4698 } else if (op->IsShr()) {
4699 __ Mov(o_l, high);
4700 __ Asr(o_h, high, 31);
4701 } else {
4702 __ Mov(o_l, high);
4703 __ Mov(o_h, 0);
4704 }
4705 } else if (shift_value == 1) {
4706 if (op->IsShl()) {
4707 __ Lsls(o_l, low, 1);
4708 __ Adc(o_h, high, high);
4709 } else if (op->IsShr()) {
4710 __ Asrs(o_h, high, 1);
4711 __ Rrx(o_l, low);
4712 } else {
4713 __ Lsrs(o_h, high, 1);
4714 __ Rrx(o_l, low);
4715 }
4716 } else {
4717 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4718 if (op->IsShl()) {
4719 __ Lsl(o_h, high, shift_value);
4720 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4721 __ Lsl(o_l, low, shift_value);
4722 } else if (op->IsShr()) {
4723 __ Lsr(o_l, low, shift_value);
4724 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4725 __ Asr(o_h, high, shift_value);
4726 } else {
4727 __ Lsr(o_l, low, shift_value);
4728 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4729 __ Lsr(o_h, high, shift_value);
4730 }
4731 }
4732 }
4733 break;
4734 }
4735 default:
4736 LOG(FATAL) << "Unexpected operation type " << type;
4737 UNREACHABLE();
4738 }
4739}
4740
4741void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4742 HandleShift(shl);
4743}
4744
4745void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4746 HandleShift(shl);
4747}
4748
4749void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4750 HandleShift(shr);
4751}
4752
4753void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4754 HandleShift(shr);
4755}
4756
4757void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4758 HandleShift(ushr);
4759}
4760
4761void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4762 HandleShift(ushr);
4763}
4764
4765void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4766 LocationSummary* locations =
4767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4768 if (instruction->IsStringAlloc()) {
4769 locations->AddTemp(LocationFrom(kMethodRegister));
4770 } else {
4771 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4772 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004773 }
4774 locations->SetOut(LocationFrom(r0));
4775}
4776
4777void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4778 // Note: if heap poisoning is enabled, the entry point takes cares
4779 // of poisoning the reference.
4780 if (instruction->IsStringAlloc()) {
4781 // String is allocated through StringFactory. Call NewEmptyString entry point.
4782 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4783 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4784 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4785 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004786 // 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 +00004787 ExactAssemblyScope aas(GetVIXLAssembler(),
4788 vixl32::k16BitT32InstructionSizeInBytes,
4789 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004790 __ blx(lr);
4791 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4792 } else {
4793 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004794 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004795 }
4796}
4797
4798void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4799 LocationSummary* locations =
4800 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4801 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004802 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004803 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4804 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004805}
4806
4807void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004808 // Note: if heap poisoning is enabled, the entry point takes cares
4809 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004810 QuickEntrypointEnum entrypoint =
4811 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4812 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004813 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004814 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004815}
4816
4817void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4818 LocationSummary* locations =
4819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4820 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4821 if (location.IsStackSlot()) {
4822 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4823 } else if (location.IsDoubleStackSlot()) {
4824 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4825 }
4826 locations->SetOut(location);
4827}
4828
4829void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4830 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4831 // Nothing to do, the parameter is already at its location.
4832}
4833
4834void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4835 LocationSummary* locations =
4836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4837 locations->SetOut(LocationFrom(kMethodRegister));
4838}
4839
4840void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4841 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4842 // Nothing to do, the method is already at its location.
4843}
4844
4845void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4846 LocationSummary* locations =
4847 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4848 locations->SetInAt(0, Location::RequiresRegister());
4849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4850}
4851
4852void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4853 LocationSummary* locations = not_->GetLocations();
4854 Location out = locations->Out();
4855 Location in = locations->InAt(0);
4856 switch (not_->GetResultType()) {
4857 case Primitive::kPrimInt:
4858 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4859 break;
4860
4861 case Primitive::kPrimLong:
4862 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4863 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4864 break;
4865
4866 default:
4867 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4868 }
4869}
4870
Scott Wakelingc34dba72016-10-03 10:14:44 +01004871void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4872 LocationSummary* locations =
4873 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4874 locations->SetInAt(0, Location::RequiresRegister());
4875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4876}
4877
4878void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4879 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4880}
4881
Artem Serov02d37832016-10-25 15:25:33 +01004882void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4883 LocationSummary* locations =
4884 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4885 switch (compare->InputAt(0)->GetType()) {
4886 case Primitive::kPrimBoolean:
4887 case Primitive::kPrimByte:
4888 case Primitive::kPrimShort:
4889 case Primitive::kPrimChar:
4890 case Primitive::kPrimInt:
4891 case Primitive::kPrimLong: {
4892 locations->SetInAt(0, Location::RequiresRegister());
4893 locations->SetInAt(1, Location::RequiresRegister());
4894 // Output overlaps because it is written before doing the low comparison.
4895 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4896 break;
4897 }
4898 case Primitive::kPrimFloat:
4899 case Primitive::kPrimDouble: {
4900 locations->SetInAt(0, Location::RequiresFpuRegister());
4901 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4902 locations->SetOut(Location::RequiresRegister());
4903 break;
4904 }
4905 default:
4906 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4907 }
4908}
4909
4910void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4911 LocationSummary* locations = compare->GetLocations();
4912 vixl32::Register out = OutputRegister(compare);
4913 Location left = locations->InAt(0);
4914 Location right = locations->InAt(1);
4915
4916 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004917 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01004918 Primitive::Type type = compare->InputAt(0)->GetType();
4919 vixl32::Condition less_cond = vixl32::Condition(kNone);
4920 switch (type) {
4921 case Primitive::kPrimBoolean:
4922 case Primitive::kPrimByte:
4923 case Primitive::kPrimShort:
4924 case Primitive::kPrimChar:
4925 case Primitive::kPrimInt: {
4926 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4927 __ Mov(out, 0);
4928 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4929 less_cond = lt;
4930 break;
4931 }
4932 case Primitive::kPrimLong: {
4933 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004934 __ B(lt, &less, /* far_target */ false);
4935 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004936 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4937 __ Mov(out, 0);
4938 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4939 less_cond = lo;
4940 break;
4941 }
4942 case Primitive::kPrimFloat:
4943 case Primitive::kPrimDouble: {
4944 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004945 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01004946 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4947 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4948 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4949 break;
4950 }
4951 default:
4952 LOG(FATAL) << "Unexpected compare type " << type;
4953 UNREACHABLE();
4954 }
4955
Anton Kirilov6f644202017-02-27 18:29:45 +00004956 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00004957 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004958
4959 __ Bind(&greater);
4960 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00004961 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01004962
4963 __ Bind(&less);
4964 __ Mov(out, -1);
4965
Anton Kirilov6f644202017-02-27 18:29:45 +00004966 if (done.IsReferenced()) {
4967 __ Bind(&done);
4968 }
Artem Serov02d37832016-10-25 15:25:33 +01004969}
4970
4971void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4972 LocationSummary* locations =
4973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4974 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4975 locations->SetInAt(i, Location::Any());
4976 }
4977 locations->SetOut(Location::Any());
4978}
4979
4980void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4981 LOG(FATAL) << "Unreachable";
4982}
4983
4984void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4985 // TODO (ported from quick): revisit ARM barrier kinds.
4986 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4987 switch (kind) {
4988 case MemBarrierKind::kAnyStore:
4989 case MemBarrierKind::kLoadAny:
4990 case MemBarrierKind::kAnyAny: {
4991 flavor = DmbOptions::ISH;
4992 break;
4993 }
4994 case MemBarrierKind::kStoreStore: {
4995 flavor = DmbOptions::ISHST;
4996 break;
4997 }
4998 default:
4999 LOG(FATAL) << "Unexpected memory barrier " << kind;
5000 }
5001 __ Dmb(flavor);
5002}
5003
5004void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5005 uint32_t offset,
5006 vixl32::Register out_lo,
5007 vixl32::Register out_hi) {
5008 UseScratchRegisterScope temps(GetVIXLAssembler());
5009 if (offset != 0) {
5010 vixl32::Register temp = temps.Acquire();
5011 __ Add(temp, addr, offset);
5012 addr = temp;
5013 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005014 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005015}
5016
5017void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5018 uint32_t offset,
5019 vixl32::Register value_lo,
5020 vixl32::Register value_hi,
5021 vixl32::Register temp1,
5022 vixl32::Register temp2,
5023 HInstruction* instruction) {
5024 UseScratchRegisterScope temps(GetVIXLAssembler());
5025 vixl32::Label fail;
5026 if (offset != 0) {
5027 vixl32::Register temp = temps.Acquire();
5028 __ Add(temp, addr, offset);
5029 addr = temp;
5030 }
5031 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005032 {
5033 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005034 ExactAssemblyScope aas(GetVIXLAssembler(),
5035 vixl32::kMaxInstructionSizeInBytes,
5036 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005037 // We need a load followed by store. (The address used in a STREX instruction must
5038 // be the same as the address in the most recently executed LDREX instruction.)
5039 __ ldrexd(temp1, temp2, MemOperand(addr));
5040 codegen_->MaybeRecordImplicitNullCheck(instruction);
5041 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005042 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005043 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005044}
Artem Serov02109dd2016-09-23 17:17:54 +01005045
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005046void LocationsBuilderARMVIXL::HandleFieldSet(
5047 HInstruction* instruction, const FieldInfo& field_info) {
5048 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5049
5050 LocationSummary* locations =
5051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5052 locations->SetInAt(0, Location::RequiresRegister());
5053
5054 Primitive::Type field_type = field_info.GetFieldType();
5055 if (Primitive::IsFloatingPointType(field_type)) {
5056 locations->SetInAt(1, Location::RequiresFpuRegister());
5057 } else {
5058 locations->SetInAt(1, Location::RequiresRegister());
5059 }
5060
5061 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5062 bool generate_volatile = field_info.IsVolatile()
5063 && is_wide
5064 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5065 bool needs_write_barrier =
5066 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5067 // Temporary registers for the write barrier.
5068 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5069 if (needs_write_barrier) {
5070 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5071 locations->AddTemp(Location::RequiresRegister());
5072 } else if (generate_volatile) {
5073 // ARM encoding have some additional constraints for ldrexd/strexd:
5074 // - registers need to be consecutive
5075 // - the first register should be even but not R14.
5076 // We don't test for ARM yet, and the assertion makes sure that we
5077 // revisit this if we ever enable ARM encoding.
5078 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5079
5080 locations->AddTemp(Location::RequiresRegister());
5081 locations->AddTemp(Location::RequiresRegister());
5082 if (field_type == Primitive::kPrimDouble) {
5083 // For doubles we need two more registers to copy the value.
5084 locations->AddTemp(LocationFrom(r2));
5085 locations->AddTemp(LocationFrom(r3));
5086 }
5087 }
5088}
5089
5090void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5091 const FieldInfo& field_info,
5092 bool value_can_be_null) {
5093 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5094
5095 LocationSummary* locations = instruction->GetLocations();
5096 vixl32::Register base = InputRegisterAt(instruction, 0);
5097 Location value = locations->InAt(1);
5098
5099 bool is_volatile = field_info.IsVolatile();
5100 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5101 Primitive::Type field_type = field_info.GetFieldType();
5102 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5103 bool needs_write_barrier =
5104 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5105
5106 if (is_volatile) {
5107 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5108 }
5109
5110 switch (field_type) {
5111 case Primitive::kPrimBoolean:
5112 case Primitive::kPrimByte: {
5113 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5114 break;
5115 }
5116
5117 case Primitive::kPrimShort:
5118 case Primitive::kPrimChar: {
5119 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5120 break;
5121 }
5122
5123 case Primitive::kPrimInt:
5124 case Primitive::kPrimNot: {
5125 if (kPoisonHeapReferences && needs_write_barrier) {
5126 // Note that in the case where `value` is a null reference,
5127 // we do not enter this block, as a null reference does not
5128 // need poisoning.
5129 DCHECK_EQ(field_type, Primitive::kPrimNot);
5130 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5131 __ Mov(temp, RegisterFrom(value));
5132 GetAssembler()->PoisonHeapReference(temp);
5133 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5134 } else {
5135 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5136 }
5137 break;
5138 }
5139
5140 case Primitive::kPrimLong: {
5141 if (is_volatile && !atomic_ldrd_strd) {
5142 GenerateWideAtomicStore(base,
5143 offset,
5144 LowRegisterFrom(value),
5145 HighRegisterFrom(value),
5146 RegisterFrom(locations->GetTemp(0)),
5147 RegisterFrom(locations->GetTemp(1)),
5148 instruction);
5149 } else {
5150 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5151 codegen_->MaybeRecordImplicitNullCheck(instruction);
5152 }
5153 break;
5154 }
5155
5156 case Primitive::kPrimFloat: {
5157 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5158 break;
5159 }
5160
5161 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005162 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005163 if (is_volatile && !atomic_ldrd_strd) {
5164 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5165 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5166
5167 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5168
5169 GenerateWideAtomicStore(base,
5170 offset,
5171 value_reg_lo,
5172 value_reg_hi,
5173 RegisterFrom(locations->GetTemp(2)),
5174 RegisterFrom(locations->GetTemp(3)),
5175 instruction);
5176 } else {
5177 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5178 codegen_->MaybeRecordImplicitNullCheck(instruction);
5179 }
5180 break;
5181 }
5182
5183 case Primitive::kPrimVoid:
5184 LOG(FATAL) << "Unreachable type " << field_type;
5185 UNREACHABLE();
5186 }
5187
5188 // Longs and doubles are handled in the switch.
5189 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005190 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5191 // should use a scope and the assembler to emit the store instruction to guarantee that we
5192 // record the pc at the correct position. But the `Assembler` does not automatically handle
5193 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5194 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005195 codegen_->MaybeRecordImplicitNullCheck(instruction);
5196 }
5197
5198 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5199 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5200 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5201 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5202 }
5203
5204 if (is_volatile) {
5205 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5206 }
5207}
5208
Artem Serov02d37832016-10-25 15:25:33 +01005209void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5210 const FieldInfo& field_info) {
5211 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5212
5213 bool object_field_get_with_read_barrier =
5214 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5215 LocationSummary* locations =
5216 new (GetGraph()->GetArena()) LocationSummary(instruction,
5217 object_field_get_with_read_barrier ?
5218 LocationSummary::kCallOnSlowPath :
5219 LocationSummary::kNoCall);
5220 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5221 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5222 }
5223 locations->SetInAt(0, Location::RequiresRegister());
5224
5225 bool volatile_for_double = field_info.IsVolatile()
5226 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5227 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5228 // The output overlaps in case of volatile long: we don't want the
5229 // code generated by GenerateWideAtomicLoad to overwrite the
5230 // object's location. Likewise, in the case of an object field get
5231 // with read barriers enabled, we do not want the load to overwrite
5232 // the object's location, as we need it to emit the read barrier.
5233 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5234 object_field_get_with_read_barrier;
5235
5236 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5237 locations->SetOut(Location::RequiresFpuRegister());
5238 } else {
5239 locations->SetOut(Location::RequiresRegister(),
5240 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5241 }
5242 if (volatile_for_double) {
5243 // ARM encoding have some additional constraints for ldrexd/strexd:
5244 // - registers need to be consecutive
5245 // - the first register should be even but not R14.
5246 // We don't test for ARM yet, and the assertion makes sure that we
5247 // revisit this if we ever enable ARM encoding.
5248 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5249 locations->AddTemp(Location::RequiresRegister());
5250 locations->AddTemp(Location::RequiresRegister());
5251 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5252 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005253 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01005254 locations->AddTemp(Location::RequiresRegister());
5255 }
5256}
5257
5258Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5259 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5260 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5261 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5262 return Location::ConstantLocation(input->AsConstant());
5263 } else {
5264 return Location::RequiresFpuRegister();
5265 }
5266}
5267
Artem Serov02109dd2016-09-23 17:17:54 +01005268Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5269 Opcode opcode) {
5270 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5271 if (constant->IsConstant() &&
5272 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5273 return Location::ConstantLocation(constant->AsConstant());
5274 }
5275 return Location::RequiresRegister();
5276}
5277
5278bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5279 Opcode opcode) {
5280 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5281 if (Primitive::Is64BitType(input_cst->GetType())) {
5282 Opcode high_opcode = opcode;
5283 SetCc low_set_cc = kCcDontCare;
5284 switch (opcode) {
5285 case SUB:
5286 // Flip the operation to an ADD.
5287 value = -value;
5288 opcode = ADD;
5289 FALLTHROUGH_INTENDED;
5290 case ADD:
5291 if (Low32Bits(value) == 0u) {
5292 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5293 }
5294 high_opcode = ADC;
5295 low_set_cc = kCcSet;
5296 break;
5297 default:
5298 break;
5299 }
5300 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5301 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5302 } else {
5303 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5304 }
5305}
5306
5307// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5308// enabled.
5309bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5310 Opcode opcode,
5311 SetCc set_cc) {
5312 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5313 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5314 return true;
5315 }
5316 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005317 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005318 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005319 case AND: neg_opcode = BIC; neg_value = ~value; break;
5320 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5321 case ADD: neg_opcode = SUB; neg_value = -value; break;
5322 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5323 case SUB: neg_opcode = ADD; neg_value = -value; break;
5324 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5325 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005326 default:
5327 return false;
5328 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005329
5330 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5331 return true;
5332 }
5333
5334 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005335}
5336
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005337void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5338 const FieldInfo& field_info) {
5339 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5340
5341 LocationSummary* locations = instruction->GetLocations();
5342 vixl32::Register base = InputRegisterAt(instruction, 0);
5343 Location out = locations->Out();
5344 bool is_volatile = field_info.IsVolatile();
5345 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5346 Primitive::Type field_type = field_info.GetFieldType();
5347 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5348
5349 switch (field_type) {
5350 case Primitive::kPrimBoolean:
5351 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5352 break;
5353
5354 case Primitive::kPrimByte:
5355 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5356 break;
5357
5358 case Primitive::kPrimShort:
5359 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5360 break;
5361
5362 case Primitive::kPrimChar:
5363 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5364 break;
5365
5366 case Primitive::kPrimInt:
5367 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5368 break;
5369
5370 case Primitive::kPrimNot: {
5371 // /* HeapReference<Object> */ out = *(base + offset)
5372 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005373 Location temp_loc = locations->GetTemp(0);
5374 // Note that a potential implicit null check is handled in this
5375 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5376 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5377 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5378 if (is_volatile) {
5379 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5380 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005381 } else {
5382 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005383 codegen_->MaybeRecordImplicitNullCheck(instruction);
5384 if (is_volatile) {
5385 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5386 }
5387 // If read barriers are enabled, emit read barriers other than
5388 // Baker's using a slow path (and also unpoison the loaded
5389 // reference, if heap poisoning is enabled).
5390 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5391 }
5392 break;
5393 }
5394
5395 case Primitive::kPrimLong:
5396 if (is_volatile && !atomic_ldrd_strd) {
5397 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5398 } else {
5399 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5400 }
5401 break;
5402
5403 case Primitive::kPrimFloat:
5404 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5405 break;
5406
5407 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005408 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005409 if (is_volatile && !atomic_ldrd_strd) {
5410 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5411 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5412 GenerateWideAtomicLoad(base, offset, lo, hi);
5413 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5414 // scope.
5415 codegen_->MaybeRecordImplicitNullCheck(instruction);
5416 __ Vmov(out_dreg, lo, hi);
5417 } else {
5418 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005419 codegen_->MaybeRecordImplicitNullCheck(instruction);
5420 }
5421 break;
5422 }
5423
5424 case Primitive::kPrimVoid:
5425 LOG(FATAL) << "Unreachable type " << field_type;
5426 UNREACHABLE();
5427 }
5428
5429 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5430 // Potential implicit null checks, in the case of reference or
5431 // double fields, are handled in the previous switch statement.
5432 } else {
5433 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005434 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5435 // should use a scope and the assembler to emit the load instruction to guarantee that we
5436 // record the pc at the correct position. But the `Assembler` does not automatically handle
5437 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5438 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
5440 }
5441
5442 if (is_volatile) {
5443 if (field_type == Primitive::kPrimNot) {
5444 // Memory barriers, in the case of references, are also handled
5445 // in the previous switch statement.
5446 } else {
5447 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5448 }
5449 }
5450}
5451
5452void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5453 HandleFieldSet(instruction, instruction->GetFieldInfo());
5454}
5455
5456void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5457 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5458}
5459
5460void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5461 HandleFieldGet(instruction, instruction->GetFieldInfo());
5462}
5463
5464void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5465 HandleFieldGet(instruction, instruction->GetFieldInfo());
5466}
5467
5468void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5469 HandleFieldGet(instruction, instruction->GetFieldInfo());
5470}
5471
5472void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5473 HandleFieldGet(instruction, instruction->GetFieldInfo());
5474}
5475
Scott Wakelingc34dba72016-10-03 10:14:44 +01005476void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5477 HandleFieldSet(instruction, instruction->GetFieldInfo());
5478}
5479
5480void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5481 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5482}
5483
Artem Serovcfbe9132016-10-14 15:58:56 +01005484void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5485 HUnresolvedInstanceFieldGet* instruction) {
5486 FieldAccessCallingConventionARMVIXL calling_convention;
5487 codegen_->CreateUnresolvedFieldLocationSummary(
5488 instruction, instruction->GetFieldType(), calling_convention);
5489}
5490
5491void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5492 HUnresolvedInstanceFieldGet* instruction) {
5493 FieldAccessCallingConventionARMVIXL calling_convention;
5494 codegen_->GenerateUnresolvedFieldAccess(instruction,
5495 instruction->GetFieldType(),
5496 instruction->GetFieldIndex(),
5497 instruction->GetDexPc(),
5498 calling_convention);
5499}
5500
5501void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5502 HUnresolvedInstanceFieldSet* instruction) {
5503 FieldAccessCallingConventionARMVIXL calling_convention;
5504 codegen_->CreateUnresolvedFieldLocationSummary(
5505 instruction, instruction->GetFieldType(), calling_convention);
5506}
5507
5508void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5509 HUnresolvedInstanceFieldSet* instruction) {
5510 FieldAccessCallingConventionARMVIXL calling_convention;
5511 codegen_->GenerateUnresolvedFieldAccess(instruction,
5512 instruction->GetFieldType(),
5513 instruction->GetFieldIndex(),
5514 instruction->GetDexPc(),
5515 calling_convention);
5516}
5517
5518void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5519 HUnresolvedStaticFieldGet* instruction) {
5520 FieldAccessCallingConventionARMVIXL calling_convention;
5521 codegen_->CreateUnresolvedFieldLocationSummary(
5522 instruction, instruction->GetFieldType(), calling_convention);
5523}
5524
5525void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5526 HUnresolvedStaticFieldGet* instruction) {
5527 FieldAccessCallingConventionARMVIXL calling_convention;
5528 codegen_->GenerateUnresolvedFieldAccess(instruction,
5529 instruction->GetFieldType(),
5530 instruction->GetFieldIndex(),
5531 instruction->GetDexPc(),
5532 calling_convention);
5533}
5534
5535void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5536 HUnresolvedStaticFieldSet* instruction) {
5537 FieldAccessCallingConventionARMVIXL calling_convention;
5538 codegen_->CreateUnresolvedFieldLocationSummary(
5539 instruction, instruction->GetFieldType(), calling_convention);
5540}
5541
5542void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5543 HUnresolvedStaticFieldSet* instruction) {
5544 FieldAccessCallingConventionARMVIXL calling_convention;
5545 codegen_->GenerateUnresolvedFieldAccess(instruction,
5546 instruction->GetFieldType(),
5547 instruction->GetFieldIndex(),
5548 instruction->GetDexPc(),
5549 calling_convention);
5550}
5551
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005552void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005553 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005554 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005555}
5556
5557void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5558 if (CanMoveNullCheckToUser(instruction)) {
5559 return;
5560 }
5561
5562 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005563 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005564 ExactAssemblyScope aas(GetVIXLAssembler(),
5565 vixl32::kMaxInstructionSizeInBytes,
5566 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005567 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5568 RecordPcInfo(instruction, instruction->GetDexPc());
5569}
5570
5571void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5572 NullCheckSlowPathARMVIXL* slow_path =
5573 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5574 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005575 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005576}
5577
5578void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5579 codegen_->GenerateNullCheck(instruction);
5580}
5581
Scott Wakelingc34dba72016-10-03 10:14:44 +01005582static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5583 switch (type) {
5584 case Primitive::kPrimNot:
5585 return kLoadWord;
5586 case Primitive::kPrimBoolean:
5587 return kLoadUnsignedByte;
5588 case Primitive::kPrimByte:
5589 return kLoadSignedByte;
5590 case Primitive::kPrimChar:
5591 return kLoadUnsignedHalfword;
5592 case Primitive::kPrimShort:
5593 return kLoadSignedHalfword;
5594 case Primitive::kPrimInt:
5595 return kLoadWord;
5596 case Primitive::kPrimLong:
5597 return kLoadWordPair;
5598 case Primitive::kPrimFloat:
5599 return kLoadSWord;
5600 case Primitive::kPrimDouble:
5601 return kLoadDWord;
5602 default:
5603 LOG(FATAL) << "Unreachable type " << type;
5604 UNREACHABLE();
5605 }
5606}
5607
5608static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5609 switch (type) {
5610 case Primitive::kPrimNot:
5611 return kStoreWord;
5612 case Primitive::kPrimBoolean:
5613 case Primitive::kPrimByte:
5614 return kStoreByte;
5615 case Primitive::kPrimChar:
5616 case Primitive::kPrimShort:
5617 return kStoreHalfword;
5618 case Primitive::kPrimInt:
5619 return kStoreWord;
5620 case Primitive::kPrimLong:
5621 return kStoreWordPair;
5622 case Primitive::kPrimFloat:
5623 return kStoreSWord;
5624 case Primitive::kPrimDouble:
5625 return kStoreDWord;
5626 default:
5627 LOG(FATAL) << "Unreachable type " << type;
5628 UNREACHABLE();
5629 }
5630}
5631
5632void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5633 Location out_loc,
5634 vixl32::Register base,
5635 vixl32::Register reg_index,
5636 vixl32::Condition cond) {
5637 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5638 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5639
5640 switch (type) {
5641 case Primitive::kPrimByte:
5642 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5643 break;
5644 case Primitive::kPrimBoolean:
5645 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5646 break;
5647 case Primitive::kPrimShort:
5648 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5649 break;
5650 case Primitive::kPrimChar:
5651 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5652 break;
5653 case Primitive::kPrimNot:
5654 case Primitive::kPrimInt:
5655 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5656 break;
5657 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5658 case Primitive::kPrimLong:
5659 case Primitive::kPrimFloat:
5660 case Primitive::kPrimDouble:
5661 default:
5662 LOG(FATAL) << "Unreachable type " << type;
5663 UNREACHABLE();
5664 }
5665}
5666
5667void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5668 Location loc,
5669 vixl32::Register base,
5670 vixl32::Register reg_index,
5671 vixl32::Condition cond) {
5672 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5673 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5674
5675 switch (type) {
5676 case Primitive::kPrimByte:
5677 case Primitive::kPrimBoolean:
5678 __ Strb(cond, RegisterFrom(loc), mem_address);
5679 break;
5680 case Primitive::kPrimShort:
5681 case Primitive::kPrimChar:
5682 __ Strh(cond, RegisterFrom(loc), mem_address);
5683 break;
5684 case Primitive::kPrimNot:
5685 case Primitive::kPrimInt:
5686 __ Str(cond, RegisterFrom(loc), mem_address);
5687 break;
5688 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5689 case Primitive::kPrimLong:
5690 case Primitive::kPrimFloat:
5691 case Primitive::kPrimDouble:
5692 default:
5693 LOG(FATAL) << "Unreachable type " << type;
5694 UNREACHABLE();
5695 }
5696}
5697
5698void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5699 bool object_array_get_with_read_barrier =
5700 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5701 LocationSummary* locations =
5702 new (GetGraph()->GetArena()) LocationSummary(instruction,
5703 object_array_get_with_read_barrier ?
5704 LocationSummary::kCallOnSlowPath :
5705 LocationSummary::kNoCall);
5706 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005707 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005708 }
5709 locations->SetInAt(0, Location::RequiresRegister());
5710 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5711 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5713 } else {
5714 // The output overlaps in the case of an object array get with
5715 // read barriers enabled: we do not want the move to overwrite the
5716 // array's location, as we need it to emit the read barrier.
5717 locations->SetOut(
5718 Location::RequiresRegister(),
5719 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5720 }
5721 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005722 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005723 // Also need for String compression feature.
5724 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5725 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005726 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005727 }
5728}
5729
5730void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005731 LocationSummary* locations = instruction->GetLocations();
5732 Location obj_loc = locations->InAt(0);
5733 vixl32::Register obj = InputRegisterAt(instruction, 0);
5734 Location index = locations->InAt(1);
5735 Location out_loc = locations->Out();
5736 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5737 Primitive::Type type = instruction->GetType();
5738 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5739 instruction->IsStringCharAt();
5740 HInstruction* array_instr = instruction->GetArray();
5741 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005742
5743 switch (type) {
5744 case Primitive::kPrimBoolean:
5745 case Primitive::kPrimByte:
5746 case Primitive::kPrimShort:
5747 case Primitive::kPrimChar:
5748 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005749 vixl32::Register length;
5750 if (maybe_compressed_char_at) {
5751 length = RegisterFrom(locations->GetTemp(0));
5752 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5753 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5754 codegen_->MaybeRecordImplicitNullCheck(instruction);
5755 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005756 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005757 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005758 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005759 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005760 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005761 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5762 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5763 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005764 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005765 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5766 RegisterFrom(out_loc),
5767 obj,
5768 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005769 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005770 __ Bind(&uncompressed_load);
5771 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5772 RegisterFrom(out_loc),
5773 obj,
5774 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005775 if (done.IsReferenced()) {
5776 __ Bind(&done);
5777 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005778 } else {
5779 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5780
5781 LoadOperandType load_type = GetLoadOperandType(type);
5782 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5783 }
5784 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005785 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005786 vixl32::Register temp = temps.Acquire();
5787
5788 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005789 // We do not need to compute the intermediate address from the array: the
5790 // input instruction has done it already. See the comment in
5791 // `TryExtractArrayAccessAddress()`.
5792 if (kIsDebugBuild) {
5793 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005794 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005795 }
5796 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005797 } else {
5798 __ Add(temp, obj, data_offset);
5799 }
5800 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005801 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005802 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005803 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5804 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5805 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005806 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005807 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005808 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005809 __ Bind(&uncompressed_load);
5810 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005811 if (done.IsReferenced()) {
5812 __ Bind(&done);
5813 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005814 } else {
5815 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5816 }
5817 }
5818 break;
5819 }
5820
5821 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005822 // The read barrier instrumentation of object ArrayGet
5823 // instructions does not support the HIntermediateAddress
5824 // instruction.
5825 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5826
Scott Wakelingc34dba72016-10-03 10:14:44 +01005827 static_assert(
5828 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5829 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5830 // /* HeapReference<Object> */ out =
5831 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5832 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005833 Location temp = locations->GetTemp(0);
5834 // Note that a potential implicit null check is handled in this
5835 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5836 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5837 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005838 } else {
5839 vixl32::Register out = OutputRegister(instruction);
5840 if (index.IsConstant()) {
5841 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005842 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005843 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005844 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5845 // we should use a scope and the assembler to emit the load instruction to guarantee that
5846 // we record the pc at the correct position. But the `Assembler` does not automatically
5847 // handle unencodable offsets. Practically, everything is fine because the helper and
5848 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005849 codegen_->MaybeRecordImplicitNullCheck(instruction);
5850 // If read barriers are enabled, emit read barriers other than
5851 // Baker's using a slow path (and also unpoison the loaded
5852 // reference, if heap poisoning is enabled).
5853 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5854 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005855 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005856 vixl32::Register temp = temps.Acquire();
5857
5858 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005859 // We do not need to compute the intermediate address from the array: the
5860 // input instruction has done it already. See the comment in
5861 // `TryExtractArrayAccessAddress()`.
5862 if (kIsDebugBuild) {
5863 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005864 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005865 }
5866 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005867 } else {
5868 __ Add(temp, obj, data_offset);
5869 }
5870 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005871 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005872 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5873 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5874 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005875 codegen_->MaybeRecordImplicitNullCheck(instruction);
5876 // If read barriers are enabled, emit read barriers other than
5877 // Baker's using a slow path (and also unpoison the loaded
5878 // reference, if heap poisoning is enabled).
5879 codegen_->MaybeGenerateReadBarrierSlow(
5880 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5881 }
5882 }
5883 break;
5884 }
5885
5886 case Primitive::kPrimLong: {
5887 if (index.IsConstant()) {
5888 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005889 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005890 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5891 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005892 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005893 vixl32::Register temp = temps.Acquire();
5894 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5895 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5896 }
5897 break;
5898 }
5899
5900 case Primitive::kPrimFloat: {
5901 vixl32::SRegister out = SRegisterFrom(out_loc);
5902 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005903 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005904 GetAssembler()->LoadSFromOffset(out, obj, offset);
5905 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005906 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005907 vixl32::Register temp = temps.Acquire();
5908 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5909 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5910 }
5911 break;
5912 }
5913
5914 case Primitive::kPrimDouble: {
5915 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005916 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005917 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5918 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005919 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005920 vixl32::Register temp = temps.Acquire();
5921 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5922 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5923 }
5924 break;
5925 }
5926
5927 case Primitive::kPrimVoid:
5928 LOG(FATAL) << "Unreachable type " << type;
5929 UNREACHABLE();
5930 }
5931
5932 if (type == Primitive::kPrimNot) {
5933 // Potential implicit null checks, in the case of reference
5934 // arrays, are handled in the previous switch statement.
5935 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005936 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5937 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005938 codegen_->MaybeRecordImplicitNullCheck(instruction);
5939 }
5940}
5941
5942void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5943 Primitive::Type value_type = instruction->GetComponentType();
5944
5945 bool needs_write_barrier =
5946 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5947 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5948
5949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5950 instruction,
5951 may_need_runtime_call_for_type_check ?
5952 LocationSummary::kCallOnSlowPath :
5953 LocationSummary::kNoCall);
5954
5955 locations->SetInAt(0, Location::RequiresRegister());
5956 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5957 if (Primitive::IsFloatingPointType(value_type)) {
5958 locations->SetInAt(2, Location::RequiresFpuRegister());
5959 } else {
5960 locations->SetInAt(2, Location::RequiresRegister());
5961 }
5962 if (needs_write_barrier) {
5963 // Temporary registers for the write barrier.
5964 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5965 locations->AddTemp(Location::RequiresRegister());
5966 }
5967}
5968
5969void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005970 LocationSummary* locations = instruction->GetLocations();
5971 vixl32::Register array = InputRegisterAt(instruction, 0);
5972 Location index = locations->InAt(1);
5973 Primitive::Type value_type = instruction->GetComponentType();
5974 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5975 bool needs_write_barrier =
5976 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5977 uint32_t data_offset =
5978 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5979 Location value_loc = locations->InAt(2);
5980 HInstruction* array_instr = instruction->GetArray();
5981 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005982
5983 switch (value_type) {
5984 case Primitive::kPrimBoolean:
5985 case Primitive::kPrimByte:
5986 case Primitive::kPrimShort:
5987 case Primitive::kPrimChar:
5988 case Primitive::kPrimInt: {
5989 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005990 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005991 uint32_t full_offset =
5992 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5993 StoreOperandType store_type = GetStoreOperandType(value_type);
5994 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5995 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005996 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005997 vixl32::Register temp = temps.Acquire();
5998
5999 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006000 // We do not need to compute the intermediate address from the array: the
6001 // input instruction has done it already. See the comment in
6002 // `TryExtractArrayAccessAddress()`.
6003 if (kIsDebugBuild) {
6004 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006005 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006006 }
6007 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006008 } else {
6009 __ Add(temp, array, data_offset);
6010 }
6011 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6012 }
6013 break;
6014 }
6015
6016 case Primitive::kPrimNot: {
6017 vixl32::Register value = RegisterFrom(value_loc);
6018 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6019 // See the comment in instruction_simplifier_shared.cc.
6020 DCHECK(!has_intermediate_address);
6021
6022 if (instruction->InputAt(2)->IsNullConstant()) {
6023 // Just setting null.
6024 if (index.IsConstant()) {
6025 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006026 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006027 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6028 } else {
6029 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006030 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006031 vixl32::Register temp = temps.Acquire();
6032 __ Add(temp, array, data_offset);
6033 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6034 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006035 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6036 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006037 codegen_->MaybeRecordImplicitNullCheck(instruction);
6038 DCHECK(!needs_write_barrier);
6039 DCHECK(!may_need_runtime_call_for_type_check);
6040 break;
6041 }
6042
6043 DCHECK(needs_write_barrier);
6044 Location temp1_loc = locations->GetTemp(0);
6045 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6046 Location temp2_loc = locations->GetTemp(1);
6047 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6048 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6049 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6050 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6051 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006052 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006053 SlowPathCodeARMVIXL* slow_path = nullptr;
6054
6055 if (may_need_runtime_call_for_type_check) {
6056 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6057 codegen_->AddSlowPath(slow_path);
6058 if (instruction->GetValueCanBeNull()) {
6059 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006060 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006061 if (index.IsConstant()) {
6062 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006063 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006064 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6065 } else {
6066 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006067 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006068 vixl32::Register temp = temps.Acquire();
6069 __ Add(temp, array, data_offset);
6070 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6071 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006072 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6073 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006075 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006076 __ Bind(&non_zero);
6077 }
6078
6079 // Note that when read barriers are enabled, the type checks
6080 // are performed without read barriers. This is fine, even in
6081 // the case where a class object is in the from-space after
6082 // the flip, as a comparison involving such a type would not
6083 // produce a false positive; it may of course produce a false
6084 // negative, in which case we would take the ArraySet slow
6085 // path.
6086
Alexandre Rames374ddf32016-11-04 10:40:49 +00006087 {
6088 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006089 ExactAssemblyScope aas(GetVIXLAssembler(),
6090 vixl32::kMaxInstructionSizeInBytes,
6091 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006092 // /* HeapReference<Class> */ temp1 = array->klass_
6093 __ ldr(temp1, MemOperand(array, class_offset));
6094 codegen_->MaybeRecordImplicitNullCheck(instruction);
6095 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006096 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6097
6098 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6099 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6100 // /* HeapReference<Class> */ temp2 = value->klass_
6101 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6102 // If heap poisoning is enabled, no need to unpoison `temp1`
6103 // nor `temp2`, as we are comparing two poisoned references.
6104 __ Cmp(temp1, temp2);
6105
6106 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6107 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006108 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006109 // If heap poisoning is enabled, the `temp1` reference has
6110 // not been unpoisoned yet; unpoison it now.
6111 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6112
6113 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6114 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6115 // If heap poisoning is enabled, no need to unpoison
6116 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006117 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006118 __ Bind(&do_put);
6119 } else {
6120 __ B(ne, slow_path->GetEntryLabel());
6121 }
6122 }
6123
6124 vixl32::Register source = value;
6125 if (kPoisonHeapReferences) {
6126 // Note that in the case where `value` is a null reference,
6127 // we do not enter this block, as a null reference does not
6128 // need poisoning.
6129 DCHECK_EQ(value_type, Primitive::kPrimNot);
6130 __ Mov(temp1, value);
6131 GetAssembler()->PoisonHeapReference(temp1);
6132 source = temp1;
6133 }
6134
6135 if (index.IsConstant()) {
6136 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006137 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006138 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6139 } else {
6140 DCHECK(index.IsRegister()) << index;
6141
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006142 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006143 vixl32::Register temp = temps.Acquire();
6144 __ Add(temp, array, data_offset);
6145 codegen_->StoreToShiftedRegOffset(value_type,
6146 LocationFrom(source),
6147 temp,
6148 RegisterFrom(index));
6149 }
6150
6151 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006152 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6153 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006154 codegen_->MaybeRecordImplicitNullCheck(instruction);
6155 }
6156
6157 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6158
6159 if (done.IsReferenced()) {
6160 __ Bind(&done);
6161 }
6162
6163 if (slow_path != nullptr) {
6164 __ Bind(slow_path->GetExitLabel());
6165 }
6166
6167 break;
6168 }
6169
6170 case Primitive::kPrimLong: {
6171 Location value = locations->InAt(2);
6172 if (index.IsConstant()) {
6173 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006174 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006175 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6176 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006177 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006178 vixl32::Register temp = temps.Acquire();
6179 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6180 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6181 }
6182 break;
6183 }
6184
6185 case Primitive::kPrimFloat: {
6186 Location value = locations->InAt(2);
6187 DCHECK(value.IsFpuRegister());
6188 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006189 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006190 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6191 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006192 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006193 vixl32::Register temp = temps.Acquire();
6194 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6195 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6196 }
6197 break;
6198 }
6199
6200 case Primitive::kPrimDouble: {
6201 Location value = locations->InAt(2);
6202 DCHECK(value.IsFpuRegisterPair());
6203 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006204 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006205 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6206 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006207 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006208 vixl32::Register temp = temps.Acquire();
6209 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6210 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6211 }
6212 break;
6213 }
6214
6215 case Primitive::kPrimVoid:
6216 LOG(FATAL) << "Unreachable type " << value_type;
6217 UNREACHABLE();
6218 }
6219
6220 // Objects are handled in the switch.
6221 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006222 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6223 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006224 codegen_->MaybeRecordImplicitNullCheck(instruction);
6225 }
6226}
6227
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006228void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6229 LocationSummary* locations =
6230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6231 locations->SetInAt(0, Location::RequiresRegister());
6232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6233}
6234
6235void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6236 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6237 vixl32::Register obj = InputRegisterAt(instruction, 0);
6238 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006239 {
Artem Serov0fb37192016-12-06 18:13:40 +00006240 ExactAssemblyScope aas(GetVIXLAssembler(),
6241 vixl32::kMaxInstructionSizeInBytes,
6242 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006243 __ ldr(out, MemOperand(obj, offset));
6244 codegen_->MaybeRecordImplicitNullCheck(instruction);
6245 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006246 // Mask out compression flag from String's array length.
6247 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006248 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006249 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006250}
6251
Artem Serov2bbc9532016-10-21 11:51:50 +01006252void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006253 LocationSummary* locations =
6254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6255
6256 locations->SetInAt(0, Location::RequiresRegister());
6257 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6259}
6260
6261void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6262 vixl32::Register out = OutputRegister(instruction);
6263 vixl32::Register first = InputRegisterAt(instruction, 0);
6264 Location second = instruction->GetLocations()->InAt(1);
6265
Artem Serov2bbc9532016-10-21 11:51:50 +01006266 if (second.IsRegister()) {
6267 __ Add(out, first, RegisterFrom(second));
6268 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006269 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006270 }
6271}
6272
Scott Wakelingc34dba72016-10-03 10:14:44 +01006273void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6274 RegisterSet caller_saves = RegisterSet::Empty();
6275 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6276 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6277 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6278 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006279
6280 HInstruction* index = instruction->InputAt(0);
6281 HInstruction* length = instruction->InputAt(1);
6282 // If both index and length are constants we can statically check the bounds. But if at least one
6283 // of them is not encodable ArmEncodableConstantOrRegister will create
6284 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6285 // locations.
6286 bool both_const = index->IsConstant() && length->IsConstant();
6287 locations->SetInAt(0, both_const
6288 ? Location::ConstantLocation(index->AsConstant())
6289 : ArmEncodableConstantOrRegister(index, CMP));
6290 locations->SetInAt(1, both_const
6291 ? Location::ConstantLocation(length->AsConstant())
6292 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006293}
6294
6295void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006296 LocationSummary* locations = instruction->GetLocations();
6297 Location index_loc = locations->InAt(0);
6298 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006299
Artem Serov2dd053d2017-03-08 14:54:06 +00006300 if (length_loc.IsConstant()) {
6301 int32_t length = Int32ConstantFrom(length_loc);
6302 if (index_loc.IsConstant()) {
6303 // BCE will remove the bounds check if we are guaranteed to pass.
6304 int32_t index = Int32ConstantFrom(index_loc);
6305 if (index < 0 || index >= length) {
6306 SlowPathCodeARMVIXL* slow_path =
6307 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6308 codegen_->AddSlowPath(slow_path);
6309 __ B(slow_path->GetEntryLabel());
6310 } else {
6311 // Some optimization after BCE may have generated this, and we should not
6312 // generate a bounds check if it is a valid range.
6313 }
6314 return;
6315 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006316
Artem Serov2dd053d2017-03-08 14:54:06 +00006317 SlowPathCodeARMVIXL* slow_path =
6318 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6319 __ Cmp(RegisterFrom(index_loc), length);
6320 codegen_->AddSlowPath(slow_path);
6321 __ B(hs, slow_path->GetEntryLabel());
6322 } else {
6323 SlowPathCodeARMVIXL* slow_path =
6324 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6325 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6326 codegen_->AddSlowPath(slow_path);
6327 __ B(ls, slow_path->GetEntryLabel());
6328 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006329}
6330
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006331void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6332 vixl32::Register card,
6333 vixl32::Register object,
6334 vixl32::Register value,
6335 bool can_be_null) {
6336 vixl32::Label is_null;
6337 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006338 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006339 }
6340 GetAssembler()->LoadFromOffset(
6341 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006342 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006343 __ Strb(card, MemOperand(card, temp));
6344 if (can_be_null) {
6345 __ Bind(&is_null);
6346 }
6347}
6348
Scott Wakelingfe885462016-09-22 10:24:38 +01006349void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6350 LOG(FATAL) << "Unreachable";
6351}
6352
6353void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6354 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6355}
6356
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006357void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006358 LocationSummary* locations =
6359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6360 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006361}
6362
6363void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6364 HBasicBlock* block = instruction->GetBlock();
6365 if (block->GetLoopInformation() != nullptr) {
6366 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6367 // The back edge will generate the suspend check.
6368 return;
6369 }
6370 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6371 // The goto will generate the suspend check.
6372 return;
6373 }
6374 GenerateSuspendCheck(instruction, nullptr);
6375}
6376
6377void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6378 HBasicBlock* successor) {
6379 SuspendCheckSlowPathARMVIXL* slow_path =
6380 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6381 if (slow_path == nullptr) {
6382 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6383 instruction->SetSlowPath(slow_path);
6384 codegen_->AddSlowPath(slow_path);
6385 if (successor != nullptr) {
6386 DCHECK(successor->IsLoopHeader());
6387 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6388 }
6389 } else {
6390 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6391 }
6392
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006393 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006394 vixl32::Register temp = temps.Acquire();
6395 GetAssembler()->LoadFromOffset(
6396 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6397 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006398 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006399 __ Bind(slow_path->GetReturnLabel());
6400 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006401 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006402 __ B(slow_path->GetEntryLabel());
6403 }
6404}
6405
Scott Wakelingfe885462016-09-22 10:24:38 +01006406ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6407 return codegen_->GetAssembler();
6408}
6409
6410void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006411 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006412 MoveOperands* move = moves_[index];
6413 Location source = move->GetSource();
6414 Location destination = move->GetDestination();
6415
6416 if (source.IsRegister()) {
6417 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006418 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006419 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006420 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006421 } else {
6422 DCHECK(destination.IsStackSlot());
6423 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006424 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006425 sp,
6426 destination.GetStackIndex());
6427 }
6428 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006429 if (destination.IsRegister()) {
6430 GetAssembler()->LoadFromOffset(kLoadWord,
6431 RegisterFrom(destination),
6432 sp,
6433 source.GetStackIndex());
6434 } else if (destination.IsFpuRegister()) {
6435 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6436 } else {
6437 DCHECK(destination.IsStackSlot());
6438 vixl32::Register temp = temps.Acquire();
6439 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6440 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6441 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006442 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006443 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006444 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006445 } else if (destination.IsFpuRegister()) {
6446 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6447 } else {
6448 DCHECK(destination.IsStackSlot());
6449 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6450 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006451 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006452 if (destination.IsDoubleStackSlot()) {
6453 vixl32::DRegister temp = temps.AcquireD();
6454 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6455 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6456 } else if (destination.IsRegisterPair()) {
6457 DCHECK(ExpectedPairLayout(destination));
6458 GetAssembler()->LoadFromOffset(
6459 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6460 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006461 DCHECK(destination.IsFpuRegisterPair()) << destination;
6462 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006463 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006464 } else if (source.IsRegisterPair()) {
6465 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006466 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6467 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006468 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006469 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006470 } else {
6471 DCHECK(destination.IsDoubleStackSlot()) << destination;
6472 DCHECK(ExpectedPairLayout(source));
6473 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006474 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006475 sp,
6476 destination.GetStackIndex());
6477 }
6478 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006479 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006480 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006481 } else if (destination.IsFpuRegisterPair()) {
6482 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6483 } else {
6484 DCHECK(destination.IsDoubleStackSlot()) << destination;
6485 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6486 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006487 } else {
6488 DCHECK(source.IsConstant()) << source;
6489 HConstant* constant = source.GetConstant();
6490 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6491 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6492 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006493 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006494 } else {
6495 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006496 vixl32::Register temp = temps.Acquire();
6497 __ Mov(temp, value);
6498 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6499 }
6500 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006501 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006502 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006503 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6504 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006505 } else {
6506 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006507 vixl32::Register temp = temps.Acquire();
6508 __ Mov(temp, Low32Bits(value));
6509 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6510 __ Mov(temp, High32Bits(value));
6511 GetAssembler()->StoreToOffset(kStoreWord,
6512 temp,
6513 sp,
6514 destination.GetHighStackIndex(kArmWordSize));
6515 }
6516 } else if (constant->IsDoubleConstant()) {
6517 double value = constant->AsDoubleConstant()->GetValue();
6518 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006519 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006520 } else {
6521 DCHECK(destination.IsDoubleStackSlot()) << destination;
6522 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006523 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006524 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006525 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006526 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006527 GetAssembler()->StoreToOffset(kStoreWord,
6528 temp,
6529 sp,
6530 destination.GetHighStackIndex(kArmWordSize));
6531 }
6532 } else {
6533 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6534 float value = constant->AsFloatConstant()->GetValue();
6535 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006536 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006537 } else {
6538 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006539 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006540 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006541 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6542 }
6543 }
6544 }
6545}
6546
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006547void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6548 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6549 vixl32::Register temp = temps.Acquire();
6550 __ Mov(temp, reg);
6551 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6552 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006553}
6554
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006555void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6556 // TODO(VIXL32): Double check the performance of this implementation.
6557 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006558 vixl32::Register temp1 = temps.Acquire();
6559 ScratchRegisterScope ensure_scratch(
6560 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6561 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006562
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006563 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6564 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6565 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6566 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6567 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006568}
6569
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006570void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6571 MoveOperands* move = moves_[index];
6572 Location source = move->GetSource();
6573 Location destination = move->GetDestination();
6574 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6575
6576 if (source.IsRegister() && destination.IsRegister()) {
6577 vixl32::Register temp = temps.Acquire();
6578 DCHECK(!RegisterFrom(source).Is(temp));
6579 DCHECK(!RegisterFrom(destination).Is(temp));
6580 __ Mov(temp, RegisterFrom(destination));
6581 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6582 __ Mov(RegisterFrom(source), temp);
6583 } else if (source.IsRegister() && destination.IsStackSlot()) {
6584 Exchange(RegisterFrom(source), destination.GetStackIndex());
6585 } else if (source.IsStackSlot() && destination.IsRegister()) {
6586 Exchange(RegisterFrom(destination), source.GetStackIndex());
6587 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006588 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006589 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006590 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006591 __ Vmov(temp, SRegisterFrom(source));
6592 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6593 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006594 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6595 vixl32::DRegister temp = temps.AcquireD();
6596 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6597 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6598 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6599 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6600 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6601 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6602 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6603 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6604 vixl32::DRegister temp = temps.AcquireD();
6605 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6606 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6607 GetAssembler()->StoreDToOffset(temp, sp, mem);
6608 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006609 vixl32::DRegister first = DRegisterFrom(source);
6610 vixl32::DRegister second = DRegisterFrom(destination);
6611 vixl32::DRegister temp = temps.AcquireD();
6612 __ Vmov(temp, first);
6613 __ Vmov(first, second);
6614 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006615 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006616 vixl32::DRegister reg = source.IsFpuRegisterPair()
6617 ? DRegisterFrom(source)
6618 : DRegisterFrom(destination);
6619 int mem = source.IsFpuRegisterPair()
6620 ? destination.GetStackIndex()
6621 : source.GetStackIndex();
6622 vixl32::DRegister temp = temps.AcquireD();
6623 __ Vmov(temp, reg);
6624 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6625 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006626 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006627 vixl32::SRegister reg = source.IsFpuRegister()
6628 ? SRegisterFrom(source)
6629 : SRegisterFrom(destination);
6630 int mem = source.IsFpuRegister()
6631 ? destination.GetStackIndex()
6632 : source.GetStackIndex();
6633 vixl32::Register temp = temps.Acquire();
6634 __ Vmov(temp, reg);
6635 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6636 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006637 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6638 vixl32::DRegister temp1 = temps.AcquireD();
6639 vixl32::DRegister temp2 = temps.AcquireD();
6640 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6641 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6642 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6643 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6644 } else {
6645 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6646 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006647}
6648
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006649void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6650 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006651}
6652
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006653void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6654 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006655}
6656
Artem Serov02d37832016-10-25 15:25:33 +01006657HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006658 HLoadClass::LoadKind desired_class_load_kind) {
6659 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006660 case HLoadClass::LoadKind::kInvalid:
6661 LOG(FATAL) << "UNREACHABLE";
6662 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006663 case HLoadClass::LoadKind::kReferrersClass:
6664 break;
6665 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006666 DCHECK(!GetCompilerOptions().GetCompilePic());
6667 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006668 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6669 DCHECK(GetCompilerOptions().GetCompilePic());
6670 break;
6671 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006672 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006673 case HLoadClass::LoadKind::kBssEntry:
6674 DCHECK(!Runtime::Current()->UseJitCompilation());
6675 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006676 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006677 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006678 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006679 case HLoadClass::LoadKind::kDexCacheViaMethod:
6680 break;
6681 }
6682 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006683}
6684
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006685void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006686 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6687 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006688 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006689 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006690 cls,
6691 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006692 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006693 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006694 return;
6695 }
Vladimir Marko41559982017-01-06 14:04:23 +00006696 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006697
Artem Serovd4cc5b22016-11-04 11:19:09 +00006698 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6699 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006700 ? LocationSummary::kCallOnSlowPath
6701 : LocationSummary::kNoCall;
6702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006703 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006704 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006705 }
6706
Vladimir Marko41559982017-01-06 14:04:23 +00006707 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006708 locations->SetInAt(0, Location::RequiresRegister());
6709 }
6710 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006711 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6712 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6713 // Rely on the type resolution or initialization and marking to save everything we need.
6714 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6715 // to the custom calling convention) or by marking, so we request a different temp.
6716 locations->AddTemp(Location::RequiresRegister());
6717 RegisterSet caller_saves = RegisterSet::Empty();
6718 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6719 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6720 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6721 // that the the kPrimNot result register is the same as the first argument register.
6722 locations->SetCustomSlowPathCallerSaves(caller_saves);
6723 } else {
6724 // For non-Baker read barrier we have a temp-clobbering call.
6725 }
6726 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006727}
6728
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006729// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6730// move.
6731void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006732 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6733 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6734 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006735 return;
6736 }
Vladimir Marko41559982017-01-06 14:04:23 +00006737 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006738
Vladimir Marko41559982017-01-06 14:04:23 +00006739 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006740 Location out_loc = locations->Out();
6741 vixl32::Register out = OutputRegister(cls);
6742
Artem Serovd4cc5b22016-11-04 11:19:09 +00006743 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6744 ? kWithoutReadBarrier
6745 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006746 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006747 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006748 case HLoadClass::LoadKind::kReferrersClass: {
6749 DCHECK(!cls->CanCallRuntime());
6750 DCHECK(!cls->MustGenerateClinitCheck());
6751 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6752 vixl32::Register current_method = InputRegisterAt(cls, 0);
6753 GenerateGcRootFieldLoad(cls,
6754 out_loc,
6755 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006756 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006757 read_barrier_option);
6758 break;
6759 }
6760 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006761 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006762 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6763 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6764 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006765 break;
6766 }
6767 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006768 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006769 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6770 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6771 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6772 codegen_->EmitMovwMovtPlaceholder(labels, out);
6773 break;
6774 }
6775 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006776 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006777 uint32_t address = dchecked_integral_cast<uint32_t>(
6778 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6779 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006780 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006781 break;
6782 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006783 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006784 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6785 ? RegisterFrom(locations->GetTemp(0))
6786 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006787 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006788 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006789 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6790 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006791 generate_null_check = true;
6792 break;
6793 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006794 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006795 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6796 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006797 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006798 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006799 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006800 break;
6801 }
Vladimir Marko41559982017-01-06 14:04:23 +00006802 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006803 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006804 LOG(FATAL) << "UNREACHABLE";
6805 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006806 }
6807
6808 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6809 DCHECK(cls->CanCallRuntime());
6810 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6811 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6812 codegen_->AddSlowPath(slow_path);
6813 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006814 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006815 }
6816 if (cls->MustGenerateClinitCheck()) {
6817 GenerateClassInitializationCheck(slow_path, out);
6818 } else {
6819 __ Bind(slow_path->GetExitLabel());
6820 }
6821 }
6822}
6823
Artem Serov02d37832016-10-25 15:25:33 +01006824void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6825 LocationSummary* locations =
6826 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6827 locations->SetInAt(0, Location::RequiresRegister());
6828 if (check->HasUses()) {
6829 locations->SetOut(Location::SameAsFirstInput());
6830 }
6831}
6832
6833void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6834 // We assume the class is not null.
6835 LoadClassSlowPathARMVIXL* slow_path =
6836 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6837 check,
6838 check->GetDexPc(),
6839 /* do_clinit */ true);
6840 codegen_->AddSlowPath(slow_path);
6841 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6842}
6843
6844void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6845 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6846 UseScratchRegisterScope temps(GetVIXLAssembler());
6847 vixl32::Register temp = temps.Acquire();
6848 GetAssembler()->LoadFromOffset(kLoadWord,
6849 temp,
6850 class_reg,
6851 mirror::Class::StatusOffset().Int32Value());
6852 __ Cmp(temp, mirror::Class::kStatusInitialized);
6853 __ B(lt, slow_path->GetEntryLabel());
6854 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6855 // properly. Therefore, we do a memory fence.
6856 __ Dmb(ISH);
6857 __ Bind(slow_path->GetExitLabel());
6858}
6859
Artem Serov02d37832016-10-25 15:25:33 +01006860HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006861 HLoadString::LoadKind desired_string_load_kind) {
6862 switch (desired_string_load_kind) {
6863 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006864 DCHECK(!GetCompilerOptions().GetCompilePic());
6865 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006866 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6867 DCHECK(GetCompilerOptions().GetCompilePic());
6868 break;
6869 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006870 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006871 case HLoadString::LoadKind::kBssEntry:
6872 DCHECK(!Runtime::Current()->UseJitCompilation());
6873 break;
6874 case HLoadString::LoadKind::kJitTableAddress:
6875 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006876 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006877 case HLoadString::LoadKind::kDexCacheViaMethod:
6878 break;
6879 }
6880 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006881}
6882
6883void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006884 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006886 HLoadString::LoadKind load_kind = load->GetLoadKind();
6887 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006888 locations->SetOut(LocationFrom(r0));
6889 } else {
6890 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006891 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6892 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006893 // Rely on the pResolveString and marking to save everything we need, including temps.
6894 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6895 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006896 locations->AddTemp(Location::RequiresRegister());
6897 RegisterSet caller_saves = RegisterSet::Empty();
6898 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6899 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6900 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6901 // that the the kPrimNot result register is the same as the first argument register.
6902 locations->SetCustomSlowPathCallerSaves(caller_saves);
6903 } else {
6904 // For non-Baker read barrier we have a temp-clobbering call.
6905 }
6906 }
Artem Serov02d37832016-10-25 15:25:33 +01006907 }
6908}
6909
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006910// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6911// move.
6912void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006913 LocationSummary* locations = load->GetLocations();
6914 Location out_loc = locations->Out();
6915 vixl32::Register out = OutputRegister(load);
6916 HLoadString::LoadKind load_kind = load->GetLoadKind();
6917
6918 switch (load_kind) {
6919 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006920 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6921 load->GetStringIndex()));
6922 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006923 }
6924 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6925 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6926 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006927 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006928 codegen_->EmitMovwMovtPlaceholder(labels, out);
6929 return; // No dex cache slow path.
6930 }
6931 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006932 uint32_t address = dchecked_integral_cast<uint32_t>(
6933 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6934 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006935 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6936 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006937 }
6938 case HLoadString::LoadKind::kBssEntry: {
6939 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006940 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6941 ? RegisterFrom(locations->GetTemp(0))
6942 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006943 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006944 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006945 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6946 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6947 LoadStringSlowPathARMVIXL* slow_path =
6948 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6949 codegen_->AddSlowPath(slow_path);
6950 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6951 __ Bind(slow_path->GetExitLabel());
6952 return;
6953 }
6954 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006955 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006956 load->GetStringIndex(),
6957 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006958 // /* GcRoot<mirror::String> */ out = *out
6959 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6960 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006961 }
6962 default:
6963 break;
6964 }
Artem Serov02d37832016-10-25 15:25:33 +01006965
6966 // TODO: Re-add the compiler code to do string dex cache lookup again.
6967 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6968 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006969 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006970 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6971 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6972}
6973
6974static int32_t GetExceptionTlsOffset() {
6975 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6976}
6977
6978void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6979 LocationSummary* locations =
6980 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6981 locations->SetOut(Location::RequiresRegister());
6982}
6983
6984void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6985 vixl32::Register out = OutputRegister(load);
6986 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6987}
6988
6989
6990void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6991 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6992}
6993
6994void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6995 UseScratchRegisterScope temps(GetVIXLAssembler());
6996 vixl32::Register temp = temps.Acquire();
6997 __ Mov(temp, 0);
6998 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6999}
7000
7001void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7002 LocationSummary* locations =
7003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7004 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7005 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7006}
7007
7008void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7009 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7010 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7011}
7012
Artem Serov657022c2016-11-23 14:19:38 +00007013// Temp is used for read barrier.
7014static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7015 if (kEmitCompilerReadBarrier &&
7016 (kUseBakerReadBarrier ||
7017 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7018 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7019 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7020 return 1;
7021 }
7022 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007023}
7024
Artem Serov657022c2016-11-23 14:19:38 +00007025// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7026// interface pointer, one for loading the current interface.
7027// The other checks have one temp for loading the object's class.
7028static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7029 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7030 return 3;
7031 }
7032 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7033}
Artem Serovcfbe9132016-10-14 15:58:56 +01007034
7035void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7036 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7037 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7038 bool baker_read_barrier_slow_path = false;
7039 switch (type_check_kind) {
7040 case TypeCheckKind::kExactCheck:
7041 case TypeCheckKind::kAbstractClassCheck:
7042 case TypeCheckKind::kClassHierarchyCheck:
7043 case TypeCheckKind::kArrayObjectCheck:
7044 call_kind =
7045 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7046 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7047 break;
7048 case TypeCheckKind::kArrayCheck:
7049 case TypeCheckKind::kUnresolvedCheck:
7050 case TypeCheckKind::kInterfaceCheck:
7051 call_kind = LocationSummary::kCallOnSlowPath;
7052 break;
7053 }
7054
7055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7056 if (baker_read_barrier_slow_path) {
7057 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7058 }
7059 locations->SetInAt(0, Location::RequiresRegister());
7060 locations->SetInAt(1, Location::RequiresRegister());
7061 // The "out" register is used as a temporary, so it overlaps with the inputs.
7062 // Note that TypeCheckSlowPathARM uses this register too.
7063 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007064 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007065}
7066
7067void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7068 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7069 LocationSummary* locations = instruction->GetLocations();
7070 Location obj_loc = locations->InAt(0);
7071 vixl32::Register obj = InputRegisterAt(instruction, 0);
7072 vixl32::Register cls = InputRegisterAt(instruction, 1);
7073 Location out_loc = locations->Out();
7074 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007075 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7076 DCHECK_LE(num_temps, 1u);
7077 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007078 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7079 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7080 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7081 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7082 vixl32::Label done, zero;
Anton Kirilov6f644202017-02-27 18:29:45 +00007083 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007084 SlowPathCodeARMVIXL* slow_path = nullptr;
7085
7086 // Return 0 if `obj` is null.
7087 // avoid null check if we know obj is not null.
7088 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007089 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007090 }
7091
Artem Serovcfbe9132016-10-14 15:58:56 +01007092 switch (type_check_kind) {
7093 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007094 // /* HeapReference<Class> */ out = obj->klass_
7095 GenerateReferenceLoadTwoRegisters(instruction,
7096 out_loc,
7097 obj_loc,
7098 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007099 maybe_temp_loc,
7100 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007101 __ Cmp(out, cls);
7102 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00007103 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007104 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00007105 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007106 break;
7107 }
7108
7109 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007110 // /* HeapReference<Class> */ out = obj->klass_
7111 GenerateReferenceLoadTwoRegisters(instruction,
7112 out_loc,
7113 obj_loc,
7114 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007115 maybe_temp_loc,
7116 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007117 // If the class is abstract, we eagerly fetch the super class of the
7118 // object to avoid doing a comparison we know will fail.
7119 vixl32::Label loop;
7120 __ Bind(&loop);
7121 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007122 GenerateReferenceLoadOneRegister(instruction,
7123 out_loc,
7124 super_offset,
7125 maybe_temp_loc,
7126 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007127 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007128 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007129 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007130 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007131 __ Mov(out, 1);
7132 if (zero.IsReferenced()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007133 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007134 }
7135 break;
7136 }
7137
7138 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007139 // /* HeapReference<Class> */ out = obj->klass_
7140 GenerateReferenceLoadTwoRegisters(instruction,
7141 out_loc,
7142 obj_loc,
7143 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007144 maybe_temp_loc,
7145 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007146 // Walk over the class hierarchy to find a match.
7147 vixl32::Label loop, success;
7148 __ Bind(&loop);
7149 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007150 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007151 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007152 GenerateReferenceLoadOneRegister(instruction,
7153 out_loc,
7154 super_offset,
7155 maybe_temp_loc,
7156 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00007157 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01007158 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007159 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007160 __ Bind(&success);
7161 __ Mov(out, 1);
7162 if (zero.IsReferenced()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007163 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007164 }
7165 break;
7166 }
7167
7168 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007169 // /* HeapReference<Class> */ out = obj->klass_
7170 GenerateReferenceLoadTwoRegisters(instruction,
7171 out_loc,
7172 obj_loc,
7173 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007174 maybe_temp_loc,
7175 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007176 // Do an exact check.
7177 vixl32::Label exact_check;
7178 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007179 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007180 // Otherwise, we need to check that the object's class is a non-primitive array.
7181 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007182 GenerateReferenceLoadOneRegister(instruction,
7183 out_loc,
7184 component_offset,
7185 maybe_temp_loc,
7186 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007187 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007188 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007189 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7190 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007191 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007192 __ Bind(&exact_check);
7193 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00007194 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007195 break;
7196 }
7197
7198 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007199 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007200 // /* HeapReference<Class> */ out = obj->klass_
7201 GenerateReferenceLoadTwoRegisters(instruction,
7202 out_loc,
7203 obj_loc,
7204 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007205 maybe_temp_loc,
7206 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007207 __ Cmp(out, cls);
7208 DCHECK(locations->OnlyCallsOnSlowPath());
7209 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7210 /* is_fatal */ false);
7211 codegen_->AddSlowPath(slow_path);
7212 __ B(ne, slow_path->GetEntryLabel());
7213 __ Mov(out, 1);
7214 if (zero.IsReferenced()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007215 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007216 }
7217 break;
7218 }
7219
7220 case TypeCheckKind::kUnresolvedCheck:
7221 case TypeCheckKind::kInterfaceCheck: {
7222 // Note that we indeed only call on slow path, but we always go
7223 // into the slow path for the unresolved and interface check
7224 // cases.
7225 //
7226 // We cannot directly call the InstanceofNonTrivial runtime
7227 // entry point without resorting to a type checking slow path
7228 // here (i.e. by calling InvokeRuntime directly), as it would
7229 // require to assign fixed registers for the inputs of this
7230 // HInstanceOf instruction (following the runtime calling
7231 // convention), which might be cluttered by the potential first
7232 // read barrier emission at the beginning of this method.
7233 //
7234 // TODO: Introduce a new runtime entry point taking the object
7235 // to test (instead of its class) as argument, and let it deal
7236 // with the read barrier issues. This will let us refactor this
7237 // case of the `switch` code as it was previously (with a direct
7238 // call to the runtime not using a type checking slow path).
7239 // This should also be beneficial for the other cases above.
7240 DCHECK(locations->OnlyCallsOnSlowPath());
7241 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7242 /* is_fatal */ false);
7243 codegen_->AddSlowPath(slow_path);
7244 __ B(slow_path->GetEntryLabel());
7245 if (zero.IsReferenced()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007246 __ B(final_label);
Artem Serovcfbe9132016-10-14 15:58:56 +01007247 }
7248 break;
7249 }
7250 }
7251
7252 if (zero.IsReferenced()) {
7253 __ Bind(&zero);
7254 __ Mov(out, 0);
7255 }
7256
7257 if (done.IsReferenced()) {
7258 __ Bind(&done);
7259 }
7260
7261 if (slow_path != nullptr) {
7262 __ Bind(slow_path->GetExitLabel());
7263 }
7264}
7265
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007266void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7267 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7268 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7269
7270 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7271 switch (type_check_kind) {
7272 case TypeCheckKind::kExactCheck:
7273 case TypeCheckKind::kAbstractClassCheck:
7274 case TypeCheckKind::kClassHierarchyCheck:
7275 case TypeCheckKind::kArrayObjectCheck:
7276 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7277 LocationSummary::kCallOnSlowPath :
7278 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7279 break;
7280 case TypeCheckKind::kArrayCheck:
7281 case TypeCheckKind::kUnresolvedCheck:
7282 case TypeCheckKind::kInterfaceCheck:
7283 call_kind = LocationSummary::kCallOnSlowPath;
7284 break;
7285 }
7286
7287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7288 locations->SetInAt(0, Location::RequiresRegister());
7289 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007290 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007291}
7292
7293void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7294 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7295 LocationSummary* locations = instruction->GetLocations();
7296 Location obj_loc = locations->InAt(0);
7297 vixl32::Register obj = InputRegisterAt(instruction, 0);
7298 vixl32::Register cls = InputRegisterAt(instruction, 1);
7299 Location temp_loc = locations->GetTemp(0);
7300 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007301 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7302 DCHECK_LE(num_temps, 3u);
7303 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7304 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7305 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7306 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7307 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7308 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7309 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7310 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7311 const uint32_t object_array_data_offset =
7312 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007313
Artem Serov657022c2016-11-23 14:19:38 +00007314 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7315 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7316 // read barriers is done for performance and code size reasons.
7317 bool is_type_check_slow_path_fatal = false;
7318 if (!kEmitCompilerReadBarrier) {
7319 is_type_check_slow_path_fatal =
7320 (type_check_kind == TypeCheckKind::kExactCheck ||
7321 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7322 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7323 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7324 !instruction->CanThrowIntoCatchBlock();
7325 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007326 SlowPathCodeARMVIXL* type_check_slow_path =
7327 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7328 is_type_check_slow_path_fatal);
7329 codegen_->AddSlowPath(type_check_slow_path);
7330
7331 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007332 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007333 // Avoid null check if we know obj is not null.
7334 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007335 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007336 }
7337
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007338 switch (type_check_kind) {
7339 case TypeCheckKind::kExactCheck:
7340 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007341 // /* HeapReference<Class> */ temp = obj->klass_
7342 GenerateReferenceLoadTwoRegisters(instruction,
7343 temp_loc,
7344 obj_loc,
7345 class_offset,
7346 maybe_temp2_loc,
7347 kWithoutReadBarrier);
7348
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007349 __ Cmp(temp, cls);
7350 // Jump to slow path for throwing the exception or doing a
7351 // more involved array check.
7352 __ B(ne, type_check_slow_path->GetEntryLabel());
7353 break;
7354 }
7355
7356 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007357 // /* HeapReference<Class> */ temp = obj->klass_
7358 GenerateReferenceLoadTwoRegisters(instruction,
7359 temp_loc,
7360 obj_loc,
7361 class_offset,
7362 maybe_temp2_loc,
7363 kWithoutReadBarrier);
7364
Artem Serovcfbe9132016-10-14 15:58:56 +01007365 // If the class is abstract, we eagerly fetch the super class of the
7366 // object to avoid doing a comparison we know will fail.
7367 vixl32::Label loop;
7368 __ Bind(&loop);
7369 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007370 GenerateReferenceLoadOneRegister(instruction,
7371 temp_loc,
7372 super_offset,
7373 maybe_temp2_loc,
7374 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007375
7376 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7377 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007378 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007379
7380 // Otherwise, compare the classes.
7381 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007382 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007383 break;
7384 }
7385
7386 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007387 // /* HeapReference<Class> */ temp = obj->klass_
7388 GenerateReferenceLoadTwoRegisters(instruction,
7389 temp_loc,
7390 obj_loc,
7391 class_offset,
7392 maybe_temp2_loc,
7393 kWithoutReadBarrier);
7394
Artem Serovcfbe9132016-10-14 15:58:56 +01007395 // Walk over the class hierarchy to find a match.
7396 vixl32::Label loop;
7397 __ Bind(&loop);
7398 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007399 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007400
7401 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007402 GenerateReferenceLoadOneRegister(instruction,
7403 temp_loc,
7404 super_offset,
7405 maybe_temp2_loc,
7406 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007407
7408 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7409 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007410 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007411 // Otherwise, jump to the beginning of the loop.
7412 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007413 break;
7414 }
7415
Artem Serovcfbe9132016-10-14 15:58:56 +01007416 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007417 // /* HeapReference<Class> */ temp = obj->klass_
7418 GenerateReferenceLoadTwoRegisters(instruction,
7419 temp_loc,
7420 obj_loc,
7421 class_offset,
7422 maybe_temp2_loc,
7423 kWithoutReadBarrier);
7424
Artem Serovcfbe9132016-10-14 15:58:56 +01007425 // Do an exact check.
7426 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007427 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007428
7429 // Otherwise, we need to check that the object's class is a non-primitive array.
7430 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007431 GenerateReferenceLoadOneRegister(instruction,
7432 temp_loc,
7433 component_offset,
7434 maybe_temp2_loc,
7435 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007436 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007437 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007438 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7439 // to further check that this component type is not a primitive type.
7440 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7441 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007442 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007443 break;
7444 }
7445
7446 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007447 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007448 // We cannot directly call the CheckCast runtime entry point
7449 // without resorting to a type checking slow path here (i.e. by
7450 // calling InvokeRuntime directly), as it would require to
7451 // assign fixed registers for the inputs of this HInstanceOf
7452 // instruction (following the runtime calling convention), which
7453 // might be cluttered by the potential first read barrier
7454 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007455
Artem Serovcfbe9132016-10-14 15:58:56 +01007456 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007457 break;
Artem Serov657022c2016-11-23 14:19:38 +00007458
7459 case TypeCheckKind::kInterfaceCheck: {
7460 // Avoid read barriers to improve performance of the fast path. We can not get false
7461 // positives by doing this.
7462 // /* HeapReference<Class> */ temp = obj->klass_
7463 GenerateReferenceLoadTwoRegisters(instruction,
7464 temp_loc,
7465 obj_loc,
7466 class_offset,
7467 maybe_temp2_loc,
7468 kWithoutReadBarrier);
7469
7470 // /* HeapReference<Class> */ temp = temp->iftable_
7471 GenerateReferenceLoadTwoRegisters(instruction,
7472 temp_loc,
7473 temp_loc,
7474 iftable_offset,
7475 maybe_temp2_loc,
7476 kWithoutReadBarrier);
7477 // Iftable is never null.
7478 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7479 // Loop through the iftable and check if any class matches.
7480 vixl32::Label start_loop;
7481 __ Bind(&start_loop);
7482 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7483 type_check_slow_path->GetEntryLabel());
7484 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7485 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7486 // Go to next interface.
7487 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7488 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7489 // Compare the classes and continue the loop if they do not match.
7490 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007491 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007492 break;
7493 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007494 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007495 if (done.IsReferenced()) {
7496 __ Bind(&done);
7497 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007498
7499 __ Bind(type_check_slow_path->GetExitLabel());
7500}
7501
Artem Serov551b28f2016-10-18 19:11:30 +01007502void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7503 LocationSummary* locations =
7504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7505 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7506 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7507}
7508
7509void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7510 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7511 instruction,
7512 instruction->GetDexPc());
7513 if (instruction->IsEnter()) {
7514 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7515 } else {
7516 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7517 }
7518}
7519
Artem Serov02109dd2016-09-23 17:17:54 +01007520void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7521 HandleBitwiseOperation(instruction, AND);
7522}
7523
7524void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7525 HandleBitwiseOperation(instruction, ORR);
7526}
7527
7528void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7529 HandleBitwiseOperation(instruction, EOR);
7530}
7531
7532void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7533 LocationSummary* locations =
7534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7535 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7536 || instruction->GetResultType() == Primitive::kPrimLong);
7537 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7538 locations->SetInAt(0, Location::RequiresRegister());
7539 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7541}
7542
7543void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7544 HandleBitwiseOperation(instruction);
7545}
7546
7547void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7548 HandleBitwiseOperation(instruction);
7549}
7550
7551void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7552 HandleBitwiseOperation(instruction);
7553}
7554
Artem Serov2bbc9532016-10-21 11:51:50 +01007555void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7556 LocationSummary* locations =
7557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7558 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7559 || instruction->GetResultType() == Primitive::kPrimLong);
7560
7561 locations->SetInAt(0, Location::RequiresRegister());
7562 locations->SetInAt(1, Location::RequiresRegister());
7563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7564}
7565
7566void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7567 LocationSummary* locations = instruction->GetLocations();
7568 Location first = locations->InAt(0);
7569 Location second = locations->InAt(1);
7570 Location out = locations->Out();
7571
7572 if (instruction->GetResultType() == Primitive::kPrimInt) {
7573 vixl32::Register first_reg = RegisterFrom(first);
7574 vixl32::Register second_reg = RegisterFrom(second);
7575 vixl32::Register out_reg = RegisterFrom(out);
7576
7577 switch (instruction->GetOpKind()) {
7578 case HInstruction::kAnd:
7579 __ Bic(out_reg, first_reg, second_reg);
7580 break;
7581 case HInstruction::kOr:
7582 __ Orn(out_reg, first_reg, second_reg);
7583 break;
7584 // There is no EON on arm.
7585 case HInstruction::kXor:
7586 default:
7587 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7588 UNREACHABLE();
7589 }
7590 return;
7591
7592 } else {
7593 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7594 vixl32::Register first_low = LowRegisterFrom(first);
7595 vixl32::Register first_high = HighRegisterFrom(first);
7596 vixl32::Register second_low = LowRegisterFrom(second);
7597 vixl32::Register second_high = HighRegisterFrom(second);
7598 vixl32::Register out_low = LowRegisterFrom(out);
7599 vixl32::Register out_high = HighRegisterFrom(out);
7600
7601 switch (instruction->GetOpKind()) {
7602 case HInstruction::kAnd:
7603 __ Bic(out_low, first_low, second_low);
7604 __ Bic(out_high, first_high, second_high);
7605 break;
7606 case HInstruction::kOr:
7607 __ Orn(out_low, first_low, second_low);
7608 __ Orn(out_high, first_high, second_high);
7609 break;
7610 // There is no EON on arm.
7611 case HInstruction::kXor:
7612 default:
7613 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7614 UNREACHABLE();
7615 }
7616 }
7617}
7618
Anton Kirilov74234da2017-01-13 14:42:47 +00007619void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7620 HDataProcWithShifterOp* instruction) {
7621 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7622 instruction->GetType() == Primitive::kPrimLong);
7623 LocationSummary* locations =
7624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7625 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7626 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7627
7628 locations->SetInAt(0, Location::RequiresRegister());
7629 locations->SetInAt(1, Location::RequiresRegister());
7630 locations->SetOut(Location::RequiresRegister(),
7631 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7632}
7633
7634void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7635 HDataProcWithShifterOp* instruction) {
7636 const LocationSummary* const locations = instruction->GetLocations();
7637 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7638 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7639
7640 if (instruction->GetType() == Primitive::kPrimInt) {
7641 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7642
7643 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7644 ? LowRegisterFrom(locations->InAt(1))
7645 : InputRegisterAt(instruction, 1);
7646
7647 GenerateDataProcInstruction(kind,
7648 OutputRegister(instruction),
7649 InputRegisterAt(instruction, 0),
7650 Operand(second,
7651 ShiftFromOpKind(op_kind),
7652 instruction->GetShiftAmount()),
7653 codegen_);
7654 } else {
7655 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7656
7657 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7658 const vixl32::Register second = InputRegisterAt(instruction, 1);
7659
7660 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7661 GenerateDataProc(kind,
7662 locations->Out(),
7663 locations->InAt(0),
7664 second,
7665 Operand(second, ShiftType::ASR, 31),
7666 codegen_);
7667 } else {
7668 GenerateLongDataProc(instruction, codegen_);
7669 }
7670 }
7671}
7672
Artem Serov02109dd2016-09-23 17:17:54 +01007673// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7674void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7675 vixl32::Register first,
7676 uint32_t value) {
7677 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7678 if (value == 0xffffffffu) {
7679 if (!out.Is(first)) {
7680 __ Mov(out, first);
7681 }
7682 return;
7683 }
7684 if (value == 0u) {
7685 __ Mov(out, 0);
7686 return;
7687 }
7688 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007689 __ And(out, first, value);
7690 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
7691 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01007692 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007693 DCHECK(IsPowerOfTwo(value + 1));
7694 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01007695 }
7696}
7697
7698// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7699void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7700 vixl32::Register first,
7701 uint32_t value) {
7702 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7703 if (value == 0u) {
7704 if (!out.Is(first)) {
7705 __ Mov(out, first);
7706 }
7707 return;
7708 }
7709 if (value == 0xffffffffu) {
7710 __ Mvn(out, 0);
7711 return;
7712 }
7713 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7714 __ Orr(out, first, value);
7715 } else {
7716 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7717 __ Orn(out, first, ~value);
7718 }
7719}
7720
7721// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7722void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7723 vixl32::Register first,
7724 uint32_t value) {
7725 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7726 if (value == 0u) {
7727 if (!out.Is(first)) {
7728 __ Mov(out, first);
7729 }
7730 return;
7731 }
7732 __ Eor(out, first, value);
7733}
7734
Anton Kirilovdda43962016-11-21 19:55:20 +00007735void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7736 Location first,
7737 uint64_t value) {
7738 vixl32::Register out_low = LowRegisterFrom(out);
7739 vixl32::Register out_high = HighRegisterFrom(out);
7740 vixl32::Register first_low = LowRegisterFrom(first);
7741 vixl32::Register first_high = HighRegisterFrom(first);
7742 uint32_t value_low = Low32Bits(value);
7743 uint32_t value_high = High32Bits(value);
7744 if (value_low == 0u) {
7745 if (!out_low.Is(first_low)) {
7746 __ Mov(out_low, first_low);
7747 }
7748 __ Add(out_high, first_high, value_high);
7749 return;
7750 }
7751 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007752 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007753 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007754 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007755 __ Sbc(out_high, first_high, ~value_high);
7756 } else {
7757 LOG(FATAL) << "Unexpected constant " << value_high;
7758 UNREACHABLE();
7759 }
7760}
7761
Artem Serov02109dd2016-09-23 17:17:54 +01007762void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7763 LocationSummary* locations = instruction->GetLocations();
7764 Location first = locations->InAt(0);
7765 Location second = locations->InAt(1);
7766 Location out = locations->Out();
7767
7768 if (second.IsConstant()) {
7769 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7770 uint32_t value_low = Low32Bits(value);
7771 if (instruction->GetResultType() == Primitive::kPrimInt) {
7772 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7773 vixl32::Register out_reg = OutputRegister(instruction);
7774 if (instruction->IsAnd()) {
7775 GenerateAndConst(out_reg, first_reg, value_low);
7776 } else if (instruction->IsOr()) {
7777 GenerateOrrConst(out_reg, first_reg, value_low);
7778 } else {
7779 DCHECK(instruction->IsXor());
7780 GenerateEorConst(out_reg, first_reg, value_low);
7781 }
7782 } else {
7783 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7784 uint32_t value_high = High32Bits(value);
7785 vixl32::Register first_low = LowRegisterFrom(first);
7786 vixl32::Register first_high = HighRegisterFrom(first);
7787 vixl32::Register out_low = LowRegisterFrom(out);
7788 vixl32::Register out_high = HighRegisterFrom(out);
7789 if (instruction->IsAnd()) {
7790 GenerateAndConst(out_low, first_low, value_low);
7791 GenerateAndConst(out_high, first_high, value_high);
7792 } else if (instruction->IsOr()) {
7793 GenerateOrrConst(out_low, first_low, value_low);
7794 GenerateOrrConst(out_high, first_high, value_high);
7795 } else {
7796 DCHECK(instruction->IsXor());
7797 GenerateEorConst(out_low, first_low, value_low);
7798 GenerateEorConst(out_high, first_high, value_high);
7799 }
7800 }
7801 return;
7802 }
7803
7804 if (instruction->GetResultType() == Primitive::kPrimInt) {
7805 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7806 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7807 vixl32::Register out_reg = OutputRegister(instruction);
7808 if (instruction->IsAnd()) {
7809 __ And(out_reg, first_reg, second_reg);
7810 } else if (instruction->IsOr()) {
7811 __ Orr(out_reg, first_reg, second_reg);
7812 } else {
7813 DCHECK(instruction->IsXor());
7814 __ Eor(out_reg, first_reg, second_reg);
7815 }
7816 } else {
7817 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7818 vixl32::Register first_low = LowRegisterFrom(first);
7819 vixl32::Register first_high = HighRegisterFrom(first);
7820 vixl32::Register second_low = LowRegisterFrom(second);
7821 vixl32::Register second_high = HighRegisterFrom(second);
7822 vixl32::Register out_low = LowRegisterFrom(out);
7823 vixl32::Register out_high = HighRegisterFrom(out);
7824 if (instruction->IsAnd()) {
7825 __ And(out_low, first_low, second_low);
7826 __ And(out_high, first_high, second_high);
7827 } else if (instruction->IsOr()) {
7828 __ Orr(out_low, first_low, second_low);
7829 __ Orr(out_high, first_high, second_high);
7830 } else {
7831 DCHECK(instruction->IsXor());
7832 __ Eor(out_low, first_low, second_low);
7833 __ Eor(out_high, first_high, second_high);
7834 }
7835 }
7836}
7837
Artem Serovcfbe9132016-10-14 15:58:56 +01007838void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007839 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007840 Location out,
7841 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007842 Location maybe_temp,
7843 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007844 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007845 if (read_barrier_option == kWithReadBarrier) {
7846 CHECK(kEmitCompilerReadBarrier);
7847 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7848 if (kUseBakerReadBarrier) {
7849 // Load with fast path based Baker's read barrier.
7850 // /* HeapReference<Object> */ out = *(out + offset)
7851 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7852 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7853 } else {
7854 // Load with slow path based read barrier.
7855 // Save the value of `out` into `maybe_temp` before overwriting it
7856 // in the following move operation, as we will need it for the
7857 // read barrier below.
7858 __ Mov(RegisterFrom(maybe_temp), out_reg);
7859 // /* HeapReference<Object> */ out = *(out + offset)
7860 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7861 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7862 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007863 } else {
7864 // Plain load with no read barrier.
7865 // /* HeapReference<Object> */ out = *(out + offset)
7866 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7867 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7868 }
7869}
7870
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007871void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007872 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007873 Location out,
7874 Location obj,
7875 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007876 Location maybe_temp,
7877 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007878 vixl32::Register out_reg = RegisterFrom(out);
7879 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007880 if (read_barrier_option == kWithReadBarrier) {
7881 CHECK(kEmitCompilerReadBarrier);
7882 if (kUseBakerReadBarrier) {
7883 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7884 // Load with fast path based Baker's read barrier.
7885 // /* HeapReference<Object> */ out = *(obj + offset)
7886 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7887 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7888 } else {
7889 // Load with slow path based read barrier.
7890 // /* HeapReference<Object> */ out = *(obj + offset)
7891 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7892 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7893 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007894 } else {
7895 // Plain load with no read barrier.
7896 // /* HeapReference<Object> */ out = *(obj + offset)
7897 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7898 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7899 }
7900}
7901
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007902void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007903 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007904 Location root,
7905 vixl32::Register obj,
7906 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007907 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007908 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007909 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007910 DCHECK(kEmitCompilerReadBarrier);
7911 if (kUseBakerReadBarrier) {
7912 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00007913 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007914 //
Roland Levillainba650a42017-03-06 13:52:32 +00007915 // Note that we do not actually check the value of
7916 // `GetIsGcMarking()` to decide whether to mark the loaded GC
7917 // root or not. Instead, we load into `temp` the read barrier
7918 // mark entry point corresponding to register `root`. If `temp`
7919 // is null, it means that `GetIsGcMarking()` is false, and vice
7920 // versa.
7921 //
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007922 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00007923 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7924 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7925 // // Slow path.
7926 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007927 // }
7928
Roland Levillainba650a42017-03-06 13:52:32 +00007929 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
7930 Location temp = LocationFrom(lr);
7931 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
7932 instruction, root, /* entrypoint */ temp);
7933 codegen_->AddSlowPath(slow_path);
7934
7935 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7936 const int32_t entry_point_offset =
7937 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7938 // Loading the entrypoint does not require a load acquire since it is only changed when
7939 // threads are suspended or running a checkpoint.
7940 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
7941
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007942 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7943 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7944 static_assert(
7945 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7946 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7947 "have different sizes.");
7948 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7949 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7950 "have different sizes.");
7951
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007952 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7953 // checking GetIsGcMarking.
7954 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7955 __ Bind(slow_path->GetExitLabel());
7956 } else {
7957 // GC root loaded through a slow path for read barriers other
7958 // than Baker's.
7959 // /* GcRoot<mirror::Object>* */ root = obj + offset
7960 __ Add(root_reg, obj, offset);
7961 // /* mirror::Object* */ root = root->Read()
7962 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7963 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007964 } else {
7965 // Plain GC root load with no read barrier.
7966 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7967 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7968 // Note that GC roots are not affected by heap poisoning, thus we
7969 // do not have to unpoison `root_reg` here.
7970 }
7971}
7972
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007973void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7974 Location ref,
7975 vixl32::Register obj,
7976 uint32_t offset,
7977 Location temp,
7978 bool needs_null_check) {
7979 DCHECK(kEmitCompilerReadBarrier);
7980 DCHECK(kUseBakerReadBarrier);
7981
7982 // /* HeapReference<Object> */ ref = *(obj + offset)
7983 Location no_index = Location::NoLocation();
7984 ScaleFactor no_scale_factor = TIMES_1;
7985 GenerateReferenceLoadWithBakerReadBarrier(
7986 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007987}
7988
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007989void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7990 Location ref,
7991 vixl32::Register obj,
7992 uint32_t data_offset,
7993 Location index,
7994 Location temp,
7995 bool needs_null_check) {
7996 DCHECK(kEmitCompilerReadBarrier);
7997 DCHECK(kUseBakerReadBarrier);
7998
7999 static_assert(
8000 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8001 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
8002 // /* HeapReference<Object> */ ref =
8003 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8004 ScaleFactor scale_factor = TIMES_4;
8005 GenerateReferenceLoadWithBakerReadBarrier(
8006 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008007}
8008
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008009void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8010 Location ref,
8011 vixl32::Register obj,
8012 uint32_t offset,
8013 Location index,
8014 ScaleFactor scale_factor,
8015 Location temp,
8016 bool needs_null_check,
8017 bool always_update_field,
8018 vixl32::Register* temp2) {
8019 DCHECK(kEmitCompilerReadBarrier);
8020 DCHECK(kUseBakerReadBarrier);
8021
Roland Levillain54f869e2017-03-06 13:54:11 +00008022 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8023 // whether we need to enter the slow path to mark the reference.
8024 // Then, in the slow path, check the gray bit in the lock word of
8025 // the reference's holder (`obj`) to decide whether to mark `ref` or
8026 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008027 //
Roland Levillainba650a42017-03-06 13:52:32 +00008028 // Note that we do not actually check the value of `GetIsGcMarking()`;
8029 // instead, we load into `temp3` the read barrier mark entry point
8030 // corresponding to register `ref`. If `temp3` is null, it means
8031 // that `GetIsGcMarking()` is false, and vice versa.
8032 //
8033 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008034 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8035 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008036 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8037 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8038 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8039 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8040 // if (is_gray) {
8041 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8042 // }
8043 // } else {
8044 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008045 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008046
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008047 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008048
Roland Levillainba650a42017-03-06 13:52:32 +00008049 // Slow path marking the object `ref` when the GC is marking. The
8050 // entrypoint will already be loaded in `temp3`.
8051 Location temp3 = LocationFrom(lr);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008052 SlowPathCodeARMVIXL* slow_path;
8053 if (always_update_field) {
8054 DCHECK(temp2 != nullptr);
Roland Levillain54f869e2017-03-06 13:54:11 +00008055 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
8056 // only supports address of the form `obj + field_offset`, where
8057 // `obj` is a register and `field_offset` is a register pair (of
8058 // which only the lower half is used). Thus `offset` and
8059 // `scale_factor` above are expected to be null in this code path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008060 DCHECK_EQ(offset, 0u);
8061 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain54f869e2017-03-06 13:54:11 +00008062 Location field_offset = index;
8063 slow_path =
8064 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8065 instruction,
8066 ref,
8067 obj,
8068 offset,
8069 /* index */ field_offset,
8070 scale_factor,
8071 needs_null_check,
8072 temp_reg,
8073 *temp2,
8074 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008075 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00008076 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8077 instruction,
8078 ref,
8079 obj,
8080 offset,
8081 index,
8082 scale_factor,
8083 needs_null_check,
8084 temp_reg,
8085 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008086 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008087 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008088
Roland Levillainba650a42017-03-06 13:52:32 +00008089 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8090 const int32_t entry_point_offset =
8091 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8092 // Loading the entrypoint does not require a load acquire since it is only changed when
8093 // threads are suspended or running a checkpoint.
8094 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008095 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8096 // checking GetIsGcMarking.
8097 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00008098 // Fast path: just load the reference.
8099 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008100 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008101}
Scott Wakelingfe885462016-09-22 10:24:38 +01008102
Roland Levillainba650a42017-03-06 13:52:32 +00008103void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8104 Location ref,
8105 vixl::aarch32::Register obj,
8106 uint32_t offset,
8107 Location index,
8108 ScaleFactor scale_factor,
8109 bool needs_null_check) {
8110 Primitive::Type type = Primitive::kPrimNot;
8111 vixl32::Register ref_reg = RegisterFrom(ref, type);
8112
8113 // If needed, vixl::EmissionCheckScope guards are used to ensure
8114 // that no pools are emitted between the load (macro) instruction
8115 // and MaybeRecordImplicitNullCheck.
8116
Scott Wakelingfe885462016-09-22 10:24:38 +01008117 if (index.IsValid()) {
8118 // Load types involving an "index": ArrayGet,
8119 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8120 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008121 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008122 if (index.IsConstant()) {
8123 size_t computed_offset =
8124 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008125 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008126 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008127 if (needs_null_check) {
8128 MaybeRecordImplicitNullCheck(instruction);
8129 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008130 } else {
8131 // Handle the special case of the
8132 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8133 // intrinsics, which use a register pair as index ("long
8134 // offset"), of which only the low part contains data.
8135 vixl32::Register index_reg = index.IsRegisterPair()
8136 ? LowRegisterFrom(index)
8137 : RegisterFrom(index);
8138 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008139 vixl32::Register temp = temps.Acquire();
8140 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8141 {
8142 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8143 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8144 if (needs_null_check) {
8145 MaybeRecordImplicitNullCheck(instruction);
8146 }
8147 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008148 }
8149 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008150 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8151 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008152 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008153 if (needs_null_check) {
8154 MaybeRecordImplicitNullCheck(instruction);
8155 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008156 }
8157
Roland Levillain844e6532016-11-03 16:09:47 +00008158 // Object* ref = ref_addr->AsMirrorPtr()
8159 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008160}
8161
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008162void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8163 Location out,
8164 Location ref,
8165 Location obj,
8166 uint32_t offset,
8167 Location index) {
8168 DCHECK(kEmitCompilerReadBarrier);
8169
8170 // Insert a slow path based read barrier *after* the reference load.
8171 //
8172 // If heap poisoning is enabled, the unpoisoning of the loaded
8173 // reference will be carried out by the runtime within the slow
8174 // path.
8175 //
8176 // Note that `ref` currently does not get unpoisoned (when heap
8177 // poisoning is enabled), which is alright as the `ref` argument is
8178 // not used by the artReadBarrierSlow entry point.
8179 //
8180 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8181 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8182 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8183 AddSlowPath(slow_path);
8184
8185 __ B(slow_path->GetEntryLabel());
8186 __ Bind(slow_path->GetExitLabel());
8187}
8188
8189void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008190 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008191 Location ref,
8192 Location obj,
8193 uint32_t offset,
8194 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008195 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008196 // Baker's read barriers shall be handled by the fast path
8197 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008198 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008199 // If heap poisoning is enabled, unpoisoning will be taken care of
8200 // by the runtime within the slow path.
8201 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008202 } else if (kPoisonHeapReferences) {
8203 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8204 }
8205}
8206
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008207void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8208 Location out,
8209 Location root) {
8210 DCHECK(kEmitCompilerReadBarrier);
8211
8212 // Insert a slow path based read barrier *after* the GC root load.
8213 //
8214 // Note that GC roots are not affected by heap poisoning, so we do
8215 // not need to do anything special for this here.
8216 SlowPathCodeARMVIXL* slow_path =
8217 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8218 AddSlowPath(slow_path);
8219
8220 __ B(slow_path->GetEntryLabel());
8221 __ Bind(slow_path->GetExitLabel());
8222}
8223
Artem Serov02d37832016-10-25 15:25:33 +01008224// Check if the desired_dispatch_info is supported. If it is, return it,
8225// otherwise return a fall-back info that should be used instead.
8226HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008227 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008228 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008229 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008230}
8231
Scott Wakelingfe885462016-09-22 10:24:38 +01008232vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8233 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8234 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8235 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8236 if (!invoke->GetLocations()->Intrinsified()) {
8237 return RegisterFrom(location);
8238 }
8239 // For intrinsics we allow any location, so it may be on the stack.
8240 if (!location.IsRegister()) {
8241 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8242 return temp;
8243 }
8244 // For register locations, check if the register was saved. If so, get it from the stack.
8245 // Note: There is a chance that the register was saved but not overwritten, so we could
8246 // save one load. However, since this is just an intrinsic slow path we prefer this
8247 // simple and more robust approach rather that trying to determine if that's the case.
8248 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008249 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008250 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8251 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8252 return temp;
8253 }
8254 return RegisterFrom(location);
8255}
8256
TatWai Chongd8c052a2016-11-02 16:12:48 +08008257Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01008258 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008259 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008260 switch (invoke->GetMethodLoadKind()) {
8261 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8262 uint32_t offset =
8263 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8264 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008265 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8266 break;
8267 }
8268 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8269 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8270 break;
8271 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8272 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8273 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008274 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8275 HArmDexCacheArraysBase* base =
8276 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8277 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
8278 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8279 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008280 break;
8281 }
8282 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
8283 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8284 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008285 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008286 if (current_method.IsRegister()) {
8287 method_reg = RegisterFrom(current_method);
8288 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008289 DCHECK(invoke->GetLocations()->Intrinsified());
8290 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008291 method_reg = reg;
8292 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008293 }
8294 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8295 GetAssembler()->LoadFromOffset(
8296 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008297 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01008298 method_reg,
8299 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
8300 // temp = temp[index_in_cache];
8301 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8302 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8303 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008304 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01008305 break;
8306 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008307 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008308 return callee_method;
8309}
8310
8311void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8312 Location temp) {
8313 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008314
Artem Serovd4cc5b22016-11-04 11:19:09 +00008315 switch (invoke->GetCodePtrLocation()) {
8316 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8317 __ Bl(GetFrameEntryLabel());
8318 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008319 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8320 // LR = callee_method->entry_point_from_quick_compiled_code_
8321 GetAssembler()->LoadFromOffset(
8322 kLoadWord,
8323 lr,
8324 RegisterFrom(callee_method),
8325 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008326 {
8327 // 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 +00008328 ExactAssemblyScope aas(GetVIXLAssembler(),
8329 vixl32::k16BitT32InstructionSizeInBytes,
8330 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008331 // LR()
8332 __ blx(lr);
8333 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008334 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008335 }
8336
Scott Wakelingfe885462016-09-22 10:24:38 +01008337 DCHECK(!IsLeafMethod());
8338}
8339
8340void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8341 vixl32::Register temp = RegisterFrom(temp_location);
8342 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8343 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8344
8345 // Use the calling convention instead of the location of the receiver, as
8346 // intrinsics may have put the receiver in a different register. In the intrinsics
8347 // slow path, the arguments have been moved to the right place, so here we are
8348 // guaranteed that the receiver is the first register of the calling convention.
8349 InvokeDexCallingConventionARMVIXL calling_convention;
8350 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8351 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008352 {
8353 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008354 ExactAssemblyScope aas(GetVIXLAssembler(),
8355 vixl32::kMaxInstructionSizeInBytes,
8356 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008357 // /* HeapReference<Class> */ temp = receiver->klass_
8358 __ ldr(temp, MemOperand(receiver, class_offset));
8359 MaybeRecordImplicitNullCheck(invoke);
8360 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008361 // Instead of simply (possibly) unpoisoning `temp` here, we should
8362 // emit a read barrier for the previous class reference load.
8363 // However this is not required in practice, as this is an
8364 // intermediate/temporary reference and because the current
8365 // concurrent copying collector keeps the from-space memory
8366 // intact/accessible until the end of the marking phase (the
8367 // concurrent copying collector may not in the future).
8368 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8369
8370 // temp = temp->GetMethodAt(method_offset);
8371 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8372 kArmPointerSize).Int32Value();
8373 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8374 // LR = temp->GetEntryPoint();
8375 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8376 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008377 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8378 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8379 // that.
8380 // 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 +00008381 ExactAssemblyScope aas(GetVIXLAssembler(),
8382 vixl32::k16BitT32InstructionSizeInBytes,
8383 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008384 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01008385}
8386
Artem Serovd4cc5b22016-11-04 11:19:09 +00008387CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008388 const DexFile& dex_file, dex::StringIndex string_index) {
8389 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008390}
8391
8392CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8393 const DexFile& dex_file, dex::TypeIndex type_index) {
8394 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8395}
8396
Vladimir Marko1998cd02017-01-13 13:02:58 +00008397CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8398 const DexFile& dex_file, dex::TypeIndex type_index) {
8399 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8400}
8401
Artem Serovd4cc5b22016-11-04 11:19:09 +00008402CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8403 const DexFile& dex_file, uint32_t element_offset) {
8404 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8405}
8406
8407CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8408 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8409 patches->emplace_back(dex_file, offset_or_index);
8410 return &patches->back();
8411}
8412
Artem Serovc5fcb442016-12-02 19:19:58 +00008413VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
8414 const DexFile& dex_file,
8415 dex::StringIndex string_index) {
8416 return boot_image_string_patches_.GetOrCreate(
8417 StringReference(&dex_file, string_index),
8418 [this]() {
8419 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8420 });
8421}
8422
8423VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
8424 const DexFile& dex_file,
8425 dex::TypeIndex type_index) {
8426 return boot_image_type_patches_.GetOrCreate(
8427 TypeReference(&dex_file, type_index),
8428 [this]() {
8429 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8430 });
8431}
8432
8433VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008434 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008435}
8436
8437VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
8438 return DeduplicateUint32Literal(address, &uint32_literals_);
8439}
8440
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008441VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8442 const DexFile& dex_file,
8443 dex::StringIndex string_index,
8444 Handle<mirror::String> handle) {
8445 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8446 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008447 return jit_string_patches_.GetOrCreate(
8448 StringReference(&dex_file, string_index),
8449 [this]() {
8450 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8451 });
8452}
8453
8454VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8455 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008456 Handle<mirror::Class> handle) {
8457 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8458 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008459 return jit_class_patches_.GetOrCreate(
8460 TypeReference(&dex_file, type_index),
8461 [this]() {
8462 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8463 });
8464}
8465
Artem Serovd4cc5b22016-11-04 11:19:09 +00008466template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8467inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8468 const ArenaDeque<PcRelativePatchInfo>& infos,
8469 ArenaVector<LinkerPatch>* linker_patches) {
8470 for (const PcRelativePatchInfo& info : infos) {
8471 const DexFile& dex_file = info.target_dex_file;
8472 size_t offset_or_index = info.offset_or_index;
8473 DCHECK(info.add_pc_label.IsBound());
8474 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8475 // Add MOVW patch.
8476 DCHECK(info.movw_label.IsBound());
8477 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8478 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8479 // Add MOVT patch.
8480 DCHECK(info.movt_label.IsBound());
8481 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8482 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8483 }
8484}
8485
8486void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8487 DCHECK(linker_patches->empty());
8488 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008489 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008490 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00008491 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008492 boot_image_type_patches_.size() +
8493 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00008494 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008495 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008496 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8497 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008498 for (const auto& entry : boot_image_string_patches_) {
8499 const StringReference& target_string = entry.first;
8500 VIXLUInt32Literal* literal = entry.second;
8501 DCHECK(literal->IsBound());
8502 uint32_t literal_offset = literal->GetLocation();
8503 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8504 target_string.dex_file,
8505 target_string.string_index.index_));
8506 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008507 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008508 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008509 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8510 linker_patches);
8511 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008512 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8513 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008514 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8515 linker_patches);
8516 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008517 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8518 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008519 for (const auto& entry : boot_image_type_patches_) {
8520 const TypeReference& target_type = entry.first;
8521 VIXLUInt32Literal* literal = entry.second;
8522 DCHECK(literal->IsBound());
8523 uint32_t literal_offset = literal->GetLocation();
8524 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8525 target_type.dex_file,
8526 target_type.type_index.index_));
8527 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008528 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00008529}
8530
8531VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
8532 uint32_t value,
8533 Uint32ToLiteralMap* map) {
8534 return map->GetOrCreate(
8535 value,
8536 [this, value]() {
8537 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
8538 });
8539}
8540
8541VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
8542 MethodReference target_method,
8543 MethodToLiteralMap* map) {
8544 return map->GetOrCreate(
8545 target_method,
8546 [this]() {
8547 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8548 });
8549}
8550
Artem Serov2bbc9532016-10-21 11:51:50 +01008551void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8552 LocationSummary* locations =
8553 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8554 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8555 Location::RequiresRegister());
8556 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8557 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8559}
8560
8561void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8562 vixl32::Register res = OutputRegister(instr);
8563 vixl32::Register accumulator =
8564 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
8565 vixl32::Register mul_left =
8566 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
8567 vixl32::Register mul_right =
8568 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
8569
8570 if (instr->GetOpKind() == HInstruction::kAdd) {
8571 __ Mla(res, mul_left, mul_right, accumulator);
8572 } else {
8573 __ Mls(res, mul_left, mul_right, accumulator);
8574 }
8575}
8576
Artem Serov551b28f2016-10-18 19:11:30 +01008577void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8578 // Nothing to do, this should be removed during prepare for register allocator.
8579 LOG(FATAL) << "Unreachable";
8580}
8581
8582void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8583 // Nothing to do, this should be removed during prepare for register allocator.
8584 LOG(FATAL) << "Unreachable";
8585}
8586
8587// Simple implementation of packed switch - generate cascaded compare/jumps.
8588void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8589 LocationSummary* locations =
8590 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8591 locations->SetInAt(0, Location::RequiresRegister());
8592 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
8593 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8594 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8595 if (switch_instr->GetStartValue() != 0) {
8596 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8597 }
8598 }
8599}
8600
8601// TODO(VIXL): Investigate and reach the parity with old arm codegen.
8602void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8603 int32_t lower_bound = switch_instr->GetStartValue();
8604 uint32_t num_entries = switch_instr->GetNumEntries();
8605 LocationSummary* locations = switch_instr->GetLocations();
8606 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
8607 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8608
8609 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
8610 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8611 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008612 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008613 vixl32::Register temp_reg = temps.Acquire();
8614 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8615 // the immediate, because IP is used as the destination register. For the other
8616 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8617 // and they can be encoded in the instruction without making use of IP register.
8618 __ Adds(temp_reg, value_reg, -lower_bound);
8619
8620 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8621 // Jump to successors[0] if value == lower_bound.
8622 __ B(eq, codegen_->GetLabelOf(successors[0]));
8623 int32_t last_index = 0;
8624 for (; num_entries - last_index > 2; last_index += 2) {
8625 __ Adds(temp_reg, temp_reg, -2);
8626 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8627 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
8628 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8629 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
8630 }
8631 if (num_entries - last_index == 2) {
8632 // The last missing case_value.
8633 __ Cmp(temp_reg, 1);
8634 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
8635 }
8636
8637 // And the default for any other value.
8638 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8639 __ B(codegen_->GetLabelOf(default_block));
8640 }
8641 } else {
8642 // Create a table lookup.
8643 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
8644
8645 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
8646
8647 // Remove the bias.
8648 vixl32::Register key_reg;
8649 if (lower_bound != 0) {
8650 key_reg = RegisterFrom(locations->GetTemp(1));
8651 __ Sub(key_reg, value_reg, lower_bound);
8652 } else {
8653 key_reg = value_reg;
8654 }
8655
8656 // Check whether the value is in the table, jump to default block if not.
8657 __ Cmp(key_reg, num_entries - 1);
8658 __ B(hi, codegen_->GetLabelOf(default_block));
8659
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008660 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008661 vixl32::Register jump_offset = temps.Acquire();
8662
8663 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00008664 {
8665 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
8666 ExactAssemblyScope aas(GetVIXLAssembler(),
8667 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
8668 CodeBufferCheckScope::kMaximumSize);
8669 __ adr(table_base, jump_table->GetTableStartLabel());
8670 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01008671
Scott Wakeling86e9d262017-01-18 15:59:24 +00008672 // Jump to target block by branching to table_base(pc related) + offset.
8673 vixl32::Register target_address = table_base;
8674 __ add(target_address, table_base, jump_offset);
8675 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00008676
Scott Wakeling86e9d262017-01-18 15:59:24 +00008677 jump_table->EmitTable(codegen_);
8678 }
Artem Serov551b28f2016-10-18 19:11:30 +01008679 }
8680}
Artem Serovd4cc5b22016-11-04 11:19:09 +00008681void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8683 locations->SetOut(Location::RequiresRegister());
8684}
8685
8686void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8687 vixl32::Register base_reg = OutputRegister(base);
8688 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8689 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
8690 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
8691}
Artem Serov551b28f2016-10-18 19:11:30 +01008692
Artem Serov02d37832016-10-25 15:25:33 +01008693// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008694void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8695 if (!trg.IsValid()) {
8696 DCHECK_EQ(type, Primitive::kPrimVoid);
8697 return;
8698 }
8699
8700 DCHECK_NE(type, Primitive::kPrimVoid);
8701
Artem Serovd4cc5b22016-11-04 11:19:09 +00008702 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008703 if (return_loc.Equals(trg)) {
8704 return;
8705 }
8706
8707 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8708 // with the last branch.
8709 if (type == Primitive::kPrimLong) {
8710 TODO_VIXL32(FATAL);
8711 } else if (type == Primitive::kPrimDouble) {
8712 TODO_VIXL32(FATAL);
8713 } else {
8714 // Let the parallel move resolver take care of all of this.
8715 HParallelMove parallel_move(GetGraph()->GetArena());
8716 parallel_move.AddMove(return_loc, trg, type, nullptr);
8717 GetMoveResolver()->EmitNativeCode(&parallel_move);
8718 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008719}
8720
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008721void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8722 LocationSummary* locations =
8723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8724 locations->SetInAt(0, Location::RequiresRegister());
8725 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008726}
8727
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008728void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8729 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8730 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8731 instruction->GetIndex(), kArmPointerSize).SizeValue();
8732 GetAssembler()->LoadFromOffset(kLoadWord,
8733 OutputRegister(instruction),
8734 InputRegisterAt(instruction, 0),
8735 method_offset);
8736 } else {
8737 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8738 instruction->GetIndex(), kArmPointerSize));
8739 GetAssembler()->LoadFromOffset(kLoadWord,
8740 OutputRegister(instruction),
8741 InputRegisterAt(instruction, 0),
8742 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8743 GetAssembler()->LoadFromOffset(kLoadWord,
8744 OutputRegister(instruction),
8745 OutputRegister(instruction),
8746 method_offset);
8747 }
Artem Serov551b28f2016-10-18 19:11:30 +01008748}
8749
Artem Serovc5fcb442016-12-02 19:19:58 +00008750static void PatchJitRootUse(uint8_t* code,
8751 const uint8_t* roots_data,
8752 VIXLUInt32Literal* literal,
8753 uint64_t index_in_table) {
8754 DCHECK(literal->IsBound());
8755 uint32_t literal_offset = literal->GetLocation();
8756 uintptr_t address =
8757 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8758 uint8_t* data = code + literal_offset;
8759 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8760}
8761
8762void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8763 for (const auto& entry : jit_string_patches_) {
8764 const auto& it = jit_string_roots_.find(entry.first);
8765 DCHECK(it != jit_string_roots_.end());
8766 PatchJitRootUse(code, roots_data, entry.second, it->second);
8767 }
8768 for (const auto& entry : jit_class_patches_) {
8769 const auto& it = jit_class_roots_.find(entry.first);
8770 DCHECK(it != jit_class_roots_.end());
8771 PatchJitRootUse(code, roots_data, entry.second, it->second);
8772 }
8773}
8774
Artem Serovd4cc5b22016-11-04 11:19:09 +00008775void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8776 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8777 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008778 ExactAssemblyScope aas(GetVIXLAssembler(),
8779 3 * vixl32::kMaxInstructionSizeInBytes,
8780 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008781 // TODO(VIXL): Think about using mov instead of movw.
8782 __ bind(&labels->movw_label);
8783 __ movw(out, /* placeholder */ 0u);
8784 __ bind(&labels->movt_label);
8785 __ movt(out, /* placeholder */ 0u);
8786 __ bind(&labels->add_pc_label);
8787 __ add(out, out, pc);
8788}
8789
Scott Wakelingfe885462016-09-22 10:24:38 +01008790#undef __
8791#undef QUICK_ENTRY_POINT
8792#undef TODO_VIXL32
8793
8794} // namespace arm
8795} // namespace art