blob: c00ee553ef9eb22ce615e5844ee17f114d77ae2f [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"
26#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "thread.h"
29#include "utils/arm/assembler_arm_vixl.h"
30#include "utils/arm/managed_register_arm.h"
31#include "utils/assembler.h"
32#include "utils/stack_checks.h"
33
34namespace art {
35namespace arm {
36
37namespace vixl32 = vixl::aarch32;
38using namespace vixl32; // NOLINT(build/namespaces)
39
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010040using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010041using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010042using helpers::HighDRegisterFrom;
43using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010044using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010045using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010046using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010047using helpers::InputSRegisterAt;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048using helpers::InputVRegisterAt;
49using helpers::LocationFrom;
50using helpers::LowRegisterFrom;
51using helpers::LowSRegisterFrom;
52using helpers::OutputRegister;
53using helpers::OutputSRegister;
54using helpers::OutputVRegister;
55using helpers::RegisterFrom;
56using helpers::SRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010057
58using RegisterList = vixl32::RegisterList;
59
60static bool ExpectedPairLayout(Location location) {
61 // We expected this for both core and fpu register pairs.
62 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
63}
64
Anton Kirilove28d9ae2016-10-25 18:17:23 +010065static constexpr int kCurrentMethodStackOffset = 0;
Scott Wakelingfe885462016-09-22 10:24:38 +010066static constexpr size_t kArmInstrMaxSizeInBytes = 4u;
67
68#ifdef __
69#error "ARM Codegen VIXL macro-assembler macro already defined."
70#endif
71
Scott Wakelingfe885462016-09-22 10:24:38 +010072// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
73#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
74#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
75
76// Marker that code is yet to be, and must, be implemented.
77#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
78
Scott Wakelinga7812ae2016-10-17 10:03:36 +010079// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
80// for each live D registers they treat two corresponding S registers as live ones.
81//
82// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
83// from a list of contiguous S registers a list of contiguous D registers (processing first/last
84// S registers corner cases) and save/restore this new list treating them as D registers.
85// - decreasing code size
86// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
87// restored and then used in regular non SlowPath code as D register.
88//
89// For the following example (v means the S register is live):
90// D names: | D0 | D1 | D2 | D4 | ...
91// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
92// Live? | | v | v | v | v | v | v | | ...
93//
94// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
95// as D registers.
96//
97// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
98// for lists of floating-point registers.
99static size_t SaveContiguousSRegisterList(size_t first,
100 size_t last,
101 CodeGenerator* codegen,
102 size_t stack_offset) {
103 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
104 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
105 DCHECK_LE(first, last);
106 if ((first == last) && (first == 0)) {
107 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
108 return stack_offset + kSRegSizeInBytes;
109 }
110 if (first % 2 == 1) {
111 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
112 stack_offset += kSRegSizeInBytes;
113 }
114
115 bool save_last = false;
116 if (last % 2 == 0) {
117 save_last = true;
118 --last;
119 }
120
121 if (first < last) {
122 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
123 DCHECK_EQ((last - first + 1) % 2, 0u);
124 size_t number_of_d_regs = (last - first + 1) / 2;
125
126 if (number_of_d_regs == 1) {
127 __ Vstr(d_reg, MemOperand(sp, stack_offset));
128 } else if (number_of_d_regs > 1) {
129 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
130 vixl32::Register base = sp;
131 if (stack_offset != 0) {
132 base = temps.Acquire();
133 __ Add(base, sp, stack_offset);
134 }
135 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
136 }
137 stack_offset += number_of_d_regs * kDRegSizeInBytes;
138 }
139
140 if (save_last) {
141 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
142 stack_offset += kSRegSizeInBytes;
143 }
144
145 return stack_offset;
146}
147
148static size_t RestoreContiguousSRegisterList(size_t first,
149 size_t last,
150 CodeGenerator* codegen,
151 size_t stack_offset) {
152 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
153 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
154 DCHECK_LE(first, last);
155 if ((first == last) && (first == 0)) {
156 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
157 return stack_offset + kSRegSizeInBytes;
158 }
159 if (first % 2 == 1) {
160 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
161 stack_offset += kSRegSizeInBytes;
162 }
163
164 bool restore_last = false;
165 if (last % 2 == 0) {
166 restore_last = true;
167 --last;
168 }
169
170 if (first < last) {
171 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
172 DCHECK_EQ((last - first + 1) % 2, 0u);
173 size_t number_of_d_regs = (last - first + 1) / 2;
174 if (number_of_d_regs == 1) {
175 __ Vldr(d_reg, MemOperand(sp, stack_offset));
176 } else if (number_of_d_regs > 1) {
177 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
178 vixl32::Register base = sp;
179 if (stack_offset != 0) {
180 base = temps.Acquire();
181 __ Add(base, sp, stack_offset);
182 }
183 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
184 }
185 stack_offset += number_of_d_regs * kDRegSizeInBytes;
186 }
187
188 if (restore_last) {
189 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
190 stack_offset += kSRegSizeInBytes;
191 }
192
193 return stack_offset;
194}
195
196void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
197 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
198 size_t orig_offset = stack_offset;
199
200 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
201 for (uint32_t i : LowToHighBits(core_spills)) {
202 // If the register holds an object, update the stack mask.
203 if (locations->RegisterContainsObject(i)) {
204 locations->SetStackBit(stack_offset / kVRegSize);
205 }
206 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
207 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
208 saved_core_stack_offsets_[i] = stack_offset;
209 stack_offset += kArmWordSize;
210 }
211
212 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
213 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
214
215 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
216 orig_offset = stack_offset;
217 for (uint32_t i : LowToHighBits(fp_spills)) {
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 saved_fpu_stack_offsets_[i] = stack_offset;
220 stack_offset += kArmWordSize;
221 }
222
223 stack_offset = orig_offset;
224 while (fp_spills != 0u) {
225 uint32_t begin = CTZ(fp_spills);
226 uint32_t tmp = fp_spills + (1u << begin);
227 fp_spills &= tmp; // Clear the contiguous range of 1s.
228 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
229 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
230 }
231 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
232}
233
234void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
235 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
236 size_t orig_offset = stack_offset;
237
238 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
239 for (uint32_t i : LowToHighBits(core_spills)) {
240 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
241 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
242 stack_offset += kArmWordSize;
243 }
244
245 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
246 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
247 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
248
249 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
250 while (fp_spills != 0u) {
251 uint32_t begin = CTZ(fp_spills);
252 uint32_t tmp = fp_spills + (1u << begin);
253 fp_spills &= tmp; // Clear the contiguous range of 1s.
254 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
255 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
256 }
257 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
258}
259
260class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
261 public:
262 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
263
264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
265 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
266 __ Bind(GetEntryLabel());
267 if (instruction_->CanThrowIntoCatchBlock()) {
268 // Live registers will be restored in the catch block if caught.
269 SaveLiveRegisters(codegen, instruction_->GetLocations());
270 }
271 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
272 instruction_,
273 instruction_->GetDexPc(),
274 this);
275 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
276 }
277
278 bool IsFatal() const OVERRIDE { return true; }
279
280 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
281
282 private:
283 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
284};
285
Scott Wakelingfe885462016-09-22 10:24:38 +0100286class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
287 public:
288 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
289 : SlowPathCodeARMVIXL(instruction) {}
290
291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100292 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100293 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100294 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100295 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
296 }
297
298 bool IsFatal() const OVERRIDE { return true; }
299
300 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
301
302 private:
303 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
304};
305
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100306class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
307 public:
308 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
309 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
310
311 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
312 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
313 __ Bind(GetEntryLabel());
314 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
315 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
316 if (successor_ == nullptr) {
317 __ B(GetReturnLabel());
318 } else {
319 __ B(arm_codegen->GetLabelOf(successor_));
320 }
321 }
322
323 vixl32::Label* GetReturnLabel() {
324 DCHECK(successor_ == nullptr);
325 return &return_label_;
326 }
327
328 HBasicBlock* GetSuccessor() const {
329 return successor_;
330 }
331
332 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
333
334 private:
335 // If not null, the block to branch to after the suspend check.
336 HBasicBlock* const successor_;
337
338 // If `successor_` is null, the label to branch to after the suspend check.
339 vixl32::Label return_label_;
340
341 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
342};
343
Scott Wakelingc34dba72016-10-03 10:14:44 +0100344class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
345 public:
346 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
347 : SlowPathCodeARMVIXL(instruction) {}
348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
350 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
351 LocationSummary* locations = instruction_->GetLocations();
352
353 __ Bind(GetEntryLabel());
354 if (instruction_->CanThrowIntoCatchBlock()) {
355 // Live registers will be restored in the catch block if caught.
356 SaveLiveRegisters(codegen, instruction_->GetLocations());
357 }
358 // We're moving two locations to locations that could overlap, so we need a parallel
359 // move resolver.
360 InvokeRuntimeCallingConventionARMVIXL calling_convention;
361 codegen->EmitParallelMoves(
362 locations->InAt(0),
363 LocationFrom(calling_convention.GetRegisterAt(0)),
364 Primitive::kPrimInt,
365 locations->InAt(1),
366 LocationFrom(calling_convention.GetRegisterAt(1)),
367 Primitive::kPrimInt);
368 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
369 ? kQuickThrowStringBounds
370 : kQuickThrowArrayBounds;
371 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
372 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
373 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
374 }
375
376 bool IsFatal() const OVERRIDE { return true; }
377
378 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
379
380 private:
381 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
382};
383
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100384class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
385 public:
386 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
387 : SlowPathCodeARMVIXL(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
388 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
389 }
390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
392 LocationSummary* locations = at_->GetLocations();
393
394 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, locations);
397
398 InvokeRuntimeCallingConventionARMVIXL calling_convention;
399 __ Mov(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
400 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
401 : kQuickInitializeType;
402 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
403 if (do_clinit_) {
404 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
405 } else {
406 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
407 }
408
409 // Move the class to the desired location.
410 Location out = locations->Out();
411 if (out.IsValid()) {
412 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
413 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
414 }
415 RestoreLiveRegisters(codegen, locations);
416 __ B(GetExitLabel());
417 }
418
419 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
420
421 private:
422 // The class this slow path will load.
423 HLoadClass* const cls_;
424
425 // The instruction where this slow path is happening.
426 // (Might be the load class or an initialization check).
427 HInstruction* const at_;
428
429 // The dex PC of `at_`.
430 const uint32_t dex_pc_;
431
432 // Whether to initialize the class.
433 const bool do_clinit_;
434
435 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
436};
437
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100438class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
439 public:
440 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
441 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
444 LocationSummary* locations = instruction_->GetLocations();
445 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
446 : locations->Out();
447 DCHECK(instruction_->IsCheckCast()
448 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
449
450 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
451 __ Bind(GetEntryLabel());
452
453 if (!is_fatal_) {
454 TODO_VIXL32(FATAL);
455 }
456
457 // We're moving two locations to locations that could overlap, so we need a parallel
458 // move resolver.
459 InvokeRuntimeCallingConventionARMVIXL calling_convention;
460 codegen->EmitParallelMoves(
461 locations->InAt(1),
462 LocationFrom(calling_convention.GetRegisterAt(0)),
463 Primitive::kPrimNot,
464 object_class,
465 LocationFrom(calling_convention.GetRegisterAt(1)),
466 Primitive::kPrimNot);
467
468 if (instruction_->IsInstanceOf()) {
469 TODO_VIXL32(FATAL);
470 } else {
471 DCHECK(instruction_->IsCheckCast());
472 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
473 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
474 }
475
476 if (!is_fatal_) {
477 TODO_VIXL32(FATAL);
478 }
479 }
480
481 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
482
483 bool IsFatal() const OVERRIDE { return is_fatal_; }
484
485 private:
486 const bool is_fatal_;
487
488 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
489};
490
Scott Wakelingc34dba72016-10-03 10:14:44 +0100491class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
492 public:
493 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
494 : SlowPathCodeARMVIXL(instruction) {}
495
496 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
497 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
498 __ Bind(GetEntryLabel());
499 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
500 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
501 }
502
503 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
504
505 private:
506 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
507};
508
509class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
510 public:
511 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
512
513 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
514 LocationSummary* locations = instruction_->GetLocations();
515 __ Bind(GetEntryLabel());
516 SaveLiveRegisters(codegen, locations);
517
518 InvokeRuntimeCallingConventionARMVIXL calling_convention;
519 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
520 parallel_move.AddMove(
521 locations->InAt(0),
522 LocationFrom(calling_convention.GetRegisterAt(0)),
523 Primitive::kPrimNot,
524 nullptr);
525 parallel_move.AddMove(
526 locations->InAt(1),
527 LocationFrom(calling_convention.GetRegisterAt(1)),
528 Primitive::kPrimInt,
529 nullptr);
530 parallel_move.AddMove(
531 locations->InAt(2),
532 LocationFrom(calling_convention.GetRegisterAt(2)),
533 Primitive::kPrimNot,
534 nullptr);
535 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
536
537 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
538 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
539 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
540 RestoreLiveRegisters(codegen, locations);
541 __ B(GetExitLabel());
542 }
543
544 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
545
546 private:
547 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
548};
549
550
Scott Wakelingfe885462016-09-22 10:24:38 +0100551inline vixl32::Condition ARMCondition(IfCondition cond) {
552 switch (cond) {
553 case kCondEQ: return eq;
554 case kCondNE: return ne;
555 case kCondLT: return lt;
556 case kCondLE: return le;
557 case kCondGT: return gt;
558 case kCondGE: return ge;
559 case kCondB: return lo;
560 case kCondBE: return ls;
561 case kCondA: return hi;
562 case kCondAE: return hs;
563 }
564 LOG(FATAL) << "Unreachable";
565 UNREACHABLE();
566}
567
568// Maps signed condition to unsigned condition.
569inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
570 switch (cond) {
571 case kCondEQ: return eq;
572 case kCondNE: return ne;
573 // Signed to unsigned.
574 case kCondLT: return lo;
575 case kCondLE: return ls;
576 case kCondGT: return hi;
577 case kCondGE: return hs;
578 // Unsigned remain unchanged.
579 case kCondB: return lo;
580 case kCondBE: return ls;
581 case kCondA: return hi;
582 case kCondAE: return hs;
583 }
584 LOG(FATAL) << "Unreachable";
585 UNREACHABLE();
586}
587
588inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
589 // The ARM condition codes can express all the necessary branches, see the
590 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
591 // There is no dex instruction or HIR that would need the missing conditions
592 // "equal or unordered" or "not equal".
593 switch (cond) {
594 case kCondEQ: return eq;
595 case kCondNE: return ne /* unordered */;
596 case kCondLT: return gt_bias ? cc : lt /* unordered */;
597 case kCondLE: return gt_bias ? ls : le /* unordered */;
598 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
599 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
600 default:
601 LOG(FATAL) << "UNREACHABLE";
602 UNREACHABLE();
603 }
604}
605
Scott Wakelingfe885462016-09-22 10:24:38 +0100606void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
607 stream << vixl32::Register(reg);
608}
609
610void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
611 stream << vixl32::SRegister(reg);
612}
613
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100614static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100615 uint32_t mask = 0;
616 for (uint32_t i = regs.GetFirstSRegister().GetCode();
617 i <= regs.GetLastSRegister().GetCode();
618 ++i) {
619 mask |= (1 << i);
620 }
621 return mask;
622}
623
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
625 GetAssembler()->LoadSFromOffset(vixl32::SRegister(reg_id), sp, stack_index);
626 return kArmWordSize;
627}
628
Scott Wakelingfe885462016-09-22 10:24:38 +0100629#undef __
630
631CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
632 const ArmInstructionSetFeatures& isa_features,
633 const CompilerOptions& compiler_options,
634 OptimizingCompilerStats* stats)
635 : CodeGenerator(graph,
636 kNumberOfCoreRegisters,
637 kNumberOfSRegisters,
638 kNumberOfRegisterPairs,
639 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100640 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +0100641 compiler_options,
642 stats),
643 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
644 location_builder_(graph, this),
645 instruction_visitor_(graph, this),
646 move_resolver_(graph->GetArena(), this),
647 assembler_(graph->GetArena()),
648 isa_features_(isa_features) {
649 // Always save the LR register to mimic Quick.
650 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100651 // Give d14 and d15 as scratch registers to VIXL.
652 // They are removed from the register allocator in `SetupBlockedRegisters()`.
653 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
654 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
655 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
656 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
657 // also want to investigate giving those 14 other D registers to the allocator.
658 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
659 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +0100660}
661
662#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()->
663
664void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
665 GetAssembler()->FinalizeCode();
666 CodeGenerator::Finalize(allocator);
667}
668
669void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +0100670 // Stack register, LR and PC are always reserved.
671 blocked_core_registers_[SP] = true;
672 blocked_core_registers_[LR] = true;
673 blocked_core_registers_[PC] = true;
674
675 // Reserve thread register.
676 blocked_core_registers_[TR] = true;
677
678 // Reserve temp register.
679 blocked_core_registers_[IP] = true;
680
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100681 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
682 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
683 blocked_fpu_registers_[28] = true;
684 blocked_fpu_registers_[29] = true;
685 blocked_fpu_registers_[30] = true;
686 blocked_fpu_registers_[31] = true;
687
Scott Wakelingfe885462016-09-22 10:24:38 +0100688 if (GetGraph()->IsDebuggable()) {
689 // Stubs do not save callee-save floating point registers. If the graph
690 // is debuggable, we need to deal with these registers differently. For
691 // now, just block them.
692 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
693 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
694 ++i) {
695 blocked_fpu_registers_[i] = true;
696 }
697 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100698}
699
Scott Wakelingfe885462016-09-22 10:24:38 +0100700InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
701 CodeGeneratorARMVIXL* codegen)
702 : InstructionCodeGenerator(graph, codegen),
703 assembler_(codegen->GetAssembler()),
704 codegen_(codegen) {}
705
706void CodeGeneratorARMVIXL::ComputeSpillMask() {
707 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
708 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
709 // There is no easy instruction to restore just the PC on thumb2. We spill and
710 // restore another arbitrary register.
711 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
712 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
713 // We use vpush and vpop for saving and restoring floating point registers, which take
714 // a SRegister and the number of registers to save/restore after that SRegister. We
715 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
716 // but in the range.
717 if (fpu_spill_mask_ != 0) {
718 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
719 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
720 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
721 fpu_spill_mask_ |= (1 << i);
722 }
723 }
724}
725
726void CodeGeneratorARMVIXL::GenerateFrameEntry() {
727 bool skip_overflow_check =
728 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
729 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
730 __ Bind(&frame_entry_label_);
731
732 if (HasEmptyFrame()) {
733 return;
734 }
735
Scott Wakelingfe885462016-09-22 10:24:38 +0100736 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100737 UseScratchRegisterScope temps(GetVIXLAssembler());
738 vixl32::Register temp = temps.Acquire();
Scott Wakelingfe885462016-09-22 10:24:38 +0100739 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
740 // The load must immediately precede RecordPcInfo.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100741 AssemblerAccurateScope aas(GetVIXLAssembler(),
742 kArmInstrMaxSizeInBytes,
743 CodeBufferCheckScope::kMaximumSize);
744 __ ldr(temp, MemOperand(temp));
745 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +0100746 }
747
748 __ Push(RegisterList(core_spill_mask_));
749 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
750 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
751 0,
752 core_spill_mask_,
753 kArmWordSize);
754 if (fpu_spill_mask_ != 0) {
755 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
756
757 // Check that list is contiguous.
758 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
759
760 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
761 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100762 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +0100763 }
764 int adjust = GetFrameSize() - FrameEntrySpillSize();
765 __ Sub(sp, sp, adjust);
766 GetAssembler()->cfi().AdjustCFAOffset(adjust);
767 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
768}
769
770void CodeGeneratorARMVIXL::GenerateFrameExit() {
771 if (HasEmptyFrame()) {
772 __ Bx(lr);
773 return;
774 }
775 GetAssembler()->cfi().RememberState();
776 int adjust = GetFrameSize() - FrameEntrySpillSize();
777 __ Add(sp, sp, adjust);
778 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
779 if (fpu_spill_mask_ != 0) {
780 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
781
782 // Check that list is contiguous.
783 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
784
785 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
786 GetAssembler()->cfi().AdjustCFAOffset(
787 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100788 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +0100789 }
790 // Pop LR into PC to return.
791 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
792 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
793 __ Pop(RegisterList(pop_mask));
794 GetAssembler()->cfi().RestoreState();
795 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
796}
797
798void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
799 __ Bind(GetLabelOf(block));
800}
801
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100802void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
803 if (source.Equals(destination)) {
804 return;
805 }
806 if (destination.IsRegister()) {
807 if (source.IsRegister()) {
808 __ Mov(RegisterFrom(destination), RegisterFrom(source));
809 } else if (source.IsFpuRegister()) {
810 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
811 } else {
812 GetAssembler()->LoadFromOffset(kLoadWord,
813 RegisterFrom(destination),
814 sp,
815 source.GetStackIndex());
816 }
817 } else if (destination.IsFpuRegister()) {
818 if (source.IsRegister()) {
819 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
820 } else if (source.IsFpuRegister()) {
821 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
822 } else {
823 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
824 }
825 } else {
826 DCHECK(destination.IsStackSlot()) << destination;
827 if (source.IsRegister()) {
828 GetAssembler()->StoreToOffset(kStoreWord,
829 RegisterFrom(source),
830 sp,
831 destination.GetStackIndex());
832 } else if (source.IsFpuRegister()) {
833 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
834 } else {
835 DCHECK(source.IsStackSlot()) << source;
836 UseScratchRegisterScope temps(GetVIXLAssembler());
837 vixl32::Register temp = temps.Acquire();
838 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
839 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
840 }
841 }
842}
843
844void CodeGeneratorARMVIXL::MoveConstant(Location destination ATTRIBUTE_UNUSED,
845 int32_t value ATTRIBUTE_UNUSED) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100846 TODO_VIXL32(FATAL);
847}
848
849void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100850 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
851 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
852 HParallelMove move(GetGraph()->GetArena());
853 move.AddMove(src, dst, dst_type, nullptr);
854 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +0100855}
856
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100857void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location ATTRIBUTE_UNUSED,
858 LocationSummary* locations ATTRIBUTE_UNUSED) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100859 TODO_VIXL32(FATAL);
860}
861
862void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
863 HInstruction* instruction,
864 uint32_t dex_pc,
865 SlowPathCode* slow_path) {
866 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
867 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
868 if (EntrypointRequiresStackMap(entrypoint)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100869 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
870 // previous instruction.
Scott Wakelingfe885462016-09-22 10:24:38 +0100871 RecordPcInfo(instruction, dex_pc, slow_path);
872 }
873}
874
875void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
876 HInstruction* instruction,
877 SlowPathCode* slow_path) {
878 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
879 GenerateInvokeRuntime(entry_point_offset);
880}
881
882void CodeGeneratorARMVIXL::GenerateInvokeRuntime(int32_t entry_point_offset) {
883 GetAssembler()->LoadFromOffset(kLoadWord, lr, tr, entry_point_offset);
884 __ Blx(lr);
885}
886
Scott Wakelingfe885462016-09-22 10:24:38 +0100887void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
888 DCHECK(!successor->IsExitBlock());
889 HBasicBlock* block = got->GetBlock();
890 HInstruction* previous = got->GetPrevious();
891 HLoopInformation* info = block->GetLoopInformation();
892
893 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
894 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
895 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
896 return;
897 }
898 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
899 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
900 }
901 if (!codegen_->GoesToNextBlock(block, successor)) {
902 __ B(codegen_->GetLabelOf(successor));
903 }
904}
905
906void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
907 got->SetLocations(nullptr);
908}
909
910void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
911 HandleGoto(got, got->GetSuccessor());
912}
913
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100914void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
915 try_boundary->SetLocations(nullptr);
916}
917
918void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
919 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
920 if (!successor->IsExitBlock()) {
921 HandleGoto(try_boundary, successor);
922 }
923}
924
Scott Wakelingfe885462016-09-22 10:24:38 +0100925void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
926 exit->SetLocations(nullptr);
927}
928
929void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
930}
931
932void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
933 Primitive::Type type = instruction->InputAt(0)->GetType();
934 Location lhs_loc = instruction->GetLocations()->InAt(0);
935 Location rhs_loc = instruction->GetLocations()->InAt(1);
936 if (rhs_loc.IsConstant()) {
937 // 0.0 is the only immediate that can be encoded directly in
938 // a VCMP instruction.
939 //
940 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
941 // specify that in a floating-point comparison, positive zero
942 // and negative zero are considered equal, so we can use the
943 // literal 0.0 for both cases here.
944 //
945 // Note however that some methods (Float.equal, Float.compare,
946 // Float.compareTo, Double.equal, Double.compare,
947 // Double.compareTo, Math.max, Math.min, StrictMath.max,
948 // StrictMath.min) consider 0.0 to be (strictly) greater than
949 // -0.0. So if we ever translate calls to these methods into a
950 // HCompare instruction, we must handle the -0.0 case with
951 // care here.
952 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
953 if (type == Primitive::kPrimFloat) {
954 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
955 } else {
956 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100957 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +0100958 }
959 } else {
960 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100961 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +0100962 } else {
963 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100964 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +0100965 }
966 }
967}
968
969void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
970 vixl32::Label* true_label,
971 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
972 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
973 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
974 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
975}
976
977void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
978 vixl32::Label* true_label,
979 vixl32::Label* false_label) {
980 LocationSummary* locations = cond->GetLocations();
981 Location left = locations->InAt(0);
982 Location right = locations->InAt(1);
983 IfCondition if_cond = cond->GetCondition();
984
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100985 vixl32::Register left_high = HighRegisterFrom(left);
986 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +0100987 IfCondition true_high_cond = if_cond;
988 IfCondition false_high_cond = cond->GetOppositeCondition();
989 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
990
991 // Set the conditions for the test, remembering that == needs to be
992 // decided using the low words.
993 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
994 switch (if_cond) {
995 case kCondEQ:
996 case kCondNE:
997 // Nothing to do.
998 break;
999 case kCondLT:
1000 false_high_cond = kCondGT;
1001 break;
1002 case kCondLE:
1003 true_high_cond = kCondLT;
1004 break;
1005 case kCondGT:
1006 false_high_cond = kCondLT;
1007 break;
1008 case kCondGE:
1009 true_high_cond = kCondGT;
1010 break;
1011 case kCondB:
1012 false_high_cond = kCondA;
1013 break;
1014 case kCondBE:
1015 true_high_cond = kCondB;
1016 break;
1017 case kCondA:
1018 false_high_cond = kCondB;
1019 break;
1020 case kCondAE:
1021 true_high_cond = kCondA;
1022 break;
1023 }
1024 if (right.IsConstant()) {
1025 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1026 int32_t val_low = Low32Bits(value);
1027 int32_t val_high = High32Bits(value);
1028
1029 __ Cmp(left_high, val_high);
1030 if (if_cond == kCondNE) {
1031 __ B(ARMCondition(true_high_cond), true_label);
1032 } else if (if_cond == kCondEQ) {
1033 __ B(ARMCondition(false_high_cond), false_label);
1034 } else {
1035 __ B(ARMCondition(true_high_cond), true_label);
1036 __ B(ARMCondition(false_high_cond), false_label);
1037 }
1038 // Must be equal high, so compare the lows.
1039 __ Cmp(left_low, val_low);
1040 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001041 vixl32::Register right_high = HighRegisterFrom(right);
1042 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001043
1044 __ Cmp(left_high, right_high);
1045 if (if_cond == kCondNE) {
1046 __ B(ARMCondition(true_high_cond), true_label);
1047 } else if (if_cond == kCondEQ) {
1048 __ B(ARMCondition(false_high_cond), false_label);
1049 } else {
1050 __ B(ARMCondition(true_high_cond), true_label);
1051 __ B(ARMCondition(false_high_cond), false_label);
1052 }
1053 // Must be equal high, so compare the lows.
1054 __ Cmp(left_low, right_low);
1055 }
1056 // The last comparison might be unsigned.
1057 // TODO: optimize cases where this is always true/false
1058 __ B(final_condition, true_label);
1059}
1060
1061void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1062 vixl32::Label* true_target_in,
1063 vixl32::Label* false_target_in) {
1064 // Generated branching requires both targets to be explicit. If either of the
1065 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1066 vixl32::Label fallthrough;
1067 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1068 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1069
1070 Primitive::Type type = condition->InputAt(0)->GetType();
1071 switch (type) {
1072 case Primitive::kPrimLong:
1073 GenerateLongComparesAndJumps(condition, true_target, false_target);
1074 break;
1075 case Primitive::kPrimFloat:
1076 case Primitive::kPrimDouble:
1077 GenerateVcmp(condition);
1078 GenerateFPJumps(condition, true_target, false_target);
1079 break;
1080 default:
1081 LOG(FATAL) << "Unexpected compare type " << type;
1082 }
1083
1084 if (false_target != &fallthrough) {
1085 __ B(false_target);
1086 }
1087
1088 if (true_target_in == nullptr || false_target_in == nullptr) {
1089 __ Bind(&fallthrough);
1090 }
1091}
1092
1093void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1094 size_t condition_input_index,
1095 vixl32::Label* true_target,
1096 vixl32::Label* false_target) {
1097 HInstruction* cond = instruction->InputAt(condition_input_index);
1098
1099 if (true_target == nullptr && false_target == nullptr) {
1100 // Nothing to do. The code always falls through.
1101 return;
1102 } else if (cond->IsIntConstant()) {
1103 // Constant condition, statically compared against "true" (integer value 1).
1104 if (cond->AsIntConstant()->IsTrue()) {
1105 if (true_target != nullptr) {
1106 __ B(true_target);
1107 }
1108 } else {
1109 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
1110 if (false_target != nullptr) {
1111 __ B(false_target);
1112 }
1113 }
1114 return;
1115 }
1116
1117 // The following code generates these patterns:
1118 // (1) true_target == nullptr && false_target != nullptr
1119 // - opposite condition true => branch to false_target
1120 // (2) true_target != nullptr && false_target == nullptr
1121 // - condition true => branch to true_target
1122 // (3) true_target != nullptr && false_target != nullptr
1123 // - condition true => branch to true_target
1124 // - branch to false_target
1125 if (IsBooleanValueOrMaterializedCondition(cond)) {
1126 // Condition has been materialized, compare the output to 0.
1127 if (kIsDebugBuild) {
1128 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1129 DCHECK(cond_val.IsRegister());
1130 }
1131 if (true_target == nullptr) {
1132 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
1133 } else {
1134 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
1135 }
1136 } else {
1137 // Condition has not been materialized. Use its inputs as the comparison and
1138 // its condition as the branch condition.
1139 HCondition* condition = cond->AsCondition();
1140
1141 // If this is a long or FP comparison that has been folded into
1142 // the HCondition, generate the comparison directly.
1143 Primitive::Type type = condition->InputAt(0)->GetType();
1144 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1145 GenerateCompareTestAndBranch(condition, true_target, false_target);
1146 return;
1147 }
1148
1149 LocationSummary* locations = cond->GetLocations();
1150 DCHECK(locations->InAt(0).IsRegister());
1151 vixl32::Register left = InputRegisterAt(cond, 0);
1152 Location right = locations->InAt(1);
1153 if (right.IsRegister()) {
1154 __ Cmp(left, InputRegisterAt(cond, 1));
1155 } else {
1156 DCHECK(right.IsConstant());
1157 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1158 }
1159 if (true_target == nullptr) {
1160 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1161 } else {
1162 __ B(ARMCondition(condition->GetCondition()), true_target);
1163 }
1164 }
1165
1166 // If neither branch falls through (case 3), the conditional branch to `true_target`
1167 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1168 if (true_target != nullptr && false_target != nullptr) {
1169 __ B(false_target);
1170 }
1171}
1172
1173void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1175 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1176 locations->SetInAt(0, Location::RequiresRegister());
1177 }
1178}
1179
1180void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1181 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1182 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001183 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1184 nullptr : codegen_->GetLabelOf(true_successor);
1185 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1186 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001187 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1188}
1189
Scott Wakelingc34dba72016-10-03 10:14:44 +01001190void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1191 LocationSummary* locations = new (GetGraph()->GetArena())
1192 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1193 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1194 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1195 locations->SetInAt(0, Location::RequiresRegister());
1196 }
1197}
1198
1199void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1200 SlowPathCodeARMVIXL* slow_path =
1201 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
1202 GenerateTestAndBranch(deoptimize,
1203 /* condition_input_index */ 0,
1204 slow_path->GetEntryLabel(),
1205 /* false_target */ nullptr);
1206}
1207
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001208void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
1209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1210 if (Primitive::IsFloatingPointType(select->GetType())) {
1211 locations->SetInAt(0, Location::RequiresFpuRegister());
1212 locations->SetInAt(1, Location::RequiresFpuRegister());
1213 } else {
1214 locations->SetInAt(0, Location::RequiresRegister());
1215 locations->SetInAt(1, Location::RequiresRegister());
1216 }
1217 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1218 locations->SetInAt(2, Location::RequiresRegister());
1219 }
1220 locations->SetOut(Location::SameAsFirstInput());
1221}
1222
1223void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
1224 LocationSummary* locations = select->GetLocations();
1225 vixl32::Label false_target;
1226 GenerateTestAndBranch(select,
1227 /* condition_input_index */ 2,
1228 /* true_target */ nullptr,
1229 &false_target);
1230 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1231 __ Bind(&false_target);
1232}
1233
Scott Wakelingfe885462016-09-22 10:24:38 +01001234void CodeGeneratorARMVIXL::GenerateNop() {
1235 __ Nop();
1236}
1237
1238void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
1239 LocationSummary* locations =
1240 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
1241 // Handle the long/FP comparisons made in instruction simplification.
1242 switch (cond->InputAt(0)->GetType()) {
1243 case Primitive::kPrimLong:
1244 locations->SetInAt(0, Location::RequiresRegister());
1245 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1246 if (!cond->IsEmittedAtUseSite()) {
1247 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1248 }
1249 break;
1250
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001251 // TODO(VIXL): https://android-review.googlesource.com/#/c/252265/
Scott Wakelingfe885462016-09-22 10:24:38 +01001252 case Primitive::kPrimFloat:
1253 case Primitive::kPrimDouble:
1254 locations->SetInAt(0, Location::RequiresFpuRegister());
1255 locations->SetInAt(1, Location::RequiresFpuRegister());
1256 if (!cond->IsEmittedAtUseSite()) {
1257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1258 }
1259 break;
1260
1261 default:
1262 locations->SetInAt(0, Location::RequiresRegister());
1263 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1264 if (!cond->IsEmittedAtUseSite()) {
1265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1266 }
1267 }
1268}
1269
1270void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
1271 if (cond->IsEmittedAtUseSite()) {
1272 return;
1273 }
1274
Scott Wakelingfe885462016-09-22 10:24:38 +01001275 vixl32::Register out = OutputRegister(cond);
1276 vixl32::Label true_label, false_label;
1277
1278 switch (cond->InputAt(0)->GetType()) {
1279 default: {
1280 // Integer case.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001281 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
1282 AssemblerAccurateScope aas(GetVIXLAssembler(),
1283 kArmInstrMaxSizeInBytes * 3u,
1284 CodeBufferCheckScope::kMaximumSize);
1285 __ ite(ARMCondition(cond->GetCondition()));
1286 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
1287 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001288 return;
1289 }
1290 case Primitive::kPrimLong:
1291 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1292 break;
1293 case Primitive::kPrimFloat:
1294 case Primitive::kPrimDouble:
1295 GenerateVcmp(cond);
1296 GenerateFPJumps(cond, &true_label, &false_label);
1297 break;
1298 }
1299
1300 // Convert the jumps into the result.
1301 vixl32::Label done_label;
1302
1303 // False case: result = 0.
1304 __ Bind(&false_label);
1305 __ Mov(out, 0);
1306 __ B(&done_label);
1307
1308 // True case: result = 1.
1309 __ Bind(&true_label);
1310 __ Mov(out, 1);
1311 __ Bind(&done_label);
1312}
1313
1314void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
1315 HandleCondition(comp);
1316}
1317
1318void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
1319 HandleCondition(comp);
1320}
1321
1322void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
1323 HandleCondition(comp);
1324}
1325
1326void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
1327 HandleCondition(comp);
1328}
1329
1330void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
1331 HandleCondition(comp);
1332}
1333
1334void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
1335 HandleCondition(comp);
1336}
1337
1338void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1339 HandleCondition(comp);
1340}
1341
1342void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1343 HandleCondition(comp);
1344}
1345
1346void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1347 HandleCondition(comp);
1348}
1349
1350void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1351 HandleCondition(comp);
1352}
1353
1354void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1355 HandleCondition(comp);
1356}
1357
1358void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1359 HandleCondition(comp);
1360}
1361
1362void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
1363 HandleCondition(comp);
1364}
1365
1366void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
1367 HandleCondition(comp);
1368}
1369
1370void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1371 HandleCondition(comp);
1372}
1373
1374void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1375 HandleCondition(comp);
1376}
1377
1378void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
1379 HandleCondition(comp);
1380}
1381
1382void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
1383 HandleCondition(comp);
1384}
1385
1386void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1387 HandleCondition(comp);
1388}
1389
1390void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1391 HandleCondition(comp);
1392}
1393
1394void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
1395 LocationSummary* locations =
1396 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1397 locations->SetOut(Location::ConstantLocation(constant));
1398}
1399
1400void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
1401 // Will be generated at use site.
1402}
1403
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001404void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
1405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1407 locations->SetOut(Location::ConstantLocation(constant));
1408}
1409
1410void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
1411 // Will be generated at use site.
1412}
1413
Scott Wakelingfe885462016-09-22 10:24:38 +01001414void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
1415 LocationSummary* locations =
1416 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1417 locations->SetOut(Location::ConstantLocation(constant));
1418}
1419
1420void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
1421 // Will be generated at use site.
1422}
1423
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001424void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
1425 LocationSummary* locations =
1426 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1427 locations->SetOut(Location::ConstantLocation(constant));
1428}
1429
Scott Wakelingc34dba72016-10-03 10:14:44 +01001430void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
1431 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001432 // Will be generated at use site.
1433}
1434
1435void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
1436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1438 locations->SetOut(Location::ConstantLocation(constant));
1439}
1440
Scott Wakelingc34dba72016-10-03 10:14:44 +01001441void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
1442 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001443 // Will be generated at use site.
1444}
1445
Scott Wakelingfe885462016-09-22 10:24:38 +01001446void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1447 memory_barrier->SetLocations(nullptr);
1448}
1449
1450void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1451 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1452}
1453
1454void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
1455 ret->SetLocations(nullptr);
1456}
1457
1458void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
1459 codegen_->GenerateFrameExit();
1460}
1461
1462void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
1463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
1465 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
1466}
1467
1468void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
1469 codegen_->GenerateFrameExit();
1470}
1471
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001472void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1473 // Explicit clinit checks triggered by static invokes must have been pruned by
1474 // art::PrepareForRegisterAllocation.
1475 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1476
1477 // TODO(VIXL): TryDispatch
1478
1479 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001480
1481 // TODO(VIXL): invoke->HasPcRelativeDexCache()
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001482}
1483
1484void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1485 // Explicit clinit checks triggered by static invokes must have been pruned by
1486 // art::PrepareForRegisterAllocation.
1487 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1488
1489 // TODO(VIXL): TryGenerateIntrinsicCode
1490
1491 LocationSummary* locations = invoke->GetLocations();
1492 DCHECK(locations->HasTemps());
1493 codegen_->GenerateStaticOrDirectCall(invoke, locations->GetTemp(0));
1494 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1495 // previous instruction.
1496 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1497}
1498
1499void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
1500 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
1501 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
1502}
1503
1504void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1505 // TODO(VIXL): TryDispatch
1506
1507 HandleInvoke(invoke);
1508}
1509
1510void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1511 // TODO(VIXL): TryGenerateIntrinsicCode
1512
1513 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
1514 DCHECK(!codegen_->IsLeafMethod());
1515 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1516 // previous instruction.
1517 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1518}
1519
Artem Serov02109dd2016-09-23 17:17:54 +01001520void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
1521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1523 switch (neg->GetResultType()) {
1524 case Primitive::kPrimInt: {
1525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1527 break;
1528 }
1529 case Primitive::kPrimLong: {
1530 locations->SetInAt(0, Location::RequiresRegister());
1531 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1532 break;
1533 }
1534
1535 case Primitive::kPrimFloat:
1536 case Primitive::kPrimDouble:
1537 locations->SetInAt(0, Location::RequiresFpuRegister());
1538 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1539 break;
1540
1541 default:
1542 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1543 }
1544}
1545
1546void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
1547 LocationSummary* locations = neg->GetLocations();
1548 Location out = locations->Out();
1549 Location in = locations->InAt(0);
1550 switch (neg->GetResultType()) {
1551 case Primitive::kPrimInt:
1552 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
1553 break;
1554
1555 case Primitive::kPrimLong:
1556 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1557 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
1558 // We cannot emit an RSC (Reverse Subtract with Carry)
1559 // instruction here, as it does not exist in the Thumb-2
1560 // instruction set. We use the following approach
1561 // using SBC and SUB instead.
1562 //
1563 // out.hi = -C
1564 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
1565 // out.hi = out.hi - in.hi
1566 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
1567 break;
1568
1569 case Primitive::kPrimFloat:
1570 case Primitive::kPrimDouble:
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001571 // TODO(VIXL): Consider introducing an InputVRegister()
1572 // helper function (equivalent to InputRegister()).
Artem Serov02109dd2016-09-23 17:17:54 +01001573 __ Vneg(OutputVRegister(neg), InputVRegisterAt(neg, 0));
1574 break;
1575
1576 default:
1577 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1578 }
1579}
1580
Scott Wakelingfe885462016-09-22 10:24:38 +01001581void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1582 Primitive::Type result_type = conversion->GetResultType();
1583 Primitive::Type input_type = conversion->GetInputType();
1584 DCHECK_NE(result_type, input_type);
1585
1586 // The float-to-long, double-to-long and long-to-float type conversions
1587 // rely on a call to the runtime.
1588 LocationSummary::CallKind call_kind =
1589 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1590 && result_type == Primitive::kPrimLong)
1591 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
1592 ? LocationSummary::kCallOnMainOnly
1593 : LocationSummary::kNoCall;
1594 LocationSummary* locations =
1595 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1596
1597 // The Java language does not allow treating boolean as an integral type but
1598 // our bit representation makes it safe.
1599
1600 switch (result_type) {
1601 case Primitive::kPrimByte:
1602 switch (input_type) {
1603 case Primitive::kPrimLong:
1604 // Type conversion from long to byte is a result of code transformations.
1605 case Primitive::kPrimBoolean:
1606 // Boolean input is a result of code transformations.
1607 case Primitive::kPrimShort:
1608 case Primitive::kPrimInt:
1609 case Primitive::kPrimChar:
1610 // Processing a Dex `int-to-byte' instruction.
1611 locations->SetInAt(0, Location::RequiresRegister());
1612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 }
1619 break;
1620
1621 case Primitive::kPrimShort:
1622 switch (input_type) {
1623 case Primitive::kPrimLong:
1624 // Type conversion from long to short is a result of code transformations.
1625 case Primitive::kPrimBoolean:
1626 // Boolean input is a result of code transformations.
1627 case Primitive::kPrimByte:
1628 case Primitive::kPrimInt:
1629 case Primitive::kPrimChar:
1630 // Processing a Dex `int-to-short' instruction.
1631 locations->SetInAt(0, Location::RequiresRegister());
1632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1633 break;
1634
1635 default:
1636 LOG(FATAL) << "Unexpected type conversion from " << input_type
1637 << " to " << result_type;
1638 }
1639 break;
1640
1641 case Primitive::kPrimInt:
1642 switch (input_type) {
1643 case Primitive::kPrimLong:
1644 // Processing a Dex `long-to-int' instruction.
1645 locations->SetInAt(0, Location::Any());
1646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1647 break;
1648
1649 case Primitive::kPrimFloat:
1650 // Processing a Dex `float-to-int' instruction.
1651 locations->SetInAt(0, Location::RequiresFpuRegister());
1652 locations->SetOut(Location::RequiresRegister());
1653 locations->AddTemp(Location::RequiresFpuRegister());
1654 break;
1655
1656 case Primitive::kPrimDouble:
1657 // Processing a Dex `double-to-int' instruction.
1658 locations->SetInAt(0, Location::RequiresFpuRegister());
1659 locations->SetOut(Location::RequiresRegister());
1660 locations->AddTemp(Location::RequiresFpuRegister());
1661 break;
1662
1663 default:
1664 LOG(FATAL) << "Unexpected type conversion from " << input_type
1665 << " to " << result_type;
1666 }
1667 break;
1668
1669 case Primitive::kPrimLong:
1670 switch (input_type) {
1671 case Primitive::kPrimBoolean:
1672 // Boolean input is a result of code transformations.
1673 case Primitive::kPrimByte:
1674 case Primitive::kPrimShort:
1675 case Primitive::kPrimInt:
1676 case Primitive::kPrimChar:
1677 // Processing a Dex `int-to-long' instruction.
1678 locations->SetInAt(0, Location::RequiresRegister());
1679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1680 break;
1681
1682 case Primitive::kPrimFloat: {
1683 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001684 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1685 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1686 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001687 break;
1688 }
1689
1690 case Primitive::kPrimDouble: {
1691 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001692 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1693 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
1694 calling_convention.GetFpuRegisterAt(1)));
1695 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001696 break;
1697 }
1698
1699 default:
1700 LOG(FATAL) << "Unexpected type conversion from " << input_type
1701 << " to " << result_type;
1702 }
1703 break;
1704
1705 case Primitive::kPrimChar:
1706 switch (input_type) {
1707 case Primitive::kPrimLong:
1708 // Type conversion from long to char is a result of code transformations.
1709 case Primitive::kPrimBoolean:
1710 // Boolean input is a result of code transformations.
1711 case Primitive::kPrimByte:
1712 case Primitive::kPrimShort:
1713 case Primitive::kPrimInt:
1714 // Processing a Dex `int-to-char' instruction.
1715 locations->SetInAt(0, Location::RequiresRegister());
1716 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1717 break;
1718
1719 default:
1720 LOG(FATAL) << "Unexpected type conversion from " << input_type
1721 << " to " << result_type;
1722 }
1723 break;
1724
1725 case Primitive::kPrimFloat:
1726 switch (input_type) {
1727 case Primitive::kPrimBoolean:
1728 // Boolean input is a result of code transformations.
1729 case Primitive::kPrimByte:
1730 case Primitive::kPrimShort:
1731 case Primitive::kPrimInt:
1732 case Primitive::kPrimChar:
1733 // Processing a Dex `int-to-float' instruction.
1734 locations->SetInAt(0, Location::RequiresRegister());
1735 locations->SetOut(Location::RequiresFpuRegister());
1736 break;
1737
1738 case Primitive::kPrimLong: {
1739 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001740 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1741 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
1742 calling_convention.GetRegisterAt(1)));
1743 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001744 break;
1745 }
1746
1747 case Primitive::kPrimDouble:
1748 // Processing a Dex `double-to-float' instruction.
1749 locations->SetInAt(0, Location::RequiresFpuRegister());
1750 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1751 break;
1752
1753 default:
1754 LOG(FATAL) << "Unexpected type conversion from " << input_type
1755 << " to " << result_type;
1756 };
1757 break;
1758
1759 case Primitive::kPrimDouble:
1760 switch (input_type) {
1761 case Primitive::kPrimBoolean:
1762 // Boolean input is a result of code transformations.
1763 case Primitive::kPrimByte:
1764 case Primitive::kPrimShort:
1765 case Primitive::kPrimInt:
1766 case Primitive::kPrimChar:
1767 // Processing a Dex `int-to-double' instruction.
1768 locations->SetInAt(0, Location::RequiresRegister());
1769 locations->SetOut(Location::RequiresFpuRegister());
1770 break;
1771
1772 case Primitive::kPrimLong:
1773 // Processing a Dex `long-to-double' instruction.
1774 locations->SetInAt(0, Location::RequiresRegister());
1775 locations->SetOut(Location::RequiresFpuRegister());
1776 locations->AddTemp(Location::RequiresFpuRegister());
1777 locations->AddTemp(Location::RequiresFpuRegister());
1778 break;
1779
1780 case Primitive::kPrimFloat:
1781 // Processing a Dex `float-to-double' instruction.
1782 locations->SetInAt(0, Location::RequiresFpuRegister());
1783 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1784 break;
1785
1786 default:
1787 LOG(FATAL) << "Unexpected type conversion from " << input_type
1788 << " to " << result_type;
1789 };
1790 break;
1791
1792 default:
1793 LOG(FATAL) << "Unexpected type conversion from " << input_type
1794 << " to " << result_type;
1795 }
1796}
1797
1798void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1799 LocationSummary* locations = conversion->GetLocations();
1800 Location out = locations->Out();
1801 Location in = locations->InAt(0);
1802 Primitive::Type result_type = conversion->GetResultType();
1803 Primitive::Type input_type = conversion->GetInputType();
1804 DCHECK_NE(result_type, input_type);
1805 switch (result_type) {
1806 case Primitive::kPrimByte:
1807 switch (input_type) {
1808 case Primitive::kPrimLong:
1809 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001810 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01001811 break;
1812 case Primitive::kPrimBoolean:
1813 // Boolean input is a result of code transformations.
1814 case Primitive::kPrimShort:
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimChar:
1817 // Processing a Dex `int-to-byte' instruction.
1818 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
1819 break;
1820
1821 default:
1822 LOG(FATAL) << "Unexpected type conversion from " << input_type
1823 << " to " << result_type;
1824 }
1825 break;
1826
1827 case Primitive::kPrimShort:
1828 switch (input_type) {
1829 case Primitive::kPrimLong:
1830 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001831 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01001832 break;
1833 case Primitive::kPrimBoolean:
1834 // Boolean input is a result of code transformations.
1835 case Primitive::kPrimByte:
1836 case Primitive::kPrimInt:
1837 case Primitive::kPrimChar:
1838 // Processing a Dex `int-to-short' instruction.
1839 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
1840 break;
1841
1842 default:
1843 LOG(FATAL) << "Unexpected type conversion from " << input_type
1844 << " to " << result_type;
1845 }
1846 break;
1847
1848 case Primitive::kPrimInt:
1849 switch (input_type) {
1850 case Primitive::kPrimLong:
1851 // Processing a Dex `long-to-int' instruction.
1852 DCHECK(out.IsRegister());
1853 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001854 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01001855 } else if (in.IsDoubleStackSlot()) {
1856 GetAssembler()->LoadFromOffset(kLoadWord,
1857 OutputRegister(conversion),
1858 sp,
1859 in.GetStackIndex());
1860 } else {
1861 DCHECK(in.IsConstant());
1862 DCHECK(in.GetConstant()->IsLongConstant());
1863 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1864 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
1865 }
1866 break;
1867
1868 case Primitive::kPrimFloat: {
1869 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001870 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001871 __ Vcvt(I32, F32, temp, InputSRegisterAt(conversion, 0));
1872 __ Vmov(OutputRegister(conversion), temp);
1873 break;
1874 }
1875
1876 case Primitive::kPrimDouble: {
1877 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001878 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01001879 __ Vcvt(I32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01001880 __ Vmov(OutputRegister(conversion), temp_s);
1881 break;
1882 }
1883
1884 default:
1885 LOG(FATAL) << "Unexpected type conversion from " << input_type
1886 << " to " << result_type;
1887 }
1888 break;
1889
1890 case Primitive::kPrimLong:
1891 switch (input_type) {
1892 case Primitive::kPrimBoolean:
1893 // Boolean input is a result of code transformations.
1894 case Primitive::kPrimByte:
1895 case Primitive::kPrimShort:
1896 case Primitive::kPrimInt:
1897 case Primitive::kPrimChar:
1898 // Processing a Dex `int-to-long' instruction.
1899 DCHECK(out.IsRegisterPair());
1900 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001901 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001902 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001903 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01001904 break;
1905
1906 case Primitive::kPrimFloat:
1907 // Processing a Dex `float-to-long' instruction.
1908 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
1909 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
1910 break;
1911
1912 case Primitive::kPrimDouble:
1913 // Processing a Dex `double-to-long' instruction.
1914 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
1915 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
1916 break;
1917
1918 default:
1919 LOG(FATAL) << "Unexpected type conversion from " << input_type
1920 << " to " << result_type;
1921 }
1922 break;
1923
1924 case Primitive::kPrimChar:
1925 switch (input_type) {
1926 case Primitive::kPrimLong:
1927 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001928 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01001929 break;
1930 case Primitive::kPrimBoolean:
1931 // Boolean input is a result of code transformations.
1932 case Primitive::kPrimByte:
1933 case Primitive::kPrimShort:
1934 case Primitive::kPrimInt:
1935 // Processing a Dex `int-to-char' instruction.
1936 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
1937 break;
1938
1939 default:
1940 LOG(FATAL) << "Unexpected type conversion from " << input_type
1941 << " to " << result_type;
1942 }
1943 break;
1944
1945 case Primitive::kPrimFloat:
1946 switch (input_type) {
1947 case Primitive::kPrimBoolean:
1948 // Boolean input is a result of code transformations.
1949 case Primitive::kPrimByte:
1950 case Primitive::kPrimShort:
1951 case Primitive::kPrimInt:
1952 case Primitive::kPrimChar: {
1953 // Processing a Dex `int-to-float' instruction.
1954 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
1955 __ Vcvt(F32, I32, OutputSRegister(conversion), OutputSRegister(conversion));
1956 break;
1957 }
1958
1959 case Primitive::kPrimLong:
1960 // Processing a Dex `long-to-float' instruction.
1961 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
1962 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
1963 break;
1964
1965 case Primitive::kPrimDouble:
1966 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01001967 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01001968 break;
1969
1970 default:
1971 LOG(FATAL) << "Unexpected type conversion from " << input_type
1972 << " to " << result_type;
1973 };
1974 break;
1975
1976 case Primitive::kPrimDouble:
1977 switch (input_type) {
1978 case Primitive::kPrimBoolean:
1979 // Boolean input is a result of code transformations.
1980 case Primitive::kPrimByte:
1981 case Primitive::kPrimShort:
1982 case Primitive::kPrimInt:
1983 case Primitive::kPrimChar: {
1984 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001985 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01001986 __ Vcvt(F64, I32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01001987 break;
1988 }
1989
1990 case Primitive::kPrimLong: {
1991 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001992 vixl32::Register low = LowRegisterFrom(in);
1993 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelingfe885462016-09-22 10:24:38 +01001994
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001995 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001996 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelingfe885462016-09-22 10:24:38 +01001997
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001998 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01001999 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002000
Scott Wakelingc34dba72016-10-03 10:14:44 +01002001 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002002
2003 // temp_d = int-to-double(high)
2004 __ Vmov(temp_s, high);
2005 __ Vcvt(F64, I32, temp_d, temp_s);
2006 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002007 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002008 // out_d = unsigned-to-double(low)
2009 __ Vmov(out_s, low);
2010 __ Vcvt(F64, U32, out_d, out_s);
2011 // out_d += temp_d * constant_d
2012 __ Vmla(F64, out_d, temp_d, constant_d);
2013 break;
2014 }
2015
2016 case Primitive::kPrimFloat:
2017 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002018 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002019 break;
2020
2021 default:
2022 LOG(FATAL) << "Unexpected type conversion from " << input_type
2023 << " to " << result_type;
2024 };
2025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 }
2031}
2032
2033void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2036 switch (add->GetResultType()) {
2037 case Primitive::kPrimInt: {
2038 locations->SetInAt(0, Location::RequiresRegister());
2039 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2041 break;
2042 }
2043
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002044 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002045 case Primitive::kPrimLong: {
2046 locations->SetInAt(0, Location::RequiresRegister());
2047 locations->SetInAt(1, Location::RequiresRegister());
2048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2049 break;
2050 }
2051
2052 case Primitive::kPrimFloat:
2053 case Primitive::kPrimDouble: {
2054 locations->SetInAt(0, Location::RequiresFpuRegister());
2055 locations->SetInAt(1, Location::RequiresFpuRegister());
2056 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2057 break;
2058 }
2059
2060 default:
2061 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2062 }
2063}
2064
2065void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
2066 LocationSummary* locations = add->GetLocations();
2067 Location out = locations->Out();
2068 Location first = locations->InAt(0);
2069 Location second = locations->InAt(1);
2070
2071 switch (add->GetResultType()) {
2072 case Primitive::kPrimInt: {
2073 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
2074 }
2075 break;
2076
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002077 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002078 case Primitive::kPrimLong: {
2079 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002080 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2081 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002082 break;
2083 }
2084
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002085 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01002086 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002087 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002088 break;
2089
2090 default:
2091 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2092 }
2093}
2094
2095void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
2096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
2098 switch (sub->GetResultType()) {
2099 case Primitive::kPrimInt: {
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
2102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2103 break;
2104 }
2105
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002106 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002107 case Primitive::kPrimLong: {
2108 locations->SetInAt(0, Location::RequiresRegister());
2109 locations->SetInAt(1, Location::RequiresRegister());
2110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2111 break;
2112 }
2113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble: {
2115 locations->SetInAt(0, Location::RequiresFpuRegister());
2116 locations->SetInAt(1, Location::RequiresFpuRegister());
2117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2118 break;
2119 }
2120 default:
2121 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2122 }
2123}
2124
2125void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
2126 LocationSummary* locations = sub->GetLocations();
2127 Location out = locations->Out();
2128 Location first = locations->InAt(0);
2129 Location second = locations->InAt(1);
2130 switch (sub->GetResultType()) {
2131 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002132 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002133 break;
2134 }
2135
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002136 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002137 case Primitive::kPrimLong: {
2138 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002139 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2140 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002141 break;
2142 }
2143
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002144 case Primitive::kPrimFloat:
2145 case Primitive::kPrimDouble:
2146 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002147 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002148
2149 default:
2150 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2151 }
2152}
2153
2154void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
2155 LocationSummary* locations =
2156 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2157 switch (mul->GetResultType()) {
2158 case Primitive::kPrimInt:
2159 case Primitive::kPrimLong: {
2160 locations->SetInAt(0, Location::RequiresRegister());
2161 locations->SetInAt(1, Location::RequiresRegister());
2162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2163 break;
2164 }
2165
2166 case Primitive::kPrimFloat:
2167 case Primitive::kPrimDouble: {
2168 locations->SetInAt(0, Location::RequiresFpuRegister());
2169 locations->SetInAt(1, Location::RequiresFpuRegister());
2170 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2171 break;
2172 }
2173
2174 default:
2175 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2176 }
2177}
2178
2179void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
2180 LocationSummary* locations = mul->GetLocations();
2181 Location out = locations->Out();
2182 Location first = locations->InAt(0);
2183 Location second = locations->InAt(1);
2184 switch (mul->GetResultType()) {
2185 case Primitive::kPrimInt: {
2186 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2187 break;
2188 }
2189 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002190 vixl32::Register out_hi = HighRegisterFrom(out);
2191 vixl32::Register out_lo = LowRegisterFrom(out);
2192 vixl32::Register in1_hi = HighRegisterFrom(first);
2193 vixl32::Register in1_lo = LowRegisterFrom(first);
2194 vixl32::Register in2_hi = HighRegisterFrom(second);
2195 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01002196
2197 // Extra checks to protect caused by the existence of R1_R2.
2198 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2199 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2200 DCHECK_NE(out_hi.GetCode(), in1_lo.GetCode());
2201 DCHECK_NE(out_hi.GetCode(), in2_lo.GetCode());
2202
2203 // input: in1 - 64 bits, in2 - 64 bits
2204 // output: out
2205 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2206 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2207 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2208
2209 UseScratchRegisterScope temps(GetVIXLAssembler());
2210 vixl32::Register temp = temps.Acquire();
2211 // temp <- in1.lo * in2.hi
2212 __ Mul(temp, in1_lo, in2_hi);
2213 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2214 __ Mla(out_hi, in1_hi, in2_lo, temp);
2215 // out.lo <- (in1.lo * in2.lo)[31:0];
2216 __ Umull(out_lo, temp, in1_lo, in2_lo);
2217 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002218 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002219 break;
2220 }
2221
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002222 case Primitive::kPrimFloat:
2223 case Primitive::kPrimDouble:
2224 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002225 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002226
2227 default:
2228 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2229 }
2230}
2231
Scott Wakelingfe885462016-09-22 10:24:38 +01002232void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2233 DCHECK(instruction->IsDiv() || instruction->IsRem());
2234 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2235
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002236 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002237 DCHECK(second.IsConstant());
2238
2239 vixl32::Register out = OutputRegister(instruction);
2240 vixl32::Register dividend = InputRegisterAt(instruction, 0);
2241 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2242 DCHECK(imm == 1 || imm == -1);
2243
2244 if (instruction->IsRem()) {
2245 __ Mov(out, 0);
2246 } else {
2247 if (imm == 1) {
2248 __ Mov(out, dividend);
2249 } else {
2250 __ Rsb(out, dividend, 0);
2251 }
2252 }
2253}
2254
2255void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2256 DCHECK(instruction->IsDiv() || instruction->IsRem());
2257 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2258
2259 LocationSummary* locations = instruction->GetLocations();
2260 Location second = locations->InAt(1);
2261 DCHECK(second.IsConstant());
2262
2263 vixl32::Register out = OutputRegister(instruction);
2264 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002265 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002266 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2267 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
2268 int ctz_imm = CTZ(abs_imm);
2269
2270 if (ctz_imm == 1) {
2271 __ Lsr(temp, dividend, 32 - ctz_imm);
2272 } else {
2273 __ Asr(temp, dividend, 31);
2274 __ Lsr(temp, temp, 32 - ctz_imm);
2275 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002276 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002277
2278 if (instruction->IsDiv()) {
2279 __ Asr(out, out, ctz_imm);
2280 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002281 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002282 }
2283 } else {
2284 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002285 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002286 }
2287}
2288
2289void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2290 DCHECK(instruction->IsDiv() || instruction->IsRem());
2291 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2292
2293 LocationSummary* locations = instruction->GetLocations();
2294 Location second = locations->InAt(1);
2295 DCHECK(second.IsConstant());
2296
2297 vixl32::Register out = OutputRegister(instruction);
2298 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002299 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
2300 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002301 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2302
2303 int64_t magic;
2304 int shift;
2305 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2306
2307 __ Mov(temp1, magic);
2308 __ Smull(temp2, temp1, dividend, temp1);
2309
2310 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002311 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002312 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002313 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002314 }
2315
2316 if (shift != 0) {
2317 __ Asr(temp1, temp1, shift);
2318 }
2319
2320 if (instruction->IsDiv()) {
2321 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2322 } else {
2323 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2324 // TODO: Strength reduction for mls.
2325 __ Mov(temp2, imm);
2326 __ Mls(out, temp1, temp2, dividend);
2327 }
2328}
2329
2330void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
2331 HBinaryOperation* instruction) {
2332 DCHECK(instruction->IsDiv() || instruction->IsRem());
2333 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2334
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002335 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002336 DCHECK(second.IsConstant());
2337
2338 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2339 if (imm == 0) {
2340 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2341 } else if (imm == 1 || imm == -1) {
2342 DivRemOneOrMinusOne(instruction);
2343 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
2344 DivRemByPowerOfTwo(instruction);
2345 } else {
2346 DCHECK(imm <= -2 || imm >= 2);
2347 GenerateDivRemWithAnyConstant(instruction);
2348 }
2349}
2350
2351void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
2352 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2353 if (div->GetResultType() == Primitive::kPrimLong) {
2354 // pLdiv runtime call.
2355 call_kind = LocationSummary::kCallOnMainOnly;
2356 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2357 // sdiv will be replaced by other instruction sequence.
2358 } else if (div->GetResultType() == Primitive::kPrimInt &&
2359 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2360 // pIdivmod runtime call.
2361 call_kind = LocationSummary::kCallOnMainOnly;
2362 }
2363
2364 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2365
2366 switch (div->GetResultType()) {
2367 case Primitive::kPrimInt: {
2368 if (div->InputAt(1)->IsConstant()) {
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
2371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2372 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2373 if (value == 1 || value == 0 || value == -1) {
2374 // No temp register required.
2375 } else {
2376 locations->AddTemp(Location::RequiresRegister());
2377 if (!IsPowerOfTwo(AbsOrMin(value))) {
2378 locations->AddTemp(Location::RequiresRegister());
2379 }
2380 }
2381 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2382 locations->SetInAt(0, Location::RequiresRegister());
2383 locations->SetInAt(1, Location::RequiresRegister());
2384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2385 } else {
2386 TODO_VIXL32(FATAL);
2387 }
2388 break;
2389 }
2390 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002391 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2392 locations->SetInAt(0, LocationFrom(
2393 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2394 locations->SetInAt(1, LocationFrom(
2395 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2396 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002397 break;
2398 }
2399 case Primitive::kPrimFloat:
2400 case Primitive::kPrimDouble: {
2401 locations->SetInAt(0, Location::RequiresFpuRegister());
2402 locations->SetInAt(1, Location::RequiresFpuRegister());
2403 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2404 break;
2405 }
2406
2407 default:
2408 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2409 }
2410}
2411
2412void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002413 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002414 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002415
2416 switch (div->GetResultType()) {
2417 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002418 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002419 GenerateDivRemConstantIntegral(div);
2420 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2421 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
2422 } else {
2423 TODO_VIXL32(FATAL);
2424 }
2425 break;
2426 }
2427
2428 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002429 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2430 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
2431 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
2432 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
2433 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
2434 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
2435 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
2436
2437 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
2438 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01002439 break;
2440 }
2441
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002442 case Primitive::kPrimFloat:
2443 case Primitive::kPrimDouble:
2444 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002445 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002446
2447 default:
2448 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2449 }
2450}
2451
2452void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002453 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
Scott Wakelingfe885462016-09-22 10:24:38 +01002454 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2455 ? LocationSummary::kCallOnSlowPath
2456 : LocationSummary::kNoCall;
2457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2458 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2459 if (instruction->HasUses()) {
2460 locations->SetOut(Location::SameAsFirstInput());
2461 }
2462}
2463
2464void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2465 DivZeroCheckSlowPathARMVIXL* slow_path =
2466 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
2467 codegen_->AddSlowPath(slow_path);
2468
2469 LocationSummary* locations = instruction->GetLocations();
2470 Location value = locations->InAt(0);
2471
2472 switch (instruction->GetType()) {
2473 case Primitive::kPrimBoolean:
2474 case Primitive::kPrimByte:
2475 case Primitive::kPrimChar:
2476 case Primitive::kPrimShort:
2477 case Primitive::kPrimInt: {
2478 if (value.IsRegister()) {
2479 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2480 } else {
2481 DCHECK(value.IsConstant()) << value;
2482 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2483 __ B(slow_path->GetEntryLabel());
2484 }
2485 }
2486 break;
2487 }
2488 case Primitive::kPrimLong: {
2489 if (value.IsRegisterPair()) {
2490 UseScratchRegisterScope temps(GetVIXLAssembler());
2491 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002492 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01002493 __ B(eq, slow_path->GetEntryLabel());
2494 } else {
2495 DCHECK(value.IsConstant()) << value;
2496 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2497 __ B(slow_path->GetEntryLabel());
2498 }
2499 }
2500 break;
2501 }
2502 default:
2503 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2504 }
2505}
2506
Artem Serov02109dd2016-09-23 17:17:54 +01002507void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
2508 LocationSummary* locations = ror->GetLocations();
2509 vixl32::Register in = InputRegisterAt(ror, 0);
2510 Location rhs = locations->InAt(1);
2511 vixl32::Register out = OutputRegister(ror);
2512
2513 if (rhs.IsConstant()) {
2514 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
2515 // so map all rotations to a +ve. equivalent in that range.
2516 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
2517 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
2518 if (rot) {
2519 // Rotate, mapping left rotations to right equivalents if necessary.
2520 // (e.g. left by 2 bits == right by 30.)
2521 __ Ror(out, in, rot);
2522 } else if (!out.Is(in)) {
2523 __ Mov(out, in);
2524 }
2525 } else {
2526 __ Ror(out, in, RegisterFrom(rhs));
2527 }
2528}
2529
2530// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
2531// rotates by swapping input regs (effectively rotating by the first 32-bits of
2532// a larger rotation) or flipping direction (thus treating larger right/left
2533// rotations as sub-word sized rotations in the other direction) as appropriate.
2534void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
2535 LocationSummary* locations = ror->GetLocations();
2536 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2537 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2538 Location rhs = locations->InAt(1);
2539 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2540 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2541
2542 if (rhs.IsConstant()) {
2543 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
2544 // Map all rotations to +ve. equivalents on the interval [0,63].
2545 rot &= kMaxLongShiftDistance;
2546 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
2547 // logic below to a simple pair of binary orr.
2548 // (e.g. 34 bits == in_reg swap + 2 bits right.)
2549 if (rot >= kArmBitsPerWord) {
2550 rot -= kArmBitsPerWord;
2551 std::swap(in_reg_hi, in_reg_lo);
2552 }
2553 // Rotate, or mov to out for zero or word size rotations.
2554 if (rot != 0u) {
2555 __ Lsr(out_reg_hi, in_reg_hi, rot);
2556 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
2557 __ Lsr(out_reg_lo, in_reg_lo, rot);
2558 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
2559 } else {
2560 __ Mov(out_reg_lo, in_reg_lo);
2561 __ Mov(out_reg_hi, in_reg_hi);
2562 }
2563 } else {
2564 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
2565 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
2566 vixl32::Label end;
2567 vixl32::Label shift_by_32_plus_shift_right;
2568
2569 __ And(shift_right, RegisterFrom(rhs), 0x1F);
2570 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
2571 // TODO(VIXL): Check that flags are kept after "vixl32::LeaveFlags" enabled.
2572 __ Rsb(shift_left, shift_right, kArmBitsPerWord);
2573 __ B(cc, &shift_by_32_plus_shift_right);
2574
2575 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
2576 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
2577 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
2578 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2579 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2580 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2581 __ Lsr(shift_left, in_reg_hi, shift_right);
2582 __ Add(out_reg_lo, out_reg_lo, shift_left);
2583 __ B(&end);
2584
2585 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
2586 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
2587 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
2588 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
2589 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2590 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2591 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2592 __ Lsl(shift_right, in_reg_hi, shift_left);
2593 __ Add(out_reg_lo, out_reg_lo, shift_right);
2594
2595 __ Bind(&end);
2596 }
2597}
2598
2599void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
2600 LocationSummary* locations =
2601 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
2602 switch (ror->GetResultType()) {
2603 case Primitive::kPrimInt: {
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
2606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2607 break;
2608 }
2609 case Primitive::kPrimLong: {
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 if (ror->InputAt(1)->IsConstant()) {
2612 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
2613 } else {
2614 locations->SetInAt(1, Location::RequiresRegister());
2615 locations->AddTemp(Location::RequiresRegister());
2616 locations->AddTemp(Location::RequiresRegister());
2617 }
2618 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2619 break;
2620 }
2621 default:
2622 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
2623 }
2624}
2625
2626void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
2627 Primitive::Type type = ror->GetResultType();
2628 switch (type) {
2629 case Primitive::kPrimInt: {
2630 HandleIntegerRotate(ror);
2631 break;
2632 }
2633 case Primitive::kPrimLong: {
2634 HandleLongRotate(ror);
2635 break;
2636 }
2637 default:
2638 LOG(FATAL) << "Unexpected operation type " << type;
2639 UNREACHABLE();
2640 }
2641}
2642
Artem Serov02d37832016-10-25 15:25:33 +01002643void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
2644 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2645
2646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2648
2649 switch (op->GetResultType()) {
2650 case Primitive::kPrimInt: {
2651 locations->SetInAt(0, Location::RequiresRegister());
2652 if (op->InputAt(1)->IsConstant()) {
2653 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2655 } else {
2656 locations->SetInAt(1, Location::RequiresRegister());
2657 // Make the output overlap, as it will be used to hold the masked
2658 // second input.
2659 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2660 }
2661 break;
2662 }
2663 case Primitive::kPrimLong: {
2664 locations->SetInAt(0, Location::RequiresRegister());
2665 if (op->InputAt(1)->IsConstant()) {
2666 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2667 // For simplicity, use kOutputOverlap even though we only require that low registers
2668 // don't clash with high registers which the register allocator currently guarantees.
2669 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2670 } else {
2671 locations->SetInAt(1, Location::RequiresRegister());
2672 locations->AddTemp(Location::RequiresRegister());
2673 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2674 }
2675 break;
2676 }
2677 default:
2678 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2679 }
2680}
2681
2682void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
2683 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2684
2685 LocationSummary* locations = op->GetLocations();
2686 Location out = locations->Out();
2687 Location first = locations->InAt(0);
2688 Location second = locations->InAt(1);
2689
2690 Primitive::Type type = op->GetResultType();
2691 switch (type) {
2692 case Primitive::kPrimInt: {
2693 vixl32::Register out_reg = OutputRegister(op);
2694 vixl32::Register first_reg = InputRegisterAt(op, 0);
2695 if (second.IsRegister()) {
2696 vixl32::Register second_reg = RegisterFrom(second);
2697 // ARM doesn't mask the shift count so we need to do it ourselves.
2698 __ And(out_reg, second_reg, kMaxIntShiftDistance);
2699 if (op->IsShl()) {
2700 __ Lsl(out_reg, first_reg, out_reg);
2701 } else if (op->IsShr()) {
2702 __ Asr(out_reg, first_reg, out_reg);
2703 } else {
2704 __ Lsr(out_reg, first_reg, out_reg);
2705 }
2706 } else {
2707 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2708 uint32_t shift_value = cst & kMaxIntShiftDistance;
2709 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
2710 __ Mov(out_reg, first_reg);
2711 } else if (op->IsShl()) {
2712 __ Lsl(out_reg, first_reg, shift_value);
2713 } else if (op->IsShr()) {
2714 __ Asr(out_reg, first_reg, shift_value);
2715 } else {
2716 __ Lsr(out_reg, first_reg, shift_value);
2717 }
2718 }
2719 break;
2720 }
2721 case Primitive::kPrimLong: {
2722 vixl32::Register o_h = HighRegisterFrom(out);
2723 vixl32::Register o_l = LowRegisterFrom(out);
2724
2725 vixl32::Register high = HighRegisterFrom(first);
2726 vixl32::Register low = LowRegisterFrom(first);
2727
2728 if (second.IsRegister()) {
2729 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2730
2731 vixl32::Register second_reg = RegisterFrom(second);
2732
2733 if (op->IsShl()) {
2734 __ And(o_l, second_reg, kMaxLongShiftDistance);
2735 // Shift the high part
2736 __ Lsl(o_h, high, o_l);
2737 // Shift the low part and `or` what overflew on the high part
2738 __ Rsb(temp, o_l, kArmBitsPerWord);
2739 __ Lsr(temp, low, temp);
2740 __ Orr(o_h, o_h, temp);
2741 // If the shift is > 32 bits, override the high part
2742 __ Subs(temp, o_l, kArmBitsPerWord);
2743 {
2744 AssemblerAccurateScope guard(GetVIXLAssembler(),
2745 3 * kArmInstrMaxSizeInBytes,
2746 CodeBufferCheckScope::kMaximumSize);
2747 __ it(pl);
2748 __ lsl(pl, o_h, low, temp);
2749 }
2750 // Shift the low part
2751 __ Lsl(o_l, low, o_l);
2752 } else if (op->IsShr()) {
2753 __ And(o_h, second_reg, kMaxLongShiftDistance);
2754 // Shift the low part
2755 __ Lsr(o_l, low, o_h);
2756 // Shift the high part and `or` what underflew on the low part
2757 __ Rsb(temp, o_h, kArmBitsPerWord);
2758 __ Lsl(temp, high, temp);
2759 __ Orr(o_l, o_l, temp);
2760 // If the shift is > 32 bits, override the low part
2761 __ Subs(temp, o_h, kArmBitsPerWord);
2762 {
2763 AssemblerAccurateScope guard(GetVIXLAssembler(),
2764 3 * kArmInstrMaxSizeInBytes,
2765 CodeBufferCheckScope::kMaximumSize);
2766 __ it(pl);
2767 __ asr(pl, o_l, high, temp);
2768 }
2769 // Shift the high part
2770 __ Asr(o_h, high, o_h);
2771 } else {
2772 __ And(o_h, second_reg, kMaxLongShiftDistance);
2773 // same as Shr except we use `Lsr`s and not `Asr`s
2774 __ Lsr(o_l, low, o_h);
2775 __ Rsb(temp, o_h, kArmBitsPerWord);
2776 __ Lsl(temp, high, temp);
2777 __ Orr(o_l, o_l, temp);
2778 __ Subs(temp, o_h, kArmBitsPerWord);
2779 {
2780 AssemblerAccurateScope guard(GetVIXLAssembler(),
2781 3 * kArmInstrMaxSizeInBytes,
2782 CodeBufferCheckScope::kMaximumSize);
2783 __ it(pl);
2784 __ lsr(pl, o_l, high, temp);
2785 }
2786 __ Lsr(o_h, high, o_h);
2787 }
2788 } else {
2789 // Register allocator doesn't create partial overlap.
2790 DCHECK(!o_l.Is(high));
2791 DCHECK(!o_h.Is(low));
2792 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2793 uint32_t shift_value = cst & kMaxLongShiftDistance;
2794 if (shift_value > 32) {
2795 if (op->IsShl()) {
2796 __ Lsl(o_h, low, shift_value - 32);
2797 __ Mov(o_l, 0);
2798 } else if (op->IsShr()) {
2799 __ Asr(o_l, high, shift_value - 32);
2800 __ Asr(o_h, high, 31);
2801 } else {
2802 __ Lsr(o_l, high, shift_value - 32);
2803 __ Mov(o_h, 0);
2804 }
2805 } else if (shift_value == 32) {
2806 if (op->IsShl()) {
2807 __ Mov(o_h, low);
2808 __ Mov(o_l, 0);
2809 } else if (op->IsShr()) {
2810 __ Mov(o_l, high);
2811 __ Asr(o_h, high, 31);
2812 } else {
2813 __ Mov(o_l, high);
2814 __ Mov(o_h, 0);
2815 }
2816 } else if (shift_value == 1) {
2817 if (op->IsShl()) {
2818 __ Lsls(o_l, low, 1);
2819 __ Adc(o_h, high, high);
2820 } else if (op->IsShr()) {
2821 __ Asrs(o_h, high, 1);
2822 __ Rrx(o_l, low);
2823 } else {
2824 __ Lsrs(o_h, high, 1);
2825 __ Rrx(o_l, low);
2826 }
2827 } else {
2828 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
2829 if (op->IsShl()) {
2830 __ Lsl(o_h, high, shift_value);
2831 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
2832 __ Lsl(o_l, low, shift_value);
2833 } else if (op->IsShr()) {
2834 __ Lsr(o_l, low, shift_value);
2835 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
2836 __ Asr(o_h, high, shift_value);
2837 } else {
2838 __ Lsr(o_l, low, shift_value);
2839 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
2840 __ Lsr(o_h, high, shift_value);
2841 }
2842 }
2843 }
2844 break;
2845 }
2846 default:
2847 LOG(FATAL) << "Unexpected operation type " << type;
2848 UNREACHABLE();
2849 }
2850}
2851
2852void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
2853 HandleShift(shl);
2854}
2855
2856void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
2857 HandleShift(shl);
2858}
2859
2860void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
2861 HandleShift(shr);
2862}
2863
2864void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
2865 HandleShift(shr);
2866}
2867
2868void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
2869 HandleShift(ushr);
2870}
2871
2872void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
2873 HandleShift(ushr);
2874}
2875
2876void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
2877 LocationSummary* locations =
2878 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
2879 if (instruction->IsStringAlloc()) {
2880 locations->AddTemp(LocationFrom(kMethodRegister));
2881 } else {
2882 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2883 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2884 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2885 }
2886 locations->SetOut(LocationFrom(r0));
2887}
2888
2889void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
2890 // Note: if heap poisoning is enabled, the entry point takes cares
2891 // of poisoning the reference.
2892 if (instruction->IsStringAlloc()) {
2893 // String is allocated through StringFactory. Call NewEmptyString entry point.
2894 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
2895 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
2896 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
2897 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
2898 AssemblerAccurateScope aas(GetVIXLAssembler(),
2899 kArmInstrMaxSizeInBytes,
2900 CodeBufferCheckScope::kMaximumSize);
2901 __ blx(lr);
2902 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2903 } else {
2904 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
2905 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
2906 }
2907}
2908
2909void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
2910 LocationSummary* locations =
2911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
2912 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2913 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2914 locations->SetOut(LocationFrom(r0));
2915 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2916 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
2917}
2918
2919void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
2920 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2921 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
2922 // Note: if heap poisoning is enabled, the entry point takes cares
2923 // of poisoning the reference.
2924 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
2925 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
2926}
2927
2928void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
2929 LocationSummary* locations =
2930 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2931 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2932 if (location.IsStackSlot()) {
2933 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2934 } else if (location.IsDoubleStackSlot()) {
2935 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2936 }
2937 locations->SetOut(location);
2938}
2939
2940void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
2941 HParameterValue* instruction ATTRIBUTE_UNUSED) {
2942 // Nothing to do, the parameter is already at its location.
2943}
2944
2945void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
2946 LocationSummary* locations =
2947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2948 locations->SetOut(LocationFrom(kMethodRegister));
2949}
2950
2951void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
2952 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2953 // Nothing to do, the method is already at its location.
2954}
2955
2956void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
2957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
2959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2961}
2962
2963void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
2964 LocationSummary* locations = not_->GetLocations();
2965 Location out = locations->Out();
2966 Location in = locations->InAt(0);
2967 switch (not_->GetResultType()) {
2968 case Primitive::kPrimInt:
2969 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
2970 break;
2971
2972 case Primitive::kPrimLong:
2973 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
2974 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
2975 break;
2976
2977 default:
2978 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2979 }
2980}
2981
Scott Wakelingc34dba72016-10-03 10:14:44 +01002982void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2985 locations->SetInAt(0, Location::RequiresRegister());
2986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2987}
2988
2989void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
2990 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
2991}
2992
Artem Serov02d37832016-10-25 15:25:33 +01002993void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
2994 LocationSummary* locations =
2995 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2996 switch (compare->InputAt(0)->GetType()) {
2997 case Primitive::kPrimBoolean:
2998 case Primitive::kPrimByte:
2999 case Primitive::kPrimShort:
3000 case Primitive::kPrimChar:
3001 case Primitive::kPrimInt:
3002 case Primitive::kPrimLong: {
3003 locations->SetInAt(0, Location::RequiresRegister());
3004 locations->SetInAt(1, Location::RequiresRegister());
3005 // Output overlaps because it is written before doing the low comparison.
3006 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3007 break;
3008 }
3009 case Primitive::kPrimFloat:
3010 case Primitive::kPrimDouble: {
3011 locations->SetInAt(0, Location::RequiresFpuRegister());
3012 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
3013 locations->SetOut(Location::RequiresRegister());
3014 break;
3015 }
3016 default:
3017 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3018 }
3019}
3020
3021void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
3022 LocationSummary* locations = compare->GetLocations();
3023 vixl32::Register out = OutputRegister(compare);
3024 Location left = locations->InAt(0);
3025 Location right = locations->InAt(1);
3026
3027 vixl32::Label less, greater, done;
3028 Primitive::Type type = compare->InputAt(0)->GetType();
3029 vixl32::Condition less_cond = vixl32::Condition(kNone);
3030 switch (type) {
3031 case Primitive::kPrimBoolean:
3032 case Primitive::kPrimByte:
3033 case Primitive::kPrimShort:
3034 case Primitive::kPrimChar:
3035 case Primitive::kPrimInt: {
3036 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
3037 __ Mov(out, 0);
3038 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
3039 less_cond = lt;
3040 break;
3041 }
3042 case Primitive::kPrimLong: {
3043 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
3044 __ B(lt, &less);
3045 __ B(gt, &greater);
3046 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
3047 __ Mov(out, 0);
3048 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
3049 less_cond = lo;
3050 break;
3051 }
3052 case Primitive::kPrimFloat:
3053 case Primitive::kPrimDouble: {
3054 __ Mov(out, 0);
3055 GenerateVcmp(compare);
3056 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
3057 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
3058 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
3059 break;
3060 }
3061 default:
3062 LOG(FATAL) << "Unexpected compare type " << type;
3063 UNREACHABLE();
3064 }
3065
3066 __ B(eq, &done);
3067 __ B(less_cond, &less);
3068
3069 __ Bind(&greater);
3070 __ Mov(out, 1);
3071 __ B(&done);
3072
3073 __ Bind(&less);
3074 __ Mov(out, -1);
3075
3076 __ Bind(&done);
3077}
3078
3079void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
3080 LocationSummary* locations =
3081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3082 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
3083 locations->SetInAt(i, Location::Any());
3084 }
3085 locations->SetOut(Location::Any());
3086}
3087
3088void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3089 LOG(FATAL) << "Unreachable";
3090}
3091
3092void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
3093 // TODO (ported from quick): revisit ARM barrier kinds.
3094 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
3095 switch (kind) {
3096 case MemBarrierKind::kAnyStore:
3097 case MemBarrierKind::kLoadAny:
3098 case MemBarrierKind::kAnyAny: {
3099 flavor = DmbOptions::ISH;
3100 break;
3101 }
3102 case MemBarrierKind::kStoreStore: {
3103 flavor = DmbOptions::ISHST;
3104 break;
3105 }
3106 default:
3107 LOG(FATAL) << "Unexpected memory barrier " << kind;
3108 }
3109 __ Dmb(flavor);
3110}
3111
3112void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
3113 uint32_t offset,
3114 vixl32::Register out_lo,
3115 vixl32::Register out_hi) {
3116 UseScratchRegisterScope temps(GetVIXLAssembler());
3117 if (offset != 0) {
3118 vixl32::Register temp = temps.Acquire();
3119 __ Add(temp, addr, offset);
3120 addr = temp;
3121 }
3122 __ Ldrexd(out_lo, out_hi, addr);
3123}
3124
3125void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
3126 uint32_t offset,
3127 vixl32::Register value_lo,
3128 vixl32::Register value_hi,
3129 vixl32::Register temp1,
3130 vixl32::Register temp2,
3131 HInstruction* instruction) {
3132 UseScratchRegisterScope temps(GetVIXLAssembler());
3133 vixl32::Label fail;
3134 if (offset != 0) {
3135 vixl32::Register temp = temps.Acquire();
3136 __ Add(temp, addr, offset);
3137 addr = temp;
3138 }
3139 __ Bind(&fail);
3140 // We need a load followed by store. (The address used in a STREX instruction must
3141 // be the same as the address in the most recently executed LDREX instruction.)
3142 __ Ldrexd(temp1, temp2, addr);
3143 codegen_->MaybeRecordImplicitNullCheck(instruction);
3144 __ Strexd(temp1, value_lo, value_hi, addr);
3145 __ Cbnz(temp1, &fail);
3146}
Artem Serov02109dd2016-09-23 17:17:54 +01003147
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003148void LocationsBuilderARMVIXL::HandleFieldSet(
3149 HInstruction* instruction, const FieldInfo& field_info) {
3150 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3151
3152 LocationSummary* locations =
3153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3154 locations->SetInAt(0, Location::RequiresRegister());
3155
3156 Primitive::Type field_type = field_info.GetFieldType();
3157 if (Primitive::IsFloatingPointType(field_type)) {
3158 locations->SetInAt(1, Location::RequiresFpuRegister());
3159 } else {
3160 locations->SetInAt(1, Location::RequiresRegister());
3161 }
3162
3163 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
3164 bool generate_volatile = field_info.IsVolatile()
3165 && is_wide
3166 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3167 bool needs_write_barrier =
3168 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3169 // Temporary registers for the write barrier.
3170 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
3171 if (needs_write_barrier) {
3172 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
3173 locations->AddTemp(Location::RequiresRegister());
3174 } else if (generate_volatile) {
3175 // ARM encoding have some additional constraints for ldrexd/strexd:
3176 // - registers need to be consecutive
3177 // - the first register should be even but not R14.
3178 // We don't test for ARM yet, and the assertion makes sure that we
3179 // revisit this if we ever enable ARM encoding.
3180 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3181
3182 locations->AddTemp(Location::RequiresRegister());
3183 locations->AddTemp(Location::RequiresRegister());
3184 if (field_type == Primitive::kPrimDouble) {
3185 // For doubles we need two more registers to copy the value.
3186 locations->AddTemp(LocationFrom(r2));
3187 locations->AddTemp(LocationFrom(r3));
3188 }
3189 }
3190}
3191
3192void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
3193 const FieldInfo& field_info,
3194 bool value_can_be_null) {
3195 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3196
3197 LocationSummary* locations = instruction->GetLocations();
3198 vixl32::Register base = InputRegisterAt(instruction, 0);
3199 Location value = locations->InAt(1);
3200
3201 bool is_volatile = field_info.IsVolatile();
3202 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3203 Primitive::Type field_type = field_info.GetFieldType();
3204 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3205 bool needs_write_barrier =
3206 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3207
3208 if (is_volatile) {
3209 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3210 }
3211
3212 switch (field_type) {
3213 case Primitive::kPrimBoolean:
3214 case Primitive::kPrimByte: {
3215 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
3216 break;
3217 }
3218
3219 case Primitive::kPrimShort:
3220 case Primitive::kPrimChar: {
3221 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
3222 break;
3223 }
3224
3225 case Primitive::kPrimInt:
3226 case Primitive::kPrimNot: {
3227 if (kPoisonHeapReferences && needs_write_barrier) {
3228 // Note that in the case where `value` is a null reference,
3229 // we do not enter this block, as a null reference does not
3230 // need poisoning.
3231 DCHECK_EQ(field_type, Primitive::kPrimNot);
3232 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3233 __ Mov(temp, RegisterFrom(value));
3234 GetAssembler()->PoisonHeapReference(temp);
3235 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
3236 } else {
3237 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
3238 }
3239 break;
3240 }
3241
3242 case Primitive::kPrimLong: {
3243 if (is_volatile && !atomic_ldrd_strd) {
3244 GenerateWideAtomicStore(base,
3245 offset,
3246 LowRegisterFrom(value),
3247 HighRegisterFrom(value),
3248 RegisterFrom(locations->GetTemp(0)),
3249 RegisterFrom(locations->GetTemp(1)),
3250 instruction);
3251 } else {
3252 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
3253 codegen_->MaybeRecordImplicitNullCheck(instruction);
3254 }
3255 break;
3256 }
3257
3258 case Primitive::kPrimFloat: {
3259 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
3260 break;
3261 }
3262
3263 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01003264 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003265 if (is_volatile && !atomic_ldrd_strd) {
3266 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
3267 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
3268
3269 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
3270
3271 GenerateWideAtomicStore(base,
3272 offset,
3273 value_reg_lo,
3274 value_reg_hi,
3275 RegisterFrom(locations->GetTemp(2)),
3276 RegisterFrom(locations->GetTemp(3)),
3277 instruction);
3278 } else {
3279 GetAssembler()->StoreDToOffset(value_reg, base, offset);
3280 codegen_->MaybeRecordImplicitNullCheck(instruction);
3281 }
3282 break;
3283 }
3284
3285 case Primitive::kPrimVoid:
3286 LOG(FATAL) << "Unreachable type " << field_type;
3287 UNREACHABLE();
3288 }
3289
3290 // Longs and doubles are handled in the switch.
3291 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3292 codegen_->MaybeRecordImplicitNullCheck(instruction);
3293 }
3294
3295 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3296 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3297 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
3298 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
3299 }
3300
3301 if (is_volatile) {
3302 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3303 }
3304}
3305
Artem Serov02d37832016-10-25 15:25:33 +01003306void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
3307 const FieldInfo& field_info) {
3308 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3309
3310 bool object_field_get_with_read_barrier =
3311 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
3312 LocationSummary* locations =
3313 new (GetGraph()->GetArena()) LocationSummary(instruction,
3314 object_field_get_with_read_barrier ?
3315 LocationSummary::kCallOnSlowPath :
3316 LocationSummary::kNoCall);
3317 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3318 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3319 }
3320 locations->SetInAt(0, Location::RequiresRegister());
3321
3322 bool volatile_for_double = field_info.IsVolatile()
3323 && (field_info.GetFieldType() == Primitive::kPrimDouble)
3324 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3325 // The output overlaps in case of volatile long: we don't want the
3326 // code generated by GenerateWideAtomicLoad to overwrite the
3327 // object's location. Likewise, in the case of an object field get
3328 // with read barriers enabled, we do not want the load to overwrite
3329 // the object's location, as we need it to emit the read barrier.
3330 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3331 object_field_get_with_read_barrier;
3332
3333 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3334 locations->SetOut(Location::RequiresFpuRegister());
3335 } else {
3336 locations->SetOut(Location::RequiresRegister(),
3337 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3338 }
3339 if (volatile_for_double) {
3340 // ARM encoding have some additional constraints for ldrexd/strexd:
3341 // - registers need to be consecutive
3342 // - the first register should be even but not R14.
3343 // We don't test for ARM yet, and the assertion makes sure that we
3344 // revisit this if we ever enable ARM encoding.
3345 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3346 locations->AddTemp(Location::RequiresRegister());
3347 locations->AddTemp(Location::RequiresRegister());
3348 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3349 // We need a temporary register for the read barrier marking slow
3350 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3351 locations->AddTemp(Location::RequiresRegister());
3352 }
3353}
3354
3355Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
3356 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
3357 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
3358 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
3359 return Location::ConstantLocation(input->AsConstant());
3360 } else {
3361 return Location::RequiresFpuRegister();
3362 }
3363}
3364
Artem Serov02109dd2016-09-23 17:17:54 +01003365Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
3366 Opcode opcode) {
3367 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3368 if (constant->IsConstant() &&
3369 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3370 return Location::ConstantLocation(constant->AsConstant());
3371 }
3372 return Location::RequiresRegister();
3373}
3374
3375bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
3376 Opcode opcode) {
3377 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3378 if (Primitive::Is64BitType(input_cst->GetType())) {
3379 Opcode high_opcode = opcode;
3380 SetCc low_set_cc = kCcDontCare;
3381 switch (opcode) {
3382 case SUB:
3383 // Flip the operation to an ADD.
3384 value = -value;
3385 opcode = ADD;
3386 FALLTHROUGH_INTENDED;
3387 case ADD:
3388 if (Low32Bits(value) == 0u) {
3389 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
3390 }
3391 high_opcode = ADC;
3392 low_set_cc = kCcSet;
3393 break;
3394 default:
3395 break;
3396 }
3397 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
3398 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
3399 } else {
3400 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3401 }
3402}
3403
3404// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
3405// enabled.
3406bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
3407 Opcode opcode,
3408 SetCc set_cc) {
3409 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
3410 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
3411 return true;
3412 }
3413 Opcode neg_opcode = kNoOperand;
3414 switch (opcode) {
3415 case AND: neg_opcode = BIC; value = ~value; break;
3416 case ORR: neg_opcode = ORN; value = ~value; break;
3417 case ADD: neg_opcode = SUB; value = -value; break;
3418 case ADC: neg_opcode = SBC; value = ~value; break;
3419 case SUB: neg_opcode = ADD; value = -value; break;
3420 case SBC: neg_opcode = ADC; value = ~value; break;
3421 default:
3422 return false;
3423 }
3424 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
3425}
3426
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003427void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
3428 const FieldInfo& field_info) {
3429 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3430
3431 LocationSummary* locations = instruction->GetLocations();
3432 vixl32::Register base = InputRegisterAt(instruction, 0);
3433 Location out = locations->Out();
3434 bool is_volatile = field_info.IsVolatile();
3435 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3436 Primitive::Type field_type = field_info.GetFieldType();
3437 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3438
3439 switch (field_type) {
3440 case Primitive::kPrimBoolean:
3441 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
3442 break;
3443
3444 case Primitive::kPrimByte:
3445 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
3446 break;
3447
3448 case Primitive::kPrimShort:
3449 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
3450 break;
3451
3452 case Primitive::kPrimChar:
3453 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
3454 break;
3455
3456 case Primitive::kPrimInt:
3457 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3458 break;
3459
3460 case Primitive::kPrimNot: {
3461 // /* HeapReference<Object> */ out = *(base + offset)
3462 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3463 TODO_VIXL32(FATAL);
3464 } else {
3465 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3466 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3467 codegen_->MaybeRecordImplicitNullCheck(instruction);
3468 if (is_volatile) {
3469 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3470 }
3471 // If read barriers are enabled, emit read barriers other than
3472 // Baker's using a slow path (and also unpoison the loaded
3473 // reference, if heap poisoning is enabled).
3474 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
3475 }
3476 break;
3477 }
3478
3479 case Primitive::kPrimLong:
3480 if (is_volatile && !atomic_ldrd_strd) {
3481 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
3482 } else {
3483 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
3484 }
3485 break;
3486
3487 case Primitive::kPrimFloat:
3488 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
3489 break;
3490
3491 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01003492 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003493 if (is_volatile && !atomic_ldrd_strd) {
3494 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
3495 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
3496 GenerateWideAtomicLoad(base, offset, lo, hi);
3497 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
3498 // scope.
3499 codegen_->MaybeRecordImplicitNullCheck(instruction);
3500 __ Vmov(out_dreg, lo, hi);
3501 } else {
3502 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
3503 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3504 codegen_->MaybeRecordImplicitNullCheck(instruction);
3505 }
3506 break;
3507 }
3508
3509 case Primitive::kPrimVoid:
3510 LOG(FATAL) << "Unreachable type " << field_type;
3511 UNREACHABLE();
3512 }
3513
3514 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
3515 // Potential implicit null checks, in the case of reference or
3516 // double fields, are handled in the previous switch statement.
3517 } else {
3518 // Address cases other than reference and double that may require an implicit null check.
3519 codegen_->MaybeRecordImplicitNullCheck(instruction);
3520 }
3521
3522 if (is_volatile) {
3523 if (field_type == Primitive::kPrimNot) {
3524 // Memory barriers, in the case of references, are also handled
3525 // in the previous switch statement.
3526 } else {
3527 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3528 }
3529 }
3530}
3531
3532void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3533 HandleFieldSet(instruction, instruction->GetFieldInfo());
3534}
3535
3536void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3537 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3538}
3539
3540void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3541 HandleFieldGet(instruction, instruction->GetFieldInfo());
3542}
3543
3544void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3545 HandleFieldGet(instruction, instruction->GetFieldInfo());
3546}
3547
3548void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3549 HandleFieldGet(instruction, instruction->GetFieldInfo());
3550}
3551
3552void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3553 HandleFieldGet(instruction, instruction->GetFieldInfo());
3554}
3555
Scott Wakelingc34dba72016-10-03 10:14:44 +01003556void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3557 HandleFieldSet(instruction, instruction->GetFieldInfo());
3558}
3559
3560void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3561 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3562}
3563
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003564void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3565 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
3566 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3567 ? LocationSummary::kCallOnSlowPath
3568 : LocationSummary::kNoCall;
3569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3570 locations->SetInAt(0, Location::RequiresRegister());
3571 if (instruction->HasUses()) {
3572 locations->SetOut(Location::SameAsFirstInput());
3573 }
3574}
3575
3576void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
3577 if (CanMoveNullCheckToUser(instruction)) {
3578 return;
3579 }
3580
3581 UseScratchRegisterScope temps(GetVIXLAssembler());
3582 AssemblerAccurateScope aas(GetVIXLAssembler(),
3583 kArmInstrMaxSizeInBytes,
3584 CodeBufferCheckScope::kMaximumSize);
3585 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
3586 RecordPcInfo(instruction, instruction->GetDexPc());
3587}
3588
3589void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
3590 NullCheckSlowPathARMVIXL* slow_path =
3591 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
3592 AddSlowPath(slow_path);
3593 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3594}
3595
3596void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3597 codegen_->GenerateNullCheck(instruction);
3598}
3599
Scott Wakelingc34dba72016-10-03 10:14:44 +01003600static LoadOperandType GetLoadOperandType(Primitive::Type type) {
3601 switch (type) {
3602 case Primitive::kPrimNot:
3603 return kLoadWord;
3604 case Primitive::kPrimBoolean:
3605 return kLoadUnsignedByte;
3606 case Primitive::kPrimByte:
3607 return kLoadSignedByte;
3608 case Primitive::kPrimChar:
3609 return kLoadUnsignedHalfword;
3610 case Primitive::kPrimShort:
3611 return kLoadSignedHalfword;
3612 case Primitive::kPrimInt:
3613 return kLoadWord;
3614 case Primitive::kPrimLong:
3615 return kLoadWordPair;
3616 case Primitive::kPrimFloat:
3617 return kLoadSWord;
3618 case Primitive::kPrimDouble:
3619 return kLoadDWord;
3620 default:
3621 LOG(FATAL) << "Unreachable type " << type;
3622 UNREACHABLE();
3623 }
3624}
3625
3626static StoreOperandType GetStoreOperandType(Primitive::Type type) {
3627 switch (type) {
3628 case Primitive::kPrimNot:
3629 return kStoreWord;
3630 case Primitive::kPrimBoolean:
3631 case Primitive::kPrimByte:
3632 return kStoreByte;
3633 case Primitive::kPrimChar:
3634 case Primitive::kPrimShort:
3635 return kStoreHalfword;
3636 case Primitive::kPrimInt:
3637 return kStoreWord;
3638 case Primitive::kPrimLong:
3639 return kStoreWordPair;
3640 case Primitive::kPrimFloat:
3641 return kStoreSWord;
3642 case Primitive::kPrimDouble:
3643 return kStoreDWord;
3644 default:
3645 LOG(FATAL) << "Unreachable type " << type;
3646 UNREACHABLE();
3647 }
3648}
3649
3650void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
3651 Location out_loc,
3652 vixl32::Register base,
3653 vixl32::Register reg_index,
3654 vixl32::Condition cond) {
3655 uint32_t shift_count = Primitive::ComponentSizeShift(type);
3656 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
3657
3658 switch (type) {
3659 case Primitive::kPrimByte:
3660 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
3661 break;
3662 case Primitive::kPrimBoolean:
3663 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
3664 break;
3665 case Primitive::kPrimShort:
3666 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
3667 break;
3668 case Primitive::kPrimChar:
3669 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
3670 break;
3671 case Primitive::kPrimNot:
3672 case Primitive::kPrimInt:
3673 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
3674 break;
3675 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
3676 case Primitive::kPrimLong:
3677 case Primitive::kPrimFloat:
3678 case Primitive::kPrimDouble:
3679 default:
3680 LOG(FATAL) << "Unreachable type " << type;
3681 UNREACHABLE();
3682 }
3683}
3684
3685void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
3686 Location loc,
3687 vixl32::Register base,
3688 vixl32::Register reg_index,
3689 vixl32::Condition cond) {
3690 uint32_t shift_count = Primitive::ComponentSizeShift(type);
3691 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
3692
3693 switch (type) {
3694 case Primitive::kPrimByte:
3695 case Primitive::kPrimBoolean:
3696 __ Strb(cond, RegisterFrom(loc), mem_address);
3697 break;
3698 case Primitive::kPrimShort:
3699 case Primitive::kPrimChar:
3700 __ Strh(cond, RegisterFrom(loc), mem_address);
3701 break;
3702 case Primitive::kPrimNot:
3703 case Primitive::kPrimInt:
3704 __ Str(cond, RegisterFrom(loc), mem_address);
3705 break;
3706 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
3707 case Primitive::kPrimLong:
3708 case Primitive::kPrimFloat:
3709 case Primitive::kPrimDouble:
3710 default:
3711 LOG(FATAL) << "Unreachable type " << type;
3712 UNREACHABLE();
3713 }
3714}
3715
3716void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
3717 bool object_array_get_with_read_barrier =
3718 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(instruction,
3721 object_array_get_with_read_barrier ?
3722 LocationSummary::kCallOnSlowPath :
3723 LocationSummary::kNoCall);
3724 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
3725 TODO_VIXL32(FATAL);
3726 }
3727 locations->SetInAt(0, Location::RequiresRegister());
3728 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3729 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3731 } else {
3732 // The output overlaps in the case of an object array get with
3733 // read barriers enabled: we do not want the move to overwrite the
3734 // array's location, as we need it to emit the read barrier.
3735 locations->SetOut(
3736 Location::RequiresRegister(),
3737 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
3738 }
3739 // We need a temporary register for the read barrier marking slow
3740 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
3741 // Also need for String compression feature.
3742 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
3743 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003744 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01003745 }
3746}
3747
3748void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
3749 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3750 LocationSummary* locations = instruction->GetLocations();
3751 Location obj_loc = locations->InAt(0);
3752 vixl32::Register obj = InputRegisterAt(instruction, 0);
3753 Location index = locations->InAt(1);
3754 Location out_loc = locations->Out();
3755 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
3756 Primitive::Type type = instruction->GetType();
3757 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
3758 instruction->IsStringCharAt();
3759 HInstruction* array_instr = instruction->GetArray();
3760 bool has_intermediate_address = array_instr->IsIntermediateAddress();
3761 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
3762 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
3763
3764 switch (type) {
3765 case Primitive::kPrimBoolean:
3766 case Primitive::kPrimByte:
3767 case Primitive::kPrimShort:
3768 case Primitive::kPrimChar:
3769 case Primitive::kPrimInt: {
3770 if (index.IsConstant()) {
3771 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
3772 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003773 vixl32::Register length = temps.Acquire();
3774 vixl32::Label uncompressed_load, done;
3775 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
3776 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
3777 codegen_->MaybeRecordImplicitNullCheck(instruction);
3778 __ Cmp(length, 0);
3779 __ B(ge, &uncompressed_load);
3780 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
3781 RegisterFrom(out_loc),
3782 obj,
3783 data_offset + const_index);
3784 __ B(&done);
3785 __ Bind(&uncompressed_load);
3786 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
3787 RegisterFrom(out_loc),
3788 obj,
3789 data_offset + (const_index << 1));
3790 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003791 } else {
3792 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
3793
3794 LoadOperandType load_type = GetLoadOperandType(type);
3795 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
3796 }
3797 } else {
3798 vixl32::Register temp = temps.Acquire();
3799
3800 if (has_intermediate_address) {
3801 TODO_VIXL32(FATAL);
3802 } else {
3803 __ Add(temp, obj, data_offset);
3804 }
3805 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003806 vixl32::Label uncompressed_load, done;
3807 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
3808 vixl32::Register length = RegisterFrom(locations->GetTemp(0));
3809 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
3810 codegen_->MaybeRecordImplicitNullCheck(instruction);
3811 __ Cmp(length, 0);
3812 __ B(ge, &uncompressed_load);
3813 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
3814 __ B(&done);
3815 __ Bind(&uncompressed_load);
3816 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
3817 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003818 } else {
3819 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
3820 }
3821 }
3822 break;
3823 }
3824
3825 case Primitive::kPrimNot: {
3826 static_assert(
3827 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3828 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
3829 // /* HeapReference<Object> */ out =
3830 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
3831 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3832 TODO_VIXL32(FATAL);
3833 } else {
3834 vixl32::Register out = OutputRegister(instruction);
3835 if (index.IsConstant()) {
3836 size_t offset =
3837 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3838 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
3839 codegen_->MaybeRecordImplicitNullCheck(instruction);
3840 // If read barriers are enabled, emit read barriers other than
3841 // Baker's using a slow path (and also unpoison the loaded
3842 // reference, if heap poisoning is enabled).
3843 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
3844 } else {
3845 vixl32::Register temp = temps.Acquire();
3846
3847 if (has_intermediate_address) {
3848 TODO_VIXL32(FATAL);
3849 } else {
3850 __ Add(temp, obj, data_offset);
3851 }
3852 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
3853
3854 codegen_->MaybeRecordImplicitNullCheck(instruction);
3855 // If read barriers are enabled, emit read barriers other than
3856 // Baker's using a slow path (and also unpoison the loaded
3857 // reference, if heap poisoning is enabled).
3858 codegen_->MaybeGenerateReadBarrierSlow(
3859 instruction, out_loc, out_loc, obj_loc, data_offset, index);
3860 }
3861 }
3862 break;
3863 }
3864
3865 case Primitive::kPrimLong: {
3866 if (index.IsConstant()) {
3867 size_t offset =
3868 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3869 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
3870 } else {
3871 vixl32::Register temp = temps.Acquire();
3872 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
3873 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
3874 }
3875 break;
3876 }
3877
3878 case Primitive::kPrimFloat: {
3879 vixl32::SRegister out = SRegisterFrom(out_loc);
3880 if (index.IsConstant()) {
3881 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3882 GetAssembler()->LoadSFromOffset(out, obj, offset);
3883 } else {
3884 vixl32::Register temp = temps.Acquire();
3885 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
3886 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
3887 }
3888 break;
3889 }
3890
3891 case Primitive::kPrimDouble: {
3892 if (index.IsConstant()) {
3893 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3894 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
3895 } else {
3896 vixl32::Register temp = temps.Acquire();
3897 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
3898 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
3899 }
3900 break;
3901 }
3902
3903 case Primitive::kPrimVoid:
3904 LOG(FATAL) << "Unreachable type " << type;
3905 UNREACHABLE();
3906 }
3907
3908 if (type == Primitive::kPrimNot) {
3909 // Potential implicit null checks, in the case of reference
3910 // arrays, are handled in the previous switch statement.
3911 } else if (!maybe_compressed_char_at) {
3912 codegen_->MaybeRecordImplicitNullCheck(instruction);
3913 }
3914}
3915
3916void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
3917 Primitive::Type value_type = instruction->GetComponentType();
3918
3919 bool needs_write_barrier =
3920 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3921 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
3922
3923 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3924 instruction,
3925 may_need_runtime_call_for_type_check ?
3926 LocationSummary::kCallOnSlowPath :
3927 LocationSummary::kNoCall);
3928
3929 locations->SetInAt(0, Location::RequiresRegister());
3930 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3931 if (Primitive::IsFloatingPointType(value_type)) {
3932 locations->SetInAt(2, Location::RequiresFpuRegister());
3933 } else {
3934 locations->SetInAt(2, Location::RequiresRegister());
3935 }
3936 if (needs_write_barrier) {
3937 // Temporary registers for the write barrier.
3938 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
3939 locations->AddTemp(Location::RequiresRegister());
3940 }
3941}
3942
3943void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
3944 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3945 LocationSummary* locations = instruction->GetLocations();
3946 vixl32::Register array = InputRegisterAt(instruction, 0);
3947 Location index = locations->InAt(1);
3948 Primitive::Type value_type = instruction->GetComponentType();
3949 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
3950 bool needs_write_barrier =
3951 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3952 uint32_t data_offset =
3953 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
3954 Location value_loc = locations->InAt(2);
3955 HInstruction* array_instr = instruction->GetArray();
3956 bool has_intermediate_address = array_instr->IsIntermediateAddress();
3957 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
3958 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
3959
3960 switch (value_type) {
3961 case Primitive::kPrimBoolean:
3962 case Primitive::kPrimByte:
3963 case Primitive::kPrimShort:
3964 case Primitive::kPrimChar:
3965 case Primitive::kPrimInt: {
3966 if (index.IsConstant()) {
3967 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
3968 uint32_t full_offset =
3969 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
3970 StoreOperandType store_type = GetStoreOperandType(value_type);
3971 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
3972 } else {
3973 vixl32::Register temp = temps.Acquire();
3974
3975 if (has_intermediate_address) {
3976 TODO_VIXL32(FATAL);
3977 } else {
3978 __ Add(temp, array, data_offset);
3979 }
3980 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
3981 }
3982 break;
3983 }
3984
3985 case Primitive::kPrimNot: {
3986 vixl32::Register value = RegisterFrom(value_loc);
3987 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
3988 // See the comment in instruction_simplifier_shared.cc.
3989 DCHECK(!has_intermediate_address);
3990
3991 if (instruction->InputAt(2)->IsNullConstant()) {
3992 // Just setting null.
3993 if (index.IsConstant()) {
3994 size_t offset =
3995 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3996 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
3997 } else {
3998 DCHECK(index.IsRegister()) << index;
3999 vixl32::Register temp = temps.Acquire();
4000 __ Add(temp, array, data_offset);
4001 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
4002 }
4003 codegen_->MaybeRecordImplicitNullCheck(instruction);
4004 DCHECK(!needs_write_barrier);
4005 DCHECK(!may_need_runtime_call_for_type_check);
4006 break;
4007 }
4008
4009 DCHECK(needs_write_barrier);
4010 Location temp1_loc = locations->GetTemp(0);
4011 vixl32::Register temp1 = RegisterFrom(temp1_loc);
4012 Location temp2_loc = locations->GetTemp(1);
4013 vixl32::Register temp2 = RegisterFrom(temp2_loc);
4014 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4015 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4016 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4017 vixl32::Label done;
4018 SlowPathCodeARMVIXL* slow_path = nullptr;
4019
4020 if (may_need_runtime_call_for_type_check) {
4021 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
4022 codegen_->AddSlowPath(slow_path);
4023 if (instruction->GetValueCanBeNull()) {
4024 vixl32::Label non_zero;
4025 __ Cbnz(value, &non_zero);
4026 if (index.IsConstant()) {
4027 size_t offset =
4028 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4029 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
4030 } else {
4031 DCHECK(index.IsRegister()) << index;
4032 vixl32::Register temp = temps.Acquire();
4033 __ Add(temp, array, data_offset);
4034 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
4035 }
4036 codegen_->MaybeRecordImplicitNullCheck(instruction);
4037 __ B(&done);
4038 __ Bind(&non_zero);
4039 }
4040
4041 // Note that when read barriers are enabled, the type checks
4042 // are performed without read barriers. This is fine, even in
4043 // the case where a class object is in the from-space after
4044 // the flip, as a comparison involving such a type would not
4045 // produce a false positive; it may of course produce a false
4046 // negative, in which case we would take the ArraySet slow
4047 // path.
4048
4049 // /* HeapReference<Class> */ temp1 = array->klass_
4050 GetAssembler()->LoadFromOffset(kLoadWord, temp1, array, class_offset);
4051 codegen_->MaybeRecordImplicitNullCheck(instruction);
4052 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
4053
4054 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4055 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4056 // /* HeapReference<Class> */ temp2 = value->klass_
4057 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
4058 // If heap poisoning is enabled, no need to unpoison `temp1`
4059 // nor `temp2`, as we are comparing two poisoned references.
4060 __ Cmp(temp1, temp2);
4061
4062 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4063 vixl32::Label do_put;
4064 __ B(eq, &do_put);
4065 // If heap poisoning is enabled, the `temp1` reference has
4066 // not been unpoisoned yet; unpoison it now.
4067 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
4068
4069 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4070 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4071 // If heap poisoning is enabled, no need to unpoison
4072 // `temp1`, as we are comparing against null below.
4073 __ Cbnz(temp1, slow_path->GetEntryLabel());
4074 __ Bind(&do_put);
4075 } else {
4076 __ B(ne, slow_path->GetEntryLabel());
4077 }
4078 }
4079
4080 vixl32::Register source = value;
4081 if (kPoisonHeapReferences) {
4082 // Note that in the case where `value` is a null reference,
4083 // we do not enter this block, as a null reference does not
4084 // need poisoning.
4085 DCHECK_EQ(value_type, Primitive::kPrimNot);
4086 __ Mov(temp1, value);
4087 GetAssembler()->PoisonHeapReference(temp1);
4088 source = temp1;
4089 }
4090
4091 if (index.IsConstant()) {
4092 size_t offset =
4093 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4094 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
4095 } else {
4096 DCHECK(index.IsRegister()) << index;
4097
4098 vixl32::Register temp = temps.Acquire();
4099 __ Add(temp, array, data_offset);
4100 codegen_->StoreToShiftedRegOffset(value_type,
4101 LocationFrom(source),
4102 temp,
4103 RegisterFrom(index));
4104 }
4105
4106 if (!may_need_runtime_call_for_type_check) {
4107 codegen_->MaybeRecordImplicitNullCheck(instruction);
4108 }
4109
4110 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4111
4112 if (done.IsReferenced()) {
4113 __ Bind(&done);
4114 }
4115
4116 if (slow_path != nullptr) {
4117 __ Bind(slow_path->GetExitLabel());
4118 }
4119
4120 break;
4121 }
4122
4123 case Primitive::kPrimLong: {
4124 Location value = locations->InAt(2);
4125 if (index.IsConstant()) {
4126 size_t offset =
4127 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4128 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
4129 } else {
4130 vixl32::Register temp = temps.Acquire();
4131 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4132 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
4133 }
4134 break;
4135 }
4136
4137 case Primitive::kPrimFloat: {
4138 Location value = locations->InAt(2);
4139 DCHECK(value.IsFpuRegister());
4140 if (index.IsConstant()) {
4141 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4142 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
4143 } else {
4144 vixl32::Register temp = temps.Acquire();
4145 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
4146 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
4147 }
4148 break;
4149 }
4150
4151 case Primitive::kPrimDouble: {
4152 Location value = locations->InAt(2);
4153 DCHECK(value.IsFpuRegisterPair());
4154 if (index.IsConstant()) {
4155 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4156 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
4157 } else {
4158 vixl32::Register temp = temps.Acquire();
4159 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4160 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
4161 }
4162 break;
4163 }
4164
4165 case Primitive::kPrimVoid:
4166 LOG(FATAL) << "Unreachable type " << value_type;
4167 UNREACHABLE();
4168 }
4169
4170 // Objects are handled in the switch.
4171 if (value_type != Primitive::kPrimNot) {
4172 codegen_->MaybeRecordImplicitNullCheck(instruction);
4173 }
4174}
4175
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004176void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
4177 LocationSummary* locations =
4178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4179 locations->SetInAt(0, Location::RequiresRegister());
4180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4181}
4182
4183void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
4184 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
4185 vixl32::Register obj = InputRegisterAt(instruction, 0);
4186 vixl32::Register out = OutputRegister(instruction);
4187 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
4188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004189 // Mask out compression flag from String's array length.
4190 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
4191 __ Bic(out, out, 1u << 31);
4192 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004193}
4194
Scott Wakelingc34dba72016-10-03 10:14:44 +01004195void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
4196 RegisterSet caller_saves = RegisterSet::Empty();
4197 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4198 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
4199 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
4200 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
4201 locations->SetInAt(0, Location::RequiresRegister());
4202 locations->SetInAt(1, Location::RequiresRegister());
4203}
4204
4205void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
4206 SlowPathCodeARMVIXL* slow_path =
4207 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
4208 codegen_->AddSlowPath(slow_path);
4209
4210 vixl32::Register index = InputRegisterAt(instruction, 0);
4211 vixl32::Register length = InputRegisterAt(instruction, 1);
4212
4213 __ Cmp(index, length);
4214 __ B(hs, slow_path->GetEntryLabel());
4215}
4216
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004217void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
4218 vixl32::Register card,
4219 vixl32::Register object,
4220 vixl32::Register value,
4221 bool can_be_null) {
4222 vixl32::Label is_null;
4223 if (can_be_null) {
4224 __ Cbz(value, &is_null);
4225 }
4226 GetAssembler()->LoadFromOffset(
4227 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
4228 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4229 __ Strb(card, MemOperand(card, temp));
4230 if (can_be_null) {
4231 __ Bind(&is_null);
4232 }
4233}
4234
Scott Wakelingfe885462016-09-22 10:24:38 +01004235void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4236 LOG(FATAL) << "Unreachable";
4237}
4238
4239void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
4240 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4241}
4242
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004243void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
4244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4245 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/ and related.
4246}
4247
4248void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
4249 HBasicBlock* block = instruction->GetBlock();
4250 if (block->GetLoopInformation() != nullptr) {
4251 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4252 // The back edge will generate the suspend check.
4253 return;
4254 }
4255 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4256 // The goto will generate the suspend check.
4257 return;
4258 }
4259 GenerateSuspendCheck(instruction, nullptr);
4260}
4261
4262void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
4263 HBasicBlock* successor) {
4264 SuspendCheckSlowPathARMVIXL* slow_path =
4265 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
4266 if (slow_path == nullptr) {
4267 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
4268 instruction->SetSlowPath(slow_path);
4269 codegen_->AddSlowPath(slow_path);
4270 if (successor != nullptr) {
4271 DCHECK(successor->IsLoopHeader());
4272 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4273 }
4274 } else {
4275 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4276 }
4277
4278 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4279 vixl32::Register temp = temps.Acquire();
4280 GetAssembler()->LoadFromOffset(
4281 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
4282 if (successor == nullptr) {
4283 __ Cbnz(temp, slow_path->GetEntryLabel());
4284 __ Bind(slow_path->GetReturnLabel());
4285 } else {
4286 __ Cbz(temp, codegen_->GetLabelOf(successor));
4287 __ B(slow_path->GetEntryLabel());
4288 }
4289}
4290
Scott Wakelingfe885462016-09-22 10:24:38 +01004291ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
4292 return codegen_->GetAssembler();
4293}
4294
4295void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004296 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01004297 MoveOperands* move = moves_[index];
4298 Location source = move->GetSource();
4299 Location destination = move->GetDestination();
4300
4301 if (source.IsRegister()) {
4302 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004303 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004304 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004305 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004306 } else {
4307 DCHECK(destination.IsStackSlot());
4308 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004309 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01004310 sp,
4311 destination.GetStackIndex());
4312 }
4313 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004314 if (destination.IsRegister()) {
4315 GetAssembler()->LoadFromOffset(kLoadWord,
4316 RegisterFrom(destination),
4317 sp,
4318 source.GetStackIndex());
4319 } else if (destination.IsFpuRegister()) {
4320 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
4321 } else {
4322 DCHECK(destination.IsStackSlot());
4323 vixl32::Register temp = temps.Acquire();
4324 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
4325 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4326 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004327 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004328 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004329 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004330 } else if (destination.IsFpuRegister()) {
4331 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
4332 } else {
4333 DCHECK(destination.IsStackSlot());
4334 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
4335 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004336 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004337 if (destination.IsDoubleStackSlot()) {
4338 vixl32::DRegister temp = temps.AcquireD();
4339 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
4340 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
4341 } else if (destination.IsRegisterPair()) {
4342 DCHECK(ExpectedPairLayout(destination));
4343 GetAssembler()->LoadFromOffset(
4344 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
4345 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004346 DCHECK(destination.IsFpuRegisterPair()) << destination;
4347 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004348 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004349 } else if (source.IsRegisterPair()) {
4350 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004351 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
4352 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004353 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004354 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004355 } else {
4356 DCHECK(destination.IsDoubleStackSlot()) << destination;
4357 DCHECK(ExpectedPairLayout(source));
4358 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004359 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01004360 sp,
4361 destination.GetStackIndex());
4362 }
4363 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004364 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004365 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004366 } else if (destination.IsFpuRegisterPair()) {
4367 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
4368 } else {
4369 DCHECK(destination.IsDoubleStackSlot()) << destination;
4370 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
4371 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004372 } else {
4373 DCHECK(source.IsConstant()) << source;
4374 HConstant* constant = source.GetConstant();
4375 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4376 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
4377 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004378 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004379 } else {
4380 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01004381 vixl32::Register temp = temps.Acquire();
4382 __ Mov(temp, value);
4383 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4384 }
4385 } else if (constant->IsLongConstant()) {
4386 int64_t value = constant->AsLongConstant()->GetValue();
4387 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004388 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
4389 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004390 } else {
4391 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01004392 vixl32::Register temp = temps.Acquire();
4393 __ Mov(temp, Low32Bits(value));
4394 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4395 __ Mov(temp, High32Bits(value));
4396 GetAssembler()->StoreToOffset(kStoreWord,
4397 temp,
4398 sp,
4399 destination.GetHighStackIndex(kArmWordSize));
4400 }
4401 } else if (constant->IsDoubleConstant()) {
4402 double value = constant->AsDoubleConstant()->GetValue();
4403 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004404 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004405 } else {
4406 DCHECK(destination.IsDoubleStackSlot()) << destination;
4407 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004408 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004409 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004410 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004411 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004412 GetAssembler()->StoreToOffset(kStoreWord,
4413 temp,
4414 sp,
4415 destination.GetHighStackIndex(kArmWordSize));
4416 }
4417 } else {
4418 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
4419 float value = constant->AsFloatConstant()->GetValue();
4420 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004421 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004422 } else {
4423 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01004424 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004425 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004426 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4427 }
4428 }
4429 }
4430}
4431
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004432void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
4433 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4434 vixl32::Register temp = temps.Acquire();
4435 __ Mov(temp, reg);
4436 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
4437 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01004438}
4439
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004440void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
4441 // TODO(VIXL32): Double check the performance of this implementation.
4442 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4443 vixl32::Register temp = temps.Acquire();
4444 vixl32::SRegister temp_s = temps.AcquireS();
4445
4446 __ Ldr(temp, MemOperand(sp, mem1));
4447 __ Vldr(temp_s, MemOperand(sp, mem2));
4448 __ Str(temp, MemOperand(sp, mem2));
4449 __ Vstr(temp_s, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004450}
4451
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004452void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
4453 MoveOperands* move = moves_[index];
4454 Location source = move->GetSource();
4455 Location destination = move->GetDestination();
4456 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4457
4458 if (source.IsRegister() && destination.IsRegister()) {
4459 vixl32::Register temp = temps.Acquire();
4460 DCHECK(!RegisterFrom(source).Is(temp));
4461 DCHECK(!RegisterFrom(destination).Is(temp));
4462 __ Mov(temp, RegisterFrom(destination));
4463 __ Mov(RegisterFrom(destination), RegisterFrom(source));
4464 __ Mov(RegisterFrom(source), temp);
4465 } else if (source.IsRegister() && destination.IsStackSlot()) {
4466 Exchange(RegisterFrom(source), destination.GetStackIndex());
4467 } else if (source.IsStackSlot() && destination.IsRegister()) {
4468 Exchange(RegisterFrom(destination), source.GetStackIndex());
4469 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4470 TODO_VIXL32(FATAL);
4471 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4472 TODO_VIXL32(FATAL);
4473 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
4474 vixl32::DRegister temp = temps.AcquireD();
4475 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
4476 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
4477 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
4478 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
4479 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
4480 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
4481 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
4482 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
4483 vixl32::DRegister temp = temps.AcquireD();
4484 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
4485 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
4486 GetAssembler()->StoreDToOffset(temp, sp, mem);
4487 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004488 vixl32::DRegister first = DRegisterFrom(source);
4489 vixl32::DRegister second = DRegisterFrom(destination);
4490 vixl32::DRegister temp = temps.AcquireD();
4491 __ Vmov(temp, first);
4492 __ Vmov(first, second);
4493 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004494 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4495 TODO_VIXL32(FATAL);
4496 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4497 TODO_VIXL32(FATAL);
4498 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4499 vixl32::DRegister temp1 = temps.AcquireD();
4500 vixl32::DRegister temp2 = temps.AcquireD();
4501 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
4502 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
4503 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
4504 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
4505 } else {
4506 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
4507 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004508}
4509
4510void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
4511 TODO_VIXL32(FATAL);
4512}
4513
4514void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
4515 TODO_VIXL32(FATAL);
4516}
4517
Artem Serov02d37832016-10-25 15:25:33 +01004518// Check if the desired_class_load_kind is supported. If it is, return it,
4519// otherwise return a fall-back kind that should be used instead.
4520HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
4521 HLoadClass::LoadKind desired_class_load_kind ATTRIBUTE_UNUSED) {
4522 // TODO(VIXL): Implement optimized code paths.
4523 return HLoadClass::LoadKind::kDexCacheViaMethod;
4524}
4525
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004526void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
4527 if (cls->NeedsAccessCheck()) {
4528 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4529 CodeGenerator::CreateLoadClassLocationSummary(
4530 cls,
4531 LocationFrom(calling_convention.GetRegisterAt(0)),
4532 LocationFrom(r0),
4533 /* code_generator_supports_read_barrier */ true);
4534 return;
4535 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004536
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004537 // TODO(VIXL): read barrier code.
4538 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4539 ? LocationSummary::kCallOnSlowPath
4540 : LocationSummary::kNoCall;
4541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4542 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4543 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4544 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
4545 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
4546 locations->SetInAt(0, Location::RequiresRegister());
4547 }
4548 locations->SetOut(Location::RequiresRegister());
4549}
4550
4551void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
4552 LocationSummary* locations = cls->GetLocations();
4553 if (cls->NeedsAccessCheck()) {
4554 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4555 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
4556 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
4557 return;
4558 }
4559
4560 Location out_loc = locations->Out();
4561 vixl32::Register out = OutputRegister(cls);
4562
4563 // TODO(VIXL): read barrier code.
4564 bool generate_null_check = false;
4565 switch (cls->GetLoadKind()) {
4566 case HLoadClass::LoadKind::kReferrersClass: {
4567 DCHECK(!cls->CanCallRuntime());
4568 DCHECK(!cls->MustGenerateClinitCheck());
4569 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4570 vixl32::Register current_method = InputRegisterAt(cls, 0);
4571 GenerateGcRootFieldLoad(cls,
4572 out_loc,
4573 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01004574 ArtMethod::DeclaringClassOffset().Int32Value(),
4575 kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004576 break;
4577 }
4578 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4579 // /* GcRoot<mirror::Class>[] */ out =
4580 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4581 vixl32::Register current_method = InputRegisterAt(cls, 0);
4582 const int32_t resolved_types_offset =
4583 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value();
4584 GetAssembler()->LoadFromOffset(kLoadWord, out, current_method, resolved_types_offset);
4585 // /* GcRoot<mirror::Class> */ out = out[type_index]
4586 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Roland Levillain00468f32016-10-27 18:02:48 +01004587 GenerateGcRootFieldLoad(cls, out_loc, out, offset, kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004588 generate_null_check = !cls->IsInDexCache();
4589 break;
4590 }
4591 default:
4592 TODO_VIXL32(FATAL);
4593 }
4594
4595 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4596 DCHECK(cls->CanCallRuntime());
4597 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
4598 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4599 codegen_->AddSlowPath(slow_path);
4600 if (generate_null_check) {
4601 __ Cbz(out, slow_path->GetEntryLabel());
4602 }
4603 if (cls->MustGenerateClinitCheck()) {
4604 GenerateClassInitializationCheck(slow_path, out);
4605 } else {
4606 __ Bind(slow_path->GetExitLabel());
4607 }
4608 }
4609}
4610
Artem Serov02d37832016-10-25 15:25:33 +01004611void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
4612 LocationSummary* locations =
4613 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4614 locations->SetInAt(0, Location::RequiresRegister());
4615 if (check->HasUses()) {
4616 locations->SetOut(Location::SameAsFirstInput());
4617 }
4618}
4619
4620void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
4621 // We assume the class is not null.
4622 LoadClassSlowPathARMVIXL* slow_path =
4623 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
4624 check,
4625 check->GetDexPc(),
4626 /* do_clinit */ true);
4627 codegen_->AddSlowPath(slow_path);
4628 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
4629}
4630
4631void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
4632 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
4633 UseScratchRegisterScope temps(GetVIXLAssembler());
4634 vixl32::Register temp = temps.Acquire();
4635 GetAssembler()->LoadFromOffset(kLoadWord,
4636 temp,
4637 class_reg,
4638 mirror::Class::StatusOffset().Int32Value());
4639 __ Cmp(temp, mirror::Class::kStatusInitialized);
4640 __ B(lt, slow_path->GetEntryLabel());
4641 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4642 // properly. Therefore, we do a memory fence.
4643 __ Dmb(ISH);
4644 __ Bind(slow_path->GetExitLabel());
4645}
4646
4647// Check if the desired_string_load_kind is supported. If it is, return it,
4648// otherwise return a fall-back kind that should be used instead.
4649HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
4650 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
4651 // TODO(VIXL): Implement optimized code paths. For now we always use the simpler fallback code.
4652 return HLoadString::LoadKind::kDexCacheViaMethod;
4653}
4654
4655void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
4656 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4657 ? LocationSummary::kCallOnMainOnly
4658 : LocationSummary::kNoCall;
4659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
4660
4661 // TODO(VIXL): Implement optimized code paths.
4662 // See InstructionCodeGeneratorARMVIXL::VisitLoadString.
4663 HLoadString::LoadKind load_kind = load->GetLoadKind();
4664 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
4665 locations->SetInAt(0, Location::RequiresRegister());
4666 // TODO(VIXL): Use InvokeRuntimeCallingConventionARMVIXL instead.
4667 locations->SetOut(LocationFrom(r0));
4668 } else {
4669 locations->SetOut(Location::RequiresRegister());
4670 }
4671}
4672
4673void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) {
4674 // TODO(VIXL): Implement optimized code paths.
4675 // We implemented the simplest solution to get first ART tests passing, we deferred the
4676 // optimized path until later, we should implement it using ARM64 implementation as a
4677 // reference. The same related to LocationsBuilderARMVIXL::VisitLoadString.
4678
4679 // TODO: Re-add the compiler code to do string dex cache lookup again.
4680 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
4681 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4682 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex());
4683 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4684 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
4685}
4686
4687static int32_t GetExceptionTlsOffset() {
4688 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
4689}
4690
4691void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
4692 LocationSummary* locations =
4693 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4694 locations->SetOut(Location::RequiresRegister());
4695}
4696
4697void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
4698 vixl32::Register out = OutputRegister(load);
4699 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
4700}
4701
4702
4703void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
4704 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4705}
4706
4707void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4708 UseScratchRegisterScope temps(GetVIXLAssembler());
4709 vixl32::Register temp = temps.Acquire();
4710 __ Mov(temp, 0);
4711 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
4712}
4713
4714void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
4715 LocationSummary* locations =
4716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4717 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4718 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4719}
4720
4721void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
4722 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
4723 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4724}
4725
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004726static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
4727 return kEmitCompilerReadBarrier &&
4728 (kUseBakerReadBarrier ||
4729 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4730 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4731 type_check_kind == TypeCheckKind::kArrayObjectCheck);
4732}
4733
4734void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
4735 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4736 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4737
4738 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4739 switch (type_check_kind) {
4740 case TypeCheckKind::kExactCheck:
4741 case TypeCheckKind::kAbstractClassCheck:
4742 case TypeCheckKind::kClassHierarchyCheck:
4743 case TypeCheckKind::kArrayObjectCheck:
4744 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4745 LocationSummary::kCallOnSlowPath :
4746 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
4747 break;
4748 case TypeCheckKind::kArrayCheck:
4749 case TypeCheckKind::kUnresolvedCheck:
4750 case TypeCheckKind::kInterfaceCheck:
4751 call_kind = LocationSummary::kCallOnSlowPath;
4752 break;
4753 }
4754
4755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4756 locations->SetInAt(0, Location::RequiresRegister());
4757 locations->SetInAt(1, Location::RequiresRegister());
4758 // Note that TypeCheckSlowPathARM uses this "temp" register too.
4759 locations->AddTemp(Location::RequiresRegister());
4760 // When read barriers are enabled, we need an additional temporary
4761 // register for some cases.
4762 if (TypeCheckNeedsATemporary(type_check_kind)) {
4763 locations->AddTemp(Location::RequiresRegister());
4764 }
4765}
4766
4767void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
4768 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4769 LocationSummary* locations = instruction->GetLocations();
4770 Location obj_loc = locations->InAt(0);
4771 vixl32::Register obj = InputRegisterAt(instruction, 0);
4772 vixl32::Register cls = InputRegisterAt(instruction, 1);
4773 Location temp_loc = locations->GetTemp(0);
4774 vixl32::Register temp = RegisterFrom(temp_loc);
4775 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
4776 locations->GetTemp(1) :
4777 Location::NoLocation();
4778 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4779
4780 bool is_type_check_slow_path_fatal =
4781 (type_check_kind == TypeCheckKind::kExactCheck ||
4782 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4783 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4784 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4785 !instruction->CanThrowIntoCatchBlock();
4786 SlowPathCodeARMVIXL* type_check_slow_path =
4787 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
4788 is_type_check_slow_path_fatal);
4789 codegen_->AddSlowPath(type_check_slow_path);
4790
4791 vixl32::Label done;
4792 // Avoid null check if we know obj is not null.
4793 if (instruction->MustDoNullCheck()) {
4794 __ Cbz(obj, &done);
4795 }
4796
4797 // /* HeapReference<Class> */ temp = obj->klass_
4798 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
4799
4800 switch (type_check_kind) {
4801 case TypeCheckKind::kExactCheck:
4802 case TypeCheckKind::kArrayCheck: {
4803 __ Cmp(temp, cls);
4804 // Jump to slow path for throwing the exception or doing a
4805 // more involved array check.
4806 __ B(ne, type_check_slow_path->GetEntryLabel());
4807 break;
4808 }
4809
4810 case TypeCheckKind::kAbstractClassCheck: {
4811 TODO_VIXL32(FATAL);
4812 break;
4813 }
4814
4815 case TypeCheckKind::kClassHierarchyCheck: {
4816 TODO_VIXL32(FATAL);
4817 break;
4818 }
4819
4820 case TypeCheckKind::kArrayObjectCheck: {
4821 TODO_VIXL32(FATAL);
4822 break;
4823 }
4824
4825 case TypeCheckKind::kUnresolvedCheck:
4826 case TypeCheckKind::kInterfaceCheck:
4827 TODO_VIXL32(FATAL);
4828 break;
4829 }
4830 __ Bind(&done);
4831
4832 __ Bind(type_check_slow_path->GetExitLabel());
4833}
4834
Artem Serov02109dd2016-09-23 17:17:54 +01004835void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
4836 HandleBitwiseOperation(instruction, AND);
4837}
4838
4839void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
4840 HandleBitwiseOperation(instruction, ORR);
4841}
4842
4843void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
4844 HandleBitwiseOperation(instruction, EOR);
4845}
4846
4847void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
4848 LocationSummary* locations =
4849 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4850 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4851 || instruction->GetResultType() == Primitive::kPrimLong);
4852 // Note: GVN reorders commutative operations to have the constant on the right hand side.
4853 locations->SetInAt(0, Location::RequiresRegister());
4854 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
4855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4856}
4857
4858void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
4859 HandleBitwiseOperation(instruction);
4860}
4861
4862void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
4863 HandleBitwiseOperation(instruction);
4864}
4865
4866void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
4867 HandleBitwiseOperation(instruction);
4868}
4869
4870// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
4871void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
4872 vixl32::Register first,
4873 uint32_t value) {
4874 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
4875 if (value == 0xffffffffu) {
4876 if (!out.Is(first)) {
4877 __ Mov(out, first);
4878 }
4879 return;
4880 }
4881 if (value == 0u) {
4882 __ Mov(out, 0);
4883 return;
4884 }
4885 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
4886 __ And(out, first, value);
4887 } else {
4888 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
4889 __ Bic(out, first, ~value);
4890 }
4891}
4892
4893// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
4894void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
4895 vixl32::Register first,
4896 uint32_t value) {
4897 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
4898 if (value == 0u) {
4899 if (!out.Is(first)) {
4900 __ Mov(out, first);
4901 }
4902 return;
4903 }
4904 if (value == 0xffffffffu) {
4905 __ Mvn(out, 0);
4906 return;
4907 }
4908 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
4909 __ Orr(out, first, value);
4910 } else {
4911 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
4912 __ Orn(out, first, ~value);
4913 }
4914}
4915
4916// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
4917void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
4918 vixl32::Register first,
4919 uint32_t value) {
4920 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
4921 if (value == 0u) {
4922 if (!out.Is(first)) {
4923 __ Mov(out, first);
4924 }
4925 return;
4926 }
4927 __ Eor(out, first, value);
4928}
4929
4930void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
4931 LocationSummary* locations = instruction->GetLocations();
4932 Location first = locations->InAt(0);
4933 Location second = locations->InAt(1);
4934 Location out = locations->Out();
4935
4936 if (second.IsConstant()) {
4937 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4938 uint32_t value_low = Low32Bits(value);
4939 if (instruction->GetResultType() == Primitive::kPrimInt) {
4940 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
4941 vixl32::Register out_reg = OutputRegister(instruction);
4942 if (instruction->IsAnd()) {
4943 GenerateAndConst(out_reg, first_reg, value_low);
4944 } else if (instruction->IsOr()) {
4945 GenerateOrrConst(out_reg, first_reg, value_low);
4946 } else {
4947 DCHECK(instruction->IsXor());
4948 GenerateEorConst(out_reg, first_reg, value_low);
4949 }
4950 } else {
4951 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4952 uint32_t value_high = High32Bits(value);
4953 vixl32::Register first_low = LowRegisterFrom(first);
4954 vixl32::Register first_high = HighRegisterFrom(first);
4955 vixl32::Register out_low = LowRegisterFrom(out);
4956 vixl32::Register out_high = HighRegisterFrom(out);
4957 if (instruction->IsAnd()) {
4958 GenerateAndConst(out_low, first_low, value_low);
4959 GenerateAndConst(out_high, first_high, value_high);
4960 } else if (instruction->IsOr()) {
4961 GenerateOrrConst(out_low, first_low, value_low);
4962 GenerateOrrConst(out_high, first_high, value_high);
4963 } else {
4964 DCHECK(instruction->IsXor());
4965 GenerateEorConst(out_low, first_low, value_low);
4966 GenerateEorConst(out_high, first_high, value_high);
4967 }
4968 }
4969 return;
4970 }
4971
4972 if (instruction->GetResultType() == Primitive::kPrimInt) {
4973 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
4974 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
4975 vixl32::Register out_reg = OutputRegister(instruction);
4976 if (instruction->IsAnd()) {
4977 __ And(out_reg, first_reg, second_reg);
4978 } else if (instruction->IsOr()) {
4979 __ Orr(out_reg, first_reg, second_reg);
4980 } else {
4981 DCHECK(instruction->IsXor());
4982 __ Eor(out_reg, first_reg, second_reg);
4983 }
4984 } else {
4985 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4986 vixl32::Register first_low = LowRegisterFrom(first);
4987 vixl32::Register first_high = HighRegisterFrom(first);
4988 vixl32::Register second_low = LowRegisterFrom(second);
4989 vixl32::Register second_high = HighRegisterFrom(second);
4990 vixl32::Register out_low = LowRegisterFrom(out);
4991 vixl32::Register out_high = HighRegisterFrom(out);
4992 if (instruction->IsAnd()) {
4993 __ And(out_low, first_low, second_low);
4994 __ And(out_high, first_high, second_high);
4995 } else if (instruction->IsOr()) {
4996 __ Orr(out_low, first_low, second_low);
4997 __ Orr(out_high, first_high, second_high);
4998 } else {
4999 DCHECK(instruction->IsXor());
5000 __ Eor(out_low, first_low, second_low);
5001 __ Eor(out_high, first_high, second_high);
5002 }
5003 }
5004}
5005
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005006void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
5007 HInstruction* instruction ATTRIBUTE_UNUSED,
5008 Location out,
5009 Location obj,
5010 uint32_t offset,
5011 Location maybe_temp ATTRIBUTE_UNUSED) {
5012 vixl32::Register out_reg = RegisterFrom(out);
5013 vixl32::Register obj_reg = RegisterFrom(obj);
5014 if (kEmitCompilerReadBarrier) {
5015 TODO_VIXL32(FATAL);
5016 } else {
5017 // Plain load with no read barrier.
5018 // /* HeapReference<Object> */ out = *(obj + offset)
5019 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5020 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5021 }
5022}
5023
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005024void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
5025 HInstruction* instruction ATTRIBUTE_UNUSED,
5026 Location root,
5027 vixl32::Register obj,
5028 uint32_t offset,
5029 bool requires_read_barrier) {
5030 vixl32::Register root_reg = RegisterFrom(root);
5031 if (requires_read_barrier) {
5032 TODO_VIXL32(FATAL);
5033 } else {
5034 // Plain GC root load with no read barrier.
5035 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5036 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
5037 // Note that GC roots are not affected by heap poisoning, thus we
5038 // do not have to unpoison `root_reg` here.
5039 }
5040}
5041
Artem Serov02d37832016-10-25 15:25:33 +01005042void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction ATTRIBUTE_UNUSED,
5043 Location out,
5044 Location ref ATTRIBUTE_UNUSED,
5045 Location obj ATTRIBUTE_UNUSED,
5046 uint32_t offset ATTRIBUTE_UNUSED,
5047 Location index ATTRIBUTE_UNUSED) {
5048 if (kEmitCompilerReadBarrier) {
5049 DCHECK(!kUseBakerReadBarrier);
5050 TODO_VIXL32(FATAL);
5051 } else if (kPoisonHeapReferences) {
5052 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
5053 }
5054}
5055
5056// Check if the desired_dispatch_info is supported. If it is, return it,
5057// otherwise return a fall-back info that should be used instead.
5058HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
5059 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info ATTRIBUTE_UNUSED,
5060 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
5061 // TODO(VIXL): Implement optimized code paths.
5062 return {
5063 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5064 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5065 0u,
5066 0u
5067 };
5068}
5069
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005070vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
5071 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
5072 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5073 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5074 if (!invoke->GetLocations()->Intrinsified()) {
5075 return RegisterFrom(location);
5076 }
5077 // For intrinsics we allow any location, so it may be on the stack.
5078 if (!location.IsRegister()) {
5079 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
5080 return temp;
5081 }
5082 // For register locations, check if the register was saved. If so, get it from the stack.
5083 // Note: There is a chance that the register was saved but not overwritten, so we could
5084 // save one load. However, since this is just an intrinsic slow path we prefer this
5085 // simple and more robust approach rather that trying to determine if that's the case.
5086 SlowPathCode* slow_path = GetCurrentSlowPath();
5087 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5088 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
5089 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
5090 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
5091 return temp;
5092 }
5093 return RegisterFrom(location);
5094}
5095
5096void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
5097 HInvokeStaticOrDirect* invoke, Location temp) {
5098 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5099 vixl32::Register temp_reg = RegisterFrom(temp);
5100
5101 switch (invoke->GetMethodLoadKind()) {
5102 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
5103 uint32_t offset =
5104 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5105 // temp = thread->string_init_entrypoint
5106 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, tr, offset);
5107 break;
5108 }
5109 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5110 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5111 vixl32::Register method_reg;
5112 if (current_method.IsRegister()) {
5113 method_reg = RegisterFrom(current_method);
5114 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005115 DCHECK(invoke->GetLocations()->Intrinsified());
5116 DCHECK(!current_method.IsValid());
5117 method_reg = temp_reg;
5118 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005119 }
5120 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
5121 GetAssembler()->LoadFromOffset(
5122 kLoadWord,
5123 temp_reg,
5124 method_reg,
5125 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
5126 // temp = temp[index_in_cache];
5127 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5128 uint32_t index_in_cache = invoke->GetDexMethodIndex();
5129 GetAssembler()->LoadFromOffset(
5130 kLoadWord, temp_reg, temp_reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5131 break;
5132 }
5133 default:
5134 TODO_VIXL32(FATAL);
5135 }
5136
5137 // TODO(VIXL): Support `CodePtrLocation` values other than `kCallArtMethod`.
5138 if (invoke->GetCodePtrLocation() != HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod) {
5139 TODO_VIXL32(FATAL);
5140 }
5141
5142 // LR = callee_method->entry_point_from_quick_compiled_code_
5143 GetAssembler()->LoadFromOffset(
5144 kLoadWord,
5145 lr,
5146 RegisterFrom(callee_method),
5147 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
5148 // LR()
5149 __ Blx(lr);
5150
5151 DCHECK(!IsLeafMethod());
5152}
5153
5154void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5155 vixl32::Register temp = RegisterFrom(temp_location);
5156 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5157 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5158
5159 // Use the calling convention instead of the location of the receiver, as
5160 // intrinsics may have put the receiver in a different register. In the intrinsics
5161 // slow path, the arguments have been moved to the right place, so here we are
5162 // guaranteed that the receiver is the first register of the calling convention.
5163 InvokeDexCallingConventionARMVIXL calling_convention;
5164 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
5165 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5166 // /* HeapReference<Class> */ temp = receiver->klass_
5167 GetAssembler()->LoadFromOffset(kLoadWord, temp, receiver, class_offset);
5168 MaybeRecordImplicitNullCheck(invoke);
5169 // Instead of simply (possibly) unpoisoning `temp` here, we should
5170 // emit a read barrier for the previous class reference load.
5171 // However this is not required in practice, as this is an
5172 // intermediate/temporary reference and because the current
5173 // concurrent copying collector keeps the from-space memory
5174 // intact/accessible until the end of the marking phase (the
5175 // concurrent copying collector may not in the future).
5176 GetAssembler()->MaybeUnpoisonHeapReference(temp);
5177
5178 // temp = temp->GetMethodAt(method_offset);
5179 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5180 kArmPointerSize).Int32Value();
5181 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
5182 // LR = temp->GetEntryPoint();
5183 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
5184 // LR();
5185 __ Blx(lr);
5186}
5187
Artem Serov02d37832016-10-25 15:25:33 +01005188// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005189void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5190 if (!trg.IsValid()) {
5191 DCHECK_EQ(type, Primitive::kPrimVoid);
5192 return;
5193 }
5194
5195 DCHECK_NE(type, Primitive::kPrimVoid);
5196
5197 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5198 if (return_loc.Equals(trg)) {
5199 return;
5200 }
5201
5202 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5203 // with the last branch.
5204 if (type == Primitive::kPrimLong) {
5205 TODO_VIXL32(FATAL);
5206 } else if (type == Primitive::kPrimDouble) {
5207 TODO_VIXL32(FATAL);
5208 } else {
5209 // Let the parallel move resolver take care of all of this.
5210 HParallelMove parallel_move(GetGraph()->GetArena());
5211 parallel_move.AddMove(return_loc, trg, type, nullptr);
5212 GetMoveResolver()->EmitNativeCode(&parallel_move);
5213 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005214}
Scott Wakelingfe885462016-09-22 10:24:38 +01005215
5216#undef __
5217#undef QUICK_ENTRY_POINT
5218#undef TODO_VIXL32
5219
5220} // namespace arm
5221} // namespace art