blob: b6678b03ef104deeb5e57c12ac163158a8cc472c [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() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001178 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001179 << "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
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001690static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1691 HCondition* condition,
1692 bool invert,
1693 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001694 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1695
1696 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001697 IfCondition cond = condition->GetCondition();
1698 IfCondition opposite = condition->GetOppositeCondition();
1699
1700 if (invert) {
1701 std::swap(cond, opposite);
1702 }
1703
1704 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001705 const Location left = locations->InAt(0);
1706 const Location right = locations->InAt(1);
1707
1708 DCHECK(right.IsConstant());
1709
1710 const vixl32::Register left_high = HighRegisterFrom(left);
1711 const vixl32::Register left_low = LowRegisterFrom(left);
1712 int64_t value = Int64ConstantFrom(right);
1713
1714 switch (cond) {
1715 case kCondEQ:
1716 case kCondNE:
1717 case kCondB:
1718 case kCondBE:
1719 case kCondA:
1720 case kCondAE: {
1721 __ Cmp(left_high, High32Bits(value));
1722
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001723 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001724 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1725 2 * vixl32::k16BitT32InstructionSizeInBytes,
1726 CodeBufferCheckScope::kExactSize);
1727
1728 __ it(eq);
1729 __ cmp(eq, left_low, Low32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001730 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001731 break;
1732 }
1733 case kCondLE:
1734 case kCondGT:
1735 // Trivially true or false.
1736 if (value == std::numeric_limits<int64_t>::max()) {
1737 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001738 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001739 break;
1740 }
1741
1742 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001743 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001744 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001745 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001746 } else {
1747 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001748 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001749 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001750 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001751 }
1752
1753 value++;
1754 FALLTHROUGH_INTENDED;
1755 case kCondGE:
1756 case kCondLT: {
1757 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1758
1759 __ Cmp(left_low, Low32Bits(value));
1760 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001761 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001762 break;
1763 }
1764 default:
1765 LOG(FATAL) << "Unreachable";
1766 UNREACHABLE();
1767 }
1768
1769 return ret;
1770}
1771
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001772static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1773 HCondition* condition,
1774 bool invert,
1775 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001776 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1777
1778 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001779 IfCondition cond = condition->GetCondition();
1780 IfCondition opposite = condition->GetOppositeCondition();
1781
1782 if (invert) {
1783 std::swap(cond, opposite);
1784 }
1785
1786 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001787 Location left = locations->InAt(0);
1788 Location right = locations->InAt(1);
1789
1790 DCHECK(right.IsRegisterPair());
1791
1792 switch (cond) {
1793 case kCondEQ:
1794 case kCondNE:
1795 case kCondB:
1796 case kCondBE:
1797 case kCondA:
1798 case kCondAE: {
1799 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1800
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001801 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001802 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1803 2 * vixl32::k16BitT32InstructionSizeInBytes,
1804 CodeBufferCheckScope::kExactSize);
1805
1806 __ it(eq);
1807 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001808 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001809 break;
1810 }
1811 case kCondLE:
1812 case kCondGT:
1813 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001814 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001815 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001816 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001817 } else {
1818 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001819 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001820 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001821 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001822 }
1823
1824 std::swap(left, right);
1825 FALLTHROUGH_INTENDED;
1826 case kCondGE:
1827 case kCondLT: {
1828 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1829
1830 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1831 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001832 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001833 break;
1834 }
1835 default:
1836 LOG(FATAL) << "Unreachable";
1837 UNREACHABLE();
1838 }
1839
1840 return ret;
1841}
1842
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001843static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1844 bool invert,
1845 CodeGeneratorARMVIXL* codegen) {
1846 const Primitive::Type type = condition->GetLeft()->GetType();
1847 IfCondition cond = condition->GetCondition();
1848 IfCondition opposite = condition->GetOppositeCondition();
1849 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001850
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001851 if (invert) {
1852 std::swap(cond, opposite);
1853 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001854
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001855 if (type == Primitive::kPrimLong) {
1856 ret = condition->GetLocations()->InAt(1).IsConstant()
1857 ? GenerateLongTestConstant(condition, invert, codegen)
1858 : GenerateLongTest(condition, invert, codegen);
1859 } else if (Primitive::IsFloatingPointType(type)) {
1860 GenerateVcmp(condition, codegen);
1861 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1862 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1863 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001864 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001865 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1866 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1867 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001868 }
1869
1870 return ret;
1871}
1872
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001873static bool CanGenerateTest(HCondition* condition, ArmVIXLAssembler* assembler) {
1874 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1875 const LocationSummary* const locations = condition->GetLocations();
1876 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001877
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001878 if (locations->InAt(1).IsConstant()) {
1879 const int64_t value = Int64ConstantFrom(locations->InAt(1));
Donghui Bai426b49c2016-11-08 14:55:38 +08001880
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001881 if (c < kCondLT || c > kCondGE) {
1882 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1883 // we check that the least significant half of the first input to be compared
1884 // is in a low register (the other half is read outside an IT block), and
1885 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1886 // encoding can be used.
1887 if (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001888 return false;
1889 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001890 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
1891 // the previous one, but are not strictly necessary.
1892 } else if (c == kCondLE || c == kCondGT) {
1893 if (value < std::numeric_limits<int64_t>::max() &&
1894 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
1895 return false;
1896 }
1897 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
1898 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001899 }
1900 }
1901 }
1902
1903 return true;
1904}
1905
1906static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1907 const Primitive::Type type = constant->GetType();
1908 bool ret = false;
1909
1910 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1911
1912 if (type == Primitive::kPrimLong) {
1913 const uint64_t value = Uint64ConstantFrom(constant);
1914
1915 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1916 } else {
1917 ret = IsUint<8>(Int32ConstantFrom(constant));
1918 }
1919
1920 return ret;
1921}
1922
1923static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1924 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1925
1926 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1927 return Location::ConstantLocation(constant->AsConstant());
1928 }
1929
1930 return Location::RequiresRegister();
1931}
1932
1933static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1934 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1935 // we check that we are not dealing with floating-point output (there is no
1936 // 16-bit VMOV encoding).
1937 if (!out.IsRegister() && !out.IsRegisterPair()) {
1938 return false;
1939 }
1940
1941 // For constants, we also check that the output is in one or two low registers,
1942 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1943 // MOV encoding can be used.
1944 if (src.IsConstant()) {
1945 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1946 return false;
1947 }
1948
1949 if (out.IsRegister()) {
1950 if (!RegisterFrom(out).IsLow()) {
1951 return false;
1952 }
1953 } else {
1954 DCHECK(out.IsRegisterPair());
1955
1956 if (!HighRegisterFrom(out).IsLow()) {
1957 return false;
1958 }
1959 }
1960 }
1961
1962 return true;
1963}
1964
Scott Wakelingfe885462016-09-22 10:24:38 +01001965#undef __
1966
Donghui Bai426b49c2016-11-08 14:55:38 +08001967vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1968 vixl32::Label* final_label) {
1969 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001970 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001971
1972 const HBasicBlock* const block = instruction->GetBlock();
1973 const HLoopInformation* const info = block->GetLoopInformation();
1974 HInstruction* const next = instruction->GetNext();
1975
1976 // Avoid a branch to a branch.
1977 if (next->IsGoto() && (info == nullptr ||
1978 !info->IsBackEdge(*block) ||
1979 !info->HasSuspendCheck())) {
1980 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1981 }
1982
1983 return final_label;
1984}
1985
Scott Wakelingfe885462016-09-22 10:24:38 +01001986CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1987 const ArmInstructionSetFeatures& isa_features,
1988 const CompilerOptions& compiler_options,
1989 OptimizingCompilerStats* stats)
1990 : CodeGenerator(graph,
1991 kNumberOfCoreRegisters,
1992 kNumberOfSRegisters,
1993 kNumberOfRegisterPairs,
1994 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001995 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001996 compiler_options,
1997 stats),
1998 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001999 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002000 location_builder_(graph, this),
2001 instruction_visitor_(graph, this),
2002 move_resolver_(graph->GetArena(), this),
2003 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002004 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002005 uint32_literals_(std::less<uint32_t>(),
2006 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002007 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002008 boot_image_string_patches_(StringReferenceValueComparator(),
2009 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002010 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002011 boot_image_type_patches_(TypeReferenceValueComparator(),
2012 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2013 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002014 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002015 jit_string_patches_(StringReferenceValueComparator(),
2016 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2017 jit_class_patches_(TypeReferenceValueComparator(),
2018 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002019 // Always save the LR register to mimic Quick.
2020 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002021 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2022 // S0-S31, which alias to D0-D15.
2023 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2024 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002025}
2026
Artem Serov551b28f2016-10-18 19:11:30 +01002027void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2028 uint32_t num_entries = switch_instr_->GetNumEntries();
2029 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2030
2031 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002032 // underlying code buffer and we have generated a jump table of the right size, using
2033 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002034 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2035 num_entries * sizeof(int32_t),
2036 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002037 // TODO(VIXL): Check that using lower case bind is fine here.
2038 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002039 for (uint32_t i = 0; i < num_entries; i++) {
2040 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2041 }
2042}
2043
2044void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2045 uint32_t num_entries = switch_instr_->GetNumEntries();
2046 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2047
Artem Serov551b28f2016-10-18 19:11:30 +01002048 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2049 for (uint32_t i = 0; i < num_entries; i++) {
2050 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2051 DCHECK(target_label->IsBound());
2052 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2053 // When doing BX to address we need to have lower bit set to 1 in T32.
2054 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2055 jump_offset++;
2056 }
2057 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2058 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002059
Scott Wakelingb77051e2016-11-21 19:46:00 +00002060 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002061 }
2062}
2063
Artem Serov09a940d2016-11-11 16:15:11 +00002064void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002065 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002066 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002067 }
2068}
2069
Andreas Gampeca620d72016-11-08 08:09:33 -08002070#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002071
2072void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002073 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002074 GetAssembler()->FinalizeCode();
2075 CodeGenerator::Finalize(allocator);
2076}
2077
2078void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002079 // Stack register, LR and PC are always reserved.
2080 blocked_core_registers_[SP] = true;
2081 blocked_core_registers_[LR] = true;
2082 blocked_core_registers_[PC] = true;
2083
2084 // Reserve thread register.
2085 blocked_core_registers_[TR] = true;
2086
2087 // Reserve temp register.
2088 blocked_core_registers_[IP] = true;
2089
2090 if (GetGraph()->IsDebuggable()) {
2091 // Stubs do not save callee-save floating point registers. If the graph
2092 // is debuggable, we need to deal with these registers differently. For
2093 // now, just block them.
2094 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2095 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2096 ++i) {
2097 blocked_fpu_registers_[i] = true;
2098 }
2099 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002100}
2101
Scott Wakelingfe885462016-09-22 10:24:38 +01002102InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2103 CodeGeneratorARMVIXL* codegen)
2104 : InstructionCodeGenerator(graph, codegen),
2105 assembler_(codegen->GetAssembler()),
2106 codegen_(codegen) {}
2107
2108void CodeGeneratorARMVIXL::ComputeSpillMask() {
2109 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2110 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2111 // There is no easy instruction to restore just the PC on thumb2. We spill and
2112 // restore another arbitrary register.
2113 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2114 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2115 // We use vpush and vpop for saving and restoring floating point registers, which take
2116 // a SRegister and the number of registers to save/restore after that SRegister. We
2117 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2118 // but in the range.
2119 if (fpu_spill_mask_ != 0) {
2120 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2121 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2122 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2123 fpu_spill_mask_ |= (1 << i);
2124 }
2125 }
2126}
2127
2128void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2129 bool skip_overflow_check =
2130 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2131 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2132 __ Bind(&frame_entry_label_);
2133
2134 if (HasEmptyFrame()) {
2135 return;
2136 }
2137
Scott Wakelingfe885462016-09-22 10:24:38 +01002138 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002139 UseScratchRegisterScope temps(GetVIXLAssembler());
2140 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002141 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002142 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002143 ExactAssemblyScope aas(GetVIXLAssembler(),
2144 vixl32::kMaxInstructionSizeInBytes,
2145 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002146 __ ldr(temp, MemOperand(temp));
2147 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002148 }
2149
2150 __ Push(RegisterList(core_spill_mask_));
2151 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2152 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2153 0,
2154 core_spill_mask_,
2155 kArmWordSize);
2156 if (fpu_spill_mask_ != 0) {
2157 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2158
2159 // Check that list is contiguous.
2160 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2161
2162 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2163 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002164 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002165 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002166
2167 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2168 UseScratchRegisterScope temps(GetVIXLAssembler());
2169 vixl32::Register temp = temps.Acquire();
2170 // Initialize should_deoptimize flag to 0.
2171 __ Mov(temp, 0);
2172 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2173 }
2174
Scott Wakelingfe885462016-09-22 10:24:38 +01002175 int adjust = GetFrameSize() - FrameEntrySpillSize();
2176 __ Sub(sp, sp, adjust);
2177 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002178
2179 // Save the current method if we need it. Note that we do not
2180 // do this in HCurrentMethod, as the instruction might have been removed
2181 // in the SSA graph.
2182 if (RequiresCurrentMethod()) {
2183 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2184 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002185}
2186
2187void CodeGeneratorARMVIXL::GenerateFrameExit() {
2188 if (HasEmptyFrame()) {
2189 __ Bx(lr);
2190 return;
2191 }
2192 GetAssembler()->cfi().RememberState();
2193 int adjust = GetFrameSize() - FrameEntrySpillSize();
2194 __ Add(sp, sp, adjust);
2195 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2196 if (fpu_spill_mask_ != 0) {
2197 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2198
2199 // Check that list is contiguous.
2200 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2201
2202 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2203 GetAssembler()->cfi().AdjustCFAOffset(
2204 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002205 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002206 }
2207 // Pop LR into PC to return.
2208 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2209 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2210 __ Pop(RegisterList(pop_mask));
2211 GetAssembler()->cfi().RestoreState();
2212 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2213}
2214
2215void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2216 __ Bind(GetLabelOf(block));
2217}
2218
Artem Serovd4cc5b22016-11-04 11:19:09 +00002219Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2220 switch (type) {
2221 case Primitive::kPrimBoolean:
2222 case Primitive::kPrimByte:
2223 case Primitive::kPrimChar:
2224 case Primitive::kPrimShort:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimNot: {
2227 uint32_t index = gp_index_++;
2228 uint32_t stack_index = stack_index_++;
2229 if (index < calling_convention.GetNumberOfRegisters()) {
2230 return LocationFrom(calling_convention.GetRegisterAt(index));
2231 } else {
2232 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2233 }
2234 }
2235
2236 case Primitive::kPrimLong: {
2237 uint32_t index = gp_index_;
2238 uint32_t stack_index = stack_index_;
2239 gp_index_ += 2;
2240 stack_index_ += 2;
2241 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2242 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2243 // Skip R1, and use R2_R3 instead.
2244 gp_index_++;
2245 index++;
2246 }
2247 }
2248 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2249 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2250 calling_convention.GetRegisterAt(index + 1).GetCode());
2251
2252 return LocationFrom(calling_convention.GetRegisterAt(index),
2253 calling_convention.GetRegisterAt(index + 1));
2254 } else {
2255 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2256 }
2257 }
2258
2259 case Primitive::kPrimFloat: {
2260 uint32_t stack_index = stack_index_++;
2261 if (float_index_ % 2 == 0) {
2262 float_index_ = std::max(double_index_, float_index_);
2263 }
2264 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2265 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2266 } else {
2267 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2268 }
2269 }
2270
2271 case Primitive::kPrimDouble: {
2272 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2273 uint32_t stack_index = stack_index_;
2274 stack_index_ += 2;
2275 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2276 uint32_t index = double_index_;
2277 double_index_ += 2;
2278 Location result = LocationFrom(
2279 calling_convention.GetFpuRegisterAt(index),
2280 calling_convention.GetFpuRegisterAt(index + 1));
2281 DCHECK(ExpectedPairLayout(result));
2282 return result;
2283 } else {
2284 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2285 }
2286 }
2287
2288 case Primitive::kPrimVoid:
2289 LOG(FATAL) << "Unexpected parameter type " << type;
2290 break;
2291 }
2292 return Location::NoLocation();
2293}
2294
2295Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2296 switch (type) {
2297 case Primitive::kPrimBoolean:
2298 case Primitive::kPrimByte:
2299 case Primitive::kPrimChar:
2300 case Primitive::kPrimShort:
2301 case Primitive::kPrimInt:
2302 case Primitive::kPrimNot: {
2303 return LocationFrom(r0);
2304 }
2305
2306 case Primitive::kPrimFloat: {
2307 return LocationFrom(s0);
2308 }
2309
2310 case Primitive::kPrimLong: {
2311 return LocationFrom(r0, r1);
2312 }
2313
2314 case Primitive::kPrimDouble: {
2315 return LocationFrom(s0, s1);
2316 }
2317
2318 case Primitive::kPrimVoid:
2319 return Location::NoLocation();
2320 }
2321
2322 UNREACHABLE();
2323}
2324
2325Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2326 return LocationFrom(kMethodRegister);
2327}
2328
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002329void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2330 if (source.Equals(destination)) {
2331 return;
2332 }
2333 if (destination.IsRegister()) {
2334 if (source.IsRegister()) {
2335 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2336 } else if (source.IsFpuRegister()) {
2337 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2338 } else {
2339 GetAssembler()->LoadFromOffset(kLoadWord,
2340 RegisterFrom(destination),
2341 sp,
2342 source.GetStackIndex());
2343 }
2344 } else if (destination.IsFpuRegister()) {
2345 if (source.IsRegister()) {
2346 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2347 } else if (source.IsFpuRegister()) {
2348 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2349 } else {
2350 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2351 }
2352 } else {
2353 DCHECK(destination.IsStackSlot()) << destination;
2354 if (source.IsRegister()) {
2355 GetAssembler()->StoreToOffset(kStoreWord,
2356 RegisterFrom(source),
2357 sp,
2358 destination.GetStackIndex());
2359 } else if (source.IsFpuRegister()) {
2360 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2361 } else {
2362 DCHECK(source.IsStackSlot()) << source;
2363 UseScratchRegisterScope temps(GetVIXLAssembler());
2364 vixl32::Register temp = temps.Acquire();
2365 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2366 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2367 }
2368 }
2369}
2370
Artem Serovcfbe9132016-10-14 15:58:56 +01002371void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2372 DCHECK(location.IsRegister());
2373 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002374}
2375
2376void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002377 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2378 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2379 HParallelMove move(GetGraph()->GetArena());
2380 move.AddMove(src, dst, dst_type, nullptr);
2381 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002382}
2383
Artem Serovcfbe9132016-10-14 15:58:56 +01002384void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2385 if (location.IsRegister()) {
2386 locations->AddTemp(location);
2387 } else if (location.IsRegisterPair()) {
2388 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2389 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2390 } else {
2391 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2392 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002393}
2394
2395void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2396 HInstruction* instruction,
2397 uint32_t dex_pc,
2398 SlowPathCode* slow_path) {
2399 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002400 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2401 // Ensure the pc position is recorded immediately after the `blx` instruction.
2402 // 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 +00002403 ExactAssemblyScope aas(GetVIXLAssembler(),
2404 vixl32::k16BitT32InstructionSizeInBytes,
2405 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002406 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002407 if (EntrypointRequiresStackMap(entrypoint)) {
2408 RecordPcInfo(instruction, dex_pc, slow_path);
2409 }
2410}
2411
2412void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2413 HInstruction* instruction,
2414 SlowPathCode* slow_path) {
2415 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002416 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002417 __ Blx(lr);
2418}
2419
Scott Wakelingfe885462016-09-22 10:24:38 +01002420void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2421 DCHECK(!successor->IsExitBlock());
2422 HBasicBlock* block = got->GetBlock();
2423 HInstruction* previous = got->GetPrevious();
2424 HLoopInformation* info = block->GetLoopInformation();
2425
2426 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2427 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2428 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2429 return;
2430 }
2431 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2432 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2433 }
2434 if (!codegen_->GoesToNextBlock(block, successor)) {
2435 __ B(codegen_->GetLabelOf(successor));
2436 }
2437}
2438
2439void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2440 got->SetLocations(nullptr);
2441}
2442
2443void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2444 HandleGoto(got, got->GetSuccessor());
2445}
2446
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002447void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2448 try_boundary->SetLocations(nullptr);
2449}
2450
2451void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2452 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2453 if (!successor->IsExitBlock()) {
2454 HandleGoto(try_boundary, successor);
2455 }
2456}
2457
Scott Wakelingfe885462016-09-22 10:24:38 +01002458void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2459 exit->SetLocations(nullptr);
2460}
2461
2462void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2463}
2464
Scott Wakelingfe885462016-09-22 10:24:38 +01002465void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2466 vixl32::Label* true_label,
2467 vixl32::Label* false_label) {
2468 LocationSummary* locations = cond->GetLocations();
2469 Location left = locations->InAt(0);
2470 Location right = locations->InAt(1);
2471 IfCondition if_cond = cond->GetCondition();
2472
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002473 vixl32::Register left_high = HighRegisterFrom(left);
2474 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002475 IfCondition true_high_cond = if_cond;
2476 IfCondition false_high_cond = cond->GetOppositeCondition();
2477 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2478
2479 // Set the conditions for the test, remembering that == needs to be
2480 // decided using the low words.
Scott Wakelingfe885462016-09-22 10:24:38 +01002481 switch (if_cond) {
2482 case kCondEQ:
2483 case kCondNE:
2484 // Nothing to do.
2485 break;
2486 case kCondLT:
2487 false_high_cond = kCondGT;
2488 break;
2489 case kCondLE:
2490 true_high_cond = kCondLT;
2491 break;
2492 case kCondGT:
2493 false_high_cond = kCondLT;
2494 break;
2495 case kCondGE:
2496 true_high_cond = kCondGT;
2497 break;
2498 case kCondB:
2499 false_high_cond = kCondA;
2500 break;
2501 case kCondBE:
2502 true_high_cond = kCondB;
2503 break;
2504 case kCondA:
2505 false_high_cond = kCondB;
2506 break;
2507 case kCondAE:
2508 true_high_cond = kCondA;
2509 break;
2510 }
2511 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002512 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002513 int32_t val_low = Low32Bits(value);
2514 int32_t val_high = High32Bits(value);
2515
2516 __ Cmp(left_high, val_high);
2517 if (if_cond == kCondNE) {
2518 __ B(ARMCondition(true_high_cond), true_label);
2519 } else if (if_cond == kCondEQ) {
2520 __ B(ARMCondition(false_high_cond), false_label);
2521 } else {
2522 __ B(ARMCondition(true_high_cond), true_label);
2523 __ B(ARMCondition(false_high_cond), false_label);
2524 }
2525 // Must be equal high, so compare the lows.
2526 __ Cmp(left_low, val_low);
2527 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002528 vixl32::Register right_high = HighRegisterFrom(right);
2529 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002530
2531 __ Cmp(left_high, right_high);
2532 if (if_cond == kCondNE) {
2533 __ B(ARMCondition(true_high_cond), true_label);
2534 } else if (if_cond == kCondEQ) {
2535 __ B(ARMCondition(false_high_cond), false_label);
2536 } else {
2537 __ B(ARMCondition(true_high_cond), true_label);
2538 __ B(ARMCondition(false_high_cond), false_label);
2539 }
2540 // Must be equal high, so compare the lows.
2541 __ Cmp(left_low, right_low);
2542 }
2543 // The last comparison might be unsigned.
2544 // TODO: optimize cases where this is always true/false
2545 __ B(final_condition, true_label);
2546}
2547
2548void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2549 vixl32::Label* true_target_in,
2550 vixl32::Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002551 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2552 vixl32::Label* non_fallthrough_target;
2553 bool invert;
2554
2555 if (true_target_in == nullptr) {
2556 DCHECK(false_target_in != nullptr);
2557 non_fallthrough_target = false_target_in;
2558 invert = true;
2559 } else {
2560 non_fallthrough_target = true_target_in;
2561 invert = false;
2562 }
2563
2564 const auto cond = GenerateTest(condition, invert, codegen_);
2565
2566 __ B(cond.first, non_fallthrough_target);
2567
2568 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2569 __ B(false_target_in);
2570 }
2571
2572 return;
2573 }
2574
Scott Wakelingfe885462016-09-22 10:24:38 +01002575 // Generated branching requires both targets to be explicit. If either of the
2576 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2577 vixl32::Label fallthrough;
2578 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2579 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2580
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002581 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
2582 GenerateLongComparesAndJumps(condition, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002583
2584 if (false_target != &fallthrough) {
2585 __ B(false_target);
2586 }
2587
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002588 if (fallthrough.IsReferenced()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002589 __ Bind(&fallthrough);
2590 }
2591}
2592
2593void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2594 size_t condition_input_index,
2595 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002596 vixl32::Label* false_target,
2597 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002598 HInstruction* cond = instruction->InputAt(condition_input_index);
2599
2600 if (true_target == nullptr && false_target == nullptr) {
2601 // Nothing to do. The code always falls through.
2602 return;
2603 } else if (cond->IsIntConstant()) {
2604 // Constant condition, statically compared against "true" (integer value 1).
2605 if (cond->AsIntConstant()->IsTrue()) {
2606 if (true_target != nullptr) {
2607 __ B(true_target);
2608 }
2609 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002610 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002611 if (false_target != nullptr) {
2612 __ B(false_target);
2613 }
2614 }
2615 return;
2616 }
2617
2618 // The following code generates these patterns:
2619 // (1) true_target == nullptr && false_target != nullptr
2620 // - opposite condition true => branch to false_target
2621 // (2) true_target != nullptr && false_target == nullptr
2622 // - condition true => branch to true_target
2623 // (3) true_target != nullptr && false_target != nullptr
2624 // - condition true => branch to true_target
2625 // - branch to false_target
2626 if (IsBooleanValueOrMaterializedCondition(cond)) {
2627 // Condition has been materialized, compare the output to 0.
2628 if (kIsDebugBuild) {
2629 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2630 DCHECK(cond_val.IsRegister());
2631 }
2632 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002633 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2634 false_target,
2635 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002636 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002637 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2638 true_target,
2639 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002640 }
2641 } else {
2642 // Condition has not been materialized. Use its inputs as the comparison and
2643 // its condition as the branch condition.
2644 HCondition* condition = cond->AsCondition();
2645
2646 // If this is a long or FP comparison that has been folded into
2647 // the HCondition, generate the comparison directly.
2648 Primitive::Type type = condition->InputAt(0)->GetType();
2649 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2650 GenerateCompareTestAndBranch(condition, true_target, false_target);
2651 return;
2652 }
2653
Donghui Bai426b49c2016-11-08 14:55:38 +08002654 vixl32::Label* non_fallthrough_target;
2655 vixl32::Condition arm_cond = vixl32::Condition::None();
2656 const vixl32::Register left = InputRegisterAt(cond, 0);
2657 const Operand right = InputOperandAt(cond, 1);
2658
Scott Wakelingfe885462016-09-22 10:24:38 +01002659 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002660 arm_cond = ARMCondition(condition->GetOppositeCondition());
2661 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002662 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002663 arm_cond = ARMCondition(condition->GetCondition());
2664 non_fallthrough_target = true_target;
2665 }
2666
2667 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2668 if (arm_cond.Is(eq)) {
2669 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2670 } else {
2671 DCHECK(arm_cond.Is(ne));
2672 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2673 }
2674 } else {
2675 __ Cmp(left, right);
2676 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002677 }
2678 }
2679
2680 // If neither branch falls through (case 3), the conditional branch to `true_target`
2681 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2682 if (true_target != nullptr && false_target != nullptr) {
2683 __ B(false_target);
2684 }
2685}
2686
2687void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2689 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 }
2692}
2693
2694void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2695 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2696 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002697 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2698 nullptr : codegen_->GetLabelOf(true_successor);
2699 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2700 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002701 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2702}
2703
Scott Wakelingc34dba72016-10-03 10:14:44 +01002704void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2705 LocationSummary* locations = new (GetGraph()->GetArena())
2706 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2707 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2708 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2709 locations->SetInAt(0, Location::RequiresRegister());
2710 }
2711}
2712
2713void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2714 SlowPathCodeARMVIXL* slow_path =
2715 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2716 GenerateTestAndBranch(deoptimize,
2717 /* condition_input_index */ 0,
2718 slow_path->GetEntryLabel(),
2719 /* false_target */ nullptr);
2720}
2721
Artem Serovd4cc5b22016-11-04 11:19:09 +00002722void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2723 LocationSummary* locations = new (GetGraph()->GetArena())
2724 LocationSummary(flag, LocationSummary::kNoCall);
2725 locations->SetOut(Location::RequiresRegister());
2726}
2727
2728void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2729 GetAssembler()->LoadFromOffset(kLoadWord,
2730 OutputRegister(flag),
2731 sp,
2732 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2733}
2734
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002735void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002737 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2738
2739 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002740 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002741 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002742 } else {
2743 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002744 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002745 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002746
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002747 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002748 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2749 // The code generator handles overlap with the values, but not with the condition.
2750 locations->SetOut(Location::SameAsFirstInput());
2751 } else if (is_floating_point) {
2752 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2753 } else {
2754 if (!locations->InAt(1).IsConstant()) {
2755 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2756 }
2757
2758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002759 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002760}
2761
2762void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002763 HInstruction* const condition = select->GetCondition();
2764 const LocationSummary* const locations = select->GetLocations();
2765 const Primitive::Type type = select->GetType();
2766 const Location first = locations->InAt(0);
2767 const Location out = locations->Out();
2768 const Location second = locations->InAt(1);
2769 Location src;
2770
2771 if (condition->IsIntConstant()) {
2772 if (condition->AsIntConstant()->IsFalse()) {
2773 src = first;
2774 } else {
2775 src = second;
2776 }
2777
2778 codegen_->MoveLocation(out, src, type);
2779 return;
2780 }
2781
2782 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002783 (IsBooleanValueOrMaterializedCondition(condition) ||
2784 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002785 bool invert = false;
2786
2787 if (out.Equals(second)) {
2788 src = first;
2789 invert = true;
2790 } else if (out.Equals(first)) {
2791 src = second;
2792 } else if (second.IsConstant()) {
2793 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2794 src = second;
2795 } else if (first.IsConstant()) {
2796 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2797 src = first;
2798 invert = true;
2799 } else {
2800 src = second;
2801 }
2802
2803 if (CanGenerateConditionalMove(out, src)) {
2804 if (!out.Equals(first) && !out.Equals(second)) {
2805 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2806 }
2807
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002808 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2809
2810 if (IsBooleanValueOrMaterializedCondition(condition)) {
2811 __ Cmp(InputRegisterAt(select, 2), 0);
2812 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2813 } else {
2814 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2815 }
2816
Donghui Bai426b49c2016-11-08 14:55:38 +08002817 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002818 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002819 ExactAssemblyScope guard(GetVIXLAssembler(),
2820 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2821 CodeBufferCheckScope::kExactSize);
2822
2823 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002824 __ it(cond.first);
2825 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002826 } else {
2827 DCHECK(out.IsRegisterPair());
2828
2829 Operand operand_high(0);
2830 Operand operand_low(0);
2831
2832 if (src.IsConstant()) {
2833 const int64_t value = Int64ConstantFrom(src);
2834
2835 operand_high = High32Bits(value);
2836 operand_low = Low32Bits(value);
2837 } else {
2838 DCHECK(src.IsRegisterPair());
2839 operand_high = HighRegisterFrom(src);
2840 operand_low = LowRegisterFrom(src);
2841 }
2842
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002843 __ it(cond.first);
2844 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2845 __ it(cond.first);
2846 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002847 }
2848
2849 return;
2850 }
2851 }
2852
2853 vixl32::Label* false_target = nullptr;
2854 vixl32::Label* true_target = nullptr;
2855 vixl32::Label select_end;
2856 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2857
2858 if (out.Equals(second)) {
2859 true_target = target;
2860 src = first;
2861 } else {
2862 false_target = target;
2863 src = second;
2864
2865 if (!out.Equals(first)) {
2866 codegen_->MoveLocation(out, first, type);
2867 }
2868 }
2869
2870 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2871 codegen_->MoveLocation(out, src, type);
2872
2873 if (select_end.IsReferenced()) {
2874 __ Bind(&select_end);
2875 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002876}
2877
Artem Serov551b28f2016-10-18 19:11:30 +01002878void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2879 new (GetGraph()->GetArena()) LocationSummary(info);
2880}
2881
2882void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2883 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2884}
2885
Scott Wakelingfe885462016-09-22 10:24:38 +01002886void CodeGeneratorARMVIXL::GenerateNop() {
2887 __ Nop();
2888}
2889
2890void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2891 LocationSummary* locations =
2892 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2893 // Handle the long/FP comparisons made in instruction simplification.
2894 switch (cond->InputAt(0)->GetType()) {
2895 case Primitive::kPrimLong:
2896 locations->SetInAt(0, Location::RequiresRegister());
2897 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2898 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01002900 }
2901 break;
2902
Scott Wakelingfe885462016-09-22 10:24:38 +01002903 case Primitive::kPrimFloat:
2904 case Primitive::kPrimDouble:
2905 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002906 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002907 if (!cond->IsEmittedAtUseSite()) {
2908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2909 }
2910 break;
2911
2912 default:
2913 locations->SetInAt(0, Location::RequiresRegister());
2914 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2915 if (!cond->IsEmittedAtUseSite()) {
2916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2917 }
2918 }
2919}
2920
2921void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2922 if (cond->IsEmittedAtUseSite()) {
2923 return;
2924 }
2925
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002926 const vixl32::Register out = OutputRegister(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002927
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002928 if (out.IsLow() && CanGenerateTest(cond, codegen_->GetAssembler())) {
2929 const auto condition = GenerateTest(cond, false, codegen_);
2930 // We use the scope because of the IT block that follows.
2931 ExactAssemblyScope guard(GetVIXLAssembler(),
2932 4 * vixl32::k16BitT32InstructionSizeInBytes,
2933 CodeBufferCheckScope::kExactSize);
2934
2935 __ it(condition.first);
2936 __ mov(condition.first, out, 1);
2937 __ it(condition.second);
2938 __ mov(condition.second, out, 0);
2939 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01002940 }
2941
2942 // Convert the jumps into the result.
2943 vixl32::Label done_label;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002944 vixl32::Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01002945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002946 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2947 vixl32::Label true_label, false_label;
Scott Wakelingfe885462016-09-22 10:24:38 +01002948
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002949 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2950
2951 // False case: result = 0.
2952 __ Bind(&false_label);
2953 __ Mov(out, 0);
2954 __ B(final_label);
2955
2956 // True case: result = 1.
2957 __ Bind(&true_label);
2958 __ Mov(out, 1);
2959 } else {
2960 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
2961
2962 const auto condition = GenerateTest(cond, false, codegen_);
2963
2964 __ Mov(LeaveFlags, out, 0);
2965 __ B(condition.second, final_label, /* far_target */ false);
2966 __ Mov(out, 1);
2967 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002968
2969 if (done_label.IsReferenced()) {
2970 __ Bind(&done_label);
2971 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002972}
2973
2974void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2975 HandleCondition(comp);
2976}
2977
2978void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2979 HandleCondition(comp);
2980}
2981
2982void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2983 HandleCondition(comp);
2984}
2985
2986void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2987 HandleCondition(comp);
2988}
2989
2990void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2991 HandleCondition(comp);
2992}
2993
2994void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2995 HandleCondition(comp);
2996}
2997
2998void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2999 HandleCondition(comp);
3000}
3001
3002void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3003 HandleCondition(comp);
3004}
3005
3006void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3007 HandleCondition(comp);
3008}
3009
3010void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3011 HandleCondition(comp);
3012}
3013
3014void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3015 HandleCondition(comp);
3016}
3017
3018void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3019 HandleCondition(comp);
3020}
3021
3022void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3023 HandleCondition(comp);
3024}
3025
3026void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3027 HandleCondition(comp);
3028}
3029
3030void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3031 HandleCondition(comp);
3032}
3033
3034void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3035 HandleCondition(comp);
3036}
3037
3038void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3039 HandleCondition(comp);
3040}
3041
3042void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3043 HandleCondition(comp);
3044}
3045
3046void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3047 HandleCondition(comp);
3048}
3049
3050void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3051 HandleCondition(comp);
3052}
3053
3054void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3057 locations->SetOut(Location::ConstantLocation(constant));
3058}
3059
3060void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3061 // Will be generated at use site.
3062}
3063
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003064void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3065 LocationSummary* locations =
3066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3067 locations->SetOut(Location::ConstantLocation(constant));
3068}
3069
3070void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3071 // Will be generated at use site.
3072}
3073
Scott Wakelingfe885462016-09-22 10:24:38 +01003074void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3075 LocationSummary* locations =
3076 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3077 locations->SetOut(Location::ConstantLocation(constant));
3078}
3079
3080void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3081 // Will be generated at use site.
3082}
3083
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003084void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3087 locations->SetOut(Location::ConstantLocation(constant));
3088}
3089
Scott Wakelingc34dba72016-10-03 10:14:44 +01003090void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3091 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003092 // Will be generated at use site.
3093}
3094
3095void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3098 locations->SetOut(Location::ConstantLocation(constant));
3099}
3100
Scott Wakelingc34dba72016-10-03 10:14:44 +01003101void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3102 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003103 // Will be generated at use site.
3104}
3105
Scott Wakelingfe885462016-09-22 10:24:38 +01003106void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3107 memory_barrier->SetLocations(nullptr);
3108}
3109
3110void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3111 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3112}
3113
3114void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3115 ret->SetLocations(nullptr);
3116}
3117
3118void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3119 codegen_->GenerateFrameExit();
3120}
3121
3122void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3123 LocationSummary* locations =
3124 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3125 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3126}
3127
3128void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3129 codegen_->GenerateFrameExit();
3130}
3131
Artem Serovcfbe9132016-10-14 15:58:56 +01003132void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3133 // The trampoline uses the same calling convention as dex calling conventions,
3134 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3135 // the method_idx.
3136 HandleInvoke(invoke);
3137}
3138
3139void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3140 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3141}
3142
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003143void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3144 // Explicit clinit checks triggered by static invokes must have been pruned by
3145 // art::PrepareForRegisterAllocation.
3146 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3147
Anton Kirilov5ec62182016-10-13 20:16:02 +01003148 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3149 if (intrinsic.TryDispatch(invoke)) {
3150 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3151 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3152 }
3153 return;
3154 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155
3156 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003157
Artem Serovd4cc5b22016-11-04 11:19:09 +00003158 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3159 if (invoke->HasPcRelativeDexCache()) {
3160 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3161 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003162}
3163
Anton Kirilov5ec62182016-10-13 20:16:02 +01003164static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3165 if (invoke->GetLocations()->Intrinsified()) {
3166 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3167 intrinsic.Dispatch(invoke);
3168 return true;
3169 }
3170 return false;
3171}
3172
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003173void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3174 // Explicit clinit checks triggered by static invokes must have been pruned by
3175 // art::PrepareForRegisterAllocation.
3176 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3177
Anton Kirilov5ec62182016-10-13 20:16:02 +01003178 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3179 return;
3180 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003181
3182 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003183 codegen_->GenerateStaticOrDirectCall(
3184 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003185 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3186}
3187
3188void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003189 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003190 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3191}
3192
3193void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003194 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3195 if (intrinsic.TryDispatch(invoke)) {
3196 return;
3197 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003198
3199 HandleInvoke(invoke);
3200}
3201
3202void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003203 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3204 return;
3205 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003206
3207 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003209 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003210}
3211
Artem Serovcfbe9132016-10-14 15:58:56 +01003212void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3213 HandleInvoke(invoke);
3214 // Add the hidden argument.
3215 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3216}
3217
3218void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3219 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3220 LocationSummary* locations = invoke->GetLocations();
3221 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3222 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3223 Location receiver = locations->InAt(0);
3224 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3225
3226 DCHECK(!receiver.IsStackSlot());
3227
Alexandre Rames374ddf32016-11-04 10:40:49 +00003228 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3229 {
Artem Serov0fb37192016-12-06 18:13:40 +00003230 ExactAssemblyScope aas(GetVIXLAssembler(),
3231 vixl32::kMaxInstructionSizeInBytes,
3232 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003233 // /* HeapReference<Class> */ temp = receiver->klass_
3234 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3235 codegen_->MaybeRecordImplicitNullCheck(invoke);
3236 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003237 // Instead of simply (possibly) unpoisoning `temp` here, we should
3238 // emit a read barrier for the previous class reference load.
3239 // However this is not required in practice, as this is an
3240 // intermediate/temporary reference and because the current
3241 // concurrent copying collector keeps the from-space memory
3242 // intact/accessible until the end of the marking phase (the
3243 // concurrent copying collector may not in the future).
3244 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3245 GetAssembler()->LoadFromOffset(kLoadWord,
3246 temp,
3247 temp,
3248 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3249 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3250 invoke->GetImtIndex(), kArmPointerSize));
3251 // temp = temp->GetImtEntryAt(method_offset);
3252 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3253 uint32_t entry_point =
3254 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3255 // LR = temp->GetEntryPoint();
3256 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3257
3258 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3259 // instruction from clobbering it as they might use r12 as a scratch register.
3260 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003261
3262 {
3263 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3264 // so it checks if the application is using them (by passing them to the macro assembler
3265 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3266 // what is available, and is the opposite of the standard usage: Instead of requesting a
3267 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3268 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3269 // (to materialize the constant), since the destination register becomes available for such use
3270 // internally for the duration of the macro instruction.
3271 UseScratchRegisterScope temps(GetVIXLAssembler());
3272 temps.Exclude(hidden_reg);
3273 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3274 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003275 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003276 // Ensure the pc position is recorded immediately after the `blx` instruction.
3277 // 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 +00003278 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003279 vixl32::k16BitT32InstructionSizeInBytes,
3280 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003281 // LR();
3282 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003284 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003285 }
3286}
3287
Orion Hodsonac141392017-01-13 11:53:47 +00003288void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3289 HandleInvoke(invoke);
3290}
3291
3292void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3293 codegen_->GenerateInvokePolymorphicCall(invoke);
3294}
3295
Artem Serov02109dd2016-09-23 17:17:54 +01003296void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3299 switch (neg->GetResultType()) {
3300 case Primitive::kPrimInt: {
3301 locations->SetInAt(0, Location::RequiresRegister());
3302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3303 break;
3304 }
3305 case Primitive::kPrimLong: {
3306 locations->SetInAt(0, Location::RequiresRegister());
3307 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3308 break;
3309 }
3310
3311 case Primitive::kPrimFloat:
3312 case Primitive::kPrimDouble:
3313 locations->SetInAt(0, Location::RequiresFpuRegister());
3314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3315 break;
3316
3317 default:
3318 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3319 }
3320}
3321
3322void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3323 LocationSummary* locations = neg->GetLocations();
3324 Location out = locations->Out();
3325 Location in = locations->InAt(0);
3326 switch (neg->GetResultType()) {
3327 case Primitive::kPrimInt:
3328 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3329 break;
3330
3331 case Primitive::kPrimLong:
3332 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3333 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3334 // We cannot emit an RSC (Reverse Subtract with Carry)
3335 // instruction here, as it does not exist in the Thumb-2
3336 // instruction set. We use the following approach
3337 // using SBC and SUB instead.
3338 //
3339 // out.hi = -C
3340 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3341 // out.hi = out.hi - in.hi
3342 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3343 break;
3344
3345 case Primitive::kPrimFloat:
3346 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003347 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003348 break;
3349
3350 default:
3351 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3352 }
3353}
3354
Scott Wakelingfe885462016-09-22 10:24:38 +01003355void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3356 Primitive::Type result_type = conversion->GetResultType();
3357 Primitive::Type input_type = conversion->GetInputType();
3358 DCHECK_NE(result_type, input_type);
3359
3360 // The float-to-long, double-to-long and long-to-float type conversions
3361 // rely on a call to the runtime.
3362 LocationSummary::CallKind call_kind =
3363 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3364 && result_type == Primitive::kPrimLong)
3365 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3366 ? LocationSummary::kCallOnMainOnly
3367 : LocationSummary::kNoCall;
3368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3370
3371 // The Java language does not allow treating boolean as an integral type but
3372 // our bit representation makes it safe.
3373
3374 switch (result_type) {
3375 case Primitive::kPrimByte:
3376 switch (input_type) {
3377 case Primitive::kPrimLong:
3378 // Type conversion from long to byte is a result of code transformations.
3379 case Primitive::kPrimBoolean:
3380 // Boolean input is a result of code transformations.
3381 case Primitive::kPrimShort:
3382 case Primitive::kPrimInt:
3383 case Primitive::kPrimChar:
3384 // Processing a Dex `int-to-byte' instruction.
3385 locations->SetInAt(0, Location::RequiresRegister());
3386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3387 break;
3388
3389 default:
3390 LOG(FATAL) << "Unexpected type conversion from " << input_type
3391 << " to " << result_type;
3392 }
3393 break;
3394
3395 case Primitive::kPrimShort:
3396 switch (input_type) {
3397 case Primitive::kPrimLong:
3398 // Type conversion from long to short is a result of code transformations.
3399 case Primitive::kPrimBoolean:
3400 // Boolean input is a result of code transformations.
3401 case Primitive::kPrimByte:
3402 case Primitive::kPrimInt:
3403 case Primitive::kPrimChar:
3404 // Processing a Dex `int-to-short' instruction.
3405 locations->SetInAt(0, Location::RequiresRegister());
3406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3407 break;
3408
3409 default:
3410 LOG(FATAL) << "Unexpected type conversion from " << input_type
3411 << " to " << result_type;
3412 }
3413 break;
3414
3415 case Primitive::kPrimInt:
3416 switch (input_type) {
3417 case Primitive::kPrimLong:
3418 // Processing a Dex `long-to-int' instruction.
3419 locations->SetInAt(0, Location::Any());
3420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3421 break;
3422
3423 case Primitive::kPrimFloat:
3424 // Processing a Dex `float-to-int' instruction.
3425 locations->SetInAt(0, Location::RequiresFpuRegister());
3426 locations->SetOut(Location::RequiresRegister());
3427 locations->AddTemp(Location::RequiresFpuRegister());
3428 break;
3429
3430 case Primitive::kPrimDouble:
3431 // Processing a Dex `double-to-int' instruction.
3432 locations->SetInAt(0, Location::RequiresFpuRegister());
3433 locations->SetOut(Location::RequiresRegister());
3434 locations->AddTemp(Location::RequiresFpuRegister());
3435 break;
3436
3437 default:
3438 LOG(FATAL) << "Unexpected type conversion from " << input_type
3439 << " to " << result_type;
3440 }
3441 break;
3442
3443 case Primitive::kPrimLong:
3444 switch (input_type) {
3445 case Primitive::kPrimBoolean:
3446 // Boolean input is a result of code transformations.
3447 case Primitive::kPrimByte:
3448 case Primitive::kPrimShort:
3449 case Primitive::kPrimInt:
3450 case Primitive::kPrimChar:
3451 // Processing a Dex `int-to-long' instruction.
3452 locations->SetInAt(0, Location::RequiresRegister());
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454 break;
3455
3456 case Primitive::kPrimFloat: {
3457 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003458 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3459 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3460 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003461 break;
3462 }
3463
3464 case Primitive::kPrimDouble: {
3465 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003466 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3467 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3468 calling_convention.GetFpuRegisterAt(1)));
3469 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003470 break;
3471 }
3472
3473 default:
3474 LOG(FATAL) << "Unexpected type conversion from " << input_type
3475 << " to " << result_type;
3476 }
3477 break;
3478
3479 case Primitive::kPrimChar:
3480 switch (input_type) {
3481 case Primitive::kPrimLong:
3482 // Type conversion from long to char is a result of code transformations.
3483 case Primitive::kPrimBoolean:
3484 // Boolean input is a result of code transformations.
3485 case Primitive::kPrimByte:
3486 case Primitive::kPrimShort:
3487 case Primitive::kPrimInt:
3488 // Processing a Dex `int-to-char' instruction.
3489 locations->SetInAt(0, Location::RequiresRegister());
3490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3491 break;
3492
3493 default:
3494 LOG(FATAL) << "Unexpected type conversion from " << input_type
3495 << " to " << result_type;
3496 }
3497 break;
3498
3499 case Primitive::kPrimFloat:
3500 switch (input_type) {
3501 case Primitive::kPrimBoolean:
3502 // Boolean input is a result of code transformations.
3503 case Primitive::kPrimByte:
3504 case Primitive::kPrimShort:
3505 case Primitive::kPrimInt:
3506 case Primitive::kPrimChar:
3507 // Processing a Dex `int-to-float' instruction.
3508 locations->SetInAt(0, Location::RequiresRegister());
3509 locations->SetOut(Location::RequiresFpuRegister());
3510 break;
3511
3512 case Primitive::kPrimLong: {
3513 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003514 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3515 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3516 calling_convention.GetRegisterAt(1)));
3517 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003518 break;
3519 }
3520
3521 case Primitive::kPrimDouble:
3522 // Processing a Dex `double-to-float' instruction.
3523 locations->SetInAt(0, Location::RequiresFpuRegister());
3524 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3525 break;
3526
3527 default:
3528 LOG(FATAL) << "Unexpected type conversion from " << input_type
3529 << " to " << result_type;
3530 };
3531 break;
3532
3533 case Primitive::kPrimDouble:
3534 switch (input_type) {
3535 case Primitive::kPrimBoolean:
3536 // Boolean input is a result of code transformations.
3537 case Primitive::kPrimByte:
3538 case Primitive::kPrimShort:
3539 case Primitive::kPrimInt:
3540 case Primitive::kPrimChar:
3541 // Processing a Dex `int-to-double' instruction.
3542 locations->SetInAt(0, Location::RequiresRegister());
3543 locations->SetOut(Location::RequiresFpuRegister());
3544 break;
3545
3546 case Primitive::kPrimLong:
3547 // Processing a Dex `long-to-double' instruction.
3548 locations->SetInAt(0, Location::RequiresRegister());
3549 locations->SetOut(Location::RequiresFpuRegister());
3550 locations->AddTemp(Location::RequiresFpuRegister());
3551 locations->AddTemp(Location::RequiresFpuRegister());
3552 break;
3553
3554 case Primitive::kPrimFloat:
3555 // Processing a Dex `float-to-double' instruction.
3556 locations->SetInAt(0, Location::RequiresFpuRegister());
3557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3558 break;
3559
3560 default:
3561 LOG(FATAL) << "Unexpected type conversion from " << input_type
3562 << " to " << result_type;
3563 };
3564 break;
3565
3566 default:
3567 LOG(FATAL) << "Unexpected type conversion from " << input_type
3568 << " to " << result_type;
3569 }
3570}
3571
3572void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3573 LocationSummary* locations = conversion->GetLocations();
3574 Location out = locations->Out();
3575 Location in = locations->InAt(0);
3576 Primitive::Type result_type = conversion->GetResultType();
3577 Primitive::Type input_type = conversion->GetInputType();
3578 DCHECK_NE(result_type, input_type);
3579 switch (result_type) {
3580 case Primitive::kPrimByte:
3581 switch (input_type) {
3582 case Primitive::kPrimLong:
3583 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003584 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003585 break;
3586 case Primitive::kPrimBoolean:
3587 // Boolean input is a result of code transformations.
3588 case Primitive::kPrimShort:
3589 case Primitive::kPrimInt:
3590 case Primitive::kPrimChar:
3591 // Processing a Dex `int-to-byte' instruction.
3592 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3593 break;
3594
3595 default:
3596 LOG(FATAL) << "Unexpected type conversion from " << input_type
3597 << " to " << result_type;
3598 }
3599 break;
3600
3601 case Primitive::kPrimShort:
3602 switch (input_type) {
3603 case Primitive::kPrimLong:
3604 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003605 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003606 break;
3607 case Primitive::kPrimBoolean:
3608 // Boolean input is a result of code transformations.
3609 case Primitive::kPrimByte:
3610 case Primitive::kPrimInt:
3611 case Primitive::kPrimChar:
3612 // Processing a Dex `int-to-short' instruction.
3613 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3614 break;
3615
3616 default:
3617 LOG(FATAL) << "Unexpected type conversion from " << input_type
3618 << " to " << result_type;
3619 }
3620 break;
3621
3622 case Primitive::kPrimInt:
3623 switch (input_type) {
3624 case Primitive::kPrimLong:
3625 // Processing a Dex `long-to-int' instruction.
3626 DCHECK(out.IsRegister());
3627 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003628 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003629 } else if (in.IsDoubleStackSlot()) {
3630 GetAssembler()->LoadFromOffset(kLoadWord,
3631 OutputRegister(conversion),
3632 sp,
3633 in.GetStackIndex());
3634 } else {
3635 DCHECK(in.IsConstant());
3636 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003637 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3638 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003639 }
3640 break;
3641
3642 case Primitive::kPrimFloat: {
3643 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003644 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003645 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003646 __ Vmov(OutputRegister(conversion), temp);
3647 break;
3648 }
3649
3650 case Primitive::kPrimDouble: {
3651 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003652 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003653 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003654 __ Vmov(OutputRegister(conversion), temp_s);
3655 break;
3656 }
3657
3658 default:
3659 LOG(FATAL) << "Unexpected type conversion from " << input_type
3660 << " to " << result_type;
3661 }
3662 break;
3663
3664 case Primitive::kPrimLong:
3665 switch (input_type) {
3666 case Primitive::kPrimBoolean:
3667 // Boolean input is a result of code transformations.
3668 case Primitive::kPrimByte:
3669 case Primitive::kPrimShort:
3670 case Primitive::kPrimInt:
3671 case Primitive::kPrimChar:
3672 // Processing a Dex `int-to-long' instruction.
3673 DCHECK(out.IsRegisterPair());
3674 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003675 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003676 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003677 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003678 break;
3679
3680 case Primitive::kPrimFloat:
3681 // Processing a Dex `float-to-long' instruction.
3682 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3683 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3684 break;
3685
3686 case Primitive::kPrimDouble:
3687 // Processing a Dex `double-to-long' instruction.
3688 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3689 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3690 break;
3691
3692 default:
3693 LOG(FATAL) << "Unexpected type conversion from " << input_type
3694 << " to " << result_type;
3695 }
3696 break;
3697
3698 case Primitive::kPrimChar:
3699 switch (input_type) {
3700 case Primitive::kPrimLong:
3701 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003702 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003703 break;
3704 case Primitive::kPrimBoolean:
3705 // Boolean input is a result of code transformations.
3706 case Primitive::kPrimByte:
3707 case Primitive::kPrimShort:
3708 case Primitive::kPrimInt:
3709 // Processing a Dex `int-to-char' instruction.
3710 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3711 break;
3712
3713 default:
3714 LOG(FATAL) << "Unexpected type conversion from " << input_type
3715 << " to " << result_type;
3716 }
3717 break;
3718
3719 case Primitive::kPrimFloat:
3720 switch (input_type) {
3721 case Primitive::kPrimBoolean:
3722 // Boolean input is a result of code transformations.
3723 case Primitive::kPrimByte:
3724 case Primitive::kPrimShort:
3725 case Primitive::kPrimInt:
3726 case Primitive::kPrimChar: {
3727 // Processing a Dex `int-to-float' instruction.
3728 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003729 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003730 break;
3731 }
3732
3733 case Primitive::kPrimLong:
3734 // Processing a Dex `long-to-float' instruction.
3735 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3736 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3737 break;
3738
3739 case Primitive::kPrimDouble:
3740 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003741 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003742 break;
3743
3744 default:
3745 LOG(FATAL) << "Unexpected type conversion from " << input_type
3746 << " to " << result_type;
3747 };
3748 break;
3749
3750 case Primitive::kPrimDouble:
3751 switch (input_type) {
3752 case Primitive::kPrimBoolean:
3753 // Boolean input is a result of code transformations.
3754 case Primitive::kPrimByte:
3755 case Primitive::kPrimShort:
3756 case Primitive::kPrimInt:
3757 case Primitive::kPrimChar: {
3758 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003759 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003760 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003761 break;
3762 }
3763
3764 case Primitive::kPrimLong: {
3765 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003766 vixl32::Register low = LowRegisterFrom(in);
3767 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003768 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003769 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003770 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003771 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003772 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003773
3774 // temp_d = int-to-double(high)
3775 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003776 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003777 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003778 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003779 // out_d = unsigned-to-double(low)
3780 __ Vmov(out_s, low);
3781 __ Vcvt(F64, U32, out_d, out_s);
3782 // out_d += temp_d * constant_d
3783 __ Vmla(F64, out_d, temp_d, constant_d);
3784 break;
3785 }
3786
3787 case Primitive::kPrimFloat:
3788 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003789 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003790 break;
3791
3792 default:
3793 LOG(FATAL) << "Unexpected type conversion from " << input_type
3794 << " to " << result_type;
3795 };
3796 break;
3797
3798 default:
3799 LOG(FATAL) << "Unexpected type conversion from " << input_type
3800 << " to " << result_type;
3801 }
3802}
3803
3804void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3805 LocationSummary* locations =
3806 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3807 switch (add->GetResultType()) {
3808 case Primitive::kPrimInt: {
3809 locations->SetInAt(0, Location::RequiresRegister());
3810 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3812 break;
3813 }
3814
Scott Wakelingfe885462016-09-22 10:24:38 +01003815 case Primitive::kPrimLong: {
3816 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003817 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3819 break;
3820 }
3821
3822 case Primitive::kPrimFloat:
3823 case Primitive::kPrimDouble: {
3824 locations->SetInAt(0, Location::RequiresFpuRegister());
3825 locations->SetInAt(1, Location::RequiresFpuRegister());
3826 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3827 break;
3828 }
3829
3830 default:
3831 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3832 }
3833}
3834
3835void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3836 LocationSummary* locations = add->GetLocations();
3837 Location out = locations->Out();
3838 Location first = locations->InAt(0);
3839 Location second = locations->InAt(1);
3840
3841 switch (add->GetResultType()) {
3842 case Primitive::kPrimInt: {
3843 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3844 }
3845 break;
3846
Scott Wakelingfe885462016-09-22 10:24:38 +01003847 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003848 if (second.IsConstant()) {
3849 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3850 GenerateAddLongConst(out, first, value);
3851 } else {
3852 DCHECK(second.IsRegisterPair());
3853 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3854 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3855 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003856 break;
3857 }
3858
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003859 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003860 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003861 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003862 break;
3863
3864 default:
3865 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3866 }
3867}
3868
3869void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3870 LocationSummary* locations =
3871 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3872 switch (sub->GetResultType()) {
3873 case Primitive::kPrimInt: {
3874 locations->SetInAt(0, Location::RequiresRegister());
3875 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3877 break;
3878 }
3879
Scott Wakelingfe885462016-09-22 10:24:38 +01003880 case Primitive::kPrimLong: {
3881 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003882 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3884 break;
3885 }
3886 case Primitive::kPrimFloat:
3887 case Primitive::kPrimDouble: {
3888 locations->SetInAt(0, Location::RequiresFpuRegister());
3889 locations->SetInAt(1, Location::RequiresFpuRegister());
3890 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3891 break;
3892 }
3893 default:
3894 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3895 }
3896}
3897
3898void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3899 LocationSummary* locations = sub->GetLocations();
3900 Location out = locations->Out();
3901 Location first = locations->InAt(0);
3902 Location second = locations->InAt(1);
3903 switch (sub->GetResultType()) {
3904 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003905 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003906 break;
3907 }
3908
Scott Wakelingfe885462016-09-22 10:24:38 +01003909 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003910 if (second.IsConstant()) {
3911 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3912 GenerateAddLongConst(out, first, -value);
3913 } else {
3914 DCHECK(second.IsRegisterPair());
3915 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3916 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3917 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003918 break;
3919 }
3920
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003921 case Primitive::kPrimFloat:
3922 case Primitive::kPrimDouble:
3923 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003924 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003925
3926 default:
3927 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3928 }
3929}
3930
3931void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3932 LocationSummary* locations =
3933 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3934 switch (mul->GetResultType()) {
3935 case Primitive::kPrimInt:
3936 case Primitive::kPrimLong: {
3937 locations->SetInAt(0, Location::RequiresRegister());
3938 locations->SetInAt(1, Location::RequiresRegister());
3939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3940 break;
3941 }
3942
3943 case Primitive::kPrimFloat:
3944 case Primitive::kPrimDouble: {
3945 locations->SetInAt(0, Location::RequiresFpuRegister());
3946 locations->SetInAt(1, Location::RequiresFpuRegister());
3947 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3948 break;
3949 }
3950
3951 default:
3952 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3953 }
3954}
3955
3956void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3957 LocationSummary* locations = mul->GetLocations();
3958 Location out = locations->Out();
3959 Location first = locations->InAt(0);
3960 Location second = locations->InAt(1);
3961 switch (mul->GetResultType()) {
3962 case Primitive::kPrimInt: {
3963 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3964 break;
3965 }
3966 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003967 vixl32::Register out_hi = HighRegisterFrom(out);
3968 vixl32::Register out_lo = LowRegisterFrom(out);
3969 vixl32::Register in1_hi = HighRegisterFrom(first);
3970 vixl32::Register in1_lo = LowRegisterFrom(first);
3971 vixl32::Register in2_hi = HighRegisterFrom(second);
3972 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003973
3974 // Extra checks to protect caused by the existence of R1_R2.
3975 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3976 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003977 DCHECK(!out_hi.Is(in1_lo));
3978 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003979
3980 // input: in1 - 64 bits, in2 - 64 bits
3981 // output: out
3982 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3983 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3984 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3985
3986 UseScratchRegisterScope temps(GetVIXLAssembler());
3987 vixl32::Register temp = temps.Acquire();
3988 // temp <- in1.lo * in2.hi
3989 __ Mul(temp, in1_lo, in2_hi);
3990 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3991 __ Mla(out_hi, in1_hi, in2_lo, temp);
3992 // out.lo <- (in1.lo * in2.lo)[31:0];
3993 __ Umull(out_lo, temp, in1_lo, in2_lo);
3994 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003995 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003996 break;
3997 }
3998
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003999 case Primitive::kPrimFloat:
4000 case Primitive::kPrimDouble:
4001 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004002 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004003
4004 default:
4005 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4006 }
4007}
4008
Scott Wakelingfe885462016-09-22 10:24:38 +01004009void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4010 DCHECK(instruction->IsDiv() || instruction->IsRem());
4011 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4012
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004013 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004014 DCHECK(second.IsConstant());
4015
4016 vixl32::Register out = OutputRegister(instruction);
4017 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004018 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 DCHECK(imm == 1 || imm == -1);
4020
4021 if (instruction->IsRem()) {
4022 __ Mov(out, 0);
4023 } else {
4024 if (imm == 1) {
4025 __ Mov(out, dividend);
4026 } else {
4027 __ Rsb(out, dividend, 0);
4028 }
4029 }
4030}
4031
4032void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4033 DCHECK(instruction->IsDiv() || instruction->IsRem());
4034 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4035
4036 LocationSummary* locations = instruction->GetLocations();
4037 Location second = locations->InAt(1);
4038 DCHECK(second.IsConstant());
4039
4040 vixl32::Register out = OutputRegister(instruction);
4041 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004042 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004043 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004044 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4045 int ctz_imm = CTZ(abs_imm);
4046
4047 if (ctz_imm == 1) {
4048 __ Lsr(temp, dividend, 32 - ctz_imm);
4049 } else {
4050 __ Asr(temp, dividend, 31);
4051 __ Lsr(temp, temp, 32 - ctz_imm);
4052 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004053 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004054
4055 if (instruction->IsDiv()) {
4056 __ Asr(out, out, ctz_imm);
4057 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004058 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004059 }
4060 } else {
4061 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004062 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004063 }
4064}
4065
4066void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4067 DCHECK(instruction->IsDiv() || instruction->IsRem());
4068 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4069
4070 LocationSummary* locations = instruction->GetLocations();
4071 Location second = locations->InAt(1);
4072 DCHECK(second.IsConstant());
4073
4074 vixl32::Register out = OutputRegister(instruction);
4075 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004076 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4077 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004078 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004079
4080 int64_t magic;
4081 int shift;
4082 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4083
Anton Kirilovdda43962016-11-21 19:55:20 +00004084 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4085 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004086 __ Smull(temp2, temp1, dividend, temp1);
4087
4088 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004089 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004090 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004091 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004092 }
4093
4094 if (shift != 0) {
4095 __ Asr(temp1, temp1, shift);
4096 }
4097
4098 if (instruction->IsDiv()) {
4099 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4100 } else {
4101 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4102 // TODO: Strength reduction for mls.
4103 __ Mov(temp2, imm);
4104 __ Mls(out, temp1, temp2, dividend);
4105 }
4106}
4107
4108void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4109 HBinaryOperation* instruction) {
4110 DCHECK(instruction->IsDiv() || instruction->IsRem());
4111 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4112
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004113 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004114 DCHECK(second.IsConstant());
4115
Anton Kirilov644032c2016-12-06 17:51:43 +00004116 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004117 if (imm == 0) {
4118 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4119 } else if (imm == 1 || imm == -1) {
4120 DivRemOneOrMinusOne(instruction);
4121 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4122 DivRemByPowerOfTwo(instruction);
4123 } else {
4124 DCHECK(imm <= -2 || imm >= 2);
4125 GenerateDivRemWithAnyConstant(instruction);
4126 }
4127}
4128
4129void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4130 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4131 if (div->GetResultType() == Primitive::kPrimLong) {
4132 // pLdiv runtime call.
4133 call_kind = LocationSummary::kCallOnMainOnly;
4134 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4135 // sdiv will be replaced by other instruction sequence.
4136 } else if (div->GetResultType() == Primitive::kPrimInt &&
4137 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4138 // pIdivmod runtime call.
4139 call_kind = LocationSummary::kCallOnMainOnly;
4140 }
4141
4142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4143
4144 switch (div->GetResultType()) {
4145 case Primitive::kPrimInt: {
4146 if (div->InputAt(1)->IsConstant()) {
4147 locations->SetInAt(0, Location::RequiresRegister());
4148 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004150 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004151 if (value == 1 || value == 0 || value == -1) {
4152 // No temp register required.
4153 } else {
4154 locations->AddTemp(Location::RequiresRegister());
4155 if (!IsPowerOfTwo(AbsOrMin(value))) {
4156 locations->AddTemp(Location::RequiresRegister());
4157 }
4158 }
4159 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4160 locations->SetInAt(0, Location::RequiresRegister());
4161 locations->SetInAt(1, Location::RequiresRegister());
4162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4163 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004164 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4165 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4166 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004167 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004168 // we only need the former.
4169 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004170 }
4171 break;
4172 }
4173 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004174 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4175 locations->SetInAt(0, LocationFrom(
4176 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4177 locations->SetInAt(1, LocationFrom(
4178 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4179 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004180 break;
4181 }
4182 case Primitive::kPrimFloat:
4183 case Primitive::kPrimDouble: {
4184 locations->SetInAt(0, Location::RequiresFpuRegister());
4185 locations->SetInAt(1, Location::RequiresFpuRegister());
4186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4187 break;
4188 }
4189
4190 default:
4191 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4192 }
4193}
4194
4195void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004196 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004197 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004198
4199 switch (div->GetResultType()) {
4200 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004201 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004202 GenerateDivRemConstantIntegral(div);
4203 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4204 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4205 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004206 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4207 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4208 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4209 DCHECK(r0.Is(OutputRegister(div)));
4210
4211 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4212 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004213 }
4214 break;
4215 }
4216
4217 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004218 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4219 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4220 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4221 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4222 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4223 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4224 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4225
4226 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4227 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004228 break;
4229 }
4230
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004231 case Primitive::kPrimFloat:
4232 case Primitive::kPrimDouble:
4233 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004234 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004235
4236 default:
4237 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4238 }
4239}
4240
Artem Serov551b28f2016-10-18 19:11:30 +01004241void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4242 Primitive::Type type = rem->GetResultType();
4243
4244 // Most remainders are implemented in the runtime.
4245 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4246 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4247 // sdiv will be replaced by other instruction sequence.
4248 call_kind = LocationSummary::kNoCall;
4249 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4250 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4251 // Have hardware divide instruction for int, do it with three instructions.
4252 call_kind = LocationSummary::kNoCall;
4253 }
4254
4255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4256
4257 switch (type) {
4258 case Primitive::kPrimInt: {
4259 if (rem->InputAt(1)->IsConstant()) {
4260 locations->SetInAt(0, Location::RequiresRegister());
4261 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004263 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004264 if (value == 1 || value == 0 || value == -1) {
4265 // No temp register required.
4266 } else {
4267 locations->AddTemp(Location::RequiresRegister());
4268 if (!IsPowerOfTwo(AbsOrMin(value))) {
4269 locations->AddTemp(Location::RequiresRegister());
4270 }
4271 }
4272 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4273 locations->SetInAt(0, Location::RequiresRegister());
4274 locations->SetInAt(1, Location::RequiresRegister());
4275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4276 locations->AddTemp(Location::RequiresRegister());
4277 } else {
4278 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4279 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4280 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004281 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004282 // we only need the latter.
4283 locations->SetOut(LocationFrom(r1));
4284 }
4285 break;
4286 }
4287 case Primitive::kPrimLong: {
4288 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4289 locations->SetInAt(0, LocationFrom(
4290 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4291 locations->SetInAt(1, LocationFrom(
4292 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4293 // The runtime helper puts the output in R2,R3.
4294 locations->SetOut(LocationFrom(r2, r3));
4295 break;
4296 }
4297 case Primitive::kPrimFloat: {
4298 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4299 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4300 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4301 locations->SetOut(LocationFrom(s0));
4302 break;
4303 }
4304
4305 case Primitive::kPrimDouble: {
4306 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4307 locations->SetInAt(0, LocationFrom(
4308 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4309 locations->SetInAt(1, LocationFrom(
4310 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4311 locations->SetOut(LocationFrom(s0, s1));
4312 break;
4313 }
4314
4315 default:
4316 LOG(FATAL) << "Unexpected rem type " << type;
4317 }
4318}
4319
4320void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4321 LocationSummary* locations = rem->GetLocations();
4322 Location second = locations->InAt(1);
4323
4324 Primitive::Type type = rem->GetResultType();
4325 switch (type) {
4326 case Primitive::kPrimInt: {
4327 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4328 vixl32::Register out_reg = OutputRegister(rem);
4329 if (second.IsConstant()) {
4330 GenerateDivRemConstantIntegral(rem);
4331 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4332 vixl32::Register reg2 = RegisterFrom(second);
4333 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4334
4335 // temp = reg1 / reg2 (integer division)
4336 // dest = reg1 - temp * reg2
4337 __ Sdiv(temp, reg1, reg2);
4338 __ Mls(out_reg, temp, reg2, reg1);
4339 } else {
4340 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4341 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4342 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4343 DCHECK(out_reg.Is(r1));
4344
4345 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4346 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4347 }
4348 break;
4349 }
4350
4351 case Primitive::kPrimLong: {
4352 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4353 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4354 break;
4355 }
4356
4357 case Primitive::kPrimFloat: {
4358 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4359 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4360 break;
4361 }
4362
4363 case Primitive::kPrimDouble: {
4364 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4365 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4366 break;
4367 }
4368
4369 default:
4370 LOG(FATAL) << "Unexpected rem type " << type;
4371 }
4372}
4373
4374
Scott Wakelingfe885462016-09-22 10:24:38 +01004375void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004376 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004377 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004378}
4379
4380void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4381 DivZeroCheckSlowPathARMVIXL* slow_path =
4382 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4383 codegen_->AddSlowPath(slow_path);
4384
4385 LocationSummary* locations = instruction->GetLocations();
4386 Location value = locations->InAt(0);
4387
4388 switch (instruction->GetType()) {
4389 case Primitive::kPrimBoolean:
4390 case Primitive::kPrimByte:
4391 case Primitive::kPrimChar:
4392 case Primitive::kPrimShort:
4393 case Primitive::kPrimInt: {
4394 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004395 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004396 } else {
4397 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004398 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004399 __ B(slow_path->GetEntryLabel());
4400 }
4401 }
4402 break;
4403 }
4404 case Primitive::kPrimLong: {
4405 if (value.IsRegisterPair()) {
4406 UseScratchRegisterScope temps(GetVIXLAssembler());
4407 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004408 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004409 __ B(eq, slow_path->GetEntryLabel());
4410 } else {
4411 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004412 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004413 __ B(slow_path->GetEntryLabel());
4414 }
4415 }
4416 break;
4417 }
4418 default:
4419 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4420 }
4421}
4422
Artem Serov02109dd2016-09-23 17:17:54 +01004423void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4424 LocationSummary* locations = ror->GetLocations();
4425 vixl32::Register in = InputRegisterAt(ror, 0);
4426 Location rhs = locations->InAt(1);
4427 vixl32::Register out = OutputRegister(ror);
4428
4429 if (rhs.IsConstant()) {
4430 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4431 // so map all rotations to a +ve. equivalent in that range.
4432 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4433 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4434 if (rot) {
4435 // Rotate, mapping left rotations to right equivalents if necessary.
4436 // (e.g. left by 2 bits == right by 30.)
4437 __ Ror(out, in, rot);
4438 } else if (!out.Is(in)) {
4439 __ Mov(out, in);
4440 }
4441 } else {
4442 __ Ror(out, in, RegisterFrom(rhs));
4443 }
4444}
4445
4446// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4447// rotates by swapping input regs (effectively rotating by the first 32-bits of
4448// a larger rotation) or flipping direction (thus treating larger right/left
4449// rotations as sub-word sized rotations in the other direction) as appropriate.
4450void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4451 LocationSummary* locations = ror->GetLocations();
4452 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4453 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4454 Location rhs = locations->InAt(1);
4455 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4456 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4457
4458 if (rhs.IsConstant()) {
4459 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4460 // Map all rotations to +ve. equivalents on the interval [0,63].
4461 rot &= kMaxLongShiftDistance;
4462 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4463 // logic below to a simple pair of binary orr.
4464 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4465 if (rot >= kArmBitsPerWord) {
4466 rot -= kArmBitsPerWord;
4467 std::swap(in_reg_hi, in_reg_lo);
4468 }
4469 // Rotate, or mov to out for zero or word size rotations.
4470 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004471 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004472 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004473 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004474 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4475 } else {
4476 __ Mov(out_reg_lo, in_reg_lo);
4477 __ Mov(out_reg_hi, in_reg_hi);
4478 }
4479 } else {
4480 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4481 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4482 vixl32::Label end;
4483 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004484 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004485
4486 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4487 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004488 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004489 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004490
4491 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4492 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4493 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4494 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4495 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4496 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4497 __ Lsr(shift_left, in_reg_hi, shift_right);
4498 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004499 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004500
4501 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4502 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4503 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4504 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4505 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4506 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4507 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4508 __ Lsl(shift_right, in_reg_hi, shift_left);
4509 __ Add(out_reg_lo, out_reg_lo, shift_right);
4510
Anton Kirilov6f644202017-02-27 18:29:45 +00004511 if (end.IsReferenced()) {
4512 __ Bind(&end);
4513 }
Artem Serov02109dd2016-09-23 17:17:54 +01004514 }
4515}
4516
4517void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4518 LocationSummary* locations =
4519 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4520 switch (ror->GetResultType()) {
4521 case Primitive::kPrimInt: {
4522 locations->SetInAt(0, Location::RequiresRegister());
4523 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4525 break;
4526 }
4527 case Primitive::kPrimLong: {
4528 locations->SetInAt(0, Location::RequiresRegister());
4529 if (ror->InputAt(1)->IsConstant()) {
4530 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4531 } else {
4532 locations->SetInAt(1, Location::RequiresRegister());
4533 locations->AddTemp(Location::RequiresRegister());
4534 locations->AddTemp(Location::RequiresRegister());
4535 }
4536 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4537 break;
4538 }
4539 default:
4540 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4541 }
4542}
4543
4544void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4545 Primitive::Type type = ror->GetResultType();
4546 switch (type) {
4547 case Primitive::kPrimInt: {
4548 HandleIntegerRotate(ror);
4549 break;
4550 }
4551 case Primitive::kPrimLong: {
4552 HandleLongRotate(ror);
4553 break;
4554 }
4555 default:
4556 LOG(FATAL) << "Unexpected operation type " << type;
4557 UNREACHABLE();
4558 }
4559}
4560
Artem Serov02d37832016-10-25 15:25:33 +01004561void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4562 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4563
4564 LocationSummary* locations =
4565 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4566
4567 switch (op->GetResultType()) {
4568 case Primitive::kPrimInt: {
4569 locations->SetInAt(0, Location::RequiresRegister());
4570 if (op->InputAt(1)->IsConstant()) {
4571 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4573 } else {
4574 locations->SetInAt(1, Location::RequiresRegister());
4575 // Make the output overlap, as it will be used to hold the masked
4576 // second input.
4577 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4578 }
4579 break;
4580 }
4581 case Primitive::kPrimLong: {
4582 locations->SetInAt(0, Location::RequiresRegister());
4583 if (op->InputAt(1)->IsConstant()) {
4584 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4585 // For simplicity, use kOutputOverlap even though we only require that low registers
4586 // don't clash with high registers which the register allocator currently guarantees.
4587 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4588 } else {
4589 locations->SetInAt(1, Location::RequiresRegister());
4590 locations->AddTemp(Location::RequiresRegister());
4591 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4592 }
4593 break;
4594 }
4595 default:
4596 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4597 }
4598}
4599
4600void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4601 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4602
4603 LocationSummary* locations = op->GetLocations();
4604 Location out = locations->Out();
4605 Location first = locations->InAt(0);
4606 Location second = locations->InAt(1);
4607
4608 Primitive::Type type = op->GetResultType();
4609 switch (type) {
4610 case Primitive::kPrimInt: {
4611 vixl32::Register out_reg = OutputRegister(op);
4612 vixl32::Register first_reg = InputRegisterAt(op, 0);
4613 if (second.IsRegister()) {
4614 vixl32::Register second_reg = RegisterFrom(second);
4615 // ARM doesn't mask the shift count so we need to do it ourselves.
4616 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4617 if (op->IsShl()) {
4618 __ Lsl(out_reg, first_reg, out_reg);
4619 } else if (op->IsShr()) {
4620 __ Asr(out_reg, first_reg, out_reg);
4621 } else {
4622 __ Lsr(out_reg, first_reg, out_reg);
4623 }
4624 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004625 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004626 uint32_t shift_value = cst & kMaxIntShiftDistance;
4627 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4628 __ Mov(out_reg, first_reg);
4629 } else if (op->IsShl()) {
4630 __ Lsl(out_reg, first_reg, shift_value);
4631 } else if (op->IsShr()) {
4632 __ Asr(out_reg, first_reg, shift_value);
4633 } else {
4634 __ Lsr(out_reg, first_reg, shift_value);
4635 }
4636 }
4637 break;
4638 }
4639 case Primitive::kPrimLong: {
4640 vixl32::Register o_h = HighRegisterFrom(out);
4641 vixl32::Register o_l = LowRegisterFrom(out);
4642
4643 vixl32::Register high = HighRegisterFrom(first);
4644 vixl32::Register low = LowRegisterFrom(first);
4645
4646 if (second.IsRegister()) {
4647 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4648
4649 vixl32::Register second_reg = RegisterFrom(second);
4650
4651 if (op->IsShl()) {
4652 __ And(o_l, second_reg, kMaxLongShiftDistance);
4653 // Shift the high part
4654 __ Lsl(o_h, high, o_l);
4655 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004656 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004657 __ Lsr(temp, low, temp);
4658 __ Orr(o_h, o_h, temp);
4659 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004660 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004661 {
Artem Serov0fb37192016-12-06 18:13:40 +00004662 ExactAssemblyScope guard(GetVIXLAssembler(),
4663 2 * vixl32::kMaxInstructionSizeInBytes,
4664 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004665 __ it(pl);
4666 __ lsl(pl, o_h, low, temp);
4667 }
4668 // Shift the low part
4669 __ Lsl(o_l, low, o_l);
4670 } else if (op->IsShr()) {
4671 __ And(o_h, second_reg, kMaxLongShiftDistance);
4672 // Shift the low part
4673 __ Lsr(o_l, low, o_h);
4674 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004675 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004676 __ Lsl(temp, high, temp);
4677 __ Orr(o_l, o_l, temp);
4678 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004679 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004680 {
Artem Serov0fb37192016-12-06 18:13:40 +00004681 ExactAssemblyScope guard(GetVIXLAssembler(),
4682 2 * vixl32::kMaxInstructionSizeInBytes,
4683 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004684 __ it(pl);
4685 __ asr(pl, o_l, high, temp);
4686 }
4687 // Shift the high part
4688 __ Asr(o_h, high, o_h);
4689 } else {
4690 __ And(o_h, second_reg, kMaxLongShiftDistance);
4691 // same as Shr except we use `Lsr`s and not `Asr`s
4692 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004693 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004694 __ Lsl(temp, high, temp);
4695 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004696 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004697 {
Artem Serov0fb37192016-12-06 18:13:40 +00004698 ExactAssemblyScope guard(GetVIXLAssembler(),
4699 2 * vixl32::kMaxInstructionSizeInBytes,
4700 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004701 __ it(pl);
4702 __ lsr(pl, o_l, high, temp);
4703 }
4704 __ Lsr(o_h, high, o_h);
4705 }
4706 } else {
4707 // Register allocator doesn't create partial overlap.
4708 DCHECK(!o_l.Is(high));
4709 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004710 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004711 uint32_t shift_value = cst & kMaxLongShiftDistance;
4712 if (shift_value > 32) {
4713 if (op->IsShl()) {
4714 __ Lsl(o_h, low, shift_value - 32);
4715 __ Mov(o_l, 0);
4716 } else if (op->IsShr()) {
4717 __ Asr(o_l, high, shift_value - 32);
4718 __ Asr(o_h, high, 31);
4719 } else {
4720 __ Lsr(o_l, high, shift_value - 32);
4721 __ Mov(o_h, 0);
4722 }
4723 } else if (shift_value == 32) {
4724 if (op->IsShl()) {
4725 __ Mov(o_h, low);
4726 __ Mov(o_l, 0);
4727 } else if (op->IsShr()) {
4728 __ Mov(o_l, high);
4729 __ Asr(o_h, high, 31);
4730 } else {
4731 __ Mov(o_l, high);
4732 __ Mov(o_h, 0);
4733 }
4734 } else if (shift_value == 1) {
4735 if (op->IsShl()) {
4736 __ Lsls(o_l, low, 1);
4737 __ Adc(o_h, high, high);
4738 } else if (op->IsShr()) {
4739 __ Asrs(o_h, high, 1);
4740 __ Rrx(o_l, low);
4741 } else {
4742 __ Lsrs(o_h, high, 1);
4743 __ Rrx(o_l, low);
4744 }
4745 } else {
4746 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4747 if (op->IsShl()) {
4748 __ Lsl(o_h, high, shift_value);
4749 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4750 __ Lsl(o_l, low, shift_value);
4751 } else if (op->IsShr()) {
4752 __ Lsr(o_l, low, shift_value);
4753 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4754 __ Asr(o_h, high, shift_value);
4755 } else {
4756 __ Lsr(o_l, low, shift_value);
4757 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4758 __ Lsr(o_h, high, shift_value);
4759 }
4760 }
4761 }
4762 break;
4763 }
4764 default:
4765 LOG(FATAL) << "Unexpected operation type " << type;
4766 UNREACHABLE();
4767 }
4768}
4769
4770void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4771 HandleShift(shl);
4772}
4773
4774void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4775 HandleShift(shl);
4776}
4777
4778void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4779 HandleShift(shr);
4780}
4781
4782void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4783 HandleShift(shr);
4784}
4785
4786void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4787 HandleShift(ushr);
4788}
4789
4790void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4791 HandleShift(ushr);
4792}
4793
4794void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4795 LocationSummary* locations =
4796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4797 if (instruction->IsStringAlloc()) {
4798 locations->AddTemp(LocationFrom(kMethodRegister));
4799 } else {
4800 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4801 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004802 }
4803 locations->SetOut(LocationFrom(r0));
4804}
4805
4806void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4807 // Note: if heap poisoning is enabled, the entry point takes cares
4808 // of poisoning the reference.
4809 if (instruction->IsStringAlloc()) {
4810 // String is allocated through StringFactory. Call NewEmptyString entry point.
4811 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4812 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4813 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4814 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004815 // 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 +00004816 ExactAssemblyScope aas(GetVIXLAssembler(),
4817 vixl32::k16BitT32InstructionSizeInBytes,
4818 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004819 __ blx(lr);
4820 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4821 } else {
4822 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004823 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004824 }
4825}
4826
4827void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4828 LocationSummary* locations =
4829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4830 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004831 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004832 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4833 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004834}
4835
4836void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004837 // Note: if heap poisoning is enabled, the entry point takes cares
4838 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004839 QuickEntrypointEnum entrypoint =
4840 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4841 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004842 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004843 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004844}
4845
4846void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4847 LocationSummary* locations =
4848 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4849 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4850 if (location.IsStackSlot()) {
4851 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4852 } else if (location.IsDoubleStackSlot()) {
4853 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4854 }
4855 locations->SetOut(location);
4856}
4857
4858void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4859 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4860 // Nothing to do, the parameter is already at its location.
4861}
4862
4863void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4866 locations->SetOut(LocationFrom(kMethodRegister));
4867}
4868
4869void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4870 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4871 // Nothing to do, the method is already at its location.
4872}
4873
4874void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4875 LocationSummary* locations =
4876 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4877 locations->SetInAt(0, Location::RequiresRegister());
4878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4879}
4880
4881void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4882 LocationSummary* locations = not_->GetLocations();
4883 Location out = locations->Out();
4884 Location in = locations->InAt(0);
4885 switch (not_->GetResultType()) {
4886 case Primitive::kPrimInt:
4887 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4888 break;
4889
4890 case Primitive::kPrimLong:
4891 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4892 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4893 break;
4894
4895 default:
4896 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4897 }
4898}
4899
Scott Wakelingc34dba72016-10-03 10:14:44 +01004900void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4901 LocationSummary* locations =
4902 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4903 locations->SetInAt(0, Location::RequiresRegister());
4904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4905}
4906
4907void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4908 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4909}
4910
Artem Serov02d37832016-10-25 15:25:33 +01004911void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4912 LocationSummary* locations =
4913 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4914 switch (compare->InputAt(0)->GetType()) {
4915 case Primitive::kPrimBoolean:
4916 case Primitive::kPrimByte:
4917 case Primitive::kPrimShort:
4918 case Primitive::kPrimChar:
4919 case Primitive::kPrimInt:
4920 case Primitive::kPrimLong: {
4921 locations->SetInAt(0, Location::RequiresRegister());
4922 locations->SetInAt(1, Location::RequiresRegister());
4923 // Output overlaps because it is written before doing the low comparison.
4924 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4925 break;
4926 }
4927 case Primitive::kPrimFloat:
4928 case Primitive::kPrimDouble: {
4929 locations->SetInAt(0, Location::RequiresFpuRegister());
4930 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4931 locations->SetOut(Location::RequiresRegister());
4932 break;
4933 }
4934 default:
4935 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4936 }
4937}
4938
4939void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4940 LocationSummary* locations = compare->GetLocations();
4941 vixl32::Register out = OutputRegister(compare);
4942 Location left = locations->InAt(0);
4943 Location right = locations->InAt(1);
4944
4945 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004946 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01004947 Primitive::Type type = compare->InputAt(0)->GetType();
4948 vixl32::Condition less_cond = vixl32::Condition(kNone);
4949 switch (type) {
4950 case Primitive::kPrimBoolean:
4951 case Primitive::kPrimByte:
4952 case Primitive::kPrimShort:
4953 case Primitive::kPrimChar:
4954 case Primitive::kPrimInt: {
4955 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4956 __ Mov(out, 0);
4957 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4958 less_cond = lt;
4959 break;
4960 }
4961 case Primitive::kPrimLong: {
4962 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004963 __ B(lt, &less, /* far_target */ false);
4964 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004965 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4966 __ Mov(out, 0);
4967 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4968 less_cond = lo;
4969 break;
4970 }
4971 case Primitive::kPrimFloat:
4972 case Primitive::kPrimDouble: {
4973 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004974 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01004975 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4976 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4977 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4978 break;
4979 }
4980 default:
4981 LOG(FATAL) << "Unexpected compare type " << type;
4982 UNREACHABLE();
4983 }
4984
Anton Kirilov6f644202017-02-27 18:29:45 +00004985 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00004986 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004987
4988 __ Bind(&greater);
4989 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00004990 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01004991
4992 __ Bind(&less);
4993 __ Mov(out, -1);
4994
Anton Kirilov6f644202017-02-27 18:29:45 +00004995 if (done.IsReferenced()) {
4996 __ Bind(&done);
4997 }
Artem Serov02d37832016-10-25 15:25:33 +01004998}
4999
5000void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5001 LocationSummary* locations =
5002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5003 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5004 locations->SetInAt(i, Location::Any());
5005 }
5006 locations->SetOut(Location::Any());
5007}
5008
5009void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5010 LOG(FATAL) << "Unreachable";
5011}
5012
5013void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5014 // TODO (ported from quick): revisit ARM barrier kinds.
5015 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5016 switch (kind) {
5017 case MemBarrierKind::kAnyStore:
5018 case MemBarrierKind::kLoadAny:
5019 case MemBarrierKind::kAnyAny: {
5020 flavor = DmbOptions::ISH;
5021 break;
5022 }
5023 case MemBarrierKind::kStoreStore: {
5024 flavor = DmbOptions::ISHST;
5025 break;
5026 }
5027 default:
5028 LOG(FATAL) << "Unexpected memory barrier " << kind;
5029 }
5030 __ Dmb(flavor);
5031}
5032
5033void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5034 uint32_t offset,
5035 vixl32::Register out_lo,
5036 vixl32::Register out_hi) {
5037 UseScratchRegisterScope temps(GetVIXLAssembler());
5038 if (offset != 0) {
5039 vixl32::Register temp = temps.Acquire();
5040 __ Add(temp, addr, offset);
5041 addr = temp;
5042 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005043 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005044}
5045
5046void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5047 uint32_t offset,
5048 vixl32::Register value_lo,
5049 vixl32::Register value_hi,
5050 vixl32::Register temp1,
5051 vixl32::Register temp2,
5052 HInstruction* instruction) {
5053 UseScratchRegisterScope temps(GetVIXLAssembler());
5054 vixl32::Label fail;
5055 if (offset != 0) {
5056 vixl32::Register temp = temps.Acquire();
5057 __ Add(temp, addr, offset);
5058 addr = temp;
5059 }
5060 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005061 {
5062 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005063 ExactAssemblyScope aas(GetVIXLAssembler(),
5064 vixl32::kMaxInstructionSizeInBytes,
5065 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005066 // We need a load followed by store. (The address used in a STREX instruction must
5067 // be the same as the address in the most recently executed LDREX instruction.)
5068 __ ldrexd(temp1, temp2, MemOperand(addr));
5069 codegen_->MaybeRecordImplicitNullCheck(instruction);
5070 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005071 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005072 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005073}
Artem Serov02109dd2016-09-23 17:17:54 +01005074
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005075void LocationsBuilderARMVIXL::HandleFieldSet(
5076 HInstruction* instruction, const FieldInfo& field_info) {
5077 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5078
5079 LocationSummary* locations =
5080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5081 locations->SetInAt(0, Location::RequiresRegister());
5082
5083 Primitive::Type field_type = field_info.GetFieldType();
5084 if (Primitive::IsFloatingPointType(field_type)) {
5085 locations->SetInAt(1, Location::RequiresFpuRegister());
5086 } else {
5087 locations->SetInAt(1, Location::RequiresRegister());
5088 }
5089
5090 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5091 bool generate_volatile = field_info.IsVolatile()
5092 && is_wide
5093 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5094 bool needs_write_barrier =
5095 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5096 // Temporary registers for the write barrier.
5097 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5098 if (needs_write_barrier) {
5099 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5100 locations->AddTemp(Location::RequiresRegister());
5101 } else if (generate_volatile) {
5102 // ARM encoding have some additional constraints for ldrexd/strexd:
5103 // - registers need to be consecutive
5104 // - the first register should be even but not R14.
5105 // We don't test for ARM yet, and the assertion makes sure that we
5106 // revisit this if we ever enable ARM encoding.
5107 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5108
5109 locations->AddTemp(Location::RequiresRegister());
5110 locations->AddTemp(Location::RequiresRegister());
5111 if (field_type == Primitive::kPrimDouble) {
5112 // For doubles we need two more registers to copy the value.
5113 locations->AddTemp(LocationFrom(r2));
5114 locations->AddTemp(LocationFrom(r3));
5115 }
5116 }
5117}
5118
5119void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5120 const FieldInfo& field_info,
5121 bool value_can_be_null) {
5122 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5123
5124 LocationSummary* locations = instruction->GetLocations();
5125 vixl32::Register base = InputRegisterAt(instruction, 0);
5126 Location value = locations->InAt(1);
5127
5128 bool is_volatile = field_info.IsVolatile();
5129 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5130 Primitive::Type field_type = field_info.GetFieldType();
5131 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5132 bool needs_write_barrier =
5133 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5134
5135 if (is_volatile) {
5136 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5137 }
5138
5139 switch (field_type) {
5140 case Primitive::kPrimBoolean:
5141 case Primitive::kPrimByte: {
5142 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5143 break;
5144 }
5145
5146 case Primitive::kPrimShort:
5147 case Primitive::kPrimChar: {
5148 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5149 break;
5150 }
5151
5152 case Primitive::kPrimInt:
5153 case Primitive::kPrimNot: {
5154 if (kPoisonHeapReferences && needs_write_barrier) {
5155 // Note that in the case where `value` is a null reference,
5156 // we do not enter this block, as a null reference does not
5157 // need poisoning.
5158 DCHECK_EQ(field_type, Primitive::kPrimNot);
5159 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5160 __ Mov(temp, RegisterFrom(value));
5161 GetAssembler()->PoisonHeapReference(temp);
5162 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5163 } else {
5164 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5165 }
5166 break;
5167 }
5168
5169 case Primitive::kPrimLong: {
5170 if (is_volatile && !atomic_ldrd_strd) {
5171 GenerateWideAtomicStore(base,
5172 offset,
5173 LowRegisterFrom(value),
5174 HighRegisterFrom(value),
5175 RegisterFrom(locations->GetTemp(0)),
5176 RegisterFrom(locations->GetTemp(1)),
5177 instruction);
5178 } else {
5179 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5180 codegen_->MaybeRecordImplicitNullCheck(instruction);
5181 }
5182 break;
5183 }
5184
5185 case Primitive::kPrimFloat: {
5186 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5187 break;
5188 }
5189
5190 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005191 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005192 if (is_volatile && !atomic_ldrd_strd) {
5193 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5194 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5195
5196 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5197
5198 GenerateWideAtomicStore(base,
5199 offset,
5200 value_reg_lo,
5201 value_reg_hi,
5202 RegisterFrom(locations->GetTemp(2)),
5203 RegisterFrom(locations->GetTemp(3)),
5204 instruction);
5205 } else {
5206 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5207 codegen_->MaybeRecordImplicitNullCheck(instruction);
5208 }
5209 break;
5210 }
5211
5212 case Primitive::kPrimVoid:
5213 LOG(FATAL) << "Unreachable type " << field_type;
5214 UNREACHABLE();
5215 }
5216
5217 // Longs and doubles are handled in the switch.
5218 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005219 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5220 // should use a scope and the assembler to emit the store instruction to guarantee that we
5221 // record the pc at the correct position. But the `Assembler` does not automatically handle
5222 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5223 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005224 codegen_->MaybeRecordImplicitNullCheck(instruction);
5225 }
5226
5227 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5228 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5229 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5230 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5231 }
5232
5233 if (is_volatile) {
5234 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5235 }
5236}
5237
Artem Serov02d37832016-10-25 15:25:33 +01005238void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5239 const FieldInfo& field_info) {
5240 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5241
5242 bool object_field_get_with_read_barrier =
5243 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5244 LocationSummary* locations =
5245 new (GetGraph()->GetArena()) LocationSummary(instruction,
5246 object_field_get_with_read_barrier ?
5247 LocationSummary::kCallOnSlowPath :
5248 LocationSummary::kNoCall);
5249 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5250 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5251 }
5252 locations->SetInAt(0, Location::RequiresRegister());
5253
5254 bool volatile_for_double = field_info.IsVolatile()
5255 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5256 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5257 // The output overlaps in case of volatile long: we don't want the
5258 // code generated by GenerateWideAtomicLoad to overwrite the
5259 // object's location. Likewise, in the case of an object field get
5260 // with read barriers enabled, we do not want the load to overwrite
5261 // the object's location, as we need it to emit the read barrier.
5262 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5263 object_field_get_with_read_barrier;
5264
5265 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5266 locations->SetOut(Location::RequiresFpuRegister());
5267 } else {
5268 locations->SetOut(Location::RequiresRegister(),
5269 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5270 }
5271 if (volatile_for_double) {
5272 // ARM encoding have some additional constraints for ldrexd/strexd:
5273 // - registers need to be consecutive
5274 // - the first register should be even but not R14.
5275 // We don't test for ARM yet, and the assertion makes sure that we
5276 // revisit this if we ever enable ARM encoding.
5277 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5278 locations->AddTemp(Location::RequiresRegister());
5279 locations->AddTemp(Location::RequiresRegister());
5280 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5281 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005282 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01005283 locations->AddTemp(Location::RequiresRegister());
5284 }
5285}
5286
5287Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5288 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5289 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5290 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5291 return Location::ConstantLocation(input->AsConstant());
5292 } else {
5293 return Location::RequiresFpuRegister();
5294 }
5295}
5296
Artem Serov02109dd2016-09-23 17:17:54 +01005297Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5298 Opcode opcode) {
5299 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5300 if (constant->IsConstant() &&
5301 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5302 return Location::ConstantLocation(constant->AsConstant());
5303 }
5304 return Location::RequiresRegister();
5305}
5306
5307bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5308 Opcode opcode) {
5309 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5310 if (Primitive::Is64BitType(input_cst->GetType())) {
5311 Opcode high_opcode = opcode;
5312 SetCc low_set_cc = kCcDontCare;
5313 switch (opcode) {
5314 case SUB:
5315 // Flip the operation to an ADD.
5316 value = -value;
5317 opcode = ADD;
5318 FALLTHROUGH_INTENDED;
5319 case ADD:
5320 if (Low32Bits(value) == 0u) {
5321 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5322 }
5323 high_opcode = ADC;
5324 low_set_cc = kCcSet;
5325 break;
5326 default:
5327 break;
5328 }
5329 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5330 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5331 } else {
5332 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5333 }
5334}
5335
5336// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5337// enabled.
5338bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5339 Opcode opcode,
5340 SetCc set_cc) {
5341 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5342 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5343 return true;
5344 }
5345 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005346 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005347 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005348 case AND: neg_opcode = BIC; neg_value = ~value; break;
5349 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5350 case ADD: neg_opcode = SUB; neg_value = -value; break;
5351 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5352 case SUB: neg_opcode = ADD; neg_value = -value; break;
5353 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5354 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005355 default:
5356 return false;
5357 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005358
5359 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5360 return true;
5361 }
5362
5363 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005364}
5365
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005366void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5367 const FieldInfo& field_info) {
5368 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5369
5370 LocationSummary* locations = instruction->GetLocations();
5371 vixl32::Register base = InputRegisterAt(instruction, 0);
5372 Location out = locations->Out();
5373 bool is_volatile = field_info.IsVolatile();
5374 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5375 Primitive::Type field_type = field_info.GetFieldType();
5376 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5377
5378 switch (field_type) {
5379 case Primitive::kPrimBoolean:
5380 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5381 break;
5382
5383 case Primitive::kPrimByte:
5384 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5385 break;
5386
5387 case Primitive::kPrimShort:
5388 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5389 break;
5390
5391 case Primitive::kPrimChar:
5392 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5393 break;
5394
5395 case Primitive::kPrimInt:
5396 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5397 break;
5398
5399 case Primitive::kPrimNot: {
5400 // /* HeapReference<Object> */ out = *(base + offset)
5401 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005402 Location temp_loc = locations->GetTemp(0);
5403 // Note that a potential implicit null check is handled in this
5404 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5405 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5406 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5407 if (is_volatile) {
5408 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5409 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005410 } else {
5411 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005412 codegen_->MaybeRecordImplicitNullCheck(instruction);
5413 if (is_volatile) {
5414 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5415 }
5416 // If read barriers are enabled, emit read barriers other than
5417 // Baker's using a slow path (and also unpoison the loaded
5418 // reference, if heap poisoning is enabled).
5419 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5420 }
5421 break;
5422 }
5423
5424 case Primitive::kPrimLong:
5425 if (is_volatile && !atomic_ldrd_strd) {
5426 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5427 } else {
5428 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5429 }
5430 break;
5431
5432 case Primitive::kPrimFloat:
5433 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5434 break;
5435
5436 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005437 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005438 if (is_volatile && !atomic_ldrd_strd) {
5439 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5440 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5441 GenerateWideAtomicLoad(base, offset, lo, hi);
5442 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5443 // scope.
5444 codegen_->MaybeRecordImplicitNullCheck(instruction);
5445 __ Vmov(out_dreg, lo, hi);
5446 } else {
5447 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005448 codegen_->MaybeRecordImplicitNullCheck(instruction);
5449 }
5450 break;
5451 }
5452
5453 case Primitive::kPrimVoid:
5454 LOG(FATAL) << "Unreachable type " << field_type;
5455 UNREACHABLE();
5456 }
5457
5458 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5459 // Potential implicit null checks, in the case of reference or
5460 // double fields, are handled in the previous switch statement.
5461 } else {
5462 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005463 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5464 // should use a scope and the assembler to emit the load instruction to guarantee that we
5465 // record the pc at the correct position. But the `Assembler` does not automatically handle
5466 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5467 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005468 codegen_->MaybeRecordImplicitNullCheck(instruction);
5469 }
5470
5471 if (is_volatile) {
5472 if (field_type == Primitive::kPrimNot) {
5473 // Memory barriers, in the case of references, are also handled
5474 // in the previous switch statement.
5475 } else {
5476 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5477 }
5478 }
5479}
5480
5481void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5482 HandleFieldSet(instruction, instruction->GetFieldInfo());
5483}
5484
5485void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5486 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5487}
5488
5489void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5490 HandleFieldGet(instruction, instruction->GetFieldInfo());
5491}
5492
5493void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5494 HandleFieldGet(instruction, instruction->GetFieldInfo());
5495}
5496
5497void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5498 HandleFieldGet(instruction, instruction->GetFieldInfo());
5499}
5500
5501void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5502 HandleFieldGet(instruction, instruction->GetFieldInfo());
5503}
5504
Scott Wakelingc34dba72016-10-03 10:14:44 +01005505void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5506 HandleFieldSet(instruction, instruction->GetFieldInfo());
5507}
5508
5509void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5510 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5511}
5512
Artem Serovcfbe9132016-10-14 15:58:56 +01005513void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5514 HUnresolvedInstanceFieldGet* instruction) {
5515 FieldAccessCallingConventionARMVIXL calling_convention;
5516 codegen_->CreateUnresolvedFieldLocationSummary(
5517 instruction, instruction->GetFieldType(), calling_convention);
5518}
5519
5520void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5521 HUnresolvedInstanceFieldGet* instruction) {
5522 FieldAccessCallingConventionARMVIXL calling_convention;
5523 codegen_->GenerateUnresolvedFieldAccess(instruction,
5524 instruction->GetFieldType(),
5525 instruction->GetFieldIndex(),
5526 instruction->GetDexPc(),
5527 calling_convention);
5528}
5529
5530void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5531 HUnresolvedInstanceFieldSet* instruction) {
5532 FieldAccessCallingConventionARMVIXL calling_convention;
5533 codegen_->CreateUnresolvedFieldLocationSummary(
5534 instruction, instruction->GetFieldType(), calling_convention);
5535}
5536
5537void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5538 HUnresolvedInstanceFieldSet* instruction) {
5539 FieldAccessCallingConventionARMVIXL calling_convention;
5540 codegen_->GenerateUnresolvedFieldAccess(instruction,
5541 instruction->GetFieldType(),
5542 instruction->GetFieldIndex(),
5543 instruction->GetDexPc(),
5544 calling_convention);
5545}
5546
5547void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5548 HUnresolvedStaticFieldGet* instruction) {
5549 FieldAccessCallingConventionARMVIXL calling_convention;
5550 codegen_->CreateUnresolvedFieldLocationSummary(
5551 instruction, instruction->GetFieldType(), calling_convention);
5552}
5553
5554void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5555 HUnresolvedStaticFieldGet* instruction) {
5556 FieldAccessCallingConventionARMVIXL calling_convention;
5557 codegen_->GenerateUnresolvedFieldAccess(instruction,
5558 instruction->GetFieldType(),
5559 instruction->GetFieldIndex(),
5560 instruction->GetDexPc(),
5561 calling_convention);
5562}
5563
5564void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5565 HUnresolvedStaticFieldSet* instruction) {
5566 FieldAccessCallingConventionARMVIXL calling_convention;
5567 codegen_->CreateUnresolvedFieldLocationSummary(
5568 instruction, instruction->GetFieldType(), calling_convention);
5569}
5570
5571void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5572 HUnresolvedStaticFieldSet* instruction) {
5573 FieldAccessCallingConventionARMVIXL calling_convention;
5574 codegen_->GenerateUnresolvedFieldAccess(instruction,
5575 instruction->GetFieldType(),
5576 instruction->GetFieldIndex(),
5577 instruction->GetDexPc(),
5578 calling_convention);
5579}
5580
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005581void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005582 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005583 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005584}
5585
5586void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5587 if (CanMoveNullCheckToUser(instruction)) {
5588 return;
5589 }
5590
5591 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005592 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005593 ExactAssemblyScope aas(GetVIXLAssembler(),
5594 vixl32::kMaxInstructionSizeInBytes,
5595 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005596 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5597 RecordPcInfo(instruction, instruction->GetDexPc());
5598}
5599
5600void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5601 NullCheckSlowPathARMVIXL* slow_path =
5602 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5603 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005604 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005605}
5606
5607void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5608 codegen_->GenerateNullCheck(instruction);
5609}
5610
Scott Wakelingc34dba72016-10-03 10:14:44 +01005611static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5612 switch (type) {
5613 case Primitive::kPrimNot:
5614 return kLoadWord;
5615 case Primitive::kPrimBoolean:
5616 return kLoadUnsignedByte;
5617 case Primitive::kPrimByte:
5618 return kLoadSignedByte;
5619 case Primitive::kPrimChar:
5620 return kLoadUnsignedHalfword;
5621 case Primitive::kPrimShort:
5622 return kLoadSignedHalfword;
5623 case Primitive::kPrimInt:
5624 return kLoadWord;
5625 case Primitive::kPrimLong:
5626 return kLoadWordPair;
5627 case Primitive::kPrimFloat:
5628 return kLoadSWord;
5629 case Primitive::kPrimDouble:
5630 return kLoadDWord;
5631 default:
5632 LOG(FATAL) << "Unreachable type " << type;
5633 UNREACHABLE();
5634 }
5635}
5636
5637static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5638 switch (type) {
5639 case Primitive::kPrimNot:
5640 return kStoreWord;
5641 case Primitive::kPrimBoolean:
5642 case Primitive::kPrimByte:
5643 return kStoreByte;
5644 case Primitive::kPrimChar:
5645 case Primitive::kPrimShort:
5646 return kStoreHalfword;
5647 case Primitive::kPrimInt:
5648 return kStoreWord;
5649 case Primitive::kPrimLong:
5650 return kStoreWordPair;
5651 case Primitive::kPrimFloat:
5652 return kStoreSWord;
5653 case Primitive::kPrimDouble:
5654 return kStoreDWord;
5655 default:
5656 LOG(FATAL) << "Unreachable type " << type;
5657 UNREACHABLE();
5658 }
5659}
5660
5661void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5662 Location out_loc,
5663 vixl32::Register base,
5664 vixl32::Register reg_index,
5665 vixl32::Condition cond) {
5666 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5667 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5668
5669 switch (type) {
5670 case Primitive::kPrimByte:
5671 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5672 break;
5673 case Primitive::kPrimBoolean:
5674 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5675 break;
5676 case Primitive::kPrimShort:
5677 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5678 break;
5679 case Primitive::kPrimChar:
5680 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5681 break;
5682 case Primitive::kPrimNot:
5683 case Primitive::kPrimInt:
5684 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5685 break;
5686 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5687 case Primitive::kPrimLong:
5688 case Primitive::kPrimFloat:
5689 case Primitive::kPrimDouble:
5690 default:
5691 LOG(FATAL) << "Unreachable type " << type;
5692 UNREACHABLE();
5693 }
5694}
5695
5696void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5697 Location loc,
5698 vixl32::Register base,
5699 vixl32::Register reg_index,
5700 vixl32::Condition cond) {
5701 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5702 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5703
5704 switch (type) {
5705 case Primitive::kPrimByte:
5706 case Primitive::kPrimBoolean:
5707 __ Strb(cond, RegisterFrom(loc), mem_address);
5708 break;
5709 case Primitive::kPrimShort:
5710 case Primitive::kPrimChar:
5711 __ Strh(cond, RegisterFrom(loc), mem_address);
5712 break;
5713 case Primitive::kPrimNot:
5714 case Primitive::kPrimInt:
5715 __ Str(cond, RegisterFrom(loc), mem_address);
5716 break;
5717 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5718 case Primitive::kPrimLong:
5719 case Primitive::kPrimFloat:
5720 case Primitive::kPrimDouble:
5721 default:
5722 LOG(FATAL) << "Unreachable type " << type;
5723 UNREACHABLE();
5724 }
5725}
5726
5727void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5728 bool object_array_get_with_read_barrier =
5729 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5730 LocationSummary* locations =
5731 new (GetGraph()->GetArena()) LocationSummary(instruction,
5732 object_array_get_with_read_barrier ?
5733 LocationSummary::kCallOnSlowPath :
5734 LocationSummary::kNoCall);
5735 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005736 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005737 }
5738 locations->SetInAt(0, Location::RequiresRegister());
5739 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5740 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5741 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5742 } else {
5743 // The output overlaps in the case of an object array get with
5744 // read barriers enabled: we do not want the move to overwrite the
5745 // array's location, as we need it to emit the read barrier.
5746 locations->SetOut(
5747 Location::RequiresRegister(),
5748 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5749 }
5750 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005751 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005752 // Also need for String compression feature.
5753 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5754 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005755 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005756 }
5757}
5758
5759void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005760 LocationSummary* locations = instruction->GetLocations();
5761 Location obj_loc = locations->InAt(0);
5762 vixl32::Register obj = InputRegisterAt(instruction, 0);
5763 Location index = locations->InAt(1);
5764 Location out_loc = locations->Out();
5765 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5766 Primitive::Type type = instruction->GetType();
5767 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5768 instruction->IsStringCharAt();
5769 HInstruction* array_instr = instruction->GetArray();
5770 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005771
5772 switch (type) {
5773 case Primitive::kPrimBoolean:
5774 case Primitive::kPrimByte:
5775 case Primitive::kPrimShort:
5776 case Primitive::kPrimChar:
5777 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005778 vixl32::Register length;
5779 if (maybe_compressed_char_at) {
5780 length = RegisterFrom(locations->GetTemp(0));
5781 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5782 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5783 codegen_->MaybeRecordImplicitNullCheck(instruction);
5784 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005785 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005786 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005787 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005788 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005789 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005790 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5791 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5792 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005793 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005794 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5795 RegisterFrom(out_loc),
5796 obj,
5797 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005798 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005799 __ Bind(&uncompressed_load);
5800 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5801 RegisterFrom(out_loc),
5802 obj,
5803 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005804 if (done.IsReferenced()) {
5805 __ Bind(&done);
5806 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005807 } else {
5808 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5809
5810 LoadOperandType load_type = GetLoadOperandType(type);
5811 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5812 }
5813 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005814 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005815 vixl32::Register temp = temps.Acquire();
5816
5817 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005818 // We do not need to compute the intermediate address from the array: the
5819 // input instruction has done it already. See the comment in
5820 // `TryExtractArrayAccessAddress()`.
5821 if (kIsDebugBuild) {
5822 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005823 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005824 }
5825 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005826 } else {
5827 __ Add(temp, obj, data_offset);
5828 }
5829 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005830 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005831 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005832 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5833 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5834 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005835 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005836 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005837 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005838 __ Bind(&uncompressed_load);
5839 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005840 if (done.IsReferenced()) {
5841 __ Bind(&done);
5842 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005843 } else {
5844 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5845 }
5846 }
5847 break;
5848 }
5849
5850 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005851 // The read barrier instrumentation of object ArrayGet
5852 // instructions does not support the HIntermediateAddress
5853 // instruction.
5854 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5855
Scott Wakelingc34dba72016-10-03 10:14:44 +01005856 static_assert(
5857 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5858 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5859 // /* HeapReference<Object> */ out =
5860 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5861 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005862 Location temp = locations->GetTemp(0);
5863 // Note that a potential implicit null check is handled in this
5864 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5865 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5866 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005867 } else {
5868 vixl32::Register out = OutputRegister(instruction);
5869 if (index.IsConstant()) {
5870 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005871 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005872 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005873 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5874 // we should use a scope and the assembler to emit the load instruction to guarantee that
5875 // we record the pc at the correct position. But the `Assembler` does not automatically
5876 // handle unencodable offsets. Practically, everything is fine because the helper and
5877 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005878 codegen_->MaybeRecordImplicitNullCheck(instruction);
5879 // If read barriers are enabled, emit read barriers other than
5880 // Baker's using a slow path (and also unpoison the loaded
5881 // reference, if heap poisoning is enabled).
5882 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5883 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005884 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005885 vixl32::Register temp = temps.Acquire();
5886
5887 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005888 // We do not need to compute the intermediate address from the array: the
5889 // input instruction has done it already. See the comment in
5890 // `TryExtractArrayAccessAddress()`.
5891 if (kIsDebugBuild) {
5892 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005893 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005894 }
5895 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005896 } else {
5897 __ Add(temp, obj, data_offset);
5898 }
5899 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005900 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005901 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5902 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5903 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005904 codegen_->MaybeRecordImplicitNullCheck(instruction);
5905 // If read barriers are enabled, emit read barriers other than
5906 // Baker's using a slow path (and also unpoison the loaded
5907 // reference, if heap poisoning is enabled).
5908 codegen_->MaybeGenerateReadBarrierSlow(
5909 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5910 }
5911 }
5912 break;
5913 }
5914
5915 case Primitive::kPrimLong: {
5916 if (index.IsConstant()) {
5917 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005918 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005919 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5920 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005921 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005922 vixl32::Register temp = temps.Acquire();
5923 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5924 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5925 }
5926 break;
5927 }
5928
5929 case Primitive::kPrimFloat: {
5930 vixl32::SRegister out = SRegisterFrom(out_loc);
5931 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005932 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005933 GetAssembler()->LoadSFromOffset(out, obj, offset);
5934 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005935 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005936 vixl32::Register temp = temps.Acquire();
5937 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5938 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5939 }
5940 break;
5941 }
5942
5943 case Primitive::kPrimDouble: {
5944 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005945 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005946 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5947 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005948 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005949 vixl32::Register temp = temps.Acquire();
5950 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5951 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5952 }
5953 break;
5954 }
5955
5956 case Primitive::kPrimVoid:
5957 LOG(FATAL) << "Unreachable type " << type;
5958 UNREACHABLE();
5959 }
5960
5961 if (type == Primitive::kPrimNot) {
5962 // Potential implicit null checks, in the case of reference
5963 // arrays, are handled in the previous switch statement.
5964 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005965 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5966 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005967 codegen_->MaybeRecordImplicitNullCheck(instruction);
5968 }
5969}
5970
5971void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5972 Primitive::Type value_type = instruction->GetComponentType();
5973
5974 bool needs_write_barrier =
5975 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5976 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5977
5978 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5979 instruction,
5980 may_need_runtime_call_for_type_check ?
5981 LocationSummary::kCallOnSlowPath :
5982 LocationSummary::kNoCall);
5983
5984 locations->SetInAt(0, Location::RequiresRegister());
5985 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5986 if (Primitive::IsFloatingPointType(value_type)) {
5987 locations->SetInAt(2, Location::RequiresFpuRegister());
5988 } else {
5989 locations->SetInAt(2, Location::RequiresRegister());
5990 }
5991 if (needs_write_barrier) {
5992 // Temporary registers for the write barrier.
5993 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5994 locations->AddTemp(Location::RequiresRegister());
5995 }
5996}
5997
5998void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005999 LocationSummary* locations = instruction->GetLocations();
6000 vixl32::Register array = InputRegisterAt(instruction, 0);
6001 Location index = locations->InAt(1);
6002 Primitive::Type value_type = instruction->GetComponentType();
6003 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6004 bool needs_write_barrier =
6005 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6006 uint32_t data_offset =
6007 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6008 Location value_loc = locations->InAt(2);
6009 HInstruction* array_instr = instruction->GetArray();
6010 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006011
6012 switch (value_type) {
6013 case Primitive::kPrimBoolean:
6014 case Primitive::kPrimByte:
6015 case Primitive::kPrimShort:
6016 case Primitive::kPrimChar:
6017 case Primitive::kPrimInt: {
6018 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006019 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 uint32_t full_offset =
6021 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6022 StoreOperandType store_type = GetStoreOperandType(value_type);
6023 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6024 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006025 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006026 vixl32::Register temp = temps.Acquire();
6027
6028 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006029 // We do not need to compute the intermediate address from the array: the
6030 // input instruction has done it already. See the comment in
6031 // `TryExtractArrayAccessAddress()`.
6032 if (kIsDebugBuild) {
6033 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006034 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006035 }
6036 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006037 } else {
6038 __ Add(temp, array, data_offset);
6039 }
6040 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6041 }
6042 break;
6043 }
6044
6045 case Primitive::kPrimNot: {
6046 vixl32::Register value = RegisterFrom(value_loc);
6047 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6048 // See the comment in instruction_simplifier_shared.cc.
6049 DCHECK(!has_intermediate_address);
6050
6051 if (instruction->InputAt(2)->IsNullConstant()) {
6052 // Just setting null.
6053 if (index.IsConstant()) {
6054 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006055 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006056 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6057 } else {
6058 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006059 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006060 vixl32::Register temp = temps.Acquire();
6061 __ Add(temp, array, data_offset);
6062 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6063 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006064 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6065 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006066 codegen_->MaybeRecordImplicitNullCheck(instruction);
6067 DCHECK(!needs_write_barrier);
6068 DCHECK(!may_need_runtime_call_for_type_check);
6069 break;
6070 }
6071
6072 DCHECK(needs_write_barrier);
6073 Location temp1_loc = locations->GetTemp(0);
6074 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6075 Location temp2_loc = locations->GetTemp(1);
6076 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6077 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6078 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6079 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6080 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006081 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006082 SlowPathCodeARMVIXL* slow_path = nullptr;
6083
6084 if (may_need_runtime_call_for_type_check) {
6085 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6086 codegen_->AddSlowPath(slow_path);
6087 if (instruction->GetValueCanBeNull()) {
6088 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006089 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006090 if (index.IsConstant()) {
6091 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006092 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6094 } else {
6095 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006096 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006097 vixl32::Register temp = temps.Acquire();
6098 __ Add(temp, array, data_offset);
6099 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6100 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006101 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6102 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006103 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006104 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006105 __ Bind(&non_zero);
6106 }
6107
6108 // Note that when read barriers are enabled, the type checks
6109 // are performed without read barriers. This is fine, even in
6110 // the case where a class object is in the from-space after
6111 // the flip, as a comparison involving such a type would not
6112 // produce a false positive; it may of course produce a false
6113 // negative, in which case we would take the ArraySet slow
6114 // path.
6115
Alexandre Rames374ddf32016-11-04 10:40:49 +00006116 {
6117 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006118 ExactAssemblyScope aas(GetVIXLAssembler(),
6119 vixl32::kMaxInstructionSizeInBytes,
6120 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006121 // /* HeapReference<Class> */ temp1 = array->klass_
6122 __ ldr(temp1, MemOperand(array, class_offset));
6123 codegen_->MaybeRecordImplicitNullCheck(instruction);
6124 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006125 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6126
6127 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6128 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6129 // /* HeapReference<Class> */ temp2 = value->klass_
6130 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6131 // If heap poisoning is enabled, no need to unpoison `temp1`
6132 // nor `temp2`, as we are comparing two poisoned references.
6133 __ Cmp(temp1, temp2);
6134
6135 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6136 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006137 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006138 // If heap poisoning is enabled, the `temp1` reference has
6139 // not been unpoisoned yet; unpoison it now.
6140 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6141
6142 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6143 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6144 // If heap poisoning is enabled, no need to unpoison
6145 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006146 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006147 __ Bind(&do_put);
6148 } else {
6149 __ B(ne, slow_path->GetEntryLabel());
6150 }
6151 }
6152
6153 vixl32::Register source = value;
6154 if (kPoisonHeapReferences) {
6155 // Note that in the case where `value` is a null reference,
6156 // we do not enter this block, as a null reference does not
6157 // need poisoning.
6158 DCHECK_EQ(value_type, Primitive::kPrimNot);
6159 __ Mov(temp1, value);
6160 GetAssembler()->PoisonHeapReference(temp1);
6161 source = temp1;
6162 }
6163
6164 if (index.IsConstant()) {
6165 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006166 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006167 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6168 } else {
6169 DCHECK(index.IsRegister()) << index;
6170
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006171 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006172 vixl32::Register temp = temps.Acquire();
6173 __ Add(temp, array, data_offset);
6174 codegen_->StoreToShiftedRegOffset(value_type,
6175 LocationFrom(source),
6176 temp,
6177 RegisterFrom(index));
6178 }
6179
6180 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006181 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6182 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006183 codegen_->MaybeRecordImplicitNullCheck(instruction);
6184 }
6185
6186 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6187
6188 if (done.IsReferenced()) {
6189 __ Bind(&done);
6190 }
6191
6192 if (slow_path != nullptr) {
6193 __ Bind(slow_path->GetExitLabel());
6194 }
6195
6196 break;
6197 }
6198
6199 case Primitive::kPrimLong: {
6200 Location value = locations->InAt(2);
6201 if (index.IsConstant()) {
6202 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006203 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006204 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6205 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006206 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006207 vixl32::Register temp = temps.Acquire();
6208 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6209 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6210 }
6211 break;
6212 }
6213
6214 case Primitive::kPrimFloat: {
6215 Location value = locations->InAt(2);
6216 DCHECK(value.IsFpuRegister());
6217 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006218 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006219 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6220 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006221 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 vixl32::Register temp = temps.Acquire();
6223 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6224 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6225 }
6226 break;
6227 }
6228
6229 case Primitive::kPrimDouble: {
6230 Location value = locations->InAt(2);
6231 DCHECK(value.IsFpuRegisterPair());
6232 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006233 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6235 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006236 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 vixl32::Register temp = temps.Acquire();
6238 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6239 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6240 }
6241 break;
6242 }
6243
6244 case Primitive::kPrimVoid:
6245 LOG(FATAL) << "Unreachable type " << value_type;
6246 UNREACHABLE();
6247 }
6248
6249 // Objects are handled in the switch.
6250 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006251 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6252 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006253 codegen_->MaybeRecordImplicitNullCheck(instruction);
6254 }
6255}
6256
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006257void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6258 LocationSummary* locations =
6259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6260 locations->SetInAt(0, Location::RequiresRegister());
6261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6262}
6263
6264void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6265 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6266 vixl32::Register obj = InputRegisterAt(instruction, 0);
6267 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006268 {
Artem Serov0fb37192016-12-06 18:13:40 +00006269 ExactAssemblyScope aas(GetVIXLAssembler(),
6270 vixl32::kMaxInstructionSizeInBytes,
6271 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006272 __ ldr(out, MemOperand(obj, offset));
6273 codegen_->MaybeRecordImplicitNullCheck(instruction);
6274 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006275 // Mask out compression flag from String's array length.
6276 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006277 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006278 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006279}
6280
Artem Serov2bbc9532016-10-21 11:51:50 +01006281void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006282 LocationSummary* locations =
6283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6284
6285 locations->SetInAt(0, Location::RequiresRegister());
6286 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6288}
6289
6290void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6291 vixl32::Register out = OutputRegister(instruction);
6292 vixl32::Register first = InputRegisterAt(instruction, 0);
6293 Location second = instruction->GetLocations()->InAt(1);
6294
Artem Serov2bbc9532016-10-21 11:51:50 +01006295 if (second.IsRegister()) {
6296 __ Add(out, first, RegisterFrom(second));
6297 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006298 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006299 }
6300}
6301
Scott Wakelingc34dba72016-10-03 10:14:44 +01006302void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6303 RegisterSet caller_saves = RegisterSet::Empty();
6304 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6305 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6306 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6307 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006308
6309 HInstruction* index = instruction->InputAt(0);
6310 HInstruction* length = instruction->InputAt(1);
6311 // If both index and length are constants we can statically check the bounds. But if at least one
6312 // of them is not encodable ArmEncodableConstantOrRegister will create
6313 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6314 // locations.
6315 bool both_const = index->IsConstant() && length->IsConstant();
6316 locations->SetInAt(0, both_const
6317 ? Location::ConstantLocation(index->AsConstant())
6318 : ArmEncodableConstantOrRegister(index, CMP));
6319 locations->SetInAt(1, both_const
6320 ? Location::ConstantLocation(length->AsConstant())
6321 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006322}
6323
6324void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006325 LocationSummary* locations = instruction->GetLocations();
6326 Location index_loc = locations->InAt(0);
6327 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006328
Artem Serov2dd053d2017-03-08 14:54:06 +00006329 if (length_loc.IsConstant()) {
6330 int32_t length = Int32ConstantFrom(length_loc);
6331 if (index_loc.IsConstant()) {
6332 // BCE will remove the bounds check if we are guaranteed to pass.
6333 int32_t index = Int32ConstantFrom(index_loc);
6334 if (index < 0 || index >= length) {
6335 SlowPathCodeARMVIXL* slow_path =
6336 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6337 codegen_->AddSlowPath(slow_path);
6338 __ B(slow_path->GetEntryLabel());
6339 } else {
6340 // Some optimization after BCE may have generated this, and we should not
6341 // generate a bounds check if it is a valid range.
6342 }
6343 return;
6344 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006345
Artem Serov2dd053d2017-03-08 14:54:06 +00006346 SlowPathCodeARMVIXL* slow_path =
6347 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6348 __ Cmp(RegisterFrom(index_loc), length);
6349 codegen_->AddSlowPath(slow_path);
6350 __ B(hs, slow_path->GetEntryLabel());
6351 } else {
6352 SlowPathCodeARMVIXL* slow_path =
6353 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6354 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6355 codegen_->AddSlowPath(slow_path);
6356 __ B(ls, slow_path->GetEntryLabel());
6357 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358}
6359
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006360void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6361 vixl32::Register card,
6362 vixl32::Register object,
6363 vixl32::Register value,
6364 bool can_be_null) {
6365 vixl32::Label is_null;
6366 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006367 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006368 }
6369 GetAssembler()->LoadFromOffset(
6370 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006371 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006372 __ Strb(card, MemOperand(card, temp));
6373 if (can_be_null) {
6374 __ Bind(&is_null);
6375 }
6376}
6377
Scott Wakelingfe885462016-09-22 10:24:38 +01006378void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6379 LOG(FATAL) << "Unreachable";
6380}
6381
6382void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6383 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6384}
6385
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006386void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006387 LocationSummary* locations =
6388 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6389 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006390}
6391
6392void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6393 HBasicBlock* block = instruction->GetBlock();
6394 if (block->GetLoopInformation() != nullptr) {
6395 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6396 // The back edge will generate the suspend check.
6397 return;
6398 }
6399 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6400 // The goto will generate the suspend check.
6401 return;
6402 }
6403 GenerateSuspendCheck(instruction, nullptr);
6404}
6405
6406void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6407 HBasicBlock* successor) {
6408 SuspendCheckSlowPathARMVIXL* slow_path =
6409 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6410 if (slow_path == nullptr) {
6411 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6412 instruction->SetSlowPath(slow_path);
6413 codegen_->AddSlowPath(slow_path);
6414 if (successor != nullptr) {
6415 DCHECK(successor->IsLoopHeader());
6416 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6417 }
6418 } else {
6419 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6420 }
6421
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006422 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006423 vixl32::Register temp = temps.Acquire();
6424 GetAssembler()->LoadFromOffset(
6425 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6426 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006427 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006428 __ Bind(slow_path->GetReturnLabel());
6429 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006430 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006431 __ B(slow_path->GetEntryLabel());
6432 }
6433}
6434
Scott Wakelingfe885462016-09-22 10:24:38 +01006435ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6436 return codegen_->GetAssembler();
6437}
6438
6439void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006440 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006441 MoveOperands* move = moves_[index];
6442 Location source = move->GetSource();
6443 Location destination = move->GetDestination();
6444
6445 if (source.IsRegister()) {
6446 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006447 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006448 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006449 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006450 } else {
6451 DCHECK(destination.IsStackSlot());
6452 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006453 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006454 sp,
6455 destination.GetStackIndex());
6456 }
6457 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006458 if (destination.IsRegister()) {
6459 GetAssembler()->LoadFromOffset(kLoadWord,
6460 RegisterFrom(destination),
6461 sp,
6462 source.GetStackIndex());
6463 } else if (destination.IsFpuRegister()) {
6464 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6465 } else {
6466 DCHECK(destination.IsStackSlot());
6467 vixl32::Register temp = temps.Acquire();
6468 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6469 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6470 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006471 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006472 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006473 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006474 } else if (destination.IsFpuRegister()) {
6475 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6476 } else {
6477 DCHECK(destination.IsStackSlot());
6478 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6479 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006480 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006481 if (destination.IsDoubleStackSlot()) {
6482 vixl32::DRegister temp = temps.AcquireD();
6483 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6484 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6485 } else if (destination.IsRegisterPair()) {
6486 DCHECK(ExpectedPairLayout(destination));
6487 GetAssembler()->LoadFromOffset(
6488 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6489 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006490 DCHECK(destination.IsFpuRegisterPair()) << destination;
6491 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006492 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006493 } else if (source.IsRegisterPair()) {
6494 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006495 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6496 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006497 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006498 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006499 } else {
6500 DCHECK(destination.IsDoubleStackSlot()) << destination;
6501 DCHECK(ExpectedPairLayout(source));
6502 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006503 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006504 sp,
6505 destination.GetStackIndex());
6506 }
6507 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006508 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006509 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006510 } else if (destination.IsFpuRegisterPair()) {
6511 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6512 } else {
6513 DCHECK(destination.IsDoubleStackSlot()) << destination;
6514 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6515 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006516 } else {
6517 DCHECK(source.IsConstant()) << source;
6518 HConstant* constant = source.GetConstant();
6519 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6520 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6521 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006522 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006523 } else {
6524 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006525 vixl32::Register temp = temps.Acquire();
6526 __ Mov(temp, value);
6527 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6528 }
6529 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006530 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006531 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006532 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6533 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006534 } else {
6535 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006536 vixl32::Register temp = temps.Acquire();
6537 __ Mov(temp, Low32Bits(value));
6538 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6539 __ Mov(temp, High32Bits(value));
6540 GetAssembler()->StoreToOffset(kStoreWord,
6541 temp,
6542 sp,
6543 destination.GetHighStackIndex(kArmWordSize));
6544 }
6545 } else if (constant->IsDoubleConstant()) {
6546 double value = constant->AsDoubleConstant()->GetValue();
6547 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006548 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006549 } else {
6550 DCHECK(destination.IsDoubleStackSlot()) << destination;
6551 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006552 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006553 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006554 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006555 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006556 GetAssembler()->StoreToOffset(kStoreWord,
6557 temp,
6558 sp,
6559 destination.GetHighStackIndex(kArmWordSize));
6560 }
6561 } else {
6562 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6563 float value = constant->AsFloatConstant()->GetValue();
6564 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006565 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006566 } else {
6567 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006568 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006569 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006570 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6571 }
6572 }
6573 }
6574}
6575
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006576void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6577 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6578 vixl32::Register temp = temps.Acquire();
6579 __ Mov(temp, reg);
6580 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6581 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006582}
6583
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006584void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6585 // TODO(VIXL32): Double check the performance of this implementation.
6586 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006587 vixl32::Register temp1 = temps.Acquire();
6588 ScratchRegisterScope ensure_scratch(
6589 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6590 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006591
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006592 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6593 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6594 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6595 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6596 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006597}
6598
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006599void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6600 MoveOperands* move = moves_[index];
6601 Location source = move->GetSource();
6602 Location destination = move->GetDestination();
6603 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6604
6605 if (source.IsRegister() && destination.IsRegister()) {
6606 vixl32::Register temp = temps.Acquire();
6607 DCHECK(!RegisterFrom(source).Is(temp));
6608 DCHECK(!RegisterFrom(destination).Is(temp));
6609 __ Mov(temp, RegisterFrom(destination));
6610 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6611 __ Mov(RegisterFrom(source), temp);
6612 } else if (source.IsRegister() && destination.IsStackSlot()) {
6613 Exchange(RegisterFrom(source), destination.GetStackIndex());
6614 } else if (source.IsStackSlot() && destination.IsRegister()) {
6615 Exchange(RegisterFrom(destination), source.GetStackIndex());
6616 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006617 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006618 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006619 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006620 __ Vmov(temp, SRegisterFrom(source));
6621 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6622 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006623 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6624 vixl32::DRegister temp = temps.AcquireD();
6625 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6626 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6627 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6628 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6629 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6630 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6631 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6632 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6633 vixl32::DRegister temp = temps.AcquireD();
6634 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6635 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6636 GetAssembler()->StoreDToOffset(temp, sp, mem);
6637 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006638 vixl32::DRegister first = DRegisterFrom(source);
6639 vixl32::DRegister second = DRegisterFrom(destination);
6640 vixl32::DRegister temp = temps.AcquireD();
6641 __ Vmov(temp, first);
6642 __ Vmov(first, second);
6643 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006644 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006645 vixl32::DRegister reg = source.IsFpuRegisterPair()
6646 ? DRegisterFrom(source)
6647 : DRegisterFrom(destination);
6648 int mem = source.IsFpuRegisterPair()
6649 ? destination.GetStackIndex()
6650 : source.GetStackIndex();
6651 vixl32::DRegister temp = temps.AcquireD();
6652 __ Vmov(temp, reg);
6653 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6654 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006655 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006656 vixl32::SRegister reg = source.IsFpuRegister()
6657 ? SRegisterFrom(source)
6658 : SRegisterFrom(destination);
6659 int mem = source.IsFpuRegister()
6660 ? destination.GetStackIndex()
6661 : source.GetStackIndex();
6662 vixl32::Register temp = temps.Acquire();
6663 __ Vmov(temp, reg);
6664 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6665 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006666 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6667 vixl32::DRegister temp1 = temps.AcquireD();
6668 vixl32::DRegister temp2 = temps.AcquireD();
6669 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6670 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6671 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6672 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6673 } else {
6674 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6675 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006676}
6677
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006678void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6679 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006680}
6681
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006682void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6683 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006684}
6685
Artem Serov02d37832016-10-25 15:25:33 +01006686HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006687 HLoadClass::LoadKind desired_class_load_kind) {
6688 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006689 case HLoadClass::LoadKind::kInvalid:
6690 LOG(FATAL) << "UNREACHABLE";
6691 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006692 case HLoadClass::LoadKind::kReferrersClass:
6693 break;
6694 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006695 DCHECK(!GetCompilerOptions().GetCompilePic());
6696 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006697 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6698 DCHECK(GetCompilerOptions().GetCompilePic());
6699 break;
6700 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006701 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006702 case HLoadClass::LoadKind::kBssEntry:
6703 DCHECK(!Runtime::Current()->UseJitCompilation());
6704 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006705 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006706 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006707 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006708 case HLoadClass::LoadKind::kDexCacheViaMethod:
6709 break;
6710 }
6711 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006712}
6713
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006714void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006715 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6716 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006717 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006718 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006719 cls,
6720 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006721 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006722 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006723 return;
6724 }
Vladimir Marko41559982017-01-06 14:04:23 +00006725 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006726
Artem Serovd4cc5b22016-11-04 11:19:09 +00006727 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6728 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006729 ? LocationSummary::kCallOnSlowPath
6730 : LocationSummary::kNoCall;
6731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006732 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006733 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006734 }
6735
Vladimir Marko41559982017-01-06 14:04:23 +00006736 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006737 locations->SetInAt(0, Location::RequiresRegister());
6738 }
6739 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006740 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6741 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6742 // Rely on the type resolution or initialization and marking to save everything we need.
6743 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6744 // to the custom calling convention) or by marking, so we request a different temp.
6745 locations->AddTemp(Location::RequiresRegister());
6746 RegisterSet caller_saves = RegisterSet::Empty();
6747 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6748 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6749 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6750 // that the the kPrimNot result register is the same as the first argument register.
6751 locations->SetCustomSlowPathCallerSaves(caller_saves);
6752 } else {
6753 // For non-Baker read barrier we have a temp-clobbering call.
6754 }
6755 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006756}
6757
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006758// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6759// move.
6760void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006761 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6762 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6763 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006764 return;
6765 }
Vladimir Marko41559982017-01-06 14:04:23 +00006766 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006767
Vladimir Marko41559982017-01-06 14:04:23 +00006768 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006769 Location out_loc = locations->Out();
6770 vixl32::Register out = OutputRegister(cls);
6771
Artem Serovd4cc5b22016-11-04 11:19:09 +00006772 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6773 ? kWithoutReadBarrier
6774 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006775 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006776 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006777 case HLoadClass::LoadKind::kReferrersClass: {
6778 DCHECK(!cls->CanCallRuntime());
6779 DCHECK(!cls->MustGenerateClinitCheck());
6780 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6781 vixl32::Register current_method = InputRegisterAt(cls, 0);
6782 GenerateGcRootFieldLoad(cls,
6783 out_loc,
6784 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006785 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006786 read_barrier_option);
6787 break;
6788 }
6789 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006790 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006791 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6792 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6793 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006794 break;
6795 }
6796 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006797 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006798 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6799 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6800 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6801 codegen_->EmitMovwMovtPlaceholder(labels, out);
6802 break;
6803 }
6804 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006805 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006806 uint32_t address = dchecked_integral_cast<uint32_t>(
6807 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6808 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006809 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006810 break;
6811 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006812 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006813 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6814 ? RegisterFrom(locations->GetTemp(0))
6815 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006816 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006817 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006818 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6819 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006820 generate_null_check = true;
6821 break;
6822 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006823 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006824 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6825 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006826 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006827 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006828 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006829 break;
6830 }
Vladimir Marko41559982017-01-06 14:04:23 +00006831 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006832 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006833 LOG(FATAL) << "UNREACHABLE";
6834 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006835 }
6836
6837 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6838 DCHECK(cls->CanCallRuntime());
6839 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6840 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6841 codegen_->AddSlowPath(slow_path);
6842 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006843 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006844 }
6845 if (cls->MustGenerateClinitCheck()) {
6846 GenerateClassInitializationCheck(slow_path, out);
6847 } else {
6848 __ Bind(slow_path->GetExitLabel());
6849 }
6850 }
6851}
6852
Artem Serov02d37832016-10-25 15:25:33 +01006853void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6854 LocationSummary* locations =
6855 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6856 locations->SetInAt(0, Location::RequiresRegister());
6857 if (check->HasUses()) {
6858 locations->SetOut(Location::SameAsFirstInput());
6859 }
6860}
6861
6862void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6863 // We assume the class is not null.
6864 LoadClassSlowPathARMVIXL* slow_path =
6865 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6866 check,
6867 check->GetDexPc(),
6868 /* do_clinit */ true);
6869 codegen_->AddSlowPath(slow_path);
6870 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6871}
6872
6873void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6874 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6875 UseScratchRegisterScope temps(GetVIXLAssembler());
6876 vixl32::Register temp = temps.Acquire();
6877 GetAssembler()->LoadFromOffset(kLoadWord,
6878 temp,
6879 class_reg,
6880 mirror::Class::StatusOffset().Int32Value());
6881 __ Cmp(temp, mirror::Class::kStatusInitialized);
6882 __ B(lt, slow_path->GetEntryLabel());
6883 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6884 // properly. Therefore, we do a memory fence.
6885 __ Dmb(ISH);
6886 __ Bind(slow_path->GetExitLabel());
6887}
6888
Artem Serov02d37832016-10-25 15:25:33 +01006889HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006890 HLoadString::LoadKind desired_string_load_kind) {
6891 switch (desired_string_load_kind) {
6892 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006893 DCHECK(!GetCompilerOptions().GetCompilePic());
6894 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006895 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6896 DCHECK(GetCompilerOptions().GetCompilePic());
6897 break;
6898 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006899 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006900 case HLoadString::LoadKind::kBssEntry:
6901 DCHECK(!Runtime::Current()->UseJitCompilation());
6902 break;
6903 case HLoadString::LoadKind::kJitTableAddress:
6904 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006905 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006906 case HLoadString::LoadKind::kDexCacheViaMethod:
6907 break;
6908 }
6909 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006910}
6911
6912void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006913 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006915 HLoadString::LoadKind load_kind = load->GetLoadKind();
6916 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006917 locations->SetOut(LocationFrom(r0));
6918 } else {
6919 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006920 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6921 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006922 // Rely on the pResolveString and marking to save everything we need, including temps.
6923 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6924 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006925 locations->AddTemp(Location::RequiresRegister());
6926 RegisterSet caller_saves = RegisterSet::Empty();
6927 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6928 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6929 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6930 // that the the kPrimNot result register is the same as the first argument register.
6931 locations->SetCustomSlowPathCallerSaves(caller_saves);
6932 } else {
6933 // For non-Baker read barrier we have a temp-clobbering call.
6934 }
6935 }
Artem Serov02d37832016-10-25 15:25:33 +01006936 }
6937}
6938
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006939// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6940// move.
6941void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006942 LocationSummary* locations = load->GetLocations();
6943 Location out_loc = locations->Out();
6944 vixl32::Register out = OutputRegister(load);
6945 HLoadString::LoadKind load_kind = load->GetLoadKind();
6946
6947 switch (load_kind) {
6948 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006949 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6950 load->GetStringIndex()));
6951 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006952 }
6953 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6954 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6955 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006956 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006957 codegen_->EmitMovwMovtPlaceholder(labels, out);
6958 return; // No dex cache slow path.
6959 }
6960 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006961 uint32_t address = dchecked_integral_cast<uint32_t>(
6962 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6963 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006964 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6965 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006966 }
6967 case HLoadString::LoadKind::kBssEntry: {
6968 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006969 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6970 ? RegisterFrom(locations->GetTemp(0))
6971 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006972 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006973 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006974 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6975 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6976 LoadStringSlowPathARMVIXL* slow_path =
6977 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6978 codegen_->AddSlowPath(slow_path);
6979 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6980 __ Bind(slow_path->GetExitLabel());
6981 return;
6982 }
6983 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006984 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006985 load->GetStringIndex(),
6986 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006987 // /* GcRoot<mirror::String> */ out = *out
6988 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6989 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006990 }
6991 default:
6992 break;
6993 }
Artem Serov02d37832016-10-25 15:25:33 +01006994
6995 // TODO: Re-add the compiler code to do string dex cache lookup again.
6996 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6997 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006998 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006999 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7000 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
7001}
7002
7003static int32_t GetExceptionTlsOffset() {
7004 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7005}
7006
7007void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7008 LocationSummary* locations =
7009 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7010 locations->SetOut(Location::RequiresRegister());
7011}
7012
7013void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7014 vixl32::Register out = OutputRegister(load);
7015 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7016}
7017
7018
7019void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7020 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7021}
7022
7023void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7024 UseScratchRegisterScope temps(GetVIXLAssembler());
7025 vixl32::Register temp = temps.Acquire();
7026 __ Mov(temp, 0);
7027 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7028}
7029
7030void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7031 LocationSummary* locations =
7032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7033 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7034 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7035}
7036
7037void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7038 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7039 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7040}
7041
Artem Serov657022c2016-11-23 14:19:38 +00007042// Temp is used for read barrier.
7043static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7044 if (kEmitCompilerReadBarrier &&
7045 (kUseBakerReadBarrier ||
7046 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7047 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7048 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7049 return 1;
7050 }
7051 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007052}
7053
Artem Serov657022c2016-11-23 14:19:38 +00007054// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7055// interface pointer, one for loading the current interface.
7056// The other checks have one temp for loading the object's class.
7057static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7058 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7059 return 3;
7060 }
7061 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7062}
Artem Serovcfbe9132016-10-14 15:58:56 +01007063
7064void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7065 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7066 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7067 bool baker_read_barrier_slow_path = false;
7068 switch (type_check_kind) {
7069 case TypeCheckKind::kExactCheck:
7070 case TypeCheckKind::kAbstractClassCheck:
7071 case TypeCheckKind::kClassHierarchyCheck:
7072 case TypeCheckKind::kArrayObjectCheck:
7073 call_kind =
7074 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7075 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7076 break;
7077 case TypeCheckKind::kArrayCheck:
7078 case TypeCheckKind::kUnresolvedCheck:
7079 case TypeCheckKind::kInterfaceCheck:
7080 call_kind = LocationSummary::kCallOnSlowPath;
7081 break;
7082 }
7083
7084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7085 if (baker_read_barrier_slow_path) {
7086 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7087 }
7088 locations->SetInAt(0, Location::RequiresRegister());
7089 locations->SetInAt(1, Location::RequiresRegister());
7090 // The "out" register is used as a temporary, so it overlaps with the inputs.
7091 // Note that TypeCheckSlowPathARM uses this register too.
7092 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007093 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007094}
7095
7096void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7097 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7098 LocationSummary* locations = instruction->GetLocations();
7099 Location obj_loc = locations->InAt(0);
7100 vixl32::Register obj = InputRegisterAt(instruction, 0);
7101 vixl32::Register cls = InputRegisterAt(instruction, 1);
7102 Location out_loc = locations->Out();
7103 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007104 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7105 DCHECK_LE(num_temps, 1u);
7106 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007107 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7108 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7109 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7110 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007111 vixl32::Label done;
7112 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007113 SlowPathCodeARMVIXL* slow_path = nullptr;
7114
7115 // Return 0 if `obj` is null.
7116 // avoid null check if we know obj is not null.
7117 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007118 DCHECK(!out.Is(obj));
7119 __ Mov(out, 0);
7120 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007121 }
7122
Artem Serovcfbe9132016-10-14 15:58:56 +01007123 switch (type_check_kind) {
7124 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007125 // /* HeapReference<Class> */ out = obj->klass_
7126 GenerateReferenceLoadTwoRegisters(instruction,
7127 out_loc,
7128 obj_loc,
7129 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007130 maybe_temp_loc,
7131 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007132 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007133 __ Cmp(out, cls);
7134 // We speculatively set the result to false without changing the condition
7135 // flags, which allows us to avoid some branching later.
7136 __ Mov(LeaveFlags, out, 0);
7137
7138 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7139 // we check that the output is in a low register, so that a 16-bit MOV
7140 // encoding can be used.
7141 if (out.IsLow()) {
7142 // We use the scope because of the IT block that follows.
7143 ExactAssemblyScope guard(GetVIXLAssembler(),
7144 2 * vixl32::k16BitT32InstructionSizeInBytes,
7145 CodeBufferCheckScope::kExactSize);
7146
7147 __ it(eq);
7148 __ mov(eq, out, 1);
7149 } else {
7150 __ B(ne, final_label, /* far_target */ false);
7151 __ Mov(out, 1);
7152 }
7153
Artem Serovcfbe9132016-10-14 15:58:56 +01007154 break;
7155 }
7156
7157 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007158 // /* HeapReference<Class> */ out = obj->klass_
7159 GenerateReferenceLoadTwoRegisters(instruction,
7160 out_loc,
7161 obj_loc,
7162 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007163 maybe_temp_loc,
7164 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007165 // If the class is abstract, we eagerly fetch the super class of the
7166 // object to avoid doing a comparison we know will fail.
7167 vixl32::Label loop;
7168 __ Bind(&loop);
7169 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007170 GenerateReferenceLoadOneRegister(instruction,
7171 out_loc,
7172 super_offset,
7173 maybe_temp_loc,
7174 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007175 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007176 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007177 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007178 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007179 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007180 break;
7181 }
7182
7183 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007184 // /* HeapReference<Class> */ out = obj->klass_
7185 GenerateReferenceLoadTwoRegisters(instruction,
7186 out_loc,
7187 obj_loc,
7188 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007189 maybe_temp_loc,
7190 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007191 // Walk over the class hierarchy to find a match.
7192 vixl32::Label loop, success;
7193 __ Bind(&loop);
7194 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007195 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007196 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007197 GenerateReferenceLoadOneRegister(instruction,
7198 out_loc,
7199 super_offset,
7200 maybe_temp_loc,
7201 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007202 // This is essentially a null check, but it sets the condition flags to the
7203 // proper value for the code that follows the loop, i.e. not `eq`.
7204 __ Cmp(out, 1);
7205 __ B(hs, &loop, /* far_target */ false);
7206
7207 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7208 // we check that the output is in a low register, so that a 16-bit MOV
7209 // encoding can be used.
7210 if (out.IsLow()) {
7211 // If `out` is null, we use it for the result, and the condition flags
7212 // have already been set to `ne`, so the IT block that comes afterwards
7213 // (and which handles the successful case) turns into a NOP (instead of
7214 // overwriting `out`).
7215 __ Bind(&success);
7216
7217 // We use the scope because of the IT block that follows.
7218 ExactAssemblyScope guard(GetVIXLAssembler(),
7219 2 * vixl32::k16BitT32InstructionSizeInBytes,
7220 CodeBufferCheckScope::kExactSize);
7221
7222 // There is only one branch to the `success` label (which is bound to this
7223 // IT block), and it has the same condition, `eq`, so in that case the MOV
7224 // is executed.
7225 __ it(eq);
7226 __ mov(eq, out, 1);
7227 } else {
7228 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007229 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007230 __ Bind(&success);
7231 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007232 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007233
Artem Serovcfbe9132016-10-14 15:58:56 +01007234 break;
7235 }
7236
7237 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007238 // /* HeapReference<Class> */ out = obj->klass_
7239 GenerateReferenceLoadTwoRegisters(instruction,
7240 out_loc,
7241 obj_loc,
7242 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007243 maybe_temp_loc,
7244 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007245 // Do an exact check.
7246 vixl32::Label exact_check;
7247 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007248 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007249 // Otherwise, we need to check that the object's class is a non-primitive array.
7250 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007251 GenerateReferenceLoadOneRegister(instruction,
7252 out_loc,
7253 component_offset,
7254 maybe_temp_loc,
7255 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007256 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007257 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007258 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7259 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007260 __ Cmp(out, 0);
7261 // We speculatively set the result to false without changing the condition
7262 // flags, which allows us to avoid some branching later.
7263 __ Mov(LeaveFlags, out, 0);
7264
7265 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7266 // we check that the output is in a low register, so that a 16-bit MOV
7267 // encoding can be used.
7268 if (out.IsLow()) {
7269 __ Bind(&exact_check);
7270
7271 // We use the scope because of the IT block that follows.
7272 ExactAssemblyScope guard(GetVIXLAssembler(),
7273 2 * vixl32::k16BitT32InstructionSizeInBytes,
7274 CodeBufferCheckScope::kExactSize);
7275
7276 __ it(eq);
7277 __ mov(eq, out, 1);
7278 } else {
7279 __ B(ne, final_label, /* far_target */ false);
7280 __ Bind(&exact_check);
7281 __ Mov(out, 1);
7282 }
7283
Artem Serovcfbe9132016-10-14 15:58:56 +01007284 break;
7285 }
7286
7287 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007288 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007289 // /* HeapReference<Class> */ out = obj->klass_
7290 GenerateReferenceLoadTwoRegisters(instruction,
7291 out_loc,
7292 obj_loc,
7293 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007294 maybe_temp_loc,
7295 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007296 __ Cmp(out, cls);
7297 DCHECK(locations->OnlyCallsOnSlowPath());
7298 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7299 /* is_fatal */ false);
7300 codegen_->AddSlowPath(slow_path);
7301 __ B(ne, slow_path->GetEntryLabel());
7302 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007303 break;
7304 }
7305
7306 case TypeCheckKind::kUnresolvedCheck:
7307 case TypeCheckKind::kInterfaceCheck: {
7308 // Note that we indeed only call on slow path, but we always go
7309 // into the slow path for the unresolved and interface check
7310 // cases.
7311 //
7312 // We cannot directly call the InstanceofNonTrivial runtime
7313 // entry point without resorting to a type checking slow path
7314 // here (i.e. by calling InvokeRuntime directly), as it would
7315 // require to assign fixed registers for the inputs of this
7316 // HInstanceOf instruction (following the runtime calling
7317 // convention), which might be cluttered by the potential first
7318 // read barrier emission at the beginning of this method.
7319 //
7320 // TODO: Introduce a new runtime entry point taking the object
7321 // to test (instead of its class) as argument, and let it deal
7322 // with the read barrier issues. This will let us refactor this
7323 // case of the `switch` code as it was previously (with a direct
7324 // call to the runtime not using a type checking slow path).
7325 // This should also be beneficial for the other cases above.
7326 DCHECK(locations->OnlyCallsOnSlowPath());
7327 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7328 /* is_fatal */ false);
7329 codegen_->AddSlowPath(slow_path);
7330 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007331 break;
7332 }
7333 }
7334
Artem Serovcfbe9132016-10-14 15:58:56 +01007335 if (done.IsReferenced()) {
7336 __ Bind(&done);
7337 }
7338
7339 if (slow_path != nullptr) {
7340 __ Bind(slow_path->GetExitLabel());
7341 }
7342}
7343
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007344void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7345 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7346 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7347
7348 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7349 switch (type_check_kind) {
7350 case TypeCheckKind::kExactCheck:
7351 case TypeCheckKind::kAbstractClassCheck:
7352 case TypeCheckKind::kClassHierarchyCheck:
7353 case TypeCheckKind::kArrayObjectCheck:
7354 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7355 LocationSummary::kCallOnSlowPath :
7356 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7357 break;
7358 case TypeCheckKind::kArrayCheck:
7359 case TypeCheckKind::kUnresolvedCheck:
7360 case TypeCheckKind::kInterfaceCheck:
7361 call_kind = LocationSummary::kCallOnSlowPath;
7362 break;
7363 }
7364
7365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7366 locations->SetInAt(0, Location::RequiresRegister());
7367 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007368 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007369}
7370
7371void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7372 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7373 LocationSummary* locations = instruction->GetLocations();
7374 Location obj_loc = locations->InAt(0);
7375 vixl32::Register obj = InputRegisterAt(instruction, 0);
7376 vixl32::Register cls = InputRegisterAt(instruction, 1);
7377 Location temp_loc = locations->GetTemp(0);
7378 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007379 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7380 DCHECK_LE(num_temps, 3u);
7381 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7382 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7383 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7384 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7385 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7386 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7387 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7388 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7389 const uint32_t object_array_data_offset =
7390 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007391
Artem Serov657022c2016-11-23 14:19:38 +00007392 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7393 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7394 // read barriers is done for performance and code size reasons.
7395 bool is_type_check_slow_path_fatal = false;
7396 if (!kEmitCompilerReadBarrier) {
7397 is_type_check_slow_path_fatal =
7398 (type_check_kind == TypeCheckKind::kExactCheck ||
7399 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7400 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7401 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7402 !instruction->CanThrowIntoCatchBlock();
7403 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007404 SlowPathCodeARMVIXL* type_check_slow_path =
7405 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7406 is_type_check_slow_path_fatal);
7407 codegen_->AddSlowPath(type_check_slow_path);
7408
7409 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007410 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007411 // Avoid null check if we know obj is not null.
7412 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007413 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007414 }
7415
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007416 switch (type_check_kind) {
7417 case TypeCheckKind::kExactCheck:
7418 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007419 // /* HeapReference<Class> */ temp = obj->klass_
7420 GenerateReferenceLoadTwoRegisters(instruction,
7421 temp_loc,
7422 obj_loc,
7423 class_offset,
7424 maybe_temp2_loc,
7425 kWithoutReadBarrier);
7426
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007427 __ Cmp(temp, cls);
7428 // Jump to slow path for throwing the exception or doing a
7429 // more involved array check.
7430 __ B(ne, type_check_slow_path->GetEntryLabel());
7431 break;
7432 }
7433
7434 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007435 // /* HeapReference<Class> */ temp = obj->klass_
7436 GenerateReferenceLoadTwoRegisters(instruction,
7437 temp_loc,
7438 obj_loc,
7439 class_offset,
7440 maybe_temp2_loc,
7441 kWithoutReadBarrier);
7442
Artem Serovcfbe9132016-10-14 15:58:56 +01007443 // If the class is abstract, we eagerly fetch the super class of the
7444 // object to avoid doing a comparison we know will fail.
7445 vixl32::Label loop;
7446 __ Bind(&loop);
7447 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007448 GenerateReferenceLoadOneRegister(instruction,
7449 temp_loc,
7450 super_offset,
7451 maybe_temp2_loc,
7452 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007453
7454 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7455 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007456 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007457
7458 // Otherwise, compare the classes.
7459 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007460 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007461 break;
7462 }
7463
7464 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007465 // /* HeapReference<Class> */ temp = obj->klass_
7466 GenerateReferenceLoadTwoRegisters(instruction,
7467 temp_loc,
7468 obj_loc,
7469 class_offset,
7470 maybe_temp2_loc,
7471 kWithoutReadBarrier);
7472
Artem Serovcfbe9132016-10-14 15:58:56 +01007473 // Walk over the class hierarchy to find a match.
7474 vixl32::Label loop;
7475 __ Bind(&loop);
7476 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007477 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007478
7479 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007480 GenerateReferenceLoadOneRegister(instruction,
7481 temp_loc,
7482 super_offset,
7483 maybe_temp2_loc,
7484 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007485
7486 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7487 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007488 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007489 // Otherwise, jump to the beginning of the loop.
7490 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007491 break;
7492 }
7493
Artem Serovcfbe9132016-10-14 15:58:56 +01007494 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007495 // /* HeapReference<Class> */ temp = obj->klass_
7496 GenerateReferenceLoadTwoRegisters(instruction,
7497 temp_loc,
7498 obj_loc,
7499 class_offset,
7500 maybe_temp2_loc,
7501 kWithoutReadBarrier);
7502
Artem Serovcfbe9132016-10-14 15:58:56 +01007503 // Do an exact check.
7504 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007505 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007506
7507 // Otherwise, we need to check that the object's class is a non-primitive array.
7508 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007509 GenerateReferenceLoadOneRegister(instruction,
7510 temp_loc,
7511 component_offset,
7512 maybe_temp2_loc,
7513 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007514 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007515 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007516 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7517 // to further check that this component type is not a primitive type.
7518 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7519 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007520 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007521 break;
7522 }
7523
7524 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007525 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007526 // We cannot directly call the CheckCast runtime entry point
7527 // without resorting to a type checking slow path here (i.e. by
7528 // calling InvokeRuntime directly), as it would require to
7529 // assign fixed registers for the inputs of this HInstanceOf
7530 // instruction (following the runtime calling convention), which
7531 // might be cluttered by the potential first read barrier
7532 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007533
Artem Serovcfbe9132016-10-14 15:58:56 +01007534 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007535 break;
Artem Serov657022c2016-11-23 14:19:38 +00007536
7537 case TypeCheckKind::kInterfaceCheck: {
7538 // Avoid read barriers to improve performance of the fast path. We can not get false
7539 // positives by doing this.
7540 // /* HeapReference<Class> */ temp = obj->klass_
7541 GenerateReferenceLoadTwoRegisters(instruction,
7542 temp_loc,
7543 obj_loc,
7544 class_offset,
7545 maybe_temp2_loc,
7546 kWithoutReadBarrier);
7547
7548 // /* HeapReference<Class> */ temp = temp->iftable_
7549 GenerateReferenceLoadTwoRegisters(instruction,
7550 temp_loc,
7551 temp_loc,
7552 iftable_offset,
7553 maybe_temp2_loc,
7554 kWithoutReadBarrier);
7555 // Iftable is never null.
7556 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7557 // Loop through the iftable and check if any class matches.
7558 vixl32::Label start_loop;
7559 __ Bind(&start_loop);
7560 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7561 type_check_slow_path->GetEntryLabel());
7562 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7563 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7564 // Go to next interface.
7565 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7566 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7567 // Compare the classes and continue the loop if they do not match.
7568 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007569 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007570 break;
7571 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007572 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007573 if (done.IsReferenced()) {
7574 __ Bind(&done);
7575 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007576
7577 __ Bind(type_check_slow_path->GetExitLabel());
7578}
7579
Artem Serov551b28f2016-10-18 19:11:30 +01007580void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7581 LocationSummary* locations =
7582 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7583 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7584 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7585}
7586
7587void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7588 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7589 instruction,
7590 instruction->GetDexPc());
7591 if (instruction->IsEnter()) {
7592 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7593 } else {
7594 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7595 }
7596}
7597
Artem Serov02109dd2016-09-23 17:17:54 +01007598void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7599 HandleBitwiseOperation(instruction, AND);
7600}
7601
7602void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7603 HandleBitwiseOperation(instruction, ORR);
7604}
7605
7606void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7607 HandleBitwiseOperation(instruction, EOR);
7608}
7609
7610void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7611 LocationSummary* locations =
7612 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7613 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7614 || instruction->GetResultType() == Primitive::kPrimLong);
7615 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7616 locations->SetInAt(0, Location::RequiresRegister());
7617 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7618 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7619}
7620
7621void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7622 HandleBitwiseOperation(instruction);
7623}
7624
7625void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7626 HandleBitwiseOperation(instruction);
7627}
7628
7629void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7630 HandleBitwiseOperation(instruction);
7631}
7632
Artem Serov2bbc9532016-10-21 11:51:50 +01007633void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7634 LocationSummary* locations =
7635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7636 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7637 || instruction->GetResultType() == Primitive::kPrimLong);
7638
7639 locations->SetInAt(0, Location::RequiresRegister());
7640 locations->SetInAt(1, Location::RequiresRegister());
7641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7642}
7643
7644void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7645 LocationSummary* locations = instruction->GetLocations();
7646 Location first = locations->InAt(0);
7647 Location second = locations->InAt(1);
7648 Location out = locations->Out();
7649
7650 if (instruction->GetResultType() == Primitive::kPrimInt) {
7651 vixl32::Register first_reg = RegisterFrom(first);
7652 vixl32::Register second_reg = RegisterFrom(second);
7653 vixl32::Register out_reg = RegisterFrom(out);
7654
7655 switch (instruction->GetOpKind()) {
7656 case HInstruction::kAnd:
7657 __ Bic(out_reg, first_reg, second_reg);
7658 break;
7659 case HInstruction::kOr:
7660 __ Orn(out_reg, first_reg, second_reg);
7661 break;
7662 // There is no EON on arm.
7663 case HInstruction::kXor:
7664 default:
7665 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7666 UNREACHABLE();
7667 }
7668 return;
7669
7670 } else {
7671 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7672 vixl32::Register first_low = LowRegisterFrom(first);
7673 vixl32::Register first_high = HighRegisterFrom(first);
7674 vixl32::Register second_low = LowRegisterFrom(second);
7675 vixl32::Register second_high = HighRegisterFrom(second);
7676 vixl32::Register out_low = LowRegisterFrom(out);
7677 vixl32::Register out_high = HighRegisterFrom(out);
7678
7679 switch (instruction->GetOpKind()) {
7680 case HInstruction::kAnd:
7681 __ Bic(out_low, first_low, second_low);
7682 __ Bic(out_high, first_high, second_high);
7683 break;
7684 case HInstruction::kOr:
7685 __ Orn(out_low, first_low, second_low);
7686 __ Orn(out_high, first_high, second_high);
7687 break;
7688 // There is no EON on arm.
7689 case HInstruction::kXor:
7690 default:
7691 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7692 UNREACHABLE();
7693 }
7694 }
7695}
7696
Anton Kirilov74234da2017-01-13 14:42:47 +00007697void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7698 HDataProcWithShifterOp* instruction) {
7699 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7700 instruction->GetType() == Primitive::kPrimLong);
7701 LocationSummary* locations =
7702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7703 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7704 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7705
7706 locations->SetInAt(0, Location::RequiresRegister());
7707 locations->SetInAt(1, Location::RequiresRegister());
7708 locations->SetOut(Location::RequiresRegister(),
7709 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7710}
7711
7712void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7713 HDataProcWithShifterOp* instruction) {
7714 const LocationSummary* const locations = instruction->GetLocations();
7715 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7716 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7717
7718 if (instruction->GetType() == Primitive::kPrimInt) {
7719 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7720
7721 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7722 ? LowRegisterFrom(locations->InAt(1))
7723 : InputRegisterAt(instruction, 1);
7724
7725 GenerateDataProcInstruction(kind,
7726 OutputRegister(instruction),
7727 InputRegisterAt(instruction, 0),
7728 Operand(second,
7729 ShiftFromOpKind(op_kind),
7730 instruction->GetShiftAmount()),
7731 codegen_);
7732 } else {
7733 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7734
7735 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7736 const vixl32::Register second = InputRegisterAt(instruction, 1);
7737
7738 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7739 GenerateDataProc(kind,
7740 locations->Out(),
7741 locations->InAt(0),
7742 second,
7743 Operand(second, ShiftType::ASR, 31),
7744 codegen_);
7745 } else {
7746 GenerateLongDataProc(instruction, codegen_);
7747 }
7748 }
7749}
7750
Artem Serov02109dd2016-09-23 17:17:54 +01007751// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7752void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7753 vixl32::Register first,
7754 uint32_t value) {
7755 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7756 if (value == 0xffffffffu) {
7757 if (!out.Is(first)) {
7758 __ Mov(out, first);
7759 }
7760 return;
7761 }
7762 if (value == 0u) {
7763 __ Mov(out, 0);
7764 return;
7765 }
7766 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007767 __ And(out, first, value);
7768 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
7769 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01007770 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007771 DCHECK(IsPowerOfTwo(value + 1));
7772 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01007773 }
7774}
7775
7776// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7777void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7778 vixl32::Register first,
7779 uint32_t value) {
7780 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7781 if (value == 0u) {
7782 if (!out.Is(first)) {
7783 __ Mov(out, first);
7784 }
7785 return;
7786 }
7787 if (value == 0xffffffffu) {
7788 __ Mvn(out, 0);
7789 return;
7790 }
7791 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7792 __ Orr(out, first, value);
7793 } else {
7794 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7795 __ Orn(out, first, ~value);
7796 }
7797}
7798
7799// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7800void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7801 vixl32::Register first,
7802 uint32_t value) {
7803 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7804 if (value == 0u) {
7805 if (!out.Is(first)) {
7806 __ Mov(out, first);
7807 }
7808 return;
7809 }
7810 __ Eor(out, first, value);
7811}
7812
Anton Kirilovdda43962016-11-21 19:55:20 +00007813void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7814 Location first,
7815 uint64_t value) {
7816 vixl32::Register out_low = LowRegisterFrom(out);
7817 vixl32::Register out_high = HighRegisterFrom(out);
7818 vixl32::Register first_low = LowRegisterFrom(first);
7819 vixl32::Register first_high = HighRegisterFrom(first);
7820 uint32_t value_low = Low32Bits(value);
7821 uint32_t value_high = High32Bits(value);
7822 if (value_low == 0u) {
7823 if (!out_low.Is(first_low)) {
7824 __ Mov(out_low, first_low);
7825 }
7826 __ Add(out_high, first_high, value_high);
7827 return;
7828 }
7829 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007830 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007831 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007832 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007833 __ Sbc(out_high, first_high, ~value_high);
7834 } else {
7835 LOG(FATAL) << "Unexpected constant " << value_high;
7836 UNREACHABLE();
7837 }
7838}
7839
Artem Serov02109dd2016-09-23 17:17:54 +01007840void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7841 LocationSummary* locations = instruction->GetLocations();
7842 Location first = locations->InAt(0);
7843 Location second = locations->InAt(1);
7844 Location out = locations->Out();
7845
7846 if (second.IsConstant()) {
7847 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7848 uint32_t value_low = Low32Bits(value);
7849 if (instruction->GetResultType() == Primitive::kPrimInt) {
7850 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7851 vixl32::Register out_reg = OutputRegister(instruction);
7852 if (instruction->IsAnd()) {
7853 GenerateAndConst(out_reg, first_reg, value_low);
7854 } else if (instruction->IsOr()) {
7855 GenerateOrrConst(out_reg, first_reg, value_low);
7856 } else {
7857 DCHECK(instruction->IsXor());
7858 GenerateEorConst(out_reg, first_reg, value_low);
7859 }
7860 } else {
7861 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7862 uint32_t value_high = High32Bits(value);
7863 vixl32::Register first_low = LowRegisterFrom(first);
7864 vixl32::Register first_high = HighRegisterFrom(first);
7865 vixl32::Register out_low = LowRegisterFrom(out);
7866 vixl32::Register out_high = HighRegisterFrom(out);
7867 if (instruction->IsAnd()) {
7868 GenerateAndConst(out_low, first_low, value_low);
7869 GenerateAndConst(out_high, first_high, value_high);
7870 } else if (instruction->IsOr()) {
7871 GenerateOrrConst(out_low, first_low, value_low);
7872 GenerateOrrConst(out_high, first_high, value_high);
7873 } else {
7874 DCHECK(instruction->IsXor());
7875 GenerateEorConst(out_low, first_low, value_low);
7876 GenerateEorConst(out_high, first_high, value_high);
7877 }
7878 }
7879 return;
7880 }
7881
7882 if (instruction->GetResultType() == Primitive::kPrimInt) {
7883 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7884 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7885 vixl32::Register out_reg = OutputRegister(instruction);
7886 if (instruction->IsAnd()) {
7887 __ And(out_reg, first_reg, second_reg);
7888 } else if (instruction->IsOr()) {
7889 __ Orr(out_reg, first_reg, second_reg);
7890 } else {
7891 DCHECK(instruction->IsXor());
7892 __ Eor(out_reg, first_reg, second_reg);
7893 }
7894 } else {
7895 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7896 vixl32::Register first_low = LowRegisterFrom(first);
7897 vixl32::Register first_high = HighRegisterFrom(first);
7898 vixl32::Register second_low = LowRegisterFrom(second);
7899 vixl32::Register second_high = HighRegisterFrom(second);
7900 vixl32::Register out_low = LowRegisterFrom(out);
7901 vixl32::Register out_high = HighRegisterFrom(out);
7902 if (instruction->IsAnd()) {
7903 __ And(out_low, first_low, second_low);
7904 __ And(out_high, first_high, second_high);
7905 } else if (instruction->IsOr()) {
7906 __ Orr(out_low, first_low, second_low);
7907 __ Orr(out_high, first_high, second_high);
7908 } else {
7909 DCHECK(instruction->IsXor());
7910 __ Eor(out_low, first_low, second_low);
7911 __ Eor(out_high, first_high, second_high);
7912 }
7913 }
7914}
7915
Artem Serovcfbe9132016-10-14 15:58:56 +01007916void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007917 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007918 Location out,
7919 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007920 Location maybe_temp,
7921 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007922 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007923 if (read_barrier_option == kWithReadBarrier) {
7924 CHECK(kEmitCompilerReadBarrier);
7925 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7926 if (kUseBakerReadBarrier) {
7927 // Load with fast path based Baker's read barrier.
7928 // /* HeapReference<Object> */ out = *(out + offset)
7929 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7930 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7931 } else {
7932 // Load with slow path based read barrier.
7933 // Save the value of `out` into `maybe_temp` before overwriting it
7934 // in the following move operation, as we will need it for the
7935 // read barrier below.
7936 __ Mov(RegisterFrom(maybe_temp), out_reg);
7937 // /* HeapReference<Object> */ out = *(out + offset)
7938 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7939 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7940 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007941 } else {
7942 // Plain load with no read barrier.
7943 // /* HeapReference<Object> */ out = *(out + offset)
7944 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7945 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7946 }
7947}
7948
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007949void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007950 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007951 Location out,
7952 Location obj,
7953 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007954 Location maybe_temp,
7955 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007956 vixl32::Register out_reg = RegisterFrom(out);
7957 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007958 if (read_barrier_option == kWithReadBarrier) {
7959 CHECK(kEmitCompilerReadBarrier);
7960 if (kUseBakerReadBarrier) {
7961 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7962 // Load with fast path based Baker's read barrier.
7963 // /* HeapReference<Object> */ out = *(obj + offset)
7964 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7965 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7966 } else {
7967 // Load with slow path based read barrier.
7968 // /* HeapReference<Object> */ out = *(obj + offset)
7969 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7970 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7971 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007972 } else {
7973 // Plain load with no read barrier.
7974 // /* HeapReference<Object> */ out = *(obj + offset)
7975 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7976 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7977 }
7978}
7979
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007980void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007981 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007982 Location root,
7983 vixl32::Register obj,
7984 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007985 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007986 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007987 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007988 DCHECK(kEmitCompilerReadBarrier);
7989 if (kUseBakerReadBarrier) {
7990 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00007991 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007992 //
Roland Levillainba650a42017-03-06 13:52:32 +00007993 // Note that we do not actually check the value of
7994 // `GetIsGcMarking()` to decide whether to mark the loaded GC
7995 // root or not. Instead, we load into `temp` the read barrier
7996 // mark entry point corresponding to register `root`. If `temp`
7997 // is null, it means that `GetIsGcMarking()` is false, and vice
7998 // versa.
7999 //
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008000 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008001 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8002 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8003 // // Slow path.
8004 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008005 // }
8006
Roland Levillainba650a42017-03-06 13:52:32 +00008007 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8008 Location temp = LocationFrom(lr);
8009 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
8010 instruction, root, /* entrypoint */ temp);
8011 codegen_->AddSlowPath(slow_path);
8012
8013 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8014 const int32_t entry_point_offset =
8015 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8016 // Loading the entrypoint does not require a load acquire since it is only changed when
8017 // threads are suspended or running a checkpoint.
8018 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
8019
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008020 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8021 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8022 static_assert(
8023 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8024 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8025 "have different sizes.");
8026 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8027 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8028 "have different sizes.");
8029
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008030 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8031 // checking GetIsGcMarking.
8032 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
8033 __ Bind(slow_path->GetExitLabel());
8034 } else {
8035 // GC root loaded through a slow path for read barriers other
8036 // than Baker's.
8037 // /* GcRoot<mirror::Object>* */ root = obj + offset
8038 __ Add(root_reg, obj, offset);
8039 // /* mirror::Object* */ root = root->Read()
8040 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8041 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008042 } else {
8043 // Plain GC root load with no read barrier.
8044 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8045 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8046 // Note that GC roots are not affected by heap poisoning, thus we
8047 // do not have to unpoison `root_reg` here.
8048 }
8049}
8050
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008051void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8052 Location ref,
8053 vixl32::Register obj,
8054 uint32_t offset,
8055 Location temp,
8056 bool needs_null_check) {
8057 DCHECK(kEmitCompilerReadBarrier);
8058 DCHECK(kUseBakerReadBarrier);
8059
8060 // /* HeapReference<Object> */ ref = *(obj + offset)
8061 Location no_index = Location::NoLocation();
8062 ScaleFactor no_scale_factor = TIMES_1;
8063 GenerateReferenceLoadWithBakerReadBarrier(
8064 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008065}
8066
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008067void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8068 Location ref,
8069 vixl32::Register obj,
8070 uint32_t data_offset,
8071 Location index,
8072 Location temp,
8073 bool needs_null_check) {
8074 DCHECK(kEmitCompilerReadBarrier);
8075 DCHECK(kUseBakerReadBarrier);
8076
8077 static_assert(
8078 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8079 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
8080 // /* HeapReference<Object> */ ref =
8081 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8082 ScaleFactor scale_factor = TIMES_4;
8083 GenerateReferenceLoadWithBakerReadBarrier(
8084 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008085}
8086
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008087void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8088 Location ref,
8089 vixl32::Register obj,
8090 uint32_t offset,
8091 Location index,
8092 ScaleFactor scale_factor,
8093 Location temp,
8094 bool needs_null_check,
8095 bool always_update_field,
8096 vixl32::Register* temp2) {
8097 DCHECK(kEmitCompilerReadBarrier);
8098 DCHECK(kUseBakerReadBarrier);
8099
Roland Levillain54f869e2017-03-06 13:54:11 +00008100 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8101 // whether we need to enter the slow path to mark the reference.
8102 // Then, in the slow path, check the gray bit in the lock word of
8103 // the reference's holder (`obj`) to decide whether to mark `ref` or
8104 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008105 //
Roland Levillainba650a42017-03-06 13:52:32 +00008106 // Note that we do not actually check the value of `GetIsGcMarking()`;
8107 // instead, we load into `temp3` the read barrier mark entry point
8108 // corresponding to register `ref`. If `temp3` is null, it means
8109 // that `GetIsGcMarking()` is false, and vice versa.
8110 //
8111 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008112 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8113 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008114 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8115 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8116 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8117 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8118 // if (is_gray) {
8119 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8120 // }
8121 // } else {
8122 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008123 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008124
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008125 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008126
Roland Levillainba650a42017-03-06 13:52:32 +00008127 // Slow path marking the object `ref` when the GC is marking. The
8128 // entrypoint will already be loaded in `temp3`.
8129 Location temp3 = LocationFrom(lr);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008130 SlowPathCodeARMVIXL* slow_path;
8131 if (always_update_field) {
8132 DCHECK(temp2 != nullptr);
Roland Levillain54f869e2017-03-06 13:54:11 +00008133 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
8134 // only supports address of the form `obj + field_offset`, where
8135 // `obj` is a register and `field_offset` is a register pair (of
8136 // which only the lower half is used). Thus `offset` and
8137 // `scale_factor` above are expected to be null in this code path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008138 DCHECK_EQ(offset, 0u);
8139 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain54f869e2017-03-06 13:54:11 +00008140 Location field_offset = index;
8141 slow_path =
8142 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8143 instruction,
8144 ref,
8145 obj,
8146 offset,
8147 /* index */ field_offset,
8148 scale_factor,
8149 needs_null_check,
8150 temp_reg,
8151 *temp2,
8152 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008153 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00008154 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8155 instruction,
8156 ref,
8157 obj,
8158 offset,
8159 index,
8160 scale_factor,
8161 needs_null_check,
8162 temp_reg,
8163 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008164 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008165 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008166
Roland Levillainba650a42017-03-06 13:52:32 +00008167 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8168 const int32_t entry_point_offset =
8169 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8170 // Loading the entrypoint does not require a load acquire since it is only changed when
8171 // threads are suspended or running a checkpoint.
8172 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008173 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8174 // checking GetIsGcMarking.
8175 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00008176 // Fast path: just load the reference.
8177 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008178 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008179}
Scott Wakelingfe885462016-09-22 10:24:38 +01008180
Roland Levillainba650a42017-03-06 13:52:32 +00008181void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8182 Location ref,
8183 vixl::aarch32::Register obj,
8184 uint32_t offset,
8185 Location index,
8186 ScaleFactor scale_factor,
8187 bool needs_null_check) {
8188 Primitive::Type type = Primitive::kPrimNot;
8189 vixl32::Register ref_reg = RegisterFrom(ref, type);
8190
8191 // If needed, vixl::EmissionCheckScope guards are used to ensure
8192 // that no pools are emitted between the load (macro) instruction
8193 // and MaybeRecordImplicitNullCheck.
8194
Scott Wakelingfe885462016-09-22 10:24:38 +01008195 if (index.IsValid()) {
8196 // Load types involving an "index": ArrayGet,
8197 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8198 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008199 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008200 if (index.IsConstant()) {
8201 size_t computed_offset =
8202 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008203 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008204 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008205 if (needs_null_check) {
8206 MaybeRecordImplicitNullCheck(instruction);
8207 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008208 } else {
8209 // Handle the special case of the
8210 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8211 // intrinsics, which use a register pair as index ("long
8212 // offset"), of which only the low part contains data.
8213 vixl32::Register index_reg = index.IsRegisterPair()
8214 ? LowRegisterFrom(index)
8215 : RegisterFrom(index);
8216 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008217 vixl32::Register temp = temps.Acquire();
8218 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8219 {
8220 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8221 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8222 if (needs_null_check) {
8223 MaybeRecordImplicitNullCheck(instruction);
8224 }
8225 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008226 }
8227 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008228 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8229 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008230 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008231 if (needs_null_check) {
8232 MaybeRecordImplicitNullCheck(instruction);
8233 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008234 }
8235
Roland Levillain844e6532016-11-03 16:09:47 +00008236 // Object* ref = ref_addr->AsMirrorPtr()
8237 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008238}
8239
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008240void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8241 Location out,
8242 Location ref,
8243 Location obj,
8244 uint32_t offset,
8245 Location index) {
8246 DCHECK(kEmitCompilerReadBarrier);
8247
8248 // Insert a slow path based read barrier *after* the reference load.
8249 //
8250 // If heap poisoning is enabled, the unpoisoning of the loaded
8251 // reference will be carried out by the runtime within the slow
8252 // path.
8253 //
8254 // Note that `ref` currently does not get unpoisoned (when heap
8255 // poisoning is enabled), which is alright as the `ref` argument is
8256 // not used by the artReadBarrierSlow entry point.
8257 //
8258 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8259 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8260 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8261 AddSlowPath(slow_path);
8262
8263 __ B(slow_path->GetEntryLabel());
8264 __ Bind(slow_path->GetExitLabel());
8265}
8266
8267void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008268 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008269 Location ref,
8270 Location obj,
8271 uint32_t offset,
8272 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008273 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008274 // Baker's read barriers shall be handled by the fast path
8275 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008276 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008277 // If heap poisoning is enabled, unpoisoning will be taken care of
8278 // by the runtime within the slow path.
8279 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008280 } else if (kPoisonHeapReferences) {
8281 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8282 }
8283}
8284
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008285void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8286 Location out,
8287 Location root) {
8288 DCHECK(kEmitCompilerReadBarrier);
8289
8290 // Insert a slow path based read barrier *after* the GC root load.
8291 //
8292 // Note that GC roots are not affected by heap poisoning, so we do
8293 // not need to do anything special for this here.
8294 SlowPathCodeARMVIXL* slow_path =
8295 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8296 AddSlowPath(slow_path);
8297
8298 __ B(slow_path->GetEntryLabel());
8299 __ Bind(slow_path->GetExitLabel());
8300}
8301
Artem Serov02d37832016-10-25 15:25:33 +01008302// Check if the desired_dispatch_info is supported. If it is, return it,
8303// otherwise return a fall-back info that should be used instead.
8304HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008305 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008306 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008307 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008308}
8309
Scott Wakelingfe885462016-09-22 10:24:38 +01008310vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8311 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8312 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8313 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8314 if (!invoke->GetLocations()->Intrinsified()) {
8315 return RegisterFrom(location);
8316 }
8317 // For intrinsics we allow any location, so it may be on the stack.
8318 if (!location.IsRegister()) {
8319 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8320 return temp;
8321 }
8322 // For register locations, check if the register was saved. If so, get it from the stack.
8323 // Note: There is a chance that the register was saved but not overwritten, so we could
8324 // save one load. However, since this is just an intrinsic slow path we prefer this
8325 // simple and more robust approach rather that trying to determine if that's the case.
8326 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008327 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008328 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8329 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8330 return temp;
8331 }
8332 return RegisterFrom(location);
8333}
8334
TatWai Chongd8c052a2016-11-02 16:12:48 +08008335Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01008336 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008337 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008338 switch (invoke->GetMethodLoadKind()) {
8339 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8340 uint32_t offset =
8341 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8342 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008343 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8344 break;
8345 }
8346 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8347 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8348 break;
8349 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8350 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8351 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008352 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8353 HArmDexCacheArraysBase* base =
8354 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8355 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
8356 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8357 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008358 break;
8359 }
8360 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
8361 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8362 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008363 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008364 if (current_method.IsRegister()) {
8365 method_reg = RegisterFrom(current_method);
8366 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008367 DCHECK(invoke->GetLocations()->Intrinsified());
8368 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008369 method_reg = reg;
8370 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008371 }
8372 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8373 GetAssembler()->LoadFromOffset(
8374 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008375 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01008376 method_reg,
8377 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
8378 // temp = temp[index_in_cache];
8379 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8380 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8381 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008382 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01008383 break;
8384 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008385 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008386 return callee_method;
8387}
8388
8389void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8390 Location temp) {
8391 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008392
Artem Serovd4cc5b22016-11-04 11:19:09 +00008393 switch (invoke->GetCodePtrLocation()) {
8394 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8395 __ Bl(GetFrameEntryLabel());
8396 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008397 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8398 // LR = callee_method->entry_point_from_quick_compiled_code_
8399 GetAssembler()->LoadFromOffset(
8400 kLoadWord,
8401 lr,
8402 RegisterFrom(callee_method),
8403 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008404 {
8405 // 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 +00008406 ExactAssemblyScope aas(GetVIXLAssembler(),
8407 vixl32::k16BitT32InstructionSizeInBytes,
8408 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008409 // LR()
8410 __ blx(lr);
8411 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008412 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008413 }
8414
Scott Wakelingfe885462016-09-22 10:24:38 +01008415 DCHECK(!IsLeafMethod());
8416}
8417
8418void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8419 vixl32::Register temp = RegisterFrom(temp_location);
8420 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8421 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8422
8423 // Use the calling convention instead of the location of the receiver, as
8424 // intrinsics may have put the receiver in a different register. In the intrinsics
8425 // slow path, the arguments have been moved to the right place, so here we are
8426 // guaranteed that the receiver is the first register of the calling convention.
8427 InvokeDexCallingConventionARMVIXL calling_convention;
8428 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8429 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008430 {
8431 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008432 ExactAssemblyScope aas(GetVIXLAssembler(),
8433 vixl32::kMaxInstructionSizeInBytes,
8434 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008435 // /* HeapReference<Class> */ temp = receiver->klass_
8436 __ ldr(temp, MemOperand(receiver, class_offset));
8437 MaybeRecordImplicitNullCheck(invoke);
8438 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008439 // Instead of simply (possibly) unpoisoning `temp` here, we should
8440 // emit a read barrier for the previous class reference load.
8441 // However this is not required in practice, as this is an
8442 // intermediate/temporary reference and because the current
8443 // concurrent copying collector keeps the from-space memory
8444 // intact/accessible until the end of the marking phase (the
8445 // concurrent copying collector may not in the future).
8446 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8447
8448 // temp = temp->GetMethodAt(method_offset);
8449 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8450 kArmPointerSize).Int32Value();
8451 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8452 // LR = temp->GetEntryPoint();
8453 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8454 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008455 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8456 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8457 // that.
8458 // 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 +00008459 ExactAssemblyScope aas(GetVIXLAssembler(),
8460 vixl32::k16BitT32InstructionSizeInBytes,
8461 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008462 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01008463}
8464
Artem Serovd4cc5b22016-11-04 11:19:09 +00008465CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008466 const DexFile& dex_file, dex::StringIndex string_index) {
8467 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008468}
8469
8470CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8471 const DexFile& dex_file, dex::TypeIndex type_index) {
8472 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8473}
8474
Vladimir Marko1998cd02017-01-13 13:02:58 +00008475CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8476 const DexFile& dex_file, dex::TypeIndex type_index) {
8477 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8478}
8479
Artem Serovd4cc5b22016-11-04 11:19:09 +00008480CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8481 const DexFile& dex_file, uint32_t element_offset) {
8482 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8483}
8484
8485CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8486 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8487 patches->emplace_back(dex_file, offset_or_index);
8488 return &patches->back();
8489}
8490
Artem Serovc5fcb442016-12-02 19:19:58 +00008491VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
8492 const DexFile& dex_file,
8493 dex::StringIndex string_index) {
8494 return boot_image_string_patches_.GetOrCreate(
8495 StringReference(&dex_file, string_index),
8496 [this]() {
8497 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8498 });
8499}
8500
8501VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
8502 const DexFile& dex_file,
8503 dex::TypeIndex type_index) {
8504 return boot_image_type_patches_.GetOrCreate(
8505 TypeReference(&dex_file, type_index),
8506 [this]() {
8507 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8508 });
8509}
8510
8511VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008512 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008513}
8514
8515VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
8516 return DeduplicateUint32Literal(address, &uint32_literals_);
8517}
8518
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008519VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8520 const DexFile& dex_file,
8521 dex::StringIndex string_index,
8522 Handle<mirror::String> handle) {
8523 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8524 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008525 return jit_string_patches_.GetOrCreate(
8526 StringReference(&dex_file, string_index),
8527 [this]() {
8528 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8529 });
8530}
8531
8532VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8533 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008534 Handle<mirror::Class> handle) {
8535 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8536 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008537 return jit_class_patches_.GetOrCreate(
8538 TypeReference(&dex_file, type_index),
8539 [this]() {
8540 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8541 });
8542}
8543
Artem Serovd4cc5b22016-11-04 11:19:09 +00008544template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8545inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8546 const ArenaDeque<PcRelativePatchInfo>& infos,
8547 ArenaVector<LinkerPatch>* linker_patches) {
8548 for (const PcRelativePatchInfo& info : infos) {
8549 const DexFile& dex_file = info.target_dex_file;
8550 size_t offset_or_index = info.offset_or_index;
8551 DCHECK(info.add_pc_label.IsBound());
8552 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8553 // Add MOVW patch.
8554 DCHECK(info.movw_label.IsBound());
8555 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8556 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8557 // Add MOVT patch.
8558 DCHECK(info.movt_label.IsBound());
8559 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8560 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8561 }
8562}
8563
8564void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8565 DCHECK(linker_patches->empty());
8566 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008567 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008568 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00008569 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008570 boot_image_type_patches_.size() +
8571 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00008572 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008573 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008574 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8575 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008576 for (const auto& entry : boot_image_string_patches_) {
8577 const StringReference& target_string = entry.first;
8578 VIXLUInt32Literal* literal = entry.second;
8579 DCHECK(literal->IsBound());
8580 uint32_t literal_offset = literal->GetLocation();
8581 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8582 target_string.dex_file,
8583 target_string.string_index.index_));
8584 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008585 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008586 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008587 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8588 linker_patches);
8589 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008590 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8591 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008592 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8593 linker_patches);
8594 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008595 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8596 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008597 for (const auto& entry : boot_image_type_patches_) {
8598 const TypeReference& target_type = entry.first;
8599 VIXLUInt32Literal* literal = entry.second;
8600 DCHECK(literal->IsBound());
8601 uint32_t literal_offset = literal->GetLocation();
8602 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8603 target_type.dex_file,
8604 target_type.type_index.index_));
8605 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008606 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00008607}
8608
8609VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
8610 uint32_t value,
8611 Uint32ToLiteralMap* map) {
8612 return map->GetOrCreate(
8613 value,
8614 [this, value]() {
8615 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
8616 });
8617}
8618
8619VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
8620 MethodReference target_method,
8621 MethodToLiteralMap* map) {
8622 return map->GetOrCreate(
8623 target_method,
8624 [this]() {
8625 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8626 });
8627}
8628
Artem Serov2bbc9532016-10-21 11:51:50 +01008629void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8630 LocationSummary* locations =
8631 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8632 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8633 Location::RequiresRegister());
8634 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8635 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8637}
8638
8639void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8640 vixl32::Register res = OutputRegister(instr);
8641 vixl32::Register accumulator =
8642 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
8643 vixl32::Register mul_left =
8644 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
8645 vixl32::Register mul_right =
8646 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
8647
8648 if (instr->GetOpKind() == HInstruction::kAdd) {
8649 __ Mla(res, mul_left, mul_right, accumulator);
8650 } else {
8651 __ Mls(res, mul_left, mul_right, accumulator);
8652 }
8653}
8654
Artem Serov551b28f2016-10-18 19:11:30 +01008655void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8656 // Nothing to do, this should be removed during prepare for register allocator.
8657 LOG(FATAL) << "Unreachable";
8658}
8659
8660void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8661 // Nothing to do, this should be removed during prepare for register allocator.
8662 LOG(FATAL) << "Unreachable";
8663}
8664
8665// Simple implementation of packed switch - generate cascaded compare/jumps.
8666void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8667 LocationSummary* locations =
8668 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8669 locations->SetInAt(0, Location::RequiresRegister());
8670 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
8671 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8672 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8673 if (switch_instr->GetStartValue() != 0) {
8674 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8675 }
8676 }
8677}
8678
8679// TODO(VIXL): Investigate and reach the parity with old arm codegen.
8680void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8681 int32_t lower_bound = switch_instr->GetStartValue();
8682 uint32_t num_entries = switch_instr->GetNumEntries();
8683 LocationSummary* locations = switch_instr->GetLocations();
8684 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
8685 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8686
8687 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
8688 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8689 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008690 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008691 vixl32::Register temp_reg = temps.Acquire();
8692 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8693 // the immediate, because IP is used as the destination register. For the other
8694 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8695 // and they can be encoded in the instruction without making use of IP register.
8696 __ Adds(temp_reg, value_reg, -lower_bound);
8697
8698 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8699 // Jump to successors[0] if value == lower_bound.
8700 __ B(eq, codegen_->GetLabelOf(successors[0]));
8701 int32_t last_index = 0;
8702 for (; num_entries - last_index > 2; last_index += 2) {
8703 __ Adds(temp_reg, temp_reg, -2);
8704 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8705 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
8706 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8707 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
8708 }
8709 if (num_entries - last_index == 2) {
8710 // The last missing case_value.
8711 __ Cmp(temp_reg, 1);
8712 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
8713 }
8714
8715 // And the default for any other value.
8716 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8717 __ B(codegen_->GetLabelOf(default_block));
8718 }
8719 } else {
8720 // Create a table lookup.
8721 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
8722
8723 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
8724
8725 // Remove the bias.
8726 vixl32::Register key_reg;
8727 if (lower_bound != 0) {
8728 key_reg = RegisterFrom(locations->GetTemp(1));
8729 __ Sub(key_reg, value_reg, lower_bound);
8730 } else {
8731 key_reg = value_reg;
8732 }
8733
8734 // Check whether the value is in the table, jump to default block if not.
8735 __ Cmp(key_reg, num_entries - 1);
8736 __ B(hi, codegen_->GetLabelOf(default_block));
8737
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008738 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008739 vixl32::Register jump_offset = temps.Acquire();
8740
8741 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00008742 {
8743 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
8744 ExactAssemblyScope aas(GetVIXLAssembler(),
8745 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
8746 CodeBufferCheckScope::kMaximumSize);
8747 __ adr(table_base, jump_table->GetTableStartLabel());
8748 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01008749
Scott Wakeling86e9d262017-01-18 15:59:24 +00008750 // Jump to target block by branching to table_base(pc related) + offset.
8751 vixl32::Register target_address = table_base;
8752 __ add(target_address, table_base, jump_offset);
8753 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00008754
Scott Wakeling86e9d262017-01-18 15:59:24 +00008755 jump_table->EmitTable(codegen_);
8756 }
Artem Serov551b28f2016-10-18 19:11:30 +01008757 }
8758}
Artem Serovd4cc5b22016-11-04 11:19:09 +00008759void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8761 locations->SetOut(Location::RequiresRegister());
8762}
8763
8764void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8765 vixl32::Register base_reg = OutputRegister(base);
8766 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8767 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
8768 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
8769}
Artem Serov551b28f2016-10-18 19:11:30 +01008770
Artem Serov02d37832016-10-25 15:25:33 +01008771// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008772void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8773 if (!trg.IsValid()) {
8774 DCHECK_EQ(type, Primitive::kPrimVoid);
8775 return;
8776 }
8777
8778 DCHECK_NE(type, Primitive::kPrimVoid);
8779
Artem Serovd4cc5b22016-11-04 11:19:09 +00008780 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008781 if (return_loc.Equals(trg)) {
8782 return;
8783 }
8784
8785 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8786 // with the last branch.
8787 if (type == Primitive::kPrimLong) {
8788 TODO_VIXL32(FATAL);
8789 } else if (type == Primitive::kPrimDouble) {
8790 TODO_VIXL32(FATAL);
8791 } else {
8792 // Let the parallel move resolver take care of all of this.
8793 HParallelMove parallel_move(GetGraph()->GetArena());
8794 parallel_move.AddMove(return_loc, trg, type, nullptr);
8795 GetMoveResolver()->EmitNativeCode(&parallel_move);
8796 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008797}
8798
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008799void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8800 LocationSummary* locations =
8801 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8802 locations->SetInAt(0, Location::RequiresRegister());
8803 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008804}
8805
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008806void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8807 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8808 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8809 instruction->GetIndex(), kArmPointerSize).SizeValue();
8810 GetAssembler()->LoadFromOffset(kLoadWord,
8811 OutputRegister(instruction),
8812 InputRegisterAt(instruction, 0),
8813 method_offset);
8814 } else {
8815 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8816 instruction->GetIndex(), kArmPointerSize));
8817 GetAssembler()->LoadFromOffset(kLoadWord,
8818 OutputRegister(instruction),
8819 InputRegisterAt(instruction, 0),
8820 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8821 GetAssembler()->LoadFromOffset(kLoadWord,
8822 OutputRegister(instruction),
8823 OutputRegister(instruction),
8824 method_offset);
8825 }
Artem Serov551b28f2016-10-18 19:11:30 +01008826}
8827
Artem Serovc5fcb442016-12-02 19:19:58 +00008828static void PatchJitRootUse(uint8_t* code,
8829 const uint8_t* roots_data,
8830 VIXLUInt32Literal* literal,
8831 uint64_t index_in_table) {
8832 DCHECK(literal->IsBound());
8833 uint32_t literal_offset = literal->GetLocation();
8834 uintptr_t address =
8835 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8836 uint8_t* data = code + literal_offset;
8837 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8838}
8839
8840void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8841 for (const auto& entry : jit_string_patches_) {
8842 const auto& it = jit_string_roots_.find(entry.first);
8843 DCHECK(it != jit_string_roots_.end());
8844 PatchJitRootUse(code, roots_data, entry.second, it->second);
8845 }
8846 for (const auto& entry : jit_class_patches_) {
8847 const auto& it = jit_class_roots_.find(entry.first);
8848 DCHECK(it != jit_class_roots_.end());
8849 PatchJitRootUse(code, roots_data, entry.second, it->second);
8850 }
8851}
8852
Artem Serovd4cc5b22016-11-04 11:19:09 +00008853void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8854 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8855 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008856 ExactAssemblyScope aas(GetVIXLAssembler(),
8857 3 * vixl32::kMaxInstructionSizeInBytes,
8858 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008859 // TODO(VIXL): Think about using mov instead of movw.
8860 __ bind(&labels->movw_label);
8861 __ movw(out, /* placeholder */ 0u);
8862 __ bind(&labels->movt_label);
8863 __ movt(out, /* placeholder */ 0u);
8864 __ bind(&labels->add_pc_label);
8865 __ add(out, out, pc);
8866}
8867
Scott Wakelingfe885462016-09-22 10:24:38 +01008868#undef __
8869#undef QUICK_ENTRY_POINT
8870#undef TODO_VIXL32
8871
8872} // namespace arm
8873} // namespace art