blob: c3ea3c7db27f196c993cead77eda3d591666ec53 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "common_arm.h"
23#include "compiled_method.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010026#include "intrinsics_arm_vixl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "thread.h"
30#include "utils/arm/assembler_arm_vixl.h"
31#include "utils/arm/managed_register_arm.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35namespace art {
36namespace arm {
37
38namespace vixl32 = vixl::aarch32;
39using namespace vixl32; // NOLINT(build/namespaces)
40
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010041using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010042using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010043using helpers::HighDRegisterFrom;
44using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010045using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010046using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010047using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputSRegisterAt;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010049using helpers::InputVRegisterAt;
50using helpers::LocationFrom;
51using helpers::LowRegisterFrom;
52using helpers::LowSRegisterFrom;
53using helpers::OutputRegister;
54using helpers::OutputSRegister;
55using helpers::OutputVRegister;
56using helpers::RegisterFrom;
57using helpers::SRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010058
59using RegisterList = vixl32::RegisterList;
60
61static bool ExpectedPairLayout(Location location) {
62 // We expected this for both core and fpu register pairs.
63 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
64}
65
Anton Kirilove28d9ae2016-10-25 18:17:23 +010066static constexpr int kCurrentMethodStackOffset = 0;
Scott Wakelingfe885462016-09-22 10:24:38 +010067static constexpr size_t kArmInstrMaxSizeInBytes = 4u;
Artem Serov551b28f2016-10-18 19:11:30 +010068static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010069
70#ifdef __
71#error "ARM Codegen VIXL macro-assembler macro already defined."
72#endif
73
Scott Wakelingfe885462016-09-22 10:24:38 +010074// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
75#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
76#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
77
78// Marker that code is yet to be, and must, be implemented.
79#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
80
Scott Wakelinga7812ae2016-10-17 10:03:36 +010081// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
82// for each live D registers they treat two corresponding S registers as live ones.
83//
84// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
85// from a list of contiguous S registers a list of contiguous D registers (processing first/last
86// S registers corner cases) and save/restore this new list treating them as D registers.
87// - decreasing code size
88// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
89// restored and then used in regular non SlowPath code as D register.
90//
91// For the following example (v means the S register is live):
92// D names: | D0 | D1 | D2 | D4 | ...
93// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
94// Live? | | v | v | v | v | v | v | | ...
95//
96// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
97// as D registers.
98//
99// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
100// for lists of floating-point registers.
101static size_t SaveContiguousSRegisterList(size_t first,
102 size_t last,
103 CodeGenerator* codegen,
104 size_t stack_offset) {
105 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
106 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
107 DCHECK_LE(first, last);
108 if ((first == last) && (first == 0)) {
109 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
110 return stack_offset + kSRegSizeInBytes;
111 }
112 if (first % 2 == 1) {
113 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
114 stack_offset += kSRegSizeInBytes;
115 }
116
117 bool save_last = false;
118 if (last % 2 == 0) {
119 save_last = true;
120 --last;
121 }
122
123 if (first < last) {
124 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
125 DCHECK_EQ((last - first + 1) % 2, 0u);
126 size_t number_of_d_regs = (last - first + 1) / 2;
127
128 if (number_of_d_regs == 1) {
129 __ Vstr(d_reg, MemOperand(sp, stack_offset));
130 } else if (number_of_d_regs > 1) {
131 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
132 vixl32::Register base = sp;
133 if (stack_offset != 0) {
134 base = temps.Acquire();
135 __ Add(base, sp, stack_offset);
136 }
137 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
138 }
139 stack_offset += number_of_d_regs * kDRegSizeInBytes;
140 }
141
142 if (save_last) {
143 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
144 stack_offset += kSRegSizeInBytes;
145 }
146
147 return stack_offset;
148}
149
150static size_t RestoreContiguousSRegisterList(size_t first,
151 size_t last,
152 CodeGenerator* codegen,
153 size_t stack_offset) {
154 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
155 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
156 DCHECK_LE(first, last);
157 if ((first == last) && (first == 0)) {
158 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
159 return stack_offset + kSRegSizeInBytes;
160 }
161 if (first % 2 == 1) {
162 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
163 stack_offset += kSRegSizeInBytes;
164 }
165
166 bool restore_last = false;
167 if (last % 2 == 0) {
168 restore_last = true;
169 --last;
170 }
171
172 if (first < last) {
173 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
174 DCHECK_EQ((last - first + 1) % 2, 0u);
175 size_t number_of_d_regs = (last - first + 1) / 2;
176 if (number_of_d_regs == 1) {
177 __ Vldr(d_reg, MemOperand(sp, stack_offset));
178 } else if (number_of_d_regs > 1) {
179 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
180 vixl32::Register base = sp;
181 if (stack_offset != 0) {
182 base = temps.Acquire();
183 __ Add(base, sp, stack_offset);
184 }
185 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
186 }
187 stack_offset += number_of_d_regs * kDRegSizeInBytes;
188 }
189
190 if (restore_last) {
191 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
192 stack_offset += kSRegSizeInBytes;
193 }
194
195 return stack_offset;
196}
197
198void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
199 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
200 size_t orig_offset = stack_offset;
201
202 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
203 for (uint32_t i : LowToHighBits(core_spills)) {
204 // If the register holds an object, update the stack mask.
205 if (locations->RegisterContainsObject(i)) {
206 locations->SetStackBit(stack_offset / kVRegSize);
207 }
208 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
209 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
210 saved_core_stack_offsets_[i] = stack_offset;
211 stack_offset += kArmWordSize;
212 }
213
214 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
215 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
216
217 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
218 orig_offset = stack_offset;
219 for (uint32_t i : LowToHighBits(fp_spills)) {
220 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
221 saved_fpu_stack_offsets_[i] = stack_offset;
222 stack_offset += kArmWordSize;
223 }
224
225 stack_offset = orig_offset;
226 while (fp_spills != 0u) {
227 uint32_t begin = CTZ(fp_spills);
228 uint32_t tmp = fp_spills + (1u << begin);
229 fp_spills &= tmp; // Clear the contiguous range of 1s.
230 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
231 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
232 }
233 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
234}
235
236void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
237 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
238 size_t orig_offset = stack_offset;
239
240 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
241 for (uint32_t i : LowToHighBits(core_spills)) {
242 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
243 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
244 stack_offset += kArmWordSize;
245 }
246
247 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
248 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
249 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
250
251 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
252 while (fp_spills != 0u) {
253 uint32_t begin = CTZ(fp_spills);
254 uint32_t tmp = fp_spills + (1u << begin);
255 fp_spills &= tmp; // Clear the contiguous range of 1s.
256 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
257 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
258 }
259 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
260}
261
262class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
263 public:
264 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
265
266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
267 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
268 __ Bind(GetEntryLabel());
269 if (instruction_->CanThrowIntoCatchBlock()) {
270 // Live registers will be restored in the catch block if caught.
271 SaveLiveRegisters(codegen, instruction_->GetLocations());
272 }
273 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
274 instruction_,
275 instruction_->GetDexPc(),
276 this);
277 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
278 }
279
280 bool IsFatal() const OVERRIDE { return true; }
281
282 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
283
284 private:
285 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
286};
287
Scott Wakelingfe885462016-09-22 10:24:38 +0100288class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
289 public:
290 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
291 : SlowPathCodeARMVIXL(instruction) {}
292
293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100294 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100295 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100296 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100297 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
298 }
299
300 bool IsFatal() const OVERRIDE { return true; }
301
302 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
303
304 private:
305 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
306};
307
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100308class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
309 public:
310 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
311 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
312
313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
314 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
315 __ Bind(GetEntryLabel());
316 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
317 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
318 if (successor_ == nullptr) {
319 __ B(GetReturnLabel());
320 } else {
321 __ B(arm_codegen->GetLabelOf(successor_));
322 }
323 }
324
325 vixl32::Label* GetReturnLabel() {
326 DCHECK(successor_ == nullptr);
327 return &return_label_;
328 }
329
330 HBasicBlock* GetSuccessor() const {
331 return successor_;
332 }
333
334 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
335
336 private:
337 // If not null, the block to branch to after the suspend check.
338 HBasicBlock* const successor_;
339
340 // If `successor_` is null, the label to branch to after the suspend check.
341 vixl32::Label return_label_;
342
343 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
344};
345
Scott Wakelingc34dba72016-10-03 10:14:44 +0100346class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
347 public:
348 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
349 : SlowPathCodeARMVIXL(instruction) {}
350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
353 LocationSummary* locations = instruction_->GetLocations();
354
355 __ Bind(GetEntryLabel());
356 if (instruction_->CanThrowIntoCatchBlock()) {
357 // Live registers will be restored in the catch block if caught.
358 SaveLiveRegisters(codegen, instruction_->GetLocations());
359 }
360 // We're moving two locations to locations that could overlap, so we need a parallel
361 // move resolver.
362 InvokeRuntimeCallingConventionARMVIXL calling_convention;
363 codegen->EmitParallelMoves(
364 locations->InAt(0),
365 LocationFrom(calling_convention.GetRegisterAt(0)),
366 Primitive::kPrimInt,
367 locations->InAt(1),
368 LocationFrom(calling_convention.GetRegisterAt(1)),
369 Primitive::kPrimInt);
370 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
371 ? kQuickThrowStringBounds
372 : kQuickThrowArrayBounds;
373 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
374 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
375 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
376 }
377
378 bool IsFatal() const OVERRIDE { return true; }
379
380 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
381
382 private:
383 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
384};
385
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100386class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
387 public:
388 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
389 : SlowPathCodeARMVIXL(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
390 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
391 }
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = at_->GetLocations();
395
396 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
397 __ Bind(GetEntryLabel());
398 SaveLiveRegisters(codegen, locations);
399
400 InvokeRuntimeCallingConventionARMVIXL calling_convention;
401 __ Mov(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
402 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
403 : kQuickInitializeType;
404 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
405 if (do_clinit_) {
406 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
407 } else {
408 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
409 }
410
411 // Move the class to the desired location.
412 Location out = locations->Out();
413 if (out.IsValid()) {
414 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
415 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
416 }
417 RestoreLiveRegisters(codegen, locations);
418 __ B(GetExitLabel());
419 }
420
421 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
422
423 private:
424 // The class this slow path will load.
425 HLoadClass* const cls_;
426
427 // The instruction where this slow path is happening.
428 // (Might be the load class or an initialization check).
429 HInstruction* const at_;
430
431 // The dex PC of `at_`.
432 const uint32_t dex_pc_;
433
434 // Whether to initialize the class.
435 const bool do_clinit_;
436
437 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
438};
439
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100440class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
441 public:
442 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
443 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100447 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_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100454 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100455 }
456
457 // We're moving two locations to locations that could overlap, so we need a parallel
458 // move resolver.
459 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100460
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800461 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462 LocationFrom(calling_convention.GetRegisterAt(0)),
463 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800464 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800465 LocationFrom(calling_convention.GetRegisterAt(1)),
466 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100467 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100468 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
469 instruction_,
470 instruction_->GetDexPc(),
471 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800472 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100473 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100474 } else {
475 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800476 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
477 instruction_,
478 instruction_->GetDexPc(),
479 this);
480 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100481 }
482
483 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100484 RestoreLiveRegisters(codegen, locations);
485 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100486 }
487 }
488
489 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
490
491 bool IsFatal() const OVERRIDE { return is_fatal_; }
492
493 private:
494 const bool is_fatal_;
495
496 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
497};
498
Scott Wakelingc34dba72016-10-03 10:14:44 +0100499class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
500 public:
501 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
502 : SlowPathCodeARMVIXL(instruction) {}
503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
505 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
506 __ Bind(GetEntryLabel());
507 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
508 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
509 }
510
511 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
512
513 private:
514 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
515};
516
517class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
518 public:
519 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
520
521 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
522 LocationSummary* locations = instruction_->GetLocations();
523 __ Bind(GetEntryLabel());
524 SaveLiveRegisters(codegen, locations);
525
526 InvokeRuntimeCallingConventionARMVIXL calling_convention;
527 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
528 parallel_move.AddMove(
529 locations->InAt(0),
530 LocationFrom(calling_convention.GetRegisterAt(0)),
531 Primitive::kPrimNot,
532 nullptr);
533 parallel_move.AddMove(
534 locations->InAt(1),
535 LocationFrom(calling_convention.GetRegisterAt(1)),
536 Primitive::kPrimInt,
537 nullptr);
538 parallel_move.AddMove(
539 locations->InAt(2),
540 LocationFrom(calling_convention.GetRegisterAt(2)),
541 Primitive::kPrimNot,
542 nullptr);
543 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
544
545 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
546 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
547 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
548 RestoreLiveRegisters(codegen, locations);
549 __ B(GetExitLabel());
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
553
554 private:
555 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
556};
557
558
Scott Wakelingfe885462016-09-22 10:24:38 +0100559inline vixl32::Condition ARMCondition(IfCondition cond) {
560 switch (cond) {
561 case kCondEQ: return eq;
562 case kCondNE: return ne;
563 case kCondLT: return lt;
564 case kCondLE: return le;
565 case kCondGT: return gt;
566 case kCondGE: return ge;
567 case kCondB: return lo;
568 case kCondBE: return ls;
569 case kCondA: return hi;
570 case kCondAE: return hs;
571 }
572 LOG(FATAL) << "Unreachable";
573 UNREACHABLE();
574}
575
576// Maps signed condition to unsigned condition.
577inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
578 switch (cond) {
579 case kCondEQ: return eq;
580 case kCondNE: return ne;
581 // Signed to unsigned.
582 case kCondLT: return lo;
583 case kCondLE: return ls;
584 case kCondGT: return hi;
585 case kCondGE: return hs;
586 // Unsigned remain unchanged.
587 case kCondB: return lo;
588 case kCondBE: return ls;
589 case kCondA: return hi;
590 case kCondAE: return hs;
591 }
592 LOG(FATAL) << "Unreachable";
593 UNREACHABLE();
594}
595
596inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
597 // The ARM condition codes can express all the necessary branches, see the
598 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
599 // There is no dex instruction or HIR that would need the missing conditions
600 // "equal or unordered" or "not equal".
601 switch (cond) {
602 case kCondEQ: return eq;
603 case kCondNE: return ne /* unordered */;
604 case kCondLT: return gt_bias ? cc : lt /* unordered */;
605 case kCondLE: return gt_bias ? ls : le /* unordered */;
606 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
607 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
608 default:
609 LOG(FATAL) << "UNREACHABLE";
610 UNREACHABLE();
611 }
612}
613
Scott Wakelingfe885462016-09-22 10:24:38 +0100614void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
615 stream << vixl32::Register(reg);
616}
617
618void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
619 stream << vixl32::SRegister(reg);
620}
621
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100622static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100623 uint32_t mask = 0;
624 for (uint32_t i = regs.GetFirstSRegister().GetCode();
625 i <= regs.GetLastSRegister().GetCode();
626 ++i) {
627 mask |= (1 << i);
628 }
629 return mask;
630}
631
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100632size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
633 GetAssembler()->LoadSFromOffset(vixl32::SRegister(reg_id), sp, stack_index);
634 return kArmWordSize;
635}
636
Scott Wakelingfe885462016-09-22 10:24:38 +0100637#undef __
638
639CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
640 const ArmInstructionSetFeatures& isa_features,
641 const CompilerOptions& compiler_options,
642 OptimizingCompilerStats* stats)
643 : CodeGenerator(graph,
644 kNumberOfCoreRegisters,
645 kNumberOfSRegisters,
646 kNumberOfRegisterPairs,
647 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100648 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +0100649 compiler_options,
650 stats),
651 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +0100652 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +0100653 location_builder_(graph, this),
654 instruction_visitor_(graph, this),
655 move_resolver_(graph->GetArena(), this),
656 assembler_(graph->GetArena()),
657 isa_features_(isa_features) {
658 // Always save the LR register to mimic Quick.
659 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100660 // Give d14 and d15 as scratch registers to VIXL.
661 // They are removed from the register allocator in `SetupBlockedRegisters()`.
662 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
663 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
664 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
665 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
666 // also want to investigate giving those 14 other D registers to the allocator.
667 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
668 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +0100669}
670
Artem Serov551b28f2016-10-18 19:11:30 +0100671void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
672 uint32_t num_entries = switch_instr_->GetNumEntries();
673 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
674
675 // We are about to use the assembler to place literals directly. Make sure we have enough
676 // underlying code buffer and we have generated the jump table with right size.
677 codegen->GetVIXLAssembler()->GetBuffer().Align();
678 AssemblerAccurateScope aas(codegen->GetVIXLAssembler(),
679 num_entries * sizeof(int32_t),
680 CodeBufferCheckScope::kMaximumSize);
681 // TODO(VIXL): Check that using lower case bind is fine here.
682 codegen->GetVIXLAssembler()->bind(&table_start_);
683 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
684 for (uint32_t i = 0; i < num_entries; i++) {
685 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
686 DCHECK(target_label->IsBound());
687 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
688 // When doing BX to address we need to have lower bit set to 1 in T32.
689 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
690 jump_offset++;
691 }
692 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
693 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
694 vixl32::Literal<int32_t> literal(jump_offset);
695 codegen->GetVIXLAssembler()->place(&literal);
696 }
697}
698
699void CodeGeneratorARMVIXL::EmitJumpTables() {
700 for (auto&& jump_table : jump_tables_) {
701 jump_table->EmitTable(this);
702 }
703}
704
Andreas Gampeca620d72016-11-08 08:09:33 -0800705#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +0100706
707void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov551b28f2016-10-18 19:11:30 +0100708 EmitJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +0100709 GetAssembler()->FinalizeCode();
710 CodeGenerator::Finalize(allocator);
711}
712
713void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +0100714 // Stack register, LR and PC are always reserved.
715 blocked_core_registers_[SP] = true;
716 blocked_core_registers_[LR] = true;
717 blocked_core_registers_[PC] = true;
718
719 // Reserve thread register.
720 blocked_core_registers_[TR] = true;
721
722 // Reserve temp register.
723 blocked_core_registers_[IP] = true;
724
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100725 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
726 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
727 blocked_fpu_registers_[28] = true;
728 blocked_fpu_registers_[29] = true;
729 blocked_fpu_registers_[30] = true;
730 blocked_fpu_registers_[31] = true;
731
Scott Wakelingfe885462016-09-22 10:24:38 +0100732 if (GetGraph()->IsDebuggable()) {
733 // Stubs do not save callee-save floating point registers. If the graph
734 // is debuggable, we need to deal with these registers differently. For
735 // now, just block them.
736 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
737 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
738 ++i) {
739 blocked_fpu_registers_[i] = true;
740 }
741 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100742}
743
Scott Wakelingfe885462016-09-22 10:24:38 +0100744InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
745 CodeGeneratorARMVIXL* codegen)
746 : InstructionCodeGenerator(graph, codegen),
747 assembler_(codegen->GetAssembler()),
748 codegen_(codegen) {}
749
750void CodeGeneratorARMVIXL::ComputeSpillMask() {
751 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
752 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
753 // There is no easy instruction to restore just the PC on thumb2. We spill and
754 // restore another arbitrary register.
755 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
756 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
757 // We use vpush and vpop for saving and restoring floating point registers, which take
758 // a SRegister and the number of registers to save/restore after that SRegister. We
759 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
760 // but in the range.
761 if (fpu_spill_mask_ != 0) {
762 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
763 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
764 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
765 fpu_spill_mask_ |= (1 << i);
766 }
767 }
768}
769
770void CodeGeneratorARMVIXL::GenerateFrameEntry() {
771 bool skip_overflow_check =
772 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
773 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
774 __ Bind(&frame_entry_label_);
775
776 if (HasEmptyFrame()) {
777 return;
778 }
779
Scott Wakelingfe885462016-09-22 10:24:38 +0100780 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100781 UseScratchRegisterScope temps(GetVIXLAssembler());
782 vixl32::Register temp = temps.Acquire();
Scott Wakelingfe885462016-09-22 10:24:38 +0100783 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
784 // The load must immediately precede RecordPcInfo.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100785 AssemblerAccurateScope aas(GetVIXLAssembler(),
786 kArmInstrMaxSizeInBytes,
787 CodeBufferCheckScope::kMaximumSize);
788 __ ldr(temp, MemOperand(temp));
789 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +0100790 }
791
792 __ Push(RegisterList(core_spill_mask_));
793 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
794 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
795 0,
796 core_spill_mask_,
797 kArmWordSize);
798 if (fpu_spill_mask_ != 0) {
799 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
800
801 // Check that list is contiguous.
802 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
803
804 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
805 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100806 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +0100807 }
808 int adjust = GetFrameSize() - FrameEntrySpillSize();
809 __ Sub(sp, sp, adjust);
810 GetAssembler()->cfi().AdjustCFAOffset(adjust);
811 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
812}
813
814void CodeGeneratorARMVIXL::GenerateFrameExit() {
815 if (HasEmptyFrame()) {
816 __ Bx(lr);
817 return;
818 }
819 GetAssembler()->cfi().RememberState();
820 int adjust = GetFrameSize() - FrameEntrySpillSize();
821 __ Add(sp, sp, adjust);
822 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
823 if (fpu_spill_mask_ != 0) {
824 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
825
826 // Check that list is contiguous.
827 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
828
829 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
830 GetAssembler()->cfi().AdjustCFAOffset(
831 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100832 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +0100833 }
834 // Pop LR into PC to return.
835 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
836 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
837 __ Pop(RegisterList(pop_mask));
838 GetAssembler()->cfi().RestoreState();
839 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
840}
841
842void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
843 __ Bind(GetLabelOf(block));
844}
845
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100846void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
847 if (source.Equals(destination)) {
848 return;
849 }
850 if (destination.IsRegister()) {
851 if (source.IsRegister()) {
852 __ Mov(RegisterFrom(destination), RegisterFrom(source));
853 } else if (source.IsFpuRegister()) {
854 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
855 } else {
856 GetAssembler()->LoadFromOffset(kLoadWord,
857 RegisterFrom(destination),
858 sp,
859 source.GetStackIndex());
860 }
861 } else if (destination.IsFpuRegister()) {
862 if (source.IsRegister()) {
863 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
864 } else if (source.IsFpuRegister()) {
865 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
866 } else {
867 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
868 }
869 } else {
870 DCHECK(destination.IsStackSlot()) << destination;
871 if (source.IsRegister()) {
872 GetAssembler()->StoreToOffset(kStoreWord,
873 RegisterFrom(source),
874 sp,
875 destination.GetStackIndex());
876 } else if (source.IsFpuRegister()) {
877 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
878 } else {
879 DCHECK(source.IsStackSlot()) << source;
880 UseScratchRegisterScope temps(GetVIXLAssembler());
881 vixl32::Register temp = temps.Acquire();
882 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
883 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
884 }
885 }
886}
887
Artem Serovcfbe9132016-10-14 15:58:56 +0100888void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
889 DCHECK(location.IsRegister());
890 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +0100891}
892
893void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100894 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
895 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
896 HParallelMove move(GetGraph()->GetArena());
897 move.AddMove(src, dst, dst_type, nullptr);
898 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +0100899}
900
Artem Serovcfbe9132016-10-14 15:58:56 +0100901void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
902 if (location.IsRegister()) {
903 locations->AddTemp(location);
904 } else if (location.IsRegisterPair()) {
905 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
906 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
907 } else {
908 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
909 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100910}
911
912void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
913 HInstruction* instruction,
914 uint32_t dex_pc,
915 SlowPathCode* slow_path) {
916 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
917 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
918 if (EntrypointRequiresStackMap(entrypoint)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100919 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
920 // previous instruction.
Scott Wakelingfe885462016-09-22 10:24:38 +0100921 RecordPcInfo(instruction, dex_pc, slow_path);
922 }
923}
924
925void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
926 HInstruction* instruction,
927 SlowPathCode* slow_path) {
928 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
929 GenerateInvokeRuntime(entry_point_offset);
930}
931
932void CodeGeneratorARMVIXL::GenerateInvokeRuntime(int32_t entry_point_offset) {
933 GetAssembler()->LoadFromOffset(kLoadWord, lr, tr, entry_point_offset);
934 __ Blx(lr);
935}
936
Scott Wakelingfe885462016-09-22 10:24:38 +0100937void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
938 DCHECK(!successor->IsExitBlock());
939 HBasicBlock* block = got->GetBlock();
940 HInstruction* previous = got->GetPrevious();
941 HLoopInformation* info = block->GetLoopInformation();
942
943 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
944 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
945 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
946 return;
947 }
948 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
949 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
950 }
951 if (!codegen_->GoesToNextBlock(block, successor)) {
952 __ B(codegen_->GetLabelOf(successor));
953 }
954}
955
956void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
957 got->SetLocations(nullptr);
958}
959
960void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
961 HandleGoto(got, got->GetSuccessor());
962}
963
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100964void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
965 try_boundary->SetLocations(nullptr);
966}
967
968void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
969 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
970 if (!successor->IsExitBlock()) {
971 HandleGoto(try_boundary, successor);
972 }
973}
974
Scott Wakelingfe885462016-09-22 10:24:38 +0100975void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
976 exit->SetLocations(nullptr);
977}
978
979void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
980}
981
982void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
983 Primitive::Type type = instruction->InputAt(0)->GetType();
984 Location lhs_loc = instruction->GetLocations()->InAt(0);
985 Location rhs_loc = instruction->GetLocations()->InAt(1);
986 if (rhs_loc.IsConstant()) {
987 // 0.0 is the only immediate that can be encoded directly in
988 // a VCMP instruction.
989 //
990 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
991 // specify that in a floating-point comparison, positive zero
992 // and negative zero are considered equal, so we can use the
993 // literal 0.0 for both cases here.
994 //
995 // Note however that some methods (Float.equal, Float.compare,
996 // Float.compareTo, Double.equal, Double.compare,
997 // Double.compareTo, Math.max, Math.min, StrictMath.max,
998 // StrictMath.min) consider 0.0 to be (strictly) greater than
999 // -0.0. So if we ever translate calls to these methods into a
1000 // HCompare instruction, we must handle the -0.0 case with
1001 // care here.
1002 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1003 if (type == Primitive::kPrimFloat) {
1004 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1005 } else {
1006 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001007 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001008 }
1009 } else {
1010 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001011 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001012 } else {
1013 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001014 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001015 }
1016 }
1017}
1018
1019void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1020 vixl32::Label* true_label,
1021 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1022 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1023 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1024 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1025}
1026
1027void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1028 vixl32::Label* true_label,
1029 vixl32::Label* false_label) {
1030 LocationSummary* locations = cond->GetLocations();
1031 Location left = locations->InAt(0);
1032 Location right = locations->InAt(1);
1033 IfCondition if_cond = cond->GetCondition();
1034
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001035 vixl32::Register left_high = HighRegisterFrom(left);
1036 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01001037 IfCondition true_high_cond = if_cond;
1038 IfCondition false_high_cond = cond->GetOppositeCondition();
1039 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
1040
1041 // Set the conditions for the test, remembering that == needs to be
1042 // decided using the low words.
1043 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1044 switch (if_cond) {
1045 case kCondEQ:
1046 case kCondNE:
1047 // Nothing to do.
1048 break;
1049 case kCondLT:
1050 false_high_cond = kCondGT;
1051 break;
1052 case kCondLE:
1053 true_high_cond = kCondLT;
1054 break;
1055 case kCondGT:
1056 false_high_cond = kCondLT;
1057 break;
1058 case kCondGE:
1059 true_high_cond = kCondGT;
1060 break;
1061 case kCondB:
1062 false_high_cond = kCondA;
1063 break;
1064 case kCondBE:
1065 true_high_cond = kCondB;
1066 break;
1067 case kCondA:
1068 false_high_cond = kCondB;
1069 break;
1070 case kCondAE:
1071 true_high_cond = kCondA;
1072 break;
1073 }
1074 if (right.IsConstant()) {
1075 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1076 int32_t val_low = Low32Bits(value);
1077 int32_t val_high = High32Bits(value);
1078
1079 __ Cmp(left_high, val_high);
1080 if (if_cond == kCondNE) {
1081 __ B(ARMCondition(true_high_cond), true_label);
1082 } else if (if_cond == kCondEQ) {
1083 __ B(ARMCondition(false_high_cond), false_label);
1084 } else {
1085 __ B(ARMCondition(true_high_cond), true_label);
1086 __ B(ARMCondition(false_high_cond), false_label);
1087 }
1088 // Must be equal high, so compare the lows.
1089 __ Cmp(left_low, val_low);
1090 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001091 vixl32::Register right_high = HighRegisterFrom(right);
1092 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001093
1094 __ Cmp(left_high, right_high);
1095 if (if_cond == kCondNE) {
1096 __ B(ARMCondition(true_high_cond), true_label);
1097 } else if (if_cond == kCondEQ) {
1098 __ B(ARMCondition(false_high_cond), false_label);
1099 } else {
1100 __ B(ARMCondition(true_high_cond), true_label);
1101 __ B(ARMCondition(false_high_cond), false_label);
1102 }
1103 // Must be equal high, so compare the lows.
1104 __ Cmp(left_low, right_low);
1105 }
1106 // The last comparison might be unsigned.
1107 // TODO: optimize cases where this is always true/false
1108 __ B(final_condition, true_label);
1109}
1110
1111void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1112 vixl32::Label* true_target_in,
1113 vixl32::Label* false_target_in) {
1114 // Generated branching requires both targets to be explicit. If either of the
1115 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1116 vixl32::Label fallthrough;
1117 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1118 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1119
1120 Primitive::Type type = condition->InputAt(0)->GetType();
1121 switch (type) {
1122 case Primitive::kPrimLong:
1123 GenerateLongComparesAndJumps(condition, true_target, false_target);
1124 break;
1125 case Primitive::kPrimFloat:
1126 case Primitive::kPrimDouble:
1127 GenerateVcmp(condition);
1128 GenerateFPJumps(condition, true_target, false_target);
1129 break;
1130 default:
1131 LOG(FATAL) << "Unexpected compare type " << type;
1132 }
1133
1134 if (false_target != &fallthrough) {
1135 __ B(false_target);
1136 }
1137
1138 if (true_target_in == nullptr || false_target_in == nullptr) {
1139 __ Bind(&fallthrough);
1140 }
1141}
1142
1143void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1144 size_t condition_input_index,
1145 vixl32::Label* true_target,
1146 vixl32::Label* false_target) {
1147 HInstruction* cond = instruction->InputAt(condition_input_index);
1148
1149 if (true_target == nullptr && false_target == nullptr) {
1150 // Nothing to do. The code always falls through.
1151 return;
1152 } else if (cond->IsIntConstant()) {
1153 // Constant condition, statically compared against "true" (integer value 1).
1154 if (cond->AsIntConstant()->IsTrue()) {
1155 if (true_target != nullptr) {
1156 __ B(true_target);
1157 }
1158 } else {
1159 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
1160 if (false_target != nullptr) {
1161 __ B(false_target);
1162 }
1163 }
1164 return;
1165 }
1166
1167 // The following code generates these patterns:
1168 // (1) true_target == nullptr && false_target != nullptr
1169 // - opposite condition true => branch to false_target
1170 // (2) true_target != nullptr && false_target == nullptr
1171 // - condition true => branch to true_target
1172 // (3) true_target != nullptr && false_target != nullptr
1173 // - condition true => branch to true_target
1174 // - branch to false_target
1175 if (IsBooleanValueOrMaterializedCondition(cond)) {
1176 // Condition has been materialized, compare the output to 0.
1177 if (kIsDebugBuild) {
1178 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1179 DCHECK(cond_val.IsRegister());
1180 }
1181 if (true_target == nullptr) {
1182 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
1183 } else {
1184 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
1185 }
1186 } else {
1187 // Condition has not been materialized. Use its inputs as the comparison and
1188 // its condition as the branch condition.
1189 HCondition* condition = cond->AsCondition();
1190
1191 // If this is a long or FP comparison that has been folded into
1192 // the HCondition, generate the comparison directly.
1193 Primitive::Type type = condition->InputAt(0)->GetType();
1194 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1195 GenerateCompareTestAndBranch(condition, true_target, false_target);
1196 return;
1197 }
1198
1199 LocationSummary* locations = cond->GetLocations();
1200 DCHECK(locations->InAt(0).IsRegister());
1201 vixl32::Register left = InputRegisterAt(cond, 0);
1202 Location right = locations->InAt(1);
1203 if (right.IsRegister()) {
1204 __ Cmp(left, InputRegisterAt(cond, 1));
1205 } else {
1206 DCHECK(right.IsConstant());
1207 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1208 }
1209 if (true_target == nullptr) {
1210 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1211 } else {
1212 __ B(ARMCondition(condition->GetCondition()), true_target);
1213 }
1214 }
1215
1216 // If neither branch falls through (case 3), the conditional branch to `true_target`
1217 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1218 if (true_target != nullptr && false_target != nullptr) {
1219 __ B(false_target);
1220 }
1221}
1222
1223void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1225 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1226 locations->SetInAt(0, Location::RequiresRegister());
1227 }
1228}
1229
1230void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1231 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1232 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001233 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1234 nullptr : codegen_->GetLabelOf(true_successor);
1235 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1236 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001237 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1238}
1239
Scott Wakelingc34dba72016-10-03 10:14:44 +01001240void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1241 LocationSummary* locations = new (GetGraph()->GetArena())
1242 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1243 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1244 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1245 locations->SetInAt(0, Location::RequiresRegister());
1246 }
1247}
1248
1249void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1250 SlowPathCodeARMVIXL* slow_path =
1251 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
1252 GenerateTestAndBranch(deoptimize,
1253 /* condition_input_index */ 0,
1254 slow_path->GetEntryLabel(),
1255 /* false_target */ nullptr);
1256}
1257
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001258void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
1259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1260 if (Primitive::IsFloatingPointType(select->GetType())) {
1261 locations->SetInAt(0, Location::RequiresFpuRegister());
1262 locations->SetInAt(1, Location::RequiresFpuRegister());
1263 } else {
1264 locations->SetInAt(0, Location::RequiresRegister());
1265 locations->SetInAt(1, Location::RequiresRegister());
1266 }
1267 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1268 locations->SetInAt(2, Location::RequiresRegister());
1269 }
1270 locations->SetOut(Location::SameAsFirstInput());
1271}
1272
1273void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
1274 LocationSummary* locations = select->GetLocations();
1275 vixl32::Label false_target;
1276 GenerateTestAndBranch(select,
1277 /* condition_input_index */ 2,
1278 /* true_target */ nullptr,
1279 &false_target);
1280 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1281 __ Bind(&false_target);
1282}
1283
Artem Serov551b28f2016-10-18 19:11:30 +01001284void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1285 new (GetGraph()->GetArena()) LocationSummary(info);
1286}
1287
1288void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
1289 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
1290}
1291
Scott Wakelingfe885462016-09-22 10:24:38 +01001292void CodeGeneratorARMVIXL::GenerateNop() {
1293 __ Nop();
1294}
1295
1296void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
1297 LocationSummary* locations =
1298 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
1299 // Handle the long/FP comparisons made in instruction simplification.
1300 switch (cond->InputAt(0)->GetType()) {
1301 case Primitive::kPrimLong:
1302 locations->SetInAt(0, Location::RequiresRegister());
1303 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1304 if (!cond->IsEmittedAtUseSite()) {
1305 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1306 }
1307 break;
1308
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001309 // TODO(VIXL): https://android-review.googlesource.com/#/c/252265/
Scott Wakelingfe885462016-09-22 10:24:38 +01001310 case Primitive::kPrimFloat:
1311 case Primitive::kPrimDouble:
1312 locations->SetInAt(0, Location::RequiresFpuRegister());
1313 locations->SetInAt(1, Location::RequiresFpuRegister());
1314 if (!cond->IsEmittedAtUseSite()) {
1315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1316 }
1317 break;
1318
1319 default:
1320 locations->SetInAt(0, Location::RequiresRegister());
1321 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1322 if (!cond->IsEmittedAtUseSite()) {
1323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1324 }
1325 }
1326}
1327
1328void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
1329 if (cond->IsEmittedAtUseSite()) {
1330 return;
1331 }
1332
Scott Wakelingfe885462016-09-22 10:24:38 +01001333 vixl32::Register out = OutputRegister(cond);
1334 vixl32::Label true_label, false_label;
1335
1336 switch (cond->InputAt(0)->GetType()) {
1337 default: {
1338 // Integer case.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001339 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
1340 AssemblerAccurateScope aas(GetVIXLAssembler(),
1341 kArmInstrMaxSizeInBytes * 3u,
1342 CodeBufferCheckScope::kMaximumSize);
1343 __ ite(ARMCondition(cond->GetCondition()));
1344 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
1345 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001346 return;
1347 }
1348 case Primitive::kPrimLong:
1349 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1350 break;
1351 case Primitive::kPrimFloat:
1352 case Primitive::kPrimDouble:
1353 GenerateVcmp(cond);
1354 GenerateFPJumps(cond, &true_label, &false_label);
1355 break;
1356 }
1357
1358 // Convert the jumps into the result.
1359 vixl32::Label done_label;
1360
1361 // False case: result = 0.
1362 __ Bind(&false_label);
1363 __ Mov(out, 0);
1364 __ B(&done_label);
1365
1366 // True case: result = 1.
1367 __ Bind(&true_label);
1368 __ Mov(out, 1);
1369 __ Bind(&done_label);
1370}
1371
1372void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
1373 HandleCondition(comp);
1374}
1375
1376void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
1377 HandleCondition(comp);
1378}
1379
1380void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
1381 HandleCondition(comp);
1382}
1383
1384void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
1385 HandleCondition(comp);
1386}
1387
1388void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
1389 HandleCondition(comp);
1390}
1391
1392void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
1393 HandleCondition(comp);
1394}
1395
1396void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1397 HandleCondition(comp);
1398}
1399
1400void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1401 HandleCondition(comp);
1402}
1403
1404void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1405 HandleCondition(comp);
1406}
1407
1408void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1409 HandleCondition(comp);
1410}
1411
1412void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1413 HandleCondition(comp);
1414}
1415
1416void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1417 HandleCondition(comp);
1418}
1419
1420void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
1421 HandleCondition(comp);
1422}
1423
1424void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
1425 HandleCondition(comp);
1426}
1427
1428void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1429 HandleCondition(comp);
1430}
1431
1432void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1433 HandleCondition(comp);
1434}
1435
1436void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
1437 HandleCondition(comp);
1438}
1439
1440void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
1441 HandleCondition(comp);
1442}
1443
1444void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1445 HandleCondition(comp);
1446}
1447
1448void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1449 HandleCondition(comp);
1450}
1451
1452void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
1453 LocationSummary* locations =
1454 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1455 locations->SetOut(Location::ConstantLocation(constant));
1456}
1457
1458void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
1459 // Will be generated at use site.
1460}
1461
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001462void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
1463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1465 locations->SetOut(Location::ConstantLocation(constant));
1466}
1467
1468void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
1469 // Will be generated at use site.
1470}
1471
Scott Wakelingfe885462016-09-22 10:24:38 +01001472void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
1473 LocationSummary* locations =
1474 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1475 locations->SetOut(Location::ConstantLocation(constant));
1476}
1477
1478void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
1479 // Will be generated at use site.
1480}
1481
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001482void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
1483 LocationSummary* locations =
1484 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1485 locations->SetOut(Location::ConstantLocation(constant));
1486}
1487
Scott Wakelingc34dba72016-10-03 10:14:44 +01001488void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
1489 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001490 // Will be generated at use site.
1491}
1492
1493void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
1494 LocationSummary* locations =
1495 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1496 locations->SetOut(Location::ConstantLocation(constant));
1497}
1498
Scott Wakelingc34dba72016-10-03 10:14:44 +01001499void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
1500 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001501 // Will be generated at use site.
1502}
1503
Scott Wakelingfe885462016-09-22 10:24:38 +01001504void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1505 memory_barrier->SetLocations(nullptr);
1506}
1507
1508void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1509 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1510}
1511
1512void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
1513 ret->SetLocations(nullptr);
1514}
1515
1516void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
1517 codegen_->GenerateFrameExit();
1518}
1519
1520void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
1521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
1523 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
1524}
1525
1526void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
1527 codegen_->GenerateFrameExit();
1528}
1529
Artem Serovcfbe9132016-10-14 15:58:56 +01001530void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1531 // The trampoline uses the same calling convention as dex calling conventions,
1532 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1533 // the method_idx.
1534 HandleInvoke(invoke);
1535}
1536
1537void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1538 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1539}
1540
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001541void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1542 // Explicit clinit checks triggered by static invokes must have been pruned by
1543 // art::PrepareForRegisterAllocation.
1544 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1545
Anton Kirilov5ec62182016-10-13 20:16:02 +01001546 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
1547 if (intrinsic.TryDispatch(invoke)) {
1548 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1549 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1550 }
1551 return;
1552 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001553
1554 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001555
1556 // TODO(VIXL): invoke->HasPcRelativeDexCache()
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001557}
1558
Anton Kirilov5ec62182016-10-13 20:16:02 +01001559static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
1560 if (invoke->GetLocations()->Intrinsified()) {
1561 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
1562 intrinsic.Dispatch(invoke);
1563 return true;
1564 }
1565 return false;
1566}
1567
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001568void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1569 // Explicit clinit checks triggered by static invokes must have been pruned by
1570 // art::PrepareForRegisterAllocation.
1571 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1572
Anton Kirilov5ec62182016-10-13 20:16:02 +01001573 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1574 return;
1575 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001576
1577 LocationSummary* locations = invoke->GetLocations();
1578 DCHECK(locations->HasTemps());
1579 codegen_->GenerateStaticOrDirectCall(invoke, locations->GetTemp(0));
1580 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1581 // previous instruction.
1582 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1583}
1584
1585void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
1586 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
1587 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
1588}
1589
1590void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001591 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
1592 if (intrinsic.TryDispatch(invoke)) {
1593 return;
1594 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001595
1596 HandleInvoke(invoke);
1597}
1598
1599void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001600 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1601 return;
1602 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001603
1604 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
1605 DCHECK(!codegen_->IsLeafMethod());
1606 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1607 // previous instruction.
1608 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1609}
1610
Artem Serovcfbe9132016-10-14 15:58:56 +01001611void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
1612 HandleInvoke(invoke);
1613 // Add the hidden argument.
1614 invoke->GetLocations()->AddTemp(LocationFrom(r12));
1615}
1616
1617void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
1618 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1619 LocationSummary* locations = invoke->GetLocations();
1620 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1621 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
1622 Location receiver = locations->InAt(0);
1623 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1624
1625 DCHECK(!receiver.IsStackSlot());
1626
1627 // /* HeapReference<Class> */ temp = receiver->klass_
1628 GetAssembler()->LoadFromOffset(kLoadWord, temp, RegisterFrom(receiver), class_offset);
1629
1630 codegen_->MaybeRecordImplicitNullCheck(invoke);
1631 // Instead of simply (possibly) unpoisoning `temp` here, we should
1632 // emit a read barrier for the previous class reference load.
1633 // However this is not required in practice, as this is an
1634 // intermediate/temporary reference and because the current
1635 // concurrent copying collector keeps the from-space memory
1636 // intact/accessible until the end of the marking phase (the
1637 // concurrent copying collector may not in the future).
1638 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1639 GetAssembler()->LoadFromOffset(kLoadWord,
1640 temp,
1641 temp,
1642 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1643 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
1644 invoke->GetImtIndex(), kArmPointerSize));
1645 // temp = temp->GetImtEntryAt(method_offset);
1646 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
1647 uint32_t entry_point =
1648 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
1649 // LR = temp->GetEntryPoint();
1650 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
1651
1652 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
1653 // instruction from clobbering it as they might use r12 as a scratch register.
1654 DCHECK(hidden_reg.Is(r12));
1655 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
1656
1657 {
1658 AssemblerAccurateScope aas(GetVIXLAssembler(),
1659 kArmInstrMaxSizeInBytes,
1660 CodeBufferCheckScope::kMaximumSize);
1661 // LR();
1662 __ blx(lr);
1663 DCHECK(!codegen_->IsLeafMethod());
1664 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1665 }
1666}
1667
Artem Serov02109dd2016-09-23 17:17:54 +01001668void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
1669 LocationSummary* locations =
1670 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1671 switch (neg->GetResultType()) {
1672 case Primitive::kPrimInt: {
1673 locations->SetInAt(0, Location::RequiresRegister());
1674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1675 break;
1676 }
1677 case Primitive::kPrimLong: {
1678 locations->SetInAt(0, Location::RequiresRegister());
1679 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1680 break;
1681 }
1682
1683 case Primitive::kPrimFloat:
1684 case Primitive::kPrimDouble:
1685 locations->SetInAt(0, Location::RequiresFpuRegister());
1686 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1687 break;
1688
1689 default:
1690 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1691 }
1692}
1693
1694void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
1695 LocationSummary* locations = neg->GetLocations();
1696 Location out = locations->Out();
1697 Location in = locations->InAt(0);
1698 switch (neg->GetResultType()) {
1699 case Primitive::kPrimInt:
1700 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
1701 break;
1702
1703 case Primitive::kPrimLong:
1704 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1705 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
1706 // We cannot emit an RSC (Reverse Subtract with Carry)
1707 // instruction here, as it does not exist in the Thumb-2
1708 // instruction set. We use the following approach
1709 // using SBC and SUB instead.
1710 //
1711 // out.hi = -C
1712 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
1713 // out.hi = out.hi - in.hi
1714 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
1715 break;
1716
1717 case Primitive::kPrimFloat:
1718 case Primitive::kPrimDouble:
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001719 // TODO(VIXL): Consider introducing an InputVRegister()
1720 // helper function (equivalent to InputRegister()).
Artem Serov02109dd2016-09-23 17:17:54 +01001721 __ Vneg(OutputVRegister(neg), InputVRegisterAt(neg, 0));
1722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1726 }
1727}
1728
Scott Wakelingfe885462016-09-22 10:24:38 +01001729void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1730 Primitive::Type result_type = conversion->GetResultType();
1731 Primitive::Type input_type = conversion->GetInputType();
1732 DCHECK_NE(result_type, input_type);
1733
1734 // The float-to-long, double-to-long and long-to-float type conversions
1735 // rely on a call to the runtime.
1736 LocationSummary::CallKind call_kind =
1737 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1738 && result_type == Primitive::kPrimLong)
1739 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
1740 ? LocationSummary::kCallOnMainOnly
1741 : LocationSummary::kNoCall;
1742 LocationSummary* locations =
1743 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1744
1745 // The Java language does not allow treating boolean as an integral type but
1746 // our bit representation makes it safe.
1747
1748 switch (result_type) {
1749 case Primitive::kPrimByte:
1750 switch (input_type) {
1751 case Primitive::kPrimLong:
1752 // Type conversion from long to byte is a result of code transformations.
1753 case Primitive::kPrimBoolean:
1754 // Boolean input is a result of code transformations.
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimInt:
1757 case Primitive::kPrimChar:
1758 // Processing a Dex `int-to-byte' instruction.
1759 locations->SetInAt(0, Location::RequiresRegister());
1760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1761 break;
1762
1763 default:
1764 LOG(FATAL) << "Unexpected type conversion from " << input_type
1765 << " to " << result_type;
1766 }
1767 break;
1768
1769 case Primitive::kPrimShort:
1770 switch (input_type) {
1771 case Primitive::kPrimLong:
1772 // Type conversion from long to short is a result of code transformations.
1773 case Primitive::kPrimBoolean:
1774 // Boolean input is a result of code transformations.
1775 case Primitive::kPrimByte:
1776 case Primitive::kPrimInt:
1777 case Primitive::kPrimChar:
1778 // Processing a Dex `int-to-short' instruction.
1779 locations->SetInAt(0, Location::RequiresRegister());
1780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1781 break;
1782
1783 default:
1784 LOG(FATAL) << "Unexpected type conversion from " << input_type
1785 << " to " << result_type;
1786 }
1787 break;
1788
1789 case Primitive::kPrimInt:
1790 switch (input_type) {
1791 case Primitive::kPrimLong:
1792 // Processing a Dex `long-to-int' instruction.
1793 locations->SetInAt(0, Location::Any());
1794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1795 break;
1796
1797 case Primitive::kPrimFloat:
1798 // Processing a Dex `float-to-int' instruction.
1799 locations->SetInAt(0, Location::RequiresFpuRegister());
1800 locations->SetOut(Location::RequiresRegister());
1801 locations->AddTemp(Location::RequiresFpuRegister());
1802 break;
1803
1804 case Primitive::kPrimDouble:
1805 // Processing a Dex `double-to-int' instruction.
1806 locations->SetInAt(0, Location::RequiresFpuRegister());
1807 locations->SetOut(Location::RequiresRegister());
1808 locations->AddTemp(Location::RequiresFpuRegister());
1809 break;
1810
1811 default:
1812 LOG(FATAL) << "Unexpected type conversion from " << input_type
1813 << " to " << result_type;
1814 }
1815 break;
1816
1817 case Primitive::kPrimLong:
1818 switch (input_type) {
1819 case Primitive::kPrimBoolean:
1820 // Boolean input is a result of code transformations.
1821 case Primitive::kPrimByte:
1822 case Primitive::kPrimShort:
1823 case Primitive::kPrimInt:
1824 case Primitive::kPrimChar:
1825 // Processing a Dex `int-to-long' instruction.
1826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1828 break;
1829
1830 case Primitive::kPrimFloat: {
1831 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001832 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1833 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1834 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001835 break;
1836 }
1837
1838 case Primitive::kPrimDouble: {
1839 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001840 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1841 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
1842 calling_convention.GetFpuRegisterAt(1)));
1843 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001844 break;
1845 }
1846
1847 default:
1848 LOG(FATAL) << "Unexpected type conversion from " << input_type
1849 << " to " << result_type;
1850 }
1851 break;
1852
1853 case Primitive::kPrimChar:
1854 switch (input_type) {
1855 case Primitive::kPrimLong:
1856 // Type conversion from long to char is a result of code transformations.
1857 case Primitive::kPrimBoolean:
1858 // Boolean input is a result of code transformations.
1859 case Primitive::kPrimByte:
1860 case Primitive::kPrimShort:
1861 case Primitive::kPrimInt:
1862 // Processing a Dex `int-to-char' instruction.
1863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1865 break;
1866
1867 default:
1868 LOG(FATAL) << "Unexpected type conversion from " << input_type
1869 << " to " << result_type;
1870 }
1871 break;
1872
1873 case Primitive::kPrimFloat:
1874 switch (input_type) {
1875 case Primitive::kPrimBoolean:
1876 // Boolean input is a result of code transformations.
1877 case Primitive::kPrimByte:
1878 case Primitive::kPrimShort:
1879 case Primitive::kPrimInt:
1880 case Primitive::kPrimChar:
1881 // Processing a Dex `int-to-float' instruction.
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetOut(Location::RequiresFpuRegister());
1884 break;
1885
1886 case Primitive::kPrimLong: {
1887 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001888 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1889 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
1890 calling_convention.GetRegisterAt(1)));
1891 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001892 break;
1893 }
1894
1895 case Primitive::kPrimDouble:
1896 // Processing a Dex `double-to-float' instruction.
1897 locations->SetInAt(0, Location::RequiresFpuRegister());
1898 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1899 break;
1900
1901 default:
1902 LOG(FATAL) << "Unexpected type conversion from " << input_type
1903 << " to " << result_type;
1904 };
1905 break;
1906
1907 case Primitive::kPrimDouble:
1908 switch (input_type) {
1909 case Primitive::kPrimBoolean:
1910 // Boolean input is a result of code transformations.
1911 case Primitive::kPrimByte:
1912 case Primitive::kPrimShort:
1913 case Primitive::kPrimInt:
1914 case Primitive::kPrimChar:
1915 // Processing a Dex `int-to-double' instruction.
1916 locations->SetInAt(0, Location::RequiresRegister());
1917 locations->SetOut(Location::RequiresFpuRegister());
1918 break;
1919
1920 case Primitive::kPrimLong:
1921 // Processing a Dex `long-to-double' instruction.
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetOut(Location::RequiresFpuRegister());
1924 locations->AddTemp(Location::RequiresFpuRegister());
1925 locations->AddTemp(Location::RequiresFpuRegister());
1926 break;
1927
1928 case Primitive::kPrimFloat:
1929 // Processing a Dex `float-to-double' instruction.
1930 locations->SetInAt(0, Location::RequiresFpuRegister());
1931 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1932 break;
1933
1934 default:
1935 LOG(FATAL) << "Unexpected type conversion from " << input_type
1936 << " to " << result_type;
1937 };
1938 break;
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type conversion from " << input_type
1942 << " to " << result_type;
1943 }
1944}
1945
1946void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1947 LocationSummary* locations = conversion->GetLocations();
1948 Location out = locations->Out();
1949 Location in = locations->InAt(0);
1950 Primitive::Type result_type = conversion->GetResultType();
1951 Primitive::Type input_type = conversion->GetInputType();
1952 DCHECK_NE(result_type, input_type);
1953 switch (result_type) {
1954 case Primitive::kPrimByte:
1955 switch (input_type) {
1956 case Primitive::kPrimLong:
1957 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001958 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01001959 break;
1960 case Primitive::kPrimBoolean:
1961 // Boolean input is a result of code transformations.
1962 case Primitive::kPrimShort:
1963 case Primitive::kPrimInt:
1964 case Primitive::kPrimChar:
1965 // Processing a Dex `int-to-byte' instruction.
1966 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
1967 break;
1968
1969 default:
1970 LOG(FATAL) << "Unexpected type conversion from " << input_type
1971 << " to " << result_type;
1972 }
1973 break;
1974
1975 case Primitive::kPrimShort:
1976 switch (input_type) {
1977 case Primitive::kPrimLong:
1978 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001979 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01001980 break;
1981 case Primitive::kPrimBoolean:
1982 // Boolean input is a result of code transformations.
1983 case Primitive::kPrimByte:
1984 case Primitive::kPrimInt:
1985 case Primitive::kPrimChar:
1986 // Processing a Dex `int-to-short' instruction.
1987 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
1988 break;
1989
1990 default:
1991 LOG(FATAL) << "Unexpected type conversion from " << input_type
1992 << " to " << result_type;
1993 }
1994 break;
1995
1996 case Primitive::kPrimInt:
1997 switch (input_type) {
1998 case Primitive::kPrimLong:
1999 // Processing a Dex `long-to-int' instruction.
2000 DCHECK(out.IsRegister());
2001 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002002 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002003 } else if (in.IsDoubleStackSlot()) {
2004 GetAssembler()->LoadFromOffset(kLoadWord,
2005 OutputRegister(conversion),
2006 sp,
2007 in.GetStackIndex());
2008 } else {
2009 DCHECK(in.IsConstant());
2010 DCHECK(in.GetConstant()->IsLongConstant());
2011 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2012 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
2013 }
2014 break;
2015
2016 case Primitive::kPrimFloat: {
2017 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002018 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002019 __ Vcvt(I32, F32, temp, InputSRegisterAt(conversion, 0));
2020 __ Vmov(OutputRegister(conversion), temp);
2021 break;
2022 }
2023
2024 case Primitive::kPrimDouble: {
2025 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002026 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002027 __ Vcvt(I32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002028 __ Vmov(OutputRegister(conversion), temp_s);
2029 break;
2030 }
2031
2032 default:
2033 LOG(FATAL) << "Unexpected type conversion from " << input_type
2034 << " to " << result_type;
2035 }
2036 break;
2037
2038 case Primitive::kPrimLong:
2039 switch (input_type) {
2040 case Primitive::kPrimBoolean:
2041 // Boolean input is a result of code transformations.
2042 case Primitive::kPrimByte:
2043 case Primitive::kPrimShort:
2044 case Primitive::kPrimInt:
2045 case Primitive::kPrimChar:
2046 // Processing a Dex `int-to-long' instruction.
2047 DCHECK(out.IsRegisterPair());
2048 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002049 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002050 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002051 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01002052 break;
2053
2054 case Primitive::kPrimFloat:
2055 // Processing a Dex `float-to-long' instruction.
2056 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
2057 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2058 break;
2059
2060 case Primitive::kPrimDouble:
2061 // Processing a Dex `double-to-long' instruction.
2062 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
2063 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 }
2070 break;
2071
2072 case Primitive::kPrimChar:
2073 switch (input_type) {
2074 case Primitive::kPrimLong:
2075 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002076 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002077 break;
2078 case Primitive::kPrimBoolean:
2079 // Boolean input is a result of code transformations.
2080 case Primitive::kPrimByte:
2081 case Primitive::kPrimShort:
2082 case Primitive::kPrimInt:
2083 // Processing a Dex `int-to-char' instruction.
2084 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
2093 case Primitive::kPrimFloat:
2094 switch (input_type) {
2095 case Primitive::kPrimBoolean:
2096 // Boolean input is a result of code transformations.
2097 case Primitive::kPrimByte:
2098 case Primitive::kPrimShort:
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimChar: {
2101 // Processing a Dex `int-to-float' instruction.
2102 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
2103 __ Vcvt(F32, I32, OutputSRegister(conversion), OutputSRegister(conversion));
2104 break;
2105 }
2106
2107 case Primitive::kPrimLong:
2108 // Processing a Dex `long-to-float' instruction.
2109 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
2110 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2111 break;
2112
2113 case Primitive::kPrimDouble:
2114 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002115 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002116 break;
2117
2118 default:
2119 LOG(FATAL) << "Unexpected type conversion from " << input_type
2120 << " to " << result_type;
2121 };
2122 break;
2123
2124 case Primitive::kPrimDouble:
2125 switch (input_type) {
2126 case Primitive::kPrimBoolean:
2127 // Boolean input is a result of code transformations.
2128 case Primitive::kPrimByte:
2129 case Primitive::kPrimShort:
2130 case Primitive::kPrimInt:
2131 case Primitive::kPrimChar: {
2132 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002133 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002134 __ Vcvt(F64, I32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01002135 break;
2136 }
2137
2138 case Primitive::kPrimLong: {
2139 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002140 vixl32::Register low = LowRegisterFrom(in);
2141 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelingfe885462016-09-22 10:24:38 +01002142
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002143 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002144 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelingfe885462016-09-22 10:24:38 +01002145
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002146 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002147 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002148
Scott Wakelingc34dba72016-10-03 10:14:44 +01002149 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002150
2151 // temp_d = int-to-double(high)
2152 __ Vmov(temp_s, high);
2153 __ Vcvt(F64, I32, temp_d, temp_s);
2154 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002155 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002156 // out_d = unsigned-to-double(low)
2157 __ Vmov(out_s, low);
2158 __ Vcvt(F64, U32, out_d, out_s);
2159 // out_d += temp_d * constant_d
2160 __ Vmla(F64, out_d, temp_d, constant_d);
2161 break;
2162 }
2163
2164 case Primitive::kPrimFloat:
2165 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002166 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002167 break;
2168
2169 default:
2170 LOG(FATAL) << "Unexpected type conversion from " << input_type
2171 << " to " << result_type;
2172 };
2173 break;
2174
2175 default:
2176 LOG(FATAL) << "Unexpected type conversion from " << input_type
2177 << " to " << result_type;
2178 }
2179}
2180
2181void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2184 switch (add->GetResultType()) {
2185 case Primitive::kPrimInt: {
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2189 break;
2190 }
2191
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002192 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002193 case Primitive::kPrimLong: {
2194 locations->SetInAt(0, Location::RequiresRegister());
2195 locations->SetInAt(1, Location::RequiresRegister());
2196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2197 break;
2198 }
2199
2200 case Primitive::kPrimFloat:
2201 case Primitive::kPrimDouble: {
2202 locations->SetInAt(0, Location::RequiresFpuRegister());
2203 locations->SetInAt(1, Location::RequiresFpuRegister());
2204 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2205 break;
2206 }
2207
2208 default:
2209 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2210 }
2211}
2212
2213void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
2214 LocationSummary* locations = add->GetLocations();
2215 Location out = locations->Out();
2216 Location first = locations->InAt(0);
2217 Location second = locations->InAt(1);
2218
2219 switch (add->GetResultType()) {
2220 case Primitive::kPrimInt: {
2221 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
2222 }
2223 break;
2224
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002225 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002226 case Primitive::kPrimLong: {
2227 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002228 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2229 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002230 break;
2231 }
2232
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002233 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01002234 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002235 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002236 break;
2237
2238 default:
2239 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2240 }
2241}
2242
2243void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
2244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
2246 switch (sub->GetResultType()) {
2247 case Primitive::kPrimInt: {
2248 locations->SetInAt(0, Location::RequiresRegister());
2249 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
2250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2251 break;
2252 }
2253
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002254 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002255 case Primitive::kPrimLong: {
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, Location::RequiresRegister());
2258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 break;
2260 }
2261 case Primitive::kPrimFloat:
2262 case Primitive::kPrimDouble: {
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetInAt(1, Location::RequiresFpuRegister());
2265 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2266 break;
2267 }
2268 default:
2269 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2270 }
2271}
2272
2273void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
2274 LocationSummary* locations = sub->GetLocations();
2275 Location out = locations->Out();
2276 Location first = locations->InAt(0);
2277 Location second = locations->InAt(1);
2278 switch (sub->GetResultType()) {
2279 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002280 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002281 break;
2282 }
2283
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002284 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002285 case Primitive::kPrimLong: {
2286 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002287 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2288 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002289 break;
2290 }
2291
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002292 case Primitive::kPrimFloat:
2293 case Primitive::kPrimDouble:
2294 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002295 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002296
2297 default:
2298 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2299 }
2300}
2301
2302void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
2303 LocationSummary* locations =
2304 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2305 switch (mul->GetResultType()) {
2306 case Primitive::kPrimInt:
2307 case Primitive::kPrimLong: {
2308 locations->SetInAt(0, Location::RequiresRegister());
2309 locations->SetInAt(1, Location::RequiresRegister());
2310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2311 break;
2312 }
2313
2314 case Primitive::kPrimFloat:
2315 case Primitive::kPrimDouble: {
2316 locations->SetInAt(0, Location::RequiresFpuRegister());
2317 locations->SetInAt(1, Location::RequiresFpuRegister());
2318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2319 break;
2320 }
2321
2322 default:
2323 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2324 }
2325}
2326
2327void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
2328 LocationSummary* locations = mul->GetLocations();
2329 Location out = locations->Out();
2330 Location first = locations->InAt(0);
2331 Location second = locations->InAt(1);
2332 switch (mul->GetResultType()) {
2333 case Primitive::kPrimInt: {
2334 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2335 break;
2336 }
2337 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002338 vixl32::Register out_hi = HighRegisterFrom(out);
2339 vixl32::Register out_lo = LowRegisterFrom(out);
2340 vixl32::Register in1_hi = HighRegisterFrom(first);
2341 vixl32::Register in1_lo = LowRegisterFrom(first);
2342 vixl32::Register in2_hi = HighRegisterFrom(second);
2343 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01002344
2345 // Extra checks to protect caused by the existence of R1_R2.
2346 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2347 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2348 DCHECK_NE(out_hi.GetCode(), in1_lo.GetCode());
2349 DCHECK_NE(out_hi.GetCode(), in2_lo.GetCode());
2350
2351 // input: in1 - 64 bits, in2 - 64 bits
2352 // output: out
2353 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2354 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2355 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2356
2357 UseScratchRegisterScope temps(GetVIXLAssembler());
2358 vixl32::Register temp = temps.Acquire();
2359 // temp <- in1.lo * in2.hi
2360 __ Mul(temp, in1_lo, in2_hi);
2361 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2362 __ Mla(out_hi, in1_hi, in2_lo, temp);
2363 // out.lo <- (in1.lo * in2.lo)[31:0];
2364 __ Umull(out_lo, temp, in1_lo, in2_lo);
2365 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002366 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002367 break;
2368 }
2369
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002370 case Primitive::kPrimFloat:
2371 case Primitive::kPrimDouble:
2372 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002373 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002374
2375 default:
2376 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2377 }
2378}
2379
Scott Wakelingfe885462016-09-22 10:24:38 +01002380void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2381 DCHECK(instruction->IsDiv() || instruction->IsRem());
2382 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2383
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002384 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002385 DCHECK(second.IsConstant());
2386
2387 vixl32::Register out = OutputRegister(instruction);
2388 vixl32::Register dividend = InputRegisterAt(instruction, 0);
2389 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2390 DCHECK(imm == 1 || imm == -1);
2391
2392 if (instruction->IsRem()) {
2393 __ Mov(out, 0);
2394 } else {
2395 if (imm == 1) {
2396 __ Mov(out, dividend);
2397 } else {
2398 __ Rsb(out, dividend, 0);
2399 }
2400 }
2401}
2402
2403void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2404 DCHECK(instruction->IsDiv() || instruction->IsRem());
2405 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2406
2407 LocationSummary* locations = instruction->GetLocations();
2408 Location second = locations->InAt(1);
2409 DCHECK(second.IsConstant());
2410
2411 vixl32::Register out = OutputRegister(instruction);
2412 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002413 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002414 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2415 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
2416 int ctz_imm = CTZ(abs_imm);
2417
2418 if (ctz_imm == 1) {
2419 __ Lsr(temp, dividend, 32 - ctz_imm);
2420 } else {
2421 __ Asr(temp, dividend, 31);
2422 __ Lsr(temp, temp, 32 - ctz_imm);
2423 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002424 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002425
2426 if (instruction->IsDiv()) {
2427 __ Asr(out, out, ctz_imm);
2428 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002429 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002430 }
2431 } else {
2432 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002433 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002434 }
2435}
2436
2437void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2438 DCHECK(instruction->IsDiv() || instruction->IsRem());
2439 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2440
2441 LocationSummary* locations = instruction->GetLocations();
2442 Location second = locations->InAt(1);
2443 DCHECK(second.IsConstant());
2444
2445 vixl32::Register out = OutputRegister(instruction);
2446 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002447 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
2448 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002449 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2450
2451 int64_t magic;
2452 int shift;
2453 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2454
2455 __ Mov(temp1, magic);
2456 __ Smull(temp2, temp1, dividend, temp1);
2457
2458 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002459 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002460 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002461 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002462 }
2463
2464 if (shift != 0) {
2465 __ Asr(temp1, temp1, shift);
2466 }
2467
2468 if (instruction->IsDiv()) {
2469 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2470 } else {
2471 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2472 // TODO: Strength reduction for mls.
2473 __ Mov(temp2, imm);
2474 __ Mls(out, temp1, temp2, dividend);
2475 }
2476}
2477
2478void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
2479 HBinaryOperation* instruction) {
2480 DCHECK(instruction->IsDiv() || instruction->IsRem());
2481 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2482
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002483 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002484 DCHECK(second.IsConstant());
2485
2486 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2487 if (imm == 0) {
2488 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2489 } else if (imm == 1 || imm == -1) {
2490 DivRemOneOrMinusOne(instruction);
2491 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
2492 DivRemByPowerOfTwo(instruction);
2493 } else {
2494 DCHECK(imm <= -2 || imm >= 2);
2495 GenerateDivRemWithAnyConstant(instruction);
2496 }
2497}
2498
2499void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
2500 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2501 if (div->GetResultType() == Primitive::kPrimLong) {
2502 // pLdiv runtime call.
2503 call_kind = LocationSummary::kCallOnMainOnly;
2504 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2505 // sdiv will be replaced by other instruction sequence.
2506 } else if (div->GetResultType() == Primitive::kPrimInt &&
2507 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2508 // pIdivmod runtime call.
2509 call_kind = LocationSummary::kCallOnMainOnly;
2510 }
2511
2512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2513
2514 switch (div->GetResultType()) {
2515 case Primitive::kPrimInt: {
2516 if (div->InputAt(1)->IsConstant()) {
2517 locations->SetInAt(0, Location::RequiresRegister());
2518 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
2519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2520 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2521 if (value == 1 || value == 0 || value == -1) {
2522 // No temp register required.
2523 } else {
2524 locations->AddTemp(Location::RequiresRegister());
2525 if (!IsPowerOfTwo(AbsOrMin(value))) {
2526 locations->AddTemp(Location::RequiresRegister());
2527 }
2528 }
2529 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2530 locations->SetInAt(0, Location::RequiresRegister());
2531 locations->SetInAt(1, Location::RequiresRegister());
2532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2533 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01002534 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2535 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2536 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2537 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2538 // we only need the former.
2539 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002540 }
2541 break;
2542 }
2543 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002544 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2545 locations->SetInAt(0, LocationFrom(
2546 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2547 locations->SetInAt(1, LocationFrom(
2548 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2549 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002550 break;
2551 }
2552 case Primitive::kPrimFloat:
2553 case Primitive::kPrimDouble: {
2554 locations->SetInAt(0, Location::RequiresFpuRegister());
2555 locations->SetInAt(1, Location::RequiresFpuRegister());
2556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2557 break;
2558 }
2559
2560 default:
2561 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2562 }
2563}
2564
2565void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002566 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002567 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002568
2569 switch (div->GetResultType()) {
2570 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002571 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002572 GenerateDivRemConstantIntegral(div);
2573 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2574 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
2575 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01002576 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2577 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
2578 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
2579 DCHECK(r0.Is(OutputRegister(div)));
2580
2581 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
2582 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01002583 }
2584 break;
2585 }
2586
2587 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002588 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2589 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
2590 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
2591 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
2592 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
2593 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
2594 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
2595
2596 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
2597 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01002598 break;
2599 }
2600
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002601 case Primitive::kPrimFloat:
2602 case Primitive::kPrimDouble:
2603 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002604 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002605
2606 default:
2607 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2608 }
2609}
2610
Artem Serov551b28f2016-10-18 19:11:30 +01002611void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
2612 Primitive::Type type = rem->GetResultType();
2613
2614 // Most remainders are implemented in the runtime.
2615 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
2616 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2617 // sdiv will be replaced by other instruction sequence.
2618 call_kind = LocationSummary::kNoCall;
2619 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2620 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2621 // Have hardware divide instruction for int, do it with three instructions.
2622 call_kind = LocationSummary::kNoCall;
2623 }
2624
2625 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2626
2627 switch (type) {
2628 case Primitive::kPrimInt: {
2629 if (rem->InputAt(1)->IsConstant()) {
2630 locations->SetInAt(0, Location::RequiresRegister());
2631 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
2632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2633 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2634 if (value == 1 || value == 0 || value == -1) {
2635 // No temp register required.
2636 } else {
2637 locations->AddTemp(Location::RequiresRegister());
2638 if (!IsPowerOfTwo(AbsOrMin(value))) {
2639 locations->AddTemp(Location::RequiresRegister());
2640 }
2641 }
2642 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2643 locations->SetInAt(0, Location::RequiresRegister());
2644 locations->SetInAt(1, Location::RequiresRegister());
2645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2646 locations->AddTemp(Location::RequiresRegister());
2647 } else {
2648 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2649 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2650 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2651 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2652 // we only need the latter.
2653 locations->SetOut(LocationFrom(r1));
2654 }
2655 break;
2656 }
2657 case Primitive::kPrimLong: {
2658 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2659 locations->SetInAt(0, LocationFrom(
2660 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2661 locations->SetInAt(1, LocationFrom(
2662 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2663 // The runtime helper puts the output in R2,R3.
2664 locations->SetOut(LocationFrom(r2, r3));
2665 break;
2666 }
2667 case Primitive::kPrimFloat: {
2668 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2669 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2670 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2671 locations->SetOut(LocationFrom(s0));
2672 break;
2673 }
2674
2675 case Primitive::kPrimDouble: {
2676 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2677 locations->SetInAt(0, LocationFrom(
2678 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2679 locations->SetInAt(1, LocationFrom(
2680 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2681 locations->SetOut(LocationFrom(s0, s1));
2682 break;
2683 }
2684
2685 default:
2686 LOG(FATAL) << "Unexpected rem type " << type;
2687 }
2688}
2689
2690void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
2691 LocationSummary* locations = rem->GetLocations();
2692 Location second = locations->InAt(1);
2693
2694 Primitive::Type type = rem->GetResultType();
2695 switch (type) {
2696 case Primitive::kPrimInt: {
2697 vixl32::Register reg1 = InputRegisterAt(rem, 0);
2698 vixl32::Register out_reg = OutputRegister(rem);
2699 if (second.IsConstant()) {
2700 GenerateDivRemConstantIntegral(rem);
2701 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2702 vixl32::Register reg2 = RegisterFrom(second);
2703 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2704
2705 // temp = reg1 / reg2 (integer division)
2706 // dest = reg1 - temp * reg2
2707 __ Sdiv(temp, reg1, reg2);
2708 __ Mls(out_reg, temp, reg2, reg1);
2709 } else {
2710 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2711 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
2712 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
2713 DCHECK(out_reg.Is(r1));
2714
2715 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
2716 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
2717 }
2718 break;
2719 }
2720
2721 case Primitive::kPrimLong: {
2722 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
2723 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
2724 break;
2725 }
2726
2727 case Primitive::kPrimFloat: {
2728 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
2729 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
2730 break;
2731 }
2732
2733 case Primitive::kPrimDouble: {
2734 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
2735 CheckEntrypointTypes<kQuickFmod, double, double, double>();
2736 break;
2737 }
2738
2739 default:
2740 LOG(FATAL) << "Unexpected rem type " << type;
2741 }
2742}
2743
2744
Scott Wakelingfe885462016-09-22 10:24:38 +01002745void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002746 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
Scott Wakelingfe885462016-09-22 10:24:38 +01002747 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2748 ? LocationSummary::kCallOnSlowPath
2749 : LocationSummary::kNoCall;
2750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2751 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2752 if (instruction->HasUses()) {
2753 locations->SetOut(Location::SameAsFirstInput());
2754 }
2755}
2756
2757void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2758 DivZeroCheckSlowPathARMVIXL* slow_path =
2759 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
2760 codegen_->AddSlowPath(slow_path);
2761
2762 LocationSummary* locations = instruction->GetLocations();
2763 Location value = locations->InAt(0);
2764
2765 switch (instruction->GetType()) {
2766 case Primitive::kPrimBoolean:
2767 case Primitive::kPrimByte:
2768 case Primitive::kPrimChar:
2769 case Primitive::kPrimShort:
2770 case Primitive::kPrimInt: {
2771 if (value.IsRegister()) {
2772 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2773 } else {
2774 DCHECK(value.IsConstant()) << value;
2775 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2776 __ B(slow_path->GetEntryLabel());
2777 }
2778 }
2779 break;
2780 }
2781 case Primitive::kPrimLong: {
2782 if (value.IsRegisterPair()) {
2783 UseScratchRegisterScope temps(GetVIXLAssembler());
2784 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002785 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01002786 __ B(eq, slow_path->GetEntryLabel());
2787 } else {
2788 DCHECK(value.IsConstant()) << value;
2789 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2790 __ B(slow_path->GetEntryLabel());
2791 }
2792 }
2793 break;
2794 }
2795 default:
2796 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2797 }
2798}
2799
Artem Serov02109dd2016-09-23 17:17:54 +01002800void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
2801 LocationSummary* locations = ror->GetLocations();
2802 vixl32::Register in = InputRegisterAt(ror, 0);
2803 Location rhs = locations->InAt(1);
2804 vixl32::Register out = OutputRegister(ror);
2805
2806 if (rhs.IsConstant()) {
2807 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
2808 // so map all rotations to a +ve. equivalent in that range.
2809 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
2810 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
2811 if (rot) {
2812 // Rotate, mapping left rotations to right equivalents if necessary.
2813 // (e.g. left by 2 bits == right by 30.)
2814 __ Ror(out, in, rot);
2815 } else if (!out.Is(in)) {
2816 __ Mov(out, in);
2817 }
2818 } else {
2819 __ Ror(out, in, RegisterFrom(rhs));
2820 }
2821}
2822
2823// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
2824// rotates by swapping input regs (effectively rotating by the first 32-bits of
2825// a larger rotation) or flipping direction (thus treating larger right/left
2826// rotations as sub-word sized rotations in the other direction) as appropriate.
2827void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
2828 LocationSummary* locations = ror->GetLocations();
2829 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2830 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2831 Location rhs = locations->InAt(1);
2832 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2833 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2834
2835 if (rhs.IsConstant()) {
2836 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
2837 // Map all rotations to +ve. equivalents on the interval [0,63].
2838 rot &= kMaxLongShiftDistance;
2839 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
2840 // logic below to a simple pair of binary orr.
2841 // (e.g. 34 bits == in_reg swap + 2 bits right.)
2842 if (rot >= kArmBitsPerWord) {
2843 rot -= kArmBitsPerWord;
2844 std::swap(in_reg_hi, in_reg_lo);
2845 }
2846 // Rotate, or mov to out for zero or word size rotations.
2847 if (rot != 0u) {
2848 __ Lsr(out_reg_hi, in_reg_hi, rot);
2849 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
2850 __ Lsr(out_reg_lo, in_reg_lo, rot);
2851 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
2852 } else {
2853 __ Mov(out_reg_lo, in_reg_lo);
2854 __ Mov(out_reg_hi, in_reg_hi);
2855 }
2856 } else {
2857 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
2858 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
2859 vixl32::Label end;
2860 vixl32::Label shift_by_32_plus_shift_right;
2861
2862 __ And(shift_right, RegisterFrom(rhs), 0x1F);
2863 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
2864 // TODO(VIXL): Check that flags are kept after "vixl32::LeaveFlags" enabled.
2865 __ Rsb(shift_left, shift_right, kArmBitsPerWord);
2866 __ B(cc, &shift_by_32_plus_shift_right);
2867
2868 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
2869 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
2870 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
2871 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2872 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2873 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2874 __ Lsr(shift_left, in_reg_hi, shift_right);
2875 __ Add(out_reg_lo, out_reg_lo, shift_left);
2876 __ B(&end);
2877
2878 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
2879 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
2880 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
2881 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
2882 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2883 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2884 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2885 __ Lsl(shift_right, in_reg_hi, shift_left);
2886 __ Add(out_reg_lo, out_reg_lo, shift_right);
2887
2888 __ Bind(&end);
2889 }
2890}
2891
2892void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
2893 LocationSummary* locations =
2894 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
2895 switch (ror->GetResultType()) {
2896 case Primitive::kPrimInt: {
2897 locations->SetInAt(0, Location::RequiresRegister());
2898 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
2899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2900 break;
2901 }
2902 case Primitive::kPrimLong: {
2903 locations->SetInAt(0, Location::RequiresRegister());
2904 if (ror->InputAt(1)->IsConstant()) {
2905 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
2906 } else {
2907 locations->SetInAt(1, Location::RequiresRegister());
2908 locations->AddTemp(Location::RequiresRegister());
2909 locations->AddTemp(Location::RequiresRegister());
2910 }
2911 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2912 break;
2913 }
2914 default:
2915 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
2916 }
2917}
2918
2919void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
2920 Primitive::Type type = ror->GetResultType();
2921 switch (type) {
2922 case Primitive::kPrimInt: {
2923 HandleIntegerRotate(ror);
2924 break;
2925 }
2926 case Primitive::kPrimLong: {
2927 HandleLongRotate(ror);
2928 break;
2929 }
2930 default:
2931 LOG(FATAL) << "Unexpected operation type " << type;
2932 UNREACHABLE();
2933 }
2934}
2935
Artem Serov02d37832016-10-25 15:25:33 +01002936void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
2937 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2938
2939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2941
2942 switch (op->GetResultType()) {
2943 case Primitive::kPrimInt: {
2944 locations->SetInAt(0, Location::RequiresRegister());
2945 if (op->InputAt(1)->IsConstant()) {
2946 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2948 } else {
2949 locations->SetInAt(1, Location::RequiresRegister());
2950 // Make the output overlap, as it will be used to hold the masked
2951 // second input.
2952 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2953 }
2954 break;
2955 }
2956 case Primitive::kPrimLong: {
2957 locations->SetInAt(0, Location::RequiresRegister());
2958 if (op->InputAt(1)->IsConstant()) {
2959 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2960 // For simplicity, use kOutputOverlap even though we only require that low registers
2961 // don't clash with high registers which the register allocator currently guarantees.
2962 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2963 } else {
2964 locations->SetInAt(1, Location::RequiresRegister());
2965 locations->AddTemp(Location::RequiresRegister());
2966 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2967 }
2968 break;
2969 }
2970 default:
2971 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2972 }
2973}
2974
2975void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
2976 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2977
2978 LocationSummary* locations = op->GetLocations();
2979 Location out = locations->Out();
2980 Location first = locations->InAt(0);
2981 Location second = locations->InAt(1);
2982
2983 Primitive::Type type = op->GetResultType();
2984 switch (type) {
2985 case Primitive::kPrimInt: {
2986 vixl32::Register out_reg = OutputRegister(op);
2987 vixl32::Register first_reg = InputRegisterAt(op, 0);
2988 if (second.IsRegister()) {
2989 vixl32::Register second_reg = RegisterFrom(second);
2990 // ARM doesn't mask the shift count so we need to do it ourselves.
2991 __ And(out_reg, second_reg, kMaxIntShiftDistance);
2992 if (op->IsShl()) {
2993 __ Lsl(out_reg, first_reg, out_reg);
2994 } else if (op->IsShr()) {
2995 __ Asr(out_reg, first_reg, out_reg);
2996 } else {
2997 __ Lsr(out_reg, first_reg, out_reg);
2998 }
2999 } else {
3000 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3001 uint32_t shift_value = cst & kMaxIntShiftDistance;
3002 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
3003 __ Mov(out_reg, first_reg);
3004 } else if (op->IsShl()) {
3005 __ Lsl(out_reg, first_reg, shift_value);
3006 } else if (op->IsShr()) {
3007 __ Asr(out_reg, first_reg, shift_value);
3008 } else {
3009 __ Lsr(out_reg, first_reg, shift_value);
3010 }
3011 }
3012 break;
3013 }
3014 case Primitive::kPrimLong: {
3015 vixl32::Register o_h = HighRegisterFrom(out);
3016 vixl32::Register o_l = LowRegisterFrom(out);
3017
3018 vixl32::Register high = HighRegisterFrom(first);
3019 vixl32::Register low = LowRegisterFrom(first);
3020
3021 if (second.IsRegister()) {
3022 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3023
3024 vixl32::Register second_reg = RegisterFrom(second);
3025
3026 if (op->IsShl()) {
3027 __ And(o_l, second_reg, kMaxLongShiftDistance);
3028 // Shift the high part
3029 __ Lsl(o_h, high, o_l);
3030 // Shift the low part and `or` what overflew on the high part
3031 __ Rsb(temp, o_l, kArmBitsPerWord);
3032 __ Lsr(temp, low, temp);
3033 __ Orr(o_h, o_h, temp);
3034 // If the shift is > 32 bits, override the high part
3035 __ Subs(temp, o_l, kArmBitsPerWord);
3036 {
3037 AssemblerAccurateScope guard(GetVIXLAssembler(),
3038 3 * kArmInstrMaxSizeInBytes,
3039 CodeBufferCheckScope::kMaximumSize);
3040 __ it(pl);
3041 __ lsl(pl, o_h, low, temp);
3042 }
3043 // Shift the low part
3044 __ Lsl(o_l, low, o_l);
3045 } else if (op->IsShr()) {
3046 __ And(o_h, second_reg, kMaxLongShiftDistance);
3047 // Shift the low part
3048 __ Lsr(o_l, low, o_h);
3049 // Shift the high part and `or` what underflew on the low part
3050 __ Rsb(temp, o_h, kArmBitsPerWord);
3051 __ Lsl(temp, high, temp);
3052 __ Orr(o_l, o_l, temp);
3053 // If the shift is > 32 bits, override the low part
3054 __ Subs(temp, o_h, kArmBitsPerWord);
3055 {
3056 AssemblerAccurateScope guard(GetVIXLAssembler(),
3057 3 * kArmInstrMaxSizeInBytes,
3058 CodeBufferCheckScope::kMaximumSize);
3059 __ it(pl);
3060 __ asr(pl, o_l, high, temp);
3061 }
3062 // Shift the high part
3063 __ Asr(o_h, high, o_h);
3064 } else {
3065 __ And(o_h, second_reg, kMaxLongShiftDistance);
3066 // same as Shr except we use `Lsr`s and not `Asr`s
3067 __ Lsr(o_l, low, o_h);
3068 __ Rsb(temp, o_h, kArmBitsPerWord);
3069 __ Lsl(temp, high, temp);
3070 __ Orr(o_l, o_l, temp);
3071 __ Subs(temp, o_h, kArmBitsPerWord);
3072 {
3073 AssemblerAccurateScope guard(GetVIXLAssembler(),
3074 3 * kArmInstrMaxSizeInBytes,
3075 CodeBufferCheckScope::kMaximumSize);
3076 __ it(pl);
3077 __ lsr(pl, o_l, high, temp);
3078 }
3079 __ Lsr(o_h, high, o_h);
3080 }
3081 } else {
3082 // Register allocator doesn't create partial overlap.
3083 DCHECK(!o_l.Is(high));
3084 DCHECK(!o_h.Is(low));
3085 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3086 uint32_t shift_value = cst & kMaxLongShiftDistance;
3087 if (shift_value > 32) {
3088 if (op->IsShl()) {
3089 __ Lsl(o_h, low, shift_value - 32);
3090 __ Mov(o_l, 0);
3091 } else if (op->IsShr()) {
3092 __ Asr(o_l, high, shift_value - 32);
3093 __ Asr(o_h, high, 31);
3094 } else {
3095 __ Lsr(o_l, high, shift_value - 32);
3096 __ Mov(o_h, 0);
3097 }
3098 } else if (shift_value == 32) {
3099 if (op->IsShl()) {
3100 __ Mov(o_h, low);
3101 __ Mov(o_l, 0);
3102 } else if (op->IsShr()) {
3103 __ Mov(o_l, high);
3104 __ Asr(o_h, high, 31);
3105 } else {
3106 __ Mov(o_l, high);
3107 __ Mov(o_h, 0);
3108 }
3109 } else if (shift_value == 1) {
3110 if (op->IsShl()) {
3111 __ Lsls(o_l, low, 1);
3112 __ Adc(o_h, high, high);
3113 } else if (op->IsShr()) {
3114 __ Asrs(o_h, high, 1);
3115 __ Rrx(o_l, low);
3116 } else {
3117 __ Lsrs(o_h, high, 1);
3118 __ Rrx(o_l, low);
3119 }
3120 } else {
3121 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3122 if (op->IsShl()) {
3123 __ Lsl(o_h, high, shift_value);
3124 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
3125 __ Lsl(o_l, low, shift_value);
3126 } else if (op->IsShr()) {
3127 __ Lsr(o_l, low, shift_value);
3128 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3129 __ Asr(o_h, high, shift_value);
3130 } else {
3131 __ Lsr(o_l, low, shift_value);
3132 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3133 __ Lsr(o_h, high, shift_value);
3134 }
3135 }
3136 }
3137 break;
3138 }
3139 default:
3140 LOG(FATAL) << "Unexpected operation type " << type;
3141 UNREACHABLE();
3142 }
3143}
3144
3145void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
3146 HandleShift(shl);
3147}
3148
3149void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
3150 HandleShift(shl);
3151}
3152
3153void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
3154 HandleShift(shr);
3155}
3156
3157void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
3158 HandleShift(shr);
3159}
3160
3161void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
3162 HandleShift(ushr);
3163}
3164
3165void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
3166 HandleShift(ushr);
3167}
3168
3169void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3170 LocationSummary* locations =
3171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3172 if (instruction->IsStringAlloc()) {
3173 locations->AddTemp(LocationFrom(kMethodRegister));
3174 } else {
3175 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3176 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3177 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3178 }
3179 locations->SetOut(LocationFrom(r0));
3180}
3181
3182void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3183 // Note: if heap poisoning is enabled, the entry point takes cares
3184 // of poisoning the reference.
3185 if (instruction->IsStringAlloc()) {
3186 // String is allocated through StringFactory. Call NewEmptyString entry point.
3187 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
3188 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3189 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
3190 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
3191 AssemblerAccurateScope aas(GetVIXLAssembler(),
3192 kArmInstrMaxSizeInBytes,
3193 CodeBufferCheckScope::kMaximumSize);
3194 __ blx(lr);
3195 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3196 } else {
3197 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3198 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3199 }
3200}
3201
3202void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
3203 LocationSummary* locations =
3204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3205 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3206 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
3207 locations->SetOut(LocationFrom(r0));
3208 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
3209 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
3210}
3211
3212void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
3213 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3214 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3215 // Note: if heap poisoning is enabled, the entry point takes cares
3216 // of poisoning the reference.
3217 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3218 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3219}
3220
3221void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
3222 LocationSummary* locations =
3223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3224 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3225 if (location.IsStackSlot()) {
3226 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3227 } else if (location.IsDoubleStackSlot()) {
3228 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3229 }
3230 locations->SetOut(location);
3231}
3232
3233void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
3234 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3235 // Nothing to do, the parameter is already at its location.
3236}
3237
3238void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
3239 LocationSummary* locations =
3240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3241 locations->SetOut(LocationFrom(kMethodRegister));
3242}
3243
3244void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
3245 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3246 // Nothing to do, the method is already at its location.
3247}
3248
3249void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
3250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
3252 locations->SetInAt(0, Location::RequiresRegister());
3253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3254}
3255
3256void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
3257 LocationSummary* locations = not_->GetLocations();
3258 Location out = locations->Out();
3259 Location in = locations->InAt(0);
3260 switch (not_->GetResultType()) {
3261 case Primitive::kPrimInt:
3262 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
3263 break;
3264
3265 case Primitive::kPrimLong:
3266 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
3267 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
3268 break;
3269
3270 default:
3271 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3272 }
3273}
3274
Scott Wakelingc34dba72016-10-03 10:14:44 +01003275void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3278 locations->SetInAt(0, Location::RequiresRegister());
3279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3280}
3281
3282void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
3283 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
3284}
3285
Artem Serov02d37832016-10-25 15:25:33 +01003286void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
3287 LocationSummary* locations =
3288 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3289 switch (compare->InputAt(0)->GetType()) {
3290 case Primitive::kPrimBoolean:
3291 case Primitive::kPrimByte:
3292 case Primitive::kPrimShort:
3293 case Primitive::kPrimChar:
3294 case Primitive::kPrimInt:
3295 case Primitive::kPrimLong: {
3296 locations->SetInAt(0, Location::RequiresRegister());
3297 locations->SetInAt(1, Location::RequiresRegister());
3298 // Output overlaps because it is written before doing the low comparison.
3299 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3300 break;
3301 }
3302 case Primitive::kPrimFloat:
3303 case Primitive::kPrimDouble: {
3304 locations->SetInAt(0, Location::RequiresFpuRegister());
3305 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
3306 locations->SetOut(Location::RequiresRegister());
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3311 }
3312}
3313
3314void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
3315 LocationSummary* locations = compare->GetLocations();
3316 vixl32::Register out = OutputRegister(compare);
3317 Location left = locations->InAt(0);
3318 Location right = locations->InAt(1);
3319
3320 vixl32::Label less, greater, done;
3321 Primitive::Type type = compare->InputAt(0)->GetType();
3322 vixl32::Condition less_cond = vixl32::Condition(kNone);
3323 switch (type) {
3324 case Primitive::kPrimBoolean:
3325 case Primitive::kPrimByte:
3326 case Primitive::kPrimShort:
3327 case Primitive::kPrimChar:
3328 case Primitive::kPrimInt: {
3329 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
3330 __ Mov(out, 0);
3331 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
3332 less_cond = lt;
3333 break;
3334 }
3335 case Primitive::kPrimLong: {
3336 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
3337 __ B(lt, &less);
3338 __ B(gt, &greater);
3339 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
3340 __ Mov(out, 0);
3341 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
3342 less_cond = lo;
3343 break;
3344 }
3345 case Primitive::kPrimFloat:
3346 case Primitive::kPrimDouble: {
3347 __ Mov(out, 0);
3348 GenerateVcmp(compare);
3349 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
3350 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
3351 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
3352 break;
3353 }
3354 default:
3355 LOG(FATAL) << "Unexpected compare type " << type;
3356 UNREACHABLE();
3357 }
3358
3359 __ B(eq, &done);
3360 __ B(less_cond, &less);
3361
3362 __ Bind(&greater);
3363 __ Mov(out, 1);
3364 __ B(&done);
3365
3366 __ Bind(&less);
3367 __ Mov(out, -1);
3368
3369 __ Bind(&done);
3370}
3371
3372void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
3373 LocationSummary* locations =
3374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3375 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
3376 locations->SetInAt(i, Location::Any());
3377 }
3378 locations->SetOut(Location::Any());
3379}
3380
3381void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3382 LOG(FATAL) << "Unreachable";
3383}
3384
3385void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
3386 // TODO (ported from quick): revisit ARM barrier kinds.
3387 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
3388 switch (kind) {
3389 case MemBarrierKind::kAnyStore:
3390 case MemBarrierKind::kLoadAny:
3391 case MemBarrierKind::kAnyAny: {
3392 flavor = DmbOptions::ISH;
3393 break;
3394 }
3395 case MemBarrierKind::kStoreStore: {
3396 flavor = DmbOptions::ISHST;
3397 break;
3398 }
3399 default:
3400 LOG(FATAL) << "Unexpected memory barrier " << kind;
3401 }
3402 __ Dmb(flavor);
3403}
3404
3405void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
3406 uint32_t offset,
3407 vixl32::Register out_lo,
3408 vixl32::Register out_hi) {
3409 UseScratchRegisterScope temps(GetVIXLAssembler());
3410 if (offset != 0) {
3411 vixl32::Register temp = temps.Acquire();
3412 __ Add(temp, addr, offset);
3413 addr = temp;
3414 }
3415 __ Ldrexd(out_lo, out_hi, addr);
3416}
3417
3418void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
3419 uint32_t offset,
3420 vixl32::Register value_lo,
3421 vixl32::Register value_hi,
3422 vixl32::Register temp1,
3423 vixl32::Register temp2,
3424 HInstruction* instruction) {
3425 UseScratchRegisterScope temps(GetVIXLAssembler());
3426 vixl32::Label fail;
3427 if (offset != 0) {
3428 vixl32::Register temp = temps.Acquire();
3429 __ Add(temp, addr, offset);
3430 addr = temp;
3431 }
3432 __ Bind(&fail);
3433 // We need a load followed by store. (The address used in a STREX instruction must
3434 // be the same as the address in the most recently executed LDREX instruction.)
3435 __ Ldrexd(temp1, temp2, addr);
3436 codegen_->MaybeRecordImplicitNullCheck(instruction);
3437 __ Strexd(temp1, value_lo, value_hi, addr);
3438 __ Cbnz(temp1, &fail);
3439}
Artem Serov02109dd2016-09-23 17:17:54 +01003440
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003441void LocationsBuilderARMVIXL::HandleFieldSet(
3442 HInstruction* instruction, const FieldInfo& field_info) {
3443 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3444
3445 LocationSummary* locations =
3446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3447 locations->SetInAt(0, Location::RequiresRegister());
3448
3449 Primitive::Type field_type = field_info.GetFieldType();
3450 if (Primitive::IsFloatingPointType(field_type)) {
3451 locations->SetInAt(1, Location::RequiresFpuRegister());
3452 } else {
3453 locations->SetInAt(1, Location::RequiresRegister());
3454 }
3455
3456 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
3457 bool generate_volatile = field_info.IsVolatile()
3458 && is_wide
3459 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3460 bool needs_write_barrier =
3461 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3462 // Temporary registers for the write barrier.
3463 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
3464 if (needs_write_barrier) {
3465 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
3466 locations->AddTemp(Location::RequiresRegister());
3467 } else if (generate_volatile) {
3468 // ARM encoding have some additional constraints for ldrexd/strexd:
3469 // - registers need to be consecutive
3470 // - the first register should be even but not R14.
3471 // We don't test for ARM yet, and the assertion makes sure that we
3472 // revisit this if we ever enable ARM encoding.
3473 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3474
3475 locations->AddTemp(Location::RequiresRegister());
3476 locations->AddTemp(Location::RequiresRegister());
3477 if (field_type == Primitive::kPrimDouble) {
3478 // For doubles we need two more registers to copy the value.
3479 locations->AddTemp(LocationFrom(r2));
3480 locations->AddTemp(LocationFrom(r3));
3481 }
3482 }
3483}
3484
3485void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
3486 const FieldInfo& field_info,
3487 bool value_can_be_null) {
3488 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3489
3490 LocationSummary* locations = instruction->GetLocations();
3491 vixl32::Register base = InputRegisterAt(instruction, 0);
3492 Location value = locations->InAt(1);
3493
3494 bool is_volatile = field_info.IsVolatile();
3495 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3496 Primitive::Type field_type = field_info.GetFieldType();
3497 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3498 bool needs_write_barrier =
3499 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3500
3501 if (is_volatile) {
3502 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3503 }
3504
3505 switch (field_type) {
3506 case Primitive::kPrimBoolean:
3507 case Primitive::kPrimByte: {
3508 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
3509 break;
3510 }
3511
3512 case Primitive::kPrimShort:
3513 case Primitive::kPrimChar: {
3514 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
3515 break;
3516 }
3517
3518 case Primitive::kPrimInt:
3519 case Primitive::kPrimNot: {
3520 if (kPoisonHeapReferences && needs_write_barrier) {
3521 // Note that in the case where `value` is a null reference,
3522 // we do not enter this block, as a null reference does not
3523 // need poisoning.
3524 DCHECK_EQ(field_type, Primitive::kPrimNot);
3525 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3526 __ Mov(temp, RegisterFrom(value));
3527 GetAssembler()->PoisonHeapReference(temp);
3528 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
3529 } else {
3530 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
3531 }
3532 break;
3533 }
3534
3535 case Primitive::kPrimLong: {
3536 if (is_volatile && !atomic_ldrd_strd) {
3537 GenerateWideAtomicStore(base,
3538 offset,
3539 LowRegisterFrom(value),
3540 HighRegisterFrom(value),
3541 RegisterFrom(locations->GetTemp(0)),
3542 RegisterFrom(locations->GetTemp(1)),
3543 instruction);
3544 } else {
3545 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
3546 codegen_->MaybeRecordImplicitNullCheck(instruction);
3547 }
3548 break;
3549 }
3550
3551 case Primitive::kPrimFloat: {
3552 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
3553 break;
3554 }
3555
3556 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01003557 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003558 if (is_volatile && !atomic_ldrd_strd) {
3559 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
3560 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
3561
3562 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
3563
3564 GenerateWideAtomicStore(base,
3565 offset,
3566 value_reg_lo,
3567 value_reg_hi,
3568 RegisterFrom(locations->GetTemp(2)),
3569 RegisterFrom(locations->GetTemp(3)),
3570 instruction);
3571 } else {
3572 GetAssembler()->StoreDToOffset(value_reg, base, offset);
3573 codegen_->MaybeRecordImplicitNullCheck(instruction);
3574 }
3575 break;
3576 }
3577
3578 case Primitive::kPrimVoid:
3579 LOG(FATAL) << "Unreachable type " << field_type;
3580 UNREACHABLE();
3581 }
3582
3583 // Longs and doubles are handled in the switch.
3584 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3585 codegen_->MaybeRecordImplicitNullCheck(instruction);
3586 }
3587
3588 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3589 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3590 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
3591 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
3592 }
3593
3594 if (is_volatile) {
3595 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3596 }
3597}
3598
Artem Serov02d37832016-10-25 15:25:33 +01003599void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
3600 const FieldInfo& field_info) {
3601 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3602
3603 bool object_field_get_with_read_barrier =
3604 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
3605 LocationSummary* locations =
3606 new (GetGraph()->GetArena()) LocationSummary(instruction,
3607 object_field_get_with_read_barrier ?
3608 LocationSummary::kCallOnSlowPath :
3609 LocationSummary::kNoCall);
3610 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3611 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3612 }
3613 locations->SetInAt(0, Location::RequiresRegister());
3614
3615 bool volatile_for_double = field_info.IsVolatile()
3616 && (field_info.GetFieldType() == Primitive::kPrimDouble)
3617 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3618 // The output overlaps in case of volatile long: we don't want the
3619 // code generated by GenerateWideAtomicLoad to overwrite the
3620 // object's location. Likewise, in the case of an object field get
3621 // with read barriers enabled, we do not want the load to overwrite
3622 // the object's location, as we need it to emit the read barrier.
3623 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3624 object_field_get_with_read_barrier;
3625
3626 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3627 locations->SetOut(Location::RequiresFpuRegister());
3628 } else {
3629 locations->SetOut(Location::RequiresRegister(),
3630 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3631 }
3632 if (volatile_for_double) {
3633 // ARM encoding have some additional constraints for ldrexd/strexd:
3634 // - registers need to be consecutive
3635 // - the first register should be even but not R14.
3636 // We don't test for ARM yet, and the assertion makes sure that we
3637 // revisit this if we ever enable ARM encoding.
3638 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3639 locations->AddTemp(Location::RequiresRegister());
3640 locations->AddTemp(Location::RequiresRegister());
3641 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3642 // We need a temporary register for the read barrier marking slow
3643 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3644 locations->AddTemp(Location::RequiresRegister());
3645 }
3646}
3647
3648Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
3649 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
3650 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
3651 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
3652 return Location::ConstantLocation(input->AsConstant());
3653 } else {
3654 return Location::RequiresFpuRegister();
3655 }
3656}
3657
Artem Serov02109dd2016-09-23 17:17:54 +01003658Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
3659 Opcode opcode) {
3660 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3661 if (constant->IsConstant() &&
3662 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3663 return Location::ConstantLocation(constant->AsConstant());
3664 }
3665 return Location::RequiresRegister();
3666}
3667
3668bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
3669 Opcode opcode) {
3670 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3671 if (Primitive::Is64BitType(input_cst->GetType())) {
3672 Opcode high_opcode = opcode;
3673 SetCc low_set_cc = kCcDontCare;
3674 switch (opcode) {
3675 case SUB:
3676 // Flip the operation to an ADD.
3677 value = -value;
3678 opcode = ADD;
3679 FALLTHROUGH_INTENDED;
3680 case ADD:
3681 if (Low32Bits(value) == 0u) {
3682 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
3683 }
3684 high_opcode = ADC;
3685 low_set_cc = kCcSet;
3686 break;
3687 default:
3688 break;
3689 }
3690 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
3691 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
3692 } else {
3693 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3694 }
3695}
3696
3697// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
3698// enabled.
3699bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
3700 Opcode opcode,
3701 SetCc set_cc) {
3702 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
3703 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
3704 return true;
3705 }
3706 Opcode neg_opcode = kNoOperand;
3707 switch (opcode) {
3708 case AND: neg_opcode = BIC; value = ~value; break;
3709 case ORR: neg_opcode = ORN; value = ~value; break;
3710 case ADD: neg_opcode = SUB; value = -value; break;
3711 case ADC: neg_opcode = SBC; value = ~value; break;
3712 case SUB: neg_opcode = ADD; value = -value; break;
3713 case SBC: neg_opcode = ADC; value = ~value; break;
3714 default:
3715 return false;
3716 }
3717 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
3718}
3719
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003720void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
3721 const FieldInfo& field_info) {
3722 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3723
3724 LocationSummary* locations = instruction->GetLocations();
3725 vixl32::Register base = InputRegisterAt(instruction, 0);
3726 Location out = locations->Out();
3727 bool is_volatile = field_info.IsVolatile();
3728 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3729 Primitive::Type field_type = field_info.GetFieldType();
3730 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3731
3732 switch (field_type) {
3733 case Primitive::kPrimBoolean:
3734 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
3735 break;
3736
3737 case Primitive::kPrimByte:
3738 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
3739 break;
3740
3741 case Primitive::kPrimShort:
3742 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
3743 break;
3744
3745 case Primitive::kPrimChar:
3746 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
3747 break;
3748
3749 case Primitive::kPrimInt:
3750 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3751 break;
3752
3753 case Primitive::kPrimNot: {
3754 // /* HeapReference<Object> */ out = *(base + offset)
3755 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3756 TODO_VIXL32(FATAL);
3757 } else {
3758 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3759 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3760 codegen_->MaybeRecordImplicitNullCheck(instruction);
3761 if (is_volatile) {
3762 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3763 }
3764 // If read barriers are enabled, emit read barriers other than
3765 // Baker's using a slow path (and also unpoison the loaded
3766 // reference, if heap poisoning is enabled).
3767 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
3768 }
3769 break;
3770 }
3771
3772 case Primitive::kPrimLong:
3773 if (is_volatile && !atomic_ldrd_strd) {
3774 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
3775 } else {
3776 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
3777 }
3778 break;
3779
3780 case Primitive::kPrimFloat:
3781 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
3782 break;
3783
3784 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01003785 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003786 if (is_volatile && !atomic_ldrd_strd) {
3787 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
3788 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
3789 GenerateWideAtomicLoad(base, offset, lo, hi);
3790 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
3791 // scope.
3792 codegen_->MaybeRecordImplicitNullCheck(instruction);
3793 __ Vmov(out_dreg, lo, hi);
3794 } else {
3795 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
3796 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3797 codegen_->MaybeRecordImplicitNullCheck(instruction);
3798 }
3799 break;
3800 }
3801
3802 case Primitive::kPrimVoid:
3803 LOG(FATAL) << "Unreachable type " << field_type;
3804 UNREACHABLE();
3805 }
3806
3807 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
3808 // Potential implicit null checks, in the case of reference or
3809 // double fields, are handled in the previous switch statement.
3810 } else {
3811 // Address cases other than reference and double that may require an implicit null check.
3812 codegen_->MaybeRecordImplicitNullCheck(instruction);
3813 }
3814
3815 if (is_volatile) {
3816 if (field_type == Primitive::kPrimNot) {
3817 // Memory barriers, in the case of references, are also handled
3818 // in the previous switch statement.
3819 } else {
3820 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3821 }
3822 }
3823}
3824
3825void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3826 HandleFieldSet(instruction, instruction->GetFieldInfo());
3827}
3828
3829void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3830 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3831}
3832
3833void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3834 HandleFieldGet(instruction, instruction->GetFieldInfo());
3835}
3836
3837void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3838 HandleFieldGet(instruction, instruction->GetFieldInfo());
3839}
3840
3841void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3842 HandleFieldGet(instruction, instruction->GetFieldInfo());
3843}
3844
3845void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3846 HandleFieldGet(instruction, instruction->GetFieldInfo());
3847}
3848
Scott Wakelingc34dba72016-10-03 10:14:44 +01003849void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3850 HandleFieldSet(instruction, instruction->GetFieldInfo());
3851}
3852
3853void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3854 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3855}
3856
Artem Serovcfbe9132016-10-14 15:58:56 +01003857void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
3858 HUnresolvedInstanceFieldGet* instruction) {
3859 FieldAccessCallingConventionARMVIXL calling_convention;
3860 codegen_->CreateUnresolvedFieldLocationSummary(
3861 instruction, instruction->GetFieldType(), calling_convention);
3862}
3863
3864void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
3865 HUnresolvedInstanceFieldGet* instruction) {
3866 FieldAccessCallingConventionARMVIXL calling_convention;
3867 codegen_->GenerateUnresolvedFieldAccess(instruction,
3868 instruction->GetFieldType(),
3869 instruction->GetFieldIndex(),
3870 instruction->GetDexPc(),
3871 calling_convention);
3872}
3873
3874void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
3875 HUnresolvedInstanceFieldSet* instruction) {
3876 FieldAccessCallingConventionARMVIXL calling_convention;
3877 codegen_->CreateUnresolvedFieldLocationSummary(
3878 instruction, instruction->GetFieldType(), calling_convention);
3879}
3880
3881void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
3882 HUnresolvedInstanceFieldSet* instruction) {
3883 FieldAccessCallingConventionARMVIXL calling_convention;
3884 codegen_->GenerateUnresolvedFieldAccess(instruction,
3885 instruction->GetFieldType(),
3886 instruction->GetFieldIndex(),
3887 instruction->GetDexPc(),
3888 calling_convention);
3889}
3890
3891void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
3892 HUnresolvedStaticFieldGet* instruction) {
3893 FieldAccessCallingConventionARMVIXL calling_convention;
3894 codegen_->CreateUnresolvedFieldLocationSummary(
3895 instruction, instruction->GetFieldType(), calling_convention);
3896}
3897
3898void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
3899 HUnresolvedStaticFieldGet* instruction) {
3900 FieldAccessCallingConventionARMVIXL calling_convention;
3901 codegen_->GenerateUnresolvedFieldAccess(instruction,
3902 instruction->GetFieldType(),
3903 instruction->GetFieldIndex(),
3904 instruction->GetDexPc(),
3905 calling_convention);
3906}
3907
3908void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
3909 HUnresolvedStaticFieldSet* instruction) {
3910 FieldAccessCallingConventionARMVIXL calling_convention;
3911 codegen_->CreateUnresolvedFieldLocationSummary(
3912 instruction, instruction->GetFieldType(), calling_convention);
3913}
3914
3915void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
3916 HUnresolvedStaticFieldSet* instruction) {
3917 FieldAccessCallingConventionARMVIXL calling_convention;
3918 codegen_->GenerateUnresolvedFieldAccess(instruction,
3919 instruction->GetFieldType(),
3920 instruction->GetFieldIndex(),
3921 instruction->GetDexPc(),
3922 calling_convention);
3923}
3924
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003925void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3926 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
3927 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3928 ? LocationSummary::kCallOnSlowPath
3929 : LocationSummary::kNoCall;
3930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3931 locations->SetInAt(0, Location::RequiresRegister());
3932 if (instruction->HasUses()) {
3933 locations->SetOut(Location::SameAsFirstInput());
3934 }
3935}
3936
3937void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
3938 if (CanMoveNullCheckToUser(instruction)) {
3939 return;
3940 }
3941
3942 UseScratchRegisterScope temps(GetVIXLAssembler());
3943 AssemblerAccurateScope aas(GetVIXLAssembler(),
3944 kArmInstrMaxSizeInBytes,
3945 CodeBufferCheckScope::kMaximumSize);
3946 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
3947 RecordPcInfo(instruction, instruction->GetDexPc());
3948}
3949
3950void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
3951 NullCheckSlowPathARMVIXL* slow_path =
3952 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
3953 AddSlowPath(slow_path);
3954 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3955}
3956
3957void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3958 codegen_->GenerateNullCheck(instruction);
3959}
3960
Scott Wakelingc34dba72016-10-03 10:14:44 +01003961static LoadOperandType GetLoadOperandType(Primitive::Type type) {
3962 switch (type) {
3963 case Primitive::kPrimNot:
3964 return kLoadWord;
3965 case Primitive::kPrimBoolean:
3966 return kLoadUnsignedByte;
3967 case Primitive::kPrimByte:
3968 return kLoadSignedByte;
3969 case Primitive::kPrimChar:
3970 return kLoadUnsignedHalfword;
3971 case Primitive::kPrimShort:
3972 return kLoadSignedHalfword;
3973 case Primitive::kPrimInt:
3974 return kLoadWord;
3975 case Primitive::kPrimLong:
3976 return kLoadWordPair;
3977 case Primitive::kPrimFloat:
3978 return kLoadSWord;
3979 case Primitive::kPrimDouble:
3980 return kLoadDWord;
3981 default:
3982 LOG(FATAL) << "Unreachable type " << type;
3983 UNREACHABLE();
3984 }
3985}
3986
3987static StoreOperandType GetStoreOperandType(Primitive::Type type) {
3988 switch (type) {
3989 case Primitive::kPrimNot:
3990 return kStoreWord;
3991 case Primitive::kPrimBoolean:
3992 case Primitive::kPrimByte:
3993 return kStoreByte;
3994 case Primitive::kPrimChar:
3995 case Primitive::kPrimShort:
3996 return kStoreHalfword;
3997 case Primitive::kPrimInt:
3998 return kStoreWord;
3999 case Primitive::kPrimLong:
4000 return kStoreWordPair;
4001 case Primitive::kPrimFloat:
4002 return kStoreSWord;
4003 case Primitive::kPrimDouble:
4004 return kStoreDWord;
4005 default:
4006 LOG(FATAL) << "Unreachable type " << type;
4007 UNREACHABLE();
4008 }
4009}
4010
4011void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
4012 Location out_loc,
4013 vixl32::Register base,
4014 vixl32::Register reg_index,
4015 vixl32::Condition cond) {
4016 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4017 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4018
4019 switch (type) {
4020 case Primitive::kPrimByte:
4021 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
4022 break;
4023 case Primitive::kPrimBoolean:
4024 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
4025 break;
4026 case Primitive::kPrimShort:
4027 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
4028 break;
4029 case Primitive::kPrimChar:
4030 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
4031 break;
4032 case Primitive::kPrimNot:
4033 case Primitive::kPrimInt:
4034 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
4035 break;
4036 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4037 case Primitive::kPrimLong:
4038 case Primitive::kPrimFloat:
4039 case Primitive::kPrimDouble:
4040 default:
4041 LOG(FATAL) << "Unreachable type " << type;
4042 UNREACHABLE();
4043 }
4044}
4045
4046void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
4047 Location loc,
4048 vixl32::Register base,
4049 vixl32::Register reg_index,
4050 vixl32::Condition cond) {
4051 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4052 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4053
4054 switch (type) {
4055 case Primitive::kPrimByte:
4056 case Primitive::kPrimBoolean:
4057 __ Strb(cond, RegisterFrom(loc), mem_address);
4058 break;
4059 case Primitive::kPrimShort:
4060 case Primitive::kPrimChar:
4061 __ Strh(cond, RegisterFrom(loc), mem_address);
4062 break;
4063 case Primitive::kPrimNot:
4064 case Primitive::kPrimInt:
4065 __ Str(cond, RegisterFrom(loc), mem_address);
4066 break;
4067 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4068 case Primitive::kPrimLong:
4069 case Primitive::kPrimFloat:
4070 case Primitive::kPrimDouble:
4071 default:
4072 LOG(FATAL) << "Unreachable type " << type;
4073 UNREACHABLE();
4074 }
4075}
4076
4077void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4078 bool object_array_get_with_read_barrier =
4079 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4080 LocationSummary* locations =
4081 new (GetGraph()->GetArena()) LocationSummary(instruction,
4082 object_array_get_with_read_barrier ?
4083 LocationSummary::kCallOnSlowPath :
4084 LocationSummary::kNoCall);
4085 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4086 TODO_VIXL32(FATAL);
4087 }
4088 locations->SetInAt(0, Location::RequiresRegister());
4089 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4090 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4091 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4092 } else {
4093 // The output overlaps in the case of an object array get with
4094 // read barriers enabled: we do not want the move to overwrite the
4095 // array's location, as we need it to emit the read barrier.
4096 locations->SetOut(
4097 Location::RequiresRegister(),
4098 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4099 }
4100 // We need a temporary register for the read barrier marking slow
4101 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4102 // Also need for String compression feature.
4103 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4104 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004105 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004106 }
4107}
4108
4109void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4110 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4111 LocationSummary* locations = instruction->GetLocations();
4112 Location obj_loc = locations->InAt(0);
4113 vixl32::Register obj = InputRegisterAt(instruction, 0);
4114 Location index = locations->InAt(1);
4115 Location out_loc = locations->Out();
4116 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4117 Primitive::Type type = instruction->GetType();
4118 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4119 instruction->IsStringCharAt();
4120 HInstruction* array_instr = instruction->GetArray();
4121 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4122 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4123 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4124
4125 switch (type) {
4126 case Primitive::kPrimBoolean:
4127 case Primitive::kPrimByte:
4128 case Primitive::kPrimShort:
4129 case Primitive::kPrimChar:
4130 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004131 vixl32::Register length;
4132 if (maybe_compressed_char_at) {
4133 length = RegisterFrom(locations->GetTemp(0));
4134 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4135 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
4136 codegen_->MaybeRecordImplicitNullCheck(instruction);
4137 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01004138 if (index.IsConstant()) {
4139 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4140 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004141 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004142 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4143 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4144 "Expecting 0=compressed, 1=uncompressed");
4145 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004146 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
4147 RegisterFrom(out_loc),
4148 obj,
4149 data_offset + const_index);
4150 __ B(&done);
4151 __ Bind(&uncompressed_load);
4152 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4153 RegisterFrom(out_loc),
4154 obj,
4155 data_offset + (const_index << 1));
4156 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004157 } else {
4158 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4159
4160 LoadOperandType load_type = GetLoadOperandType(type);
4161 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
4162 }
4163 } else {
4164 vixl32::Register temp = temps.Acquire();
4165
4166 if (has_intermediate_address) {
4167 TODO_VIXL32(FATAL);
4168 } else {
4169 __ Add(temp, obj, data_offset);
4170 }
4171 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004172 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004173 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4174 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4175 "Expecting 0=compressed, 1=uncompressed");
4176 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004177 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
4178 __ B(&done);
4179 __ Bind(&uncompressed_load);
4180 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
4181 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004182 } else {
4183 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4184 }
4185 }
4186 break;
4187 }
4188
4189 case Primitive::kPrimNot: {
4190 static_assert(
4191 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4192 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4193 // /* HeapReference<Object> */ out =
4194 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4195 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4196 TODO_VIXL32(FATAL);
4197 } else {
4198 vixl32::Register out = OutputRegister(instruction);
4199 if (index.IsConstant()) {
4200 size_t offset =
4201 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4202 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
4203 codegen_->MaybeRecordImplicitNullCheck(instruction);
4204 // If read barriers are enabled, emit read barriers other than
4205 // Baker's using a slow path (and also unpoison the loaded
4206 // reference, if heap poisoning is enabled).
4207 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4208 } else {
4209 vixl32::Register temp = temps.Acquire();
4210
4211 if (has_intermediate_address) {
4212 TODO_VIXL32(FATAL);
4213 } else {
4214 __ Add(temp, obj, data_offset);
4215 }
4216 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4217
4218 codegen_->MaybeRecordImplicitNullCheck(instruction);
4219 // If read barriers are enabled, emit read barriers other than
4220 // Baker's using a slow path (and also unpoison the loaded
4221 // reference, if heap poisoning is enabled).
4222 codegen_->MaybeGenerateReadBarrierSlow(
4223 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4224 }
4225 }
4226 break;
4227 }
4228
4229 case Primitive::kPrimLong: {
4230 if (index.IsConstant()) {
4231 size_t offset =
4232 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4233 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
4234 } else {
4235 vixl32::Register temp = temps.Acquire();
4236 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4237 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
4238 }
4239 break;
4240 }
4241
4242 case Primitive::kPrimFloat: {
4243 vixl32::SRegister out = SRegisterFrom(out_loc);
4244 if (index.IsConstant()) {
4245 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4246 GetAssembler()->LoadSFromOffset(out, obj, offset);
4247 } else {
4248 vixl32::Register temp = temps.Acquire();
4249 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
4250 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
4251 }
4252 break;
4253 }
4254
4255 case Primitive::kPrimDouble: {
4256 if (index.IsConstant()) {
4257 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4258 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
4259 } else {
4260 vixl32::Register temp = temps.Acquire();
4261 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4262 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
4263 }
4264 break;
4265 }
4266
4267 case Primitive::kPrimVoid:
4268 LOG(FATAL) << "Unreachable type " << type;
4269 UNREACHABLE();
4270 }
4271
4272 if (type == Primitive::kPrimNot) {
4273 // Potential implicit null checks, in the case of reference
4274 // arrays, are handled in the previous switch statement.
4275 } else if (!maybe_compressed_char_at) {
4276 codegen_->MaybeRecordImplicitNullCheck(instruction);
4277 }
4278}
4279
4280void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
4281 Primitive::Type value_type = instruction->GetComponentType();
4282
4283 bool needs_write_barrier =
4284 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4285 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4286
4287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4288 instruction,
4289 may_need_runtime_call_for_type_check ?
4290 LocationSummary::kCallOnSlowPath :
4291 LocationSummary::kNoCall);
4292
4293 locations->SetInAt(0, Location::RequiresRegister());
4294 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4295 if (Primitive::IsFloatingPointType(value_type)) {
4296 locations->SetInAt(2, Location::RequiresFpuRegister());
4297 } else {
4298 locations->SetInAt(2, Location::RequiresRegister());
4299 }
4300 if (needs_write_barrier) {
4301 // Temporary registers for the write barrier.
4302 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4303 locations->AddTemp(Location::RequiresRegister());
4304 }
4305}
4306
4307void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
4308 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4309 LocationSummary* locations = instruction->GetLocations();
4310 vixl32::Register array = InputRegisterAt(instruction, 0);
4311 Location index = locations->InAt(1);
4312 Primitive::Type value_type = instruction->GetComponentType();
4313 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4314 bool needs_write_barrier =
4315 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4316 uint32_t data_offset =
4317 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4318 Location value_loc = locations->InAt(2);
4319 HInstruction* array_instr = instruction->GetArray();
4320 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4321 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4322 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4323
4324 switch (value_type) {
4325 case Primitive::kPrimBoolean:
4326 case Primitive::kPrimByte:
4327 case Primitive::kPrimShort:
4328 case Primitive::kPrimChar:
4329 case Primitive::kPrimInt: {
4330 if (index.IsConstant()) {
4331 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4332 uint32_t full_offset =
4333 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4334 StoreOperandType store_type = GetStoreOperandType(value_type);
4335 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
4336 } else {
4337 vixl32::Register temp = temps.Acquire();
4338
4339 if (has_intermediate_address) {
4340 TODO_VIXL32(FATAL);
4341 } else {
4342 __ Add(temp, array, data_offset);
4343 }
4344 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
4345 }
4346 break;
4347 }
4348
4349 case Primitive::kPrimNot: {
4350 vixl32::Register value = RegisterFrom(value_loc);
4351 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4352 // See the comment in instruction_simplifier_shared.cc.
4353 DCHECK(!has_intermediate_address);
4354
4355 if (instruction->InputAt(2)->IsNullConstant()) {
4356 // Just setting null.
4357 if (index.IsConstant()) {
4358 size_t offset =
4359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4360 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
4361 } else {
4362 DCHECK(index.IsRegister()) << index;
4363 vixl32::Register temp = temps.Acquire();
4364 __ Add(temp, array, data_offset);
4365 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
4366 }
4367 codegen_->MaybeRecordImplicitNullCheck(instruction);
4368 DCHECK(!needs_write_barrier);
4369 DCHECK(!may_need_runtime_call_for_type_check);
4370 break;
4371 }
4372
4373 DCHECK(needs_write_barrier);
4374 Location temp1_loc = locations->GetTemp(0);
4375 vixl32::Register temp1 = RegisterFrom(temp1_loc);
4376 Location temp2_loc = locations->GetTemp(1);
4377 vixl32::Register temp2 = RegisterFrom(temp2_loc);
4378 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4379 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4380 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4381 vixl32::Label done;
4382 SlowPathCodeARMVIXL* slow_path = nullptr;
4383
4384 if (may_need_runtime_call_for_type_check) {
4385 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
4386 codegen_->AddSlowPath(slow_path);
4387 if (instruction->GetValueCanBeNull()) {
4388 vixl32::Label non_zero;
4389 __ Cbnz(value, &non_zero);
4390 if (index.IsConstant()) {
4391 size_t offset =
4392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4393 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
4394 } else {
4395 DCHECK(index.IsRegister()) << index;
4396 vixl32::Register temp = temps.Acquire();
4397 __ Add(temp, array, data_offset);
4398 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
4399 }
4400 codegen_->MaybeRecordImplicitNullCheck(instruction);
4401 __ B(&done);
4402 __ Bind(&non_zero);
4403 }
4404
4405 // Note that when read barriers are enabled, the type checks
4406 // are performed without read barriers. This is fine, even in
4407 // the case where a class object is in the from-space after
4408 // the flip, as a comparison involving such a type would not
4409 // produce a false positive; it may of course produce a false
4410 // negative, in which case we would take the ArraySet slow
4411 // path.
4412
4413 // /* HeapReference<Class> */ temp1 = array->klass_
4414 GetAssembler()->LoadFromOffset(kLoadWord, temp1, array, class_offset);
4415 codegen_->MaybeRecordImplicitNullCheck(instruction);
4416 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
4417
4418 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4419 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4420 // /* HeapReference<Class> */ temp2 = value->klass_
4421 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
4422 // If heap poisoning is enabled, no need to unpoison `temp1`
4423 // nor `temp2`, as we are comparing two poisoned references.
4424 __ Cmp(temp1, temp2);
4425
4426 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4427 vixl32::Label do_put;
4428 __ B(eq, &do_put);
4429 // If heap poisoning is enabled, the `temp1` reference has
4430 // not been unpoisoned yet; unpoison it now.
4431 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
4432
4433 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4434 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4435 // If heap poisoning is enabled, no need to unpoison
4436 // `temp1`, as we are comparing against null below.
4437 __ Cbnz(temp1, slow_path->GetEntryLabel());
4438 __ Bind(&do_put);
4439 } else {
4440 __ B(ne, slow_path->GetEntryLabel());
4441 }
4442 }
4443
4444 vixl32::Register source = value;
4445 if (kPoisonHeapReferences) {
4446 // Note that in the case where `value` is a null reference,
4447 // we do not enter this block, as a null reference does not
4448 // need poisoning.
4449 DCHECK_EQ(value_type, Primitive::kPrimNot);
4450 __ Mov(temp1, value);
4451 GetAssembler()->PoisonHeapReference(temp1);
4452 source = temp1;
4453 }
4454
4455 if (index.IsConstant()) {
4456 size_t offset =
4457 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4458 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
4459 } else {
4460 DCHECK(index.IsRegister()) << index;
4461
4462 vixl32::Register temp = temps.Acquire();
4463 __ Add(temp, array, data_offset);
4464 codegen_->StoreToShiftedRegOffset(value_type,
4465 LocationFrom(source),
4466 temp,
4467 RegisterFrom(index));
4468 }
4469
4470 if (!may_need_runtime_call_for_type_check) {
4471 codegen_->MaybeRecordImplicitNullCheck(instruction);
4472 }
4473
4474 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4475
4476 if (done.IsReferenced()) {
4477 __ Bind(&done);
4478 }
4479
4480 if (slow_path != nullptr) {
4481 __ Bind(slow_path->GetExitLabel());
4482 }
4483
4484 break;
4485 }
4486
4487 case Primitive::kPrimLong: {
4488 Location value = locations->InAt(2);
4489 if (index.IsConstant()) {
4490 size_t offset =
4491 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4492 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
4493 } else {
4494 vixl32::Register temp = temps.Acquire();
4495 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4496 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
4497 }
4498 break;
4499 }
4500
4501 case Primitive::kPrimFloat: {
4502 Location value = locations->InAt(2);
4503 DCHECK(value.IsFpuRegister());
4504 if (index.IsConstant()) {
4505 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4506 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
4507 } else {
4508 vixl32::Register temp = temps.Acquire();
4509 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
4510 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
4511 }
4512 break;
4513 }
4514
4515 case Primitive::kPrimDouble: {
4516 Location value = locations->InAt(2);
4517 DCHECK(value.IsFpuRegisterPair());
4518 if (index.IsConstant()) {
4519 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4520 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
4521 } else {
4522 vixl32::Register temp = temps.Acquire();
4523 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
4524 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
4525 }
4526 break;
4527 }
4528
4529 case Primitive::kPrimVoid:
4530 LOG(FATAL) << "Unreachable type " << value_type;
4531 UNREACHABLE();
4532 }
4533
4534 // Objects are handled in the switch.
4535 if (value_type != Primitive::kPrimNot) {
4536 codegen_->MaybeRecordImplicitNullCheck(instruction);
4537 }
4538}
4539
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004540void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
4541 LocationSummary* locations =
4542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4543 locations->SetInAt(0, Location::RequiresRegister());
4544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4545}
4546
4547void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
4548 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
4549 vixl32::Register obj = InputRegisterAt(instruction, 0);
4550 vixl32::Register out = OutputRegister(instruction);
4551 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
4552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004553 // Mask out compression flag from String's array length.
4554 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004555 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004556 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004557}
4558
Scott Wakelingc34dba72016-10-03 10:14:44 +01004559void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
4560 RegisterSet caller_saves = RegisterSet::Empty();
4561 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4562 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
4563 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
4564 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
4565 locations->SetInAt(0, Location::RequiresRegister());
4566 locations->SetInAt(1, Location::RequiresRegister());
4567}
4568
4569void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
4570 SlowPathCodeARMVIXL* slow_path =
4571 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
4572 codegen_->AddSlowPath(slow_path);
4573
4574 vixl32::Register index = InputRegisterAt(instruction, 0);
4575 vixl32::Register length = InputRegisterAt(instruction, 1);
4576
4577 __ Cmp(index, length);
4578 __ B(hs, slow_path->GetEntryLabel());
4579}
4580
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004581void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
4582 vixl32::Register card,
4583 vixl32::Register object,
4584 vixl32::Register value,
4585 bool can_be_null) {
4586 vixl32::Label is_null;
4587 if (can_be_null) {
4588 __ Cbz(value, &is_null);
4589 }
4590 GetAssembler()->LoadFromOffset(
4591 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
4592 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4593 __ Strb(card, MemOperand(card, temp));
4594 if (can_be_null) {
4595 __ Bind(&is_null);
4596 }
4597}
4598
Scott Wakelingfe885462016-09-22 10:24:38 +01004599void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4600 LOG(FATAL) << "Unreachable";
4601}
4602
4603void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
4604 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4605}
4606
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004607void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
4608 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4609 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/ and related.
4610}
4611
4612void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
4613 HBasicBlock* block = instruction->GetBlock();
4614 if (block->GetLoopInformation() != nullptr) {
4615 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4616 // The back edge will generate the suspend check.
4617 return;
4618 }
4619 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4620 // The goto will generate the suspend check.
4621 return;
4622 }
4623 GenerateSuspendCheck(instruction, nullptr);
4624}
4625
4626void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
4627 HBasicBlock* successor) {
4628 SuspendCheckSlowPathARMVIXL* slow_path =
4629 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
4630 if (slow_path == nullptr) {
4631 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
4632 instruction->SetSlowPath(slow_path);
4633 codegen_->AddSlowPath(slow_path);
4634 if (successor != nullptr) {
4635 DCHECK(successor->IsLoopHeader());
4636 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4637 }
4638 } else {
4639 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4640 }
4641
4642 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4643 vixl32::Register temp = temps.Acquire();
4644 GetAssembler()->LoadFromOffset(
4645 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
4646 if (successor == nullptr) {
4647 __ Cbnz(temp, slow_path->GetEntryLabel());
4648 __ Bind(slow_path->GetReturnLabel());
4649 } else {
4650 __ Cbz(temp, codegen_->GetLabelOf(successor));
4651 __ B(slow_path->GetEntryLabel());
4652 }
4653}
4654
Scott Wakelingfe885462016-09-22 10:24:38 +01004655ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
4656 return codegen_->GetAssembler();
4657}
4658
4659void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004660 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01004661 MoveOperands* move = moves_[index];
4662 Location source = move->GetSource();
4663 Location destination = move->GetDestination();
4664
4665 if (source.IsRegister()) {
4666 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004667 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004668 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004669 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004670 } else {
4671 DCHECK(destination.IsStackSlot());
4672 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004673 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01004674 sp,
4675 destination.GetStackIndex());
4676 }
4677 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004678 if (destination.IsRegister()) {
4679 GetAssembler()->LoadFromOffset(kLoadWord,
4680 RegisterFrom(destination),
4681 sp,
4682 source.GetStackIndex());
4683 } else if (destination.IsFpuRegister()) {
4684 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
4685 } else {
4686 DCHECK(destination.IsStackSlot());
4687 vixl32::Register temp = temps.Acquire();
4688 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
4689 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4690 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004691 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004692 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004693 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004694 } else if (destination.IsFpuRegister()) {
4695 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
4696 } else {
4697 DCHECK(destination.IsStackSlot());
4698 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
4699 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004700 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004701 if (destination.IsDoubleStackSlot()) {
4702 vixl32::DRegister temp = temps.AcquireD();
4703 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
4704 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
4705 } else if (destination.IsRegisterPair()) {
4706 DCHECK(ExpectedPairLayout(destination));
4707 GetAssembler()->LoadFromOffset(
4708 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
4709 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004710 DCHECK(destination.IsFpuRegisterPair()) << destination;
4711 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004712 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004713 } else if (source.IsRegisterPair()) {
4714 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004715 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
4716 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004717 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004718 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01004719 } else {
4720 DCHECK(destination.IsDoubleStackSlot()) << destination;
4721 DCHECK(ExpectedPairLayout(source));
4722 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004723 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01004724 sp,
4725 destination.GetStackIndex());
4726 }
4727 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004728 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004729 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01004730 } else if (destination.IsFpuRegisterPair()) {
4731 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
4732 } else {
4733 DCHECK(destination.IsDoubleStackSlot()) << destination;
4734 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
4735 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004736 } else {
4737 DCHECK(source.IsConstant()) << source;
4738 HConstant* constant = source.GetConstant();
4739 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4740 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
4741 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004742 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004743 } else {
4744 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01004745 vixl32::Register temp = temps.Acquire();
4746 __ Mov(temp, value);
4747 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4748 }
4749 } else if (constant->IsLongConstant()) {
4750 int64_t value = constant->AsLongConstant()->GetValue();
4751 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004752 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
4753 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004754 } else {
4755 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01004756 vixl32::Register temp = temps.Acquire();
4757 __ Mov(temp, Low32Bits(value));
4758 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4759 __ Mov(temp, High32Bits(value));
4760 GetAssembler()->StoreToOffset(kStoreWord,
4761 temp,
4762 sp,
4763 destination.GetHighStackIndex(kArmWordSize));
4764 }
4765 } else if (constant->IsDoubleConstant()) {
4766 double value = constant->AsDoubleConstant()->GetValue();
4767 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004768 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004769 } else {
4770 DCHECK(destination.IsDoubleStackSlot()) << destination;
4771 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004772 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004773 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004774 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004775 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004776 GetAssembler()->StoreToOffset(kStoreWord,
4777 temp,
4778 sp,
4779 destination.GetHighStackIndex(kArmWordSize));
4780 }
4781 } else {
4782 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
4783 float value = constant->AsFloatConstant()->GetValue();
4784 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004785 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01004786 } else {
4787 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01004788 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004789 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004790 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
4791 }
4792 }
4793 }
4794}
4795
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004796void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
4797 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4798 vixl32::Register temp = temps.Acquire();
4799 __ Mov(temp, reg);
4800 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
4801 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01004802}
4803
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004804void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
4805 // TODO(VIXL32): Double check the performance of this implementation.
4806 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4807 vixl32::Register temp = temps.Acquire();
4808 vixl32::SRegister temp_s = temps.AcquireS();
4809
4810 __ Ldr(temp, MemOperand(sp, mem1));
4811 __ Vldr(temp_s, MemOperand(sp, mem2));
4812 __ Str(temp, MemOperand(sp, mem2));
4813 __ Vstr(temp_s, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004814}
4815
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004816void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
4817 MoveOperands* move = moves_[index];
4818 Location source = move->GetSource();
4819 Location destination = move->GetDestination();
4820 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
4821
4822 if (source.IsRegister() && destination.IsRegister()) {
4823 vixl32::Register temp = temps.Acquire();
4824 DCHECK(!RegisterFrom(source).Is(temp));
4825 DCHECK(!RegisterFrom(destination).Is(temp));
4826 __ Mov(temp, RegisterFrom(destination));
4827 __ Mov(RegisterFrom(destination), RegisterFrom(source));
4828 __ Mov(RegisterFrom(source), temp);
4829 } else if (source.IsRegister() && destination.IsStackSlot()) {
4830 Exchange(RegisterFrom(source), destination.GetStackIndex());
4831 } else if (source.IsStackSlot() && destination.IsRegister()) {
4832 Exchange(RegisterFrom(destination), source.GetStackIndex());
4833 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4834 TODO_VIXL32(FATAL);
4835 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4836 TODO_VIXL32(FATAL);
4837 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
4838 vixl32::DRegister temp = temps.AcquireD();
4839 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
4840 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
4841 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
4842 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
4843 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
4844 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
4845 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
4846 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
4847 vixl32::DRegister temp = temps.AcquireD();
4848 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
4849 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
4850 GetAssembler()->StoreDToOffset(temp, sp, mem);
4851 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004852 vixl32::DRegister first = DRegisterFrom(source);
4853 vixl32::DRegister second = DRegisterFrom(destination);
4854 vixl32::DRegister temp = temps.AcquireD();
4855 __ Vmov(temp, first);
4856 __ Vmov(first, second);
4857 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01004858 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4859 TODO_VIXL32(FATAL);
4860 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4861 TODO_VIXL32(FATAL);
4862 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4863 vixl32::DRegister temp1 = temps.AcquireD();
4864 vixl32::DRegister temp2 = temps.AcquireD();
4865 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
4866 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
4867 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
4868 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
4869 } else {
4870 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
4871 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004872}
4873
4874void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
4875 TODO_VIXL32(FATAL);
4876}
4877
4878void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
4879 TODO_VIXL32(FATAL);
4880}
4881
Artem Serov02d37832016-10-25 15:25:33 +01004882// Check if the desired_class_load_kind is supported. If it is, return it,
4883// otherwise return a fall-back kind that should be used instead.
4884HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
4885 HLoadClass::LoadKind desired_class_load_kind ATTRIBUTE_UNUSED) {
4886 // TODO(VIXL): Implement optimized code paths.
4887 return HLoadClass::LoadKind::kDexCacheViaMethod;
4888}
4889
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004890void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
4891 if (cls->NeedsAccessCheck()) {
4892 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4893 CodeGenerator::CreateLoadClassLocationSummary(
4894 cls,
4895 LocationFrom(calling_convention.GetRegisterAt(0)),
4896 LocationFrom(r0),
4897 /* code_generator_supports_read_barrier */ true);
4898 return;
4899 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004900
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004901 // TODO(VIXL): read barrier code.
4902 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4903 ? LocationSummary::kCallOnSlowPath
4904 : LocationSummary::kNoCall;
4905 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4906 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4907 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4908 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
4909 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
4910 locations->SetInAt(0, Location::RequiresRegister());
4911 }
4912 locations->SetOut(Location::RequiresRegister());
4913}
4914
4915void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
4916 LocationSummary* locations = cls->GetLocations();
4917 if (cls->NeedsAccessCheck()) {
4918 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4919 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
4920 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
4921 return;
4922 }
4923
4924 Location out_loc = locations->Out();
4925 vixl32::Register out = OutputRegister(cls);
4926
4927 // TODO(VIXL): read barrier code.
4928 bool generate_null_check = false;
4929 switch (cls->GetLoadKind()) {
4930 case HLoadClass::LoadKind::kReferrersClass: {
4931 DCHECK(!cls->CanCallRuntime());
4932 DCHECK(!cls->MustGenerateClinitCheck());
4933 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4934 vixl32::Register current_method = InputRegisterAt(cls, 0);
4935 GenerateGcRootFieldLoad(cls,
4936 out_loc,
4937 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01004938 ArtMethod::DeclaringClassOffset().Int32Value(),
4939 kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004940 break;
4941 }
4942 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4943 // /* GcRoot<mirror::Class>[] */ out =
4944 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4945 vixl32::Register current_method = InputRegisterAt(cls, 0);
4946 const int32_t resolved_types_offset =
4947 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value();
4948 GetAssembler()->LoadFromOffset(kLoadWord, out, current_method, resolved_types_offset);
4949 // /* GcRoot<mirror::Class> */ out = out[type_index]
4950 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Roland Levillain00468f32016-10-27 18:02:48 +01004951 GenerateGcRootFieldLoad(cls, out_loc, out, offset, kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004952 generate_null_check = !cls->IsInDexCache();
4953 break;
4954 }
4955 default:
4956 TODO_VIXL32(FATAL);
4957 }
4958
4959 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4960 DCHECK(cls->CanCallRuntime());
4961 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
4962 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4963 codegen_->AddSlowPath(slow_path);
4964 if (generate_null_check) {
4965 __ Cbz(out, slow_path->GetEntryLabel());
4966 }
4967 if (cls->MustGenerateClinitCheck()) {
4968 GenerateClassInitializationCheck(slow_path, out);
4969 } else {
4970 __ Bind(slow_path->GetExitLabel());
4971 }
4972 }
4973}
4974
Artem Serov02d37832016-10-25 15:25:33 +01004975void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
4976 LocationSummary* locations =
4977 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4978 locations->SetInAt(0, Location::RequiresRegister());
4979 if (check->HasUses()) {
4980 locations->SetOut(Location::SameAsFirstInput());
4981 }
4982}
4983
4984void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
4985 // We assume the class is not null.
4986 LoadClassSlowPathARMVIXL* slow_path =
4987 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
4988 check,
4989 check->GetDexPc(),
4990 /* do_clinit */ true);
4991 codegen_->AddSlowPath(slow_path);
4992 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
4993}
4994
4995void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
4996 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
4997 UseScratchRegisterScope temps(GetVIXLAssembler());
4998 vixl32::Register temp = temps.Acquire();
4999 GetAssembler()->LoadFromOffset(kLoadWord,
5000 temp,
5001 class_reg,
5002 mirror::Class::StatusOffset().Int32Value());
5003 __ Cmp(temp, mirror::Class::kStatusInitialized);
5004 __ B(lt, slow_path->GetEntryLabel());
5005 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5006 // properly. Therefore, we do a memory fence.
5007 __ Dmb(ISH);
5008 __ Bind(slow_path->GetExitLabel());
5009}
5010
5011// Check if the desired_string_load_kind is supported. If it is, return it,
5012// otherwise return a fall-back kind that should be used instead.
5013HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
5014 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
5015 // TODO(VIXL): Implement optimized code paths. For now we always use the simpler fallback code.
5016 return HLoadString::LoadKind::kDexCacheViaMethod;
5017}
5018
5019void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
5020 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5021 ? LocationSummary::kCallOnMainOnly
5022 : LocationSummary::kNoCall;
5023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
5024
5025 // TODO(VIXL): Implement optimized code paths.
5026 // See InstructionCodeGeneratorARMVIXL::VisitLoadString.
5027 HLoadString::LoadKind load_kind = load->GetLoadKind();
5028 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5029 locations->SetInAt(0, Location::RequiresRegister());
5030 // TODO(VIXL): Use InvokeRuntimeCallingConventionARMVIXL instead.
5031 locations->SetOut(LocationFrom(r0));
5032 } else {
5033 locations->SetOut(Location::RequiresRegister());
5034 }
5035}
5036
5037void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) {
5038 // TODO(VIXL): Implement optimized code paths.
5039 // We implemented the simplest solution to get first ART tests passing, we deferred the
5040 // optimized path until later, we should implement it using ARM64 implementation as a
5041 // reference. The same related to LocationsBuilderARMVIXL::VisitLoadString.
5042
5043 // TODO: Re-add the compiler code to do string dex cache lookup again.
5044 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
5045 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5046 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5047 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5048 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
5049}
5050
5051static int32_t GetExceptionTlsOffset() {
5052 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
5053}
5054
5055void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
5056 LocationSummary* locations =
5057 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5058 locations->SetOut(Location::RequiresRegister());
5059}
5060
5061void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
5062 vixl32::Register out = OutputRegister(load);
5063 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
5064}
5065
5066
5067void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
5068 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5069}
5070
5071void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5072 UseScratchRegisterScope temps(GetVIXLAssembler());
5073 vixl32::Register temp = temps.Acquire();
5074 __ Mov(temp, 0);
5075 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
5076}
5077
5078void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
5079 LocationSummary* locations =
5080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5081 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5082 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5083}
5084
5085void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
5086 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
5087 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5088}
5089
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005090static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5091 return kEmitCompilerReadBarrier &&
5092 (kUseBakerReadBarrier ||
5093 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5094 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5095 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5096}
5097
Artem Serovcfbe9132016-10-14 15:58:56 +01005098
5099void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
5100 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5101 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5102 bool baker_read_barrier_slow_path = false;
5103 switch (type_check_kind) {
5104 case TypeCheckKind::kExactCheck:
5105 case TypeCheckKind::kAbstractClassCheck:
5106 case TypeCheckKind::kClassHierarchyCheck:
5107 case TypeCheckKind::kArrayObjectCheck:
5108 call_kind =
5109 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5110 baker_read_barrier_slow_path = kUseBakerReadBarrier;
5111 break;
5112 case TypeCheckKind::kArrayCheck:
5113 case TypeCheckKind::kUnresolvedCheck:
5114 case TypeCheckKind::kInterfaceCheck:
5115 call_kind = LocationSummary::kCallOnSlowPath;
5116 break;
5117 }
5118
5119 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5120 if (baker_read_barrier_slow_path) {
5121 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5122 }
5123 locations->SetInAt(0, Location::RequiresRegister());
5124 locations->SetInAt(1, Location::RequiresRegister());
5125 // The "out" register is used as a temporary, so it overlaps with the inputs.
5126 // Note that TypeCheckSlowPathARM uses this register too.
5127 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5128 // When read barriers are enabled, we need a temporary register for
5129 // some cases.
5130 if (TypeCheckNeedsATemporary(type_check_kind)) {
5131 locations->AddTemp(Location::RequiresRegister());
5132 }
5133}
5134
5135void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
5136 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5137 LocationSummary* locations = instruction->GetLocations();
5138 Location obj_loc = locations->InAt(0);
5139 vixl32::Register obj = InputRegisterAt(instruction, 0);
5140 vixl32::Register cls = InputRegisterAt(instruction, 1);
5141 Location out_loc = locations->Out();
5142 vixl32::Register out = OutputRegister(instruction);
5143 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5144 locations->GetTemp(0) :
5145 Location::NoLocation();
5146 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5147 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5148 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5149 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5150 vixl32::Label done, zero;
5151 SlowPathCodeARMVIXL* slow_path = nullptr;
5152
5153 // Return 0 if `obj` is null.
5154 // avoid null check if we know obj is not null.
5155 if (instruction->MustDoNullCheck()) {
5156 __ Cbz(obj, &zero);
5157 }
5158
Artem Serovcfbe9132016-10-14 15:58:56 +01005159 switch (type_check_kind) {
5160 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08005161 // /* HeapReference<Class> */ out = obj->klass_
5162 GenerateReferenceLoadTwoRegisters(instruction,
5163 out_loc,
5164 obj_loc,
5165 class_offset,
5166 maybe_temp_loc);
Artem Serovcfbe9132016-10-14 15:58:56 +01005167 __ Cmp(out, cls);
5168 // Classes must be equal for the instanceof to succeed.
5169 __ B(ne, &zero);
5170 __ Mov(out, 1);
5171 __ B(&done);
5172 break;
5173 }
5174
5175 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08005176 // /* HeapReference<Class> */ out = obj->klass_
5177 GenerateReferenceLoadTwoRegisters(instruction,
5178 out_loc,
5179 obj_loc,
5180 class_offset,
5181 maybe_temp_loc);
Artem Serovcfbe9132016-10-14 15:58:56 +01005182 // If the class is abstract, we eagerly fetch the super class of the
5183 // object to avoid doing a comparison we know will fail.
5184 vixl32::Label loop;
5185 __ Bind(&loop);
5186 // /* HeapReference<Class> */ out = out->super_class_
5187 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
5188 // If `out` is null, we use it for the result, and jump to `done`.
5189 __ Cbz(out, &done);
5190 __ Cmp(out, cls);
5191 __ B(ne, &loop);
5192 __ Mov(out, 1);
5193 if (zero.IsReferenced()) {
5194 __ B(&done);
5195 }
5196 break;
5197 }
5198
5199 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08005200 // /* HeapReference<Class> */ out = obj->klass_
5201 GenerateReferenceLoadTwoRegisters(instruction,
5202 out_loc,
5203 obj_loc,
5204 class_offset,
5205 maybe_temp_loc);
Artem Serovcfbe9132016-10-14 15:58:56 +01005206 // Walk over the class hierarchy to find a match.
5207 vixl32::Label loop, success;
5208 __ Bind(&loop);
5209 __ Cmp(out, cls);
5210 __ B(eq, &success);
5211 // /* HeapReference<Class> */ out = out->super_class_
5212 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
5213 __ Cbnz(out, &loop);
5214 // If `out` is null, we use it for the result, and jump to `done`.
5215 __ B(&done);
5216 __ Bind(&success);
5217 __ Mov(out, 1);
5218 if (zero.IsReferenced()) {
5219 __ B(&done);
5220 }
5221 break;
5222 }
5223
5224 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08005225 // /* HeapReference<Class> */ out = obj->klass_
5226 GenerateReferenceLoadTwoRegisters(instruction,
5227 out_loc,
5228 obj_loc,
5229 class_offset,
5230 maybe_temp_loc);
Artem Serovcfbe9132016-10-14 15:58:56 +01005231 // Do an exact check.
5232 vixl32::Label exact_check;
5233 __ Cmp(out, cls);
5234 __ B(eq, &exact_check);
5235 // Otherwise, we need to check that the object's class is a non-primitive array.
5236 // /* HeapReference<Class> */ out = out->component_type_
5237 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
5238 // If `out` is null, we use it for the result, and jump to `done`.
5239 __ Cbz(out, &done);
5240 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5241 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5242 __ Cbnz(out, &zero);
5243 __ Bind(&exact_check);
5244 __ Mov(out, 1);
5245 __ B(&done);
5246 break;
5247 }
5248
5249 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08005250 // /* HeapReference<Class> */ out = obj->klass_
5251 GenerateReferenceLoadTwoRegisters(instruction,
5252 out_loc,
5253 obj_loc,
5254 class_offset,
5255 maybe_temp_loc);
Artem Serovcfbe9132016-10-14 15:58:56 +01005256 __ Cmp(out, cls);
5257 DCHECK(locations->OnlyCallsOnSlowPath());
5258 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
5259 /* is_fatal */ false);
5260 codegen_->AddSlowPath(slow_path);
5261 __ B(ne, slow_path->GetEntryLabel());
5262 __ Mov(out, 1);
5263 if (zero.IsReferenced()) {
5264 __ B(&done);
5265 }
5266 break;
5267 }
5268
5269 case TypeCheckKind::kUnresolvedCheck:
5270 case TypeCheckKind::kInterfaceCheck: {
5271 // Note that we indeed only call on slow path, but we always go
5272 // into the slow path for the unresolved and interface check
5273 // cases.
5274 //
5275 // We cannot directly call the InstanceofNonTrivial runtime
5276 // entry point without resorting to a type checking slow path
5277 // here (i.e. by calling InvokeRuntime directly), as it would
5278 // require to assign fixed registers for the inputs of this
5279 // HInstanceOf instruction (following the runtime calling
5280 // convention), which might be cluttered by the potential first
5281 // read barrier emission at the beginning of this method.
5282 //
5283 // TODO: Introduce a new runtime entry point taking the object
5284 // to test (instead of its class) as argument, and let it deal
5285 // with the read barrier issues. This will let us refactor this
5286 // case of the `switch` code as it was previously (with a direct
5287 // call to the runtime not using a type checking slow path).
5288 // This should also be beneficial for the other cases above.
5289 DCHECK(locations->OnlyCallsOnSlowPath());
5290 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
5291 /* is_fatal */ false);
5292 codegen_->AddSlowPath(slow_path);
5293 __ B(slow_path->GetEntryLabel());
5294 if (zero.IsReferenced()) {
5295 __ B(&done);
5296 }
5297 break;
5298 }
5299 }
5300
5301 if (zero.IsReferenced()) {
5302 __ Bind(&zero);
5303 __ Mov(out, 0);
5304 }
5305
5306 if (done.IsReferenced()) {
5307 __ Bind(&done);
5308 }
5309
5310 if (slow_path != nullptr) {
5311 __ Bind(slow_path->GetExitLabel());
5312 }
5313}
5314
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005315void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
5316 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5317 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5318
5319 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5320 switch (type_check_kind) {
5321 case TypeCheckKind::kExactCheck:
5322 case TypeCheckKind::kAbstractClassCheck:
5323 case TypeCheckKind::kClassHierarchyCheck:
5324 case TypeCheckKind::kArrayObjectCheck:
5325 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5326 LocationSummary::kCallOnSlowPath :
5327 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
5328 break;
5329 case TypeCheckKind::kArrayCheck:
5330 case TypeCheckKind::kUnresolvedCheck:
5331 case TypeCheckKind::kInterfaceCheck:
5332 call_kind = LocationSummary::kCallOnSlowPath;
5333 break;
5334 }
5335
5336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5337 locations->SetInAt(0, Location::RequiresRegister());
5338 locations->SetInAt(1, Location::RequiresRegister());
5339 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5340 locations->AddTemp(Location::RequiresRegister());
5341 // When read barriers are enabled, we need an additional temporary
5342 // register for some cases.
5343 if (TypeCheckNeedsATemporary(type_check_kind)) {
5344 locations->AddTemp(Location::RequiresRegister());
5345 }
5346}
5347
5348void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
5349 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5350 LocationSummary* locations = instruction->GetLocations();
5351 Location obj_loc = locations->InAt(0);
5352 vixl32::Register obj = InputRegisterAt(instruction, 0);
5353 vixl32::Register cls = InputRegisterAt(instruction, 1);
5354 Location temp_loc = locations->GetTemp(0);
5355 vixl32::Register temp = RegisterFrom(temp_loc);
5356 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5357 locations->GetTemp(1) :
5358 Location::NoLocation();
5359 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Artem Serovcfbe9132016-10-14 15:58:56 +01005360 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5361 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5362 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005363
5364 bool is_type_check_slow_path_fatal =
5365 (type_check_kind == TypeCheckKind::kExactCheck ||
5366 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5367 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5368 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5369 !instruction->CanThrowIntoCatchBlock();
5370 SlowPathCodeARMVIXL* type_check_slow_path =
5371 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
5372 is_type_check_slow_path_fatal);
5373 codegen_->AddSlowPath(type_check_slow_path);
5374
5375 vixl32::Label done;
5376 // Avoid null check if we know obj is not null.
5377 if (instruction->MustDoNullCheck()) {
5378 __ Cbz(obj, &done);
5379 }
5380
5381 // /* HeapReference<Class> */ temp = obj->klass_
5382 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5383
5384 switch (type_check_kind) {
5385 case TypeCheckKind::kExactCheck:
5386 case TypeCheckKind::kArrayCheck: {
5387 __ Cmp(temp, cls);
5388 // Jump to slow path for throwing the exception or doing a
5389 // more involved array check.
5390 __ B(ne, type_check_slow_path->GetEntryLabel());
5391 break;
5392 }
5393
5394 case TypeCheckKind::kAbstractClassCheck: {
Artem Serovcfbe9132016-10-14 15:58:56 +01005395 // If the class is abstract, we eagerly fetch the super class of the
5396 // object to avoid doing a comparison we know will fail.
5397 vixl32::Label loop;
5398 __ Bind(&loop);
5399 // /* HeapReference<Class> */ temp = temp->super_class_
5400 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
5401
5402 // If the class reference currently in `temp` is null, jump to the slow path to throw the
5403 // exception.
5404 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
5405
5406 // Otherwise, compare the classes.
5407 __ Cmp(temp, cls);
5408 __ B(ne, &loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005409 break;
5410 }
5411
5412 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serovcfbe9132016-10-14 15:58:56 +01005413 // Walk over the class hierarchy to find a match.
5414 vixl32::Label loop;
5415 __ Bind(&loop);
5416 __ Cmp(temp, cls);
5417 __ B(eq, &done);
5418
5419 // /* HeapReference<Class> */ temp = temp->super_class_
5420 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
5421
5422 // If the class reference currently in `temp` is null, jump to the slow path to throw the
5423 // exception.
5424 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
5425 // Otherwise, jump to the beginning of the loop.
5426 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005427 break;
5428 }
5429
Artem Serovcfbe9132016-10-14 15:58:56 +01005430 case TypeCheckKind::kArrayObjectCheck: {
5431 // Do an exact check.
5432 __ Cmp(temp, cls);
5433 __ B(eq, &done);
5434
5435 // Otherwise, we need to check that the object's class is a non-primitive array.
5436 // /* HeapReference<Class> */ temp = temp->component_type_
5437 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
5438 // If the component type is null, jump to the slow path to throw the exception.
5439 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
5440 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
5441 // to further check that this component type is not a primitive type.
5442 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
5443 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5444 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005445 break;
5446 }
5447
5448 case TypeCheckKind::kUnresolvedCheck:
5449 case TypeCheckKind::kInterfaceCheck:
Artem Serovcfbe9132016-10-14 15:58:56 +01005450 // We always go into the type check slow path for the unresolved
5451 // and interface check cases.
5452 //
5453 // We cannot directly call the CheckCast runtime entry point
5454 // without resorting to a type checking slow path here (i.e. by
5455 // calling InvokeRuntime directly), as it would require to
5456 // assign fixed registers for the inputs of this HInstanceOf
5457 // instruction (following the runtime calling convention), which
5458 // might be cluttered by the potential first read barrier
5459 // emission at the beginning of this method.
5460 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005461 break;
5462 }
5463 __ Bind(&done);
5464
5465 __ Bind(type_check_slow_path->GetExitLabel());
5466}
5467
Artem Serov551b28f2016-10-18 19:11:30 +01005468void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
5469 LocationSummary* locations =
5470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5471 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5472 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5473}
5474
5475void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
5476 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
5477 instruction,
5478 instruction->GetDexPc());
5479 if (instruction->IsEnter()) {
5480 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5481 } else {
5482 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5483 }
5484}
5485
Artem Serov02109dd2016-09-23 17:17:54 +01005486void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
5487 HandleBitwiseOperation(instruction, AND);
5488}
5489
5490void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
5491 HandleBitwiseOperation(instruction, ORR);
5492}
5493
5494void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
5495 HandleBitwiseOperation(instruction, EOR);
5496}
5497
5498void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
5499 LocationSummary* locations =
5500 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5501 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5502 || instruction->GetResultType() == Primitive::kPrimLong);
5503 // Note: GVN reorders commutative operations to have the constant on the right hand side.
5504 locations->SetInAt(0, Location::RequiresRegister());
5505 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
5506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5507}
5508
5509void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
5510 HandleBitwiseOperation(instruction);
5511}
5512
5513void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
5514 HandleBitwiseOperation(instruction);
5515}
5516
5517void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
5518 HandleBitwiseOperation(instruction);
5519}
5520
5521// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
5522void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
5523 vixl32::Register first,
5524 uint32_t value) {
5525 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5526 if (value == 0xffffffffu) {
5527 if (!out.Is(first)) {
5528 __ Mov(out, first);
5529 }
5530 return;
5531 }
5532 if (value == 0u) {
5533 __ Mov(out, 0);
5534 return;
5535 }
5536 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
5537 __ And(out, first, value);
5538 } else {
5539 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
5540 __ Bic(out, first, ~value);
5541 }
5542}
5543
5544// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
5545void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
5546 vixl32::Register first,
5547 uint32_t value) {
5548 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5549 if (value == 0u) {
5550 if (!out.Is(first)) {
5551 __ Mov(out, first);
5552 }
5553 return;
5554 }
5555 if (value == 0xffffffffu) {
5556 __ Mvn(out, 0);
5557 return;
5558 }
5559 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
5560 __ Orr(out, first, value);
5561 } else {
5562 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
5563 __ Orn(out, first, ~value);
5564 }
5565}
5566
5567// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
5568void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
5569 vixl32::Register first,
5570 uint32_t value) {
5571 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5572 if (value == 0u) {
5573 if (!out.Is(first)) {
5574 __ Mov(out, first);
5575 }
5576 return;
5577 }
5578 __ Eor(out, first, value);
5579}
5580
5581void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
5582 LocationSummary* locations = instruction->GetLocations();
5583 Location first = locations->InAt(0);
5584 Location second = locations->InAt(1);
5585 Location out = locations->Out();
5586
5587 if (second.IsConstant()) {
5588 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5589 uint32_t value_low = Low32Bits(value);
5590 if (instruction->GetResultType() == Primitive::kPrimInt) {
5591 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
5592 vixl32::Register out_reg = OutputRegister(instruction);
5593 if (instruction->IsAnd()) {
5594 GenerateAndConst(out_reg, first_reg, value_low);
5595 } else if (instruction->IsOr()) {
5596 GenerateOrrConst(out_reg, first_reg, value_low);
5597 } else {
5598 DCHECK(instruction->IsXor());
5599 GenerateEorConst(out_reg, first_reg, value_low);
5600 }
5601 } else {
5602 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5603 uint32_t value_high = High32Bits(value);
5604 vixl32::Register first_low = LowRegisterFrom(first);
5605 vixl32::Register first_high = HighRegisterFrom(first);
5606 vixl32::Register out_low = LowRegisterFrom(out);
5607 vixl32::Register out_high = HighRegisterFrom(out);
5608 if (instruction->IsAnd()) {
5609 GenerateAndConst(out_low, first_low, value_low);
5610 GenerateAndConst(out_high, first_high, value_high);
5611 } else if (instruction->IsOr()) {
5612 GenerateOrrConst(out_low, first_low, value_low);
5613 GenerateOrrConst(out_high, first_high, value_high);
5614 } else {
5615 DCHECK(instruction->IsXor());
5616 GenerateEorConst(out_low, first_low, value_low);
5617 GenerateEorConst(out_high, first_high, value_high);
5618 }
5619 }
5620 return;
5621 }
5622
5623 if (instruction->GetResultType() == Primitive::kPrimInt) {
5624 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
5625 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
5626 vixl32::Register out_reg = OutputRegister(instruction);
5627 if (instruction->IsAnd()) {
5628 __ And(out_reg, first_reg, second_reg);
5629 } else if (instruction->IsOr()) {
5630 __ Orr(out_reg, first_reg, second_reg);
5631 } else {
5632 DCHECK(instruction->IsXor());
5633 __ Eor(out_reg, first_reg, second_reg);
5634 }
5635 } else {
5636 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5637 vixl32::Register first_low = LowRegisterFrom(first);
5638 vixl32::Register first_high = HighRegisterFrom(first);
5639 vixl32::Register second_low = LowRegisterFrom(second);
5640 vixl32::Register second_high = HighRegisterFrom(second);
5641 vixl32::Register out_low = LowRegisterFrom(out);
5642 vixl32::Register out_high = HighRegisterFrom(out);
5643 if (instruction->IsAnd()) {
5644 __ And(out_low, first_low, second_low);
5645 __ And(out_high, first_high, second_high);
5646 } else if (instruction->IsOr()) {
5647 __ Orr(out_low, first_low, second_low);
5648 __ Orr(out_high, first_high, second_high);
5649 } else {
5650 DCHECK(instruction->IsXor());
5651 __ Eor(out_low, first_low, second_low);
5652 __ Eor(out_high, first_high, second_high);
5653 }
5654 }
5655}
5656
Artem Serovcfbe9132016-10-14 15:58:56 +01005657void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
5658 HInstruction* instruction ATTRIBUTE_UNUSED,
5659 Location out,
5660 uint32_t offset,
5661 Location maybe_temp ATTRIBUTE_UNUSED) {
5662 vixl32::Register out_reg = RegisterFrom(out);
5663 if (kEmitCompilerReadBarrier) {
5664 TODO_VIXL32(FATAL);
5665 } else {
5666 // Plain load with no read barrier.
5667 // /* HeapReference<Object> */ out = *(out + offset)
5668 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5669 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5670 }
5671}
5672
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005673void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
5674 HInstruction* instruction ATTRIBUTE_UNUSED,
5675 Location out,
5676 Location obj,
5677 uint32_t offset,
5678 Location maybe_temp ATTRIBUTE_UNUSED) {
5679 vixl32::Register out_reg = RegisterFrom(out);
5680 vixl32::Register obj_reg = RegisterFrom(obj);
5681 if (kEmitCompilerReadBarrier) {
5682 TODO_VIXL32(FATAL);
5683 } else {
5684 // Plain load with no read barrier.
5685 // /* HeapReference<Object> */ out = *(obj + offset)
5686 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5687 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5688 }
5689}
5690
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005691void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
5692 HInstruction* instruction ATTRIBUTE_UNUSED,
5693 Location root,
5694 vixl32::Register obj,
5695 uint32_t offset,
5696 bool requires_read_barrier) {
5697 vixl32::Register root_reg = RegisterFrom(root);
5698 if (requires_read_barrier) {
5699 TODO_VIXL32(FATAL);
5700 } else {
5701 // Plain GC root load with no read barrier.
5702 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5703 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
5704 // Note that GC roots are not affected by heap poisoning, thus we
5705 // do not have to unpoison `root_reg` here.
5706 }
5707}
5708
Roland Levillain6070e882016-11-03 17:51:58 +00005709void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(
5710 HInstruction* instruction ATTRIBUTE_UNUSED,
5711 Location ref ATTRIBUTE_UNUSED,
5712 vixl::aarch32::Register obj ATTRIBUTE_UNUSED,
5713 uint32_t offset ATTRIBUTE_UNUSED,
5714 Location temp ATTRIBUTE_UNUSED,
5715 bool needs_null_check ATTRIBUTE_UNUSED) {
5716 TODO_VIXL32(FATAL);
5717}
5718
5719void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(
5720 HInstruction* instruction ATTRIBUTE_UNUSED,
5721 Location ref ATTRIBUTE_UNUSED,
5722 vixl::aarch32::Register obj ATTRIBUTE_UNUSED,
5723 uint32_t offset ATTRIBUTE_UNUSED,
5724 Location index ATTRIBUTE_UNUSED,
5725 ScaleFactor scale_factor ATTRIBUTE_UNUSED,
5726 Location temp ATTRIBUTE_UNUSED,
5727 bool needs_null_check ATTRIBUTE_UNUSED,
5728 bool always_update_field ATTRIBUTE_UNUSED,
5729 vixl::aarch32::Register* temp2 ATTRIBUTE_UNUSED) {
5730 TODO_VIXL32(FATAL);
5731}
5732
Roland Levillain844e6532016-11-03 16:09:47 +00005733void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction ATTRIBUTE_UNUSED,
5734 Location out ATTRIBUTE_UNUSED,
5735 Location ref ATTRIBUTE_UNUSED,
5736 Location obj ATTRIBUTE_UNUSED,
5737 uint32_t offset ATTRIBUTE_UNUSED,
5738 Location index ATTRIBUTE_UNUSED) {
5739 TODO_VIXL32(FATAL);
5740}
5741
Artem Serov02d37832016-10-25 15:25:33 +01005742void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction ATTRIBUTE_UNUSED,
5743 Location out,
5744 Location ref ATTRIBUTE_UNUSED,
5745 Location obj ATTRIBUTE_UNUSED,
5746 uint32_t offset ATTRIBUTE_UNUSED,
5747 Location index ATTRIBUTE_UNUSED) {
5748 if (kEmitCompilerReadBarrier) {
5749 DCHECK(!kUseBakerReadBarrier);
5750 TODO_VIXL32(FATAL);
5751 } else if (kPoisonHeapReferences) {
5752 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
5753 }
5754}
5755
5756// Check if the desired_dispatch_info is supported. If it is, return it,
5757// otherwise return a fall-back info that should be used instead.
5758HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
5759 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info ATTRIBUTE_UNUSED,
5760 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
5761 // TODO(VIXL): Implement optimized code paths.
5762 return {
5763 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5764 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5765 0u,
5766 0u
5767 };
5768}
5769
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005770vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
5771 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
5772 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5773 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5774 if (!invoke->GetLocations()->Intrinsified()) {
5775 return RegisterFrom(location);
5776 }
5777 // For intrinsics we allow any location, so it may be on the stack.
5778 if (!location.IsRegister()) {
5779 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
5780 return temp;
5781 }
5782 // For register locations, check if the register was saved. If so, get it from the stack.
5783 // Note: There is a chance that the register was saved but not overwritten, so we could
5784 // save one load. However, since this is just an intrinsic slow path we prefer this
5785 // simple and more robust approach rather that trying to determine if that's the case.
5786 SlowPathCode* slow_path = GetCurrentSlowPath();
5787 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5788 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
5789 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
5790 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
5791 return temp;
5792 }
5793 return RegisterFrom(location);
5794}
5795
5796void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
5797 HInvokeStaticOrDirect* invoke, Location temp) {
5798 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5799 vixl32::Register temp_reg = RegisterFrom(temp);
5800
5801 switch (invoke->GetMethodLoadKind()) {
5802 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
5803 uint32_t offset =
5804 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5805 // temp = thread->string_init_entrypoint
5806 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, tr, offset);
5807 break;
5808 }
5809 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5810 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5811 vixl32::Register method_reg;
5812 if (current_method.IsRegister()) {
5813 method_reg = RegisterFrom(current_method);
5814 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005815 DCHECK(invoke->GetLocations()->Intrinsified());
5816 DCHECK(!current_method.IsValid());
5817 method_reg = temp_reg;
5818 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005819 }
5820 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
5821 GetAssembler()->LoadFromOffset(
5822 kLoadWord,
5823 temp_reg,
5824 method_reg,
5825 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
5826 // temp = temp[index_in_cache];
5827 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5828 uint32_t index_in_cache = invoke->GetDexMethodIndex();
5829 GetAssembler()->LoadFromOffset(
5830 kLoadWord, temp_reg, temp_reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5831 break;
5832 }
5833 default:
5834 TODO_VIXL32(FATAL);
5835 }
5836
5837 // TODO(VIXL): Support `CodePtrLocation` values other than `kCallArtMethod`.
5838 if (invoke->GetCodePtrLocation() != HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod) {
5839 TODO_VIXL32(FATAL);
5840 }
5841
5842 // LR = callee_method->entry_point_from_quick_compiled_code_
5843 GetAssembler()->LoadFromOffset(
5844 kLoadWord,
5845 lr,
5846 RegisterFrom(callee_method),
5847 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
5848 // LR()
5849 __ Blx(lr);
5850
5851 DCHECK(!IsLeafMethod());
5852}
5853
5854void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5855 vixl32::Register temp = RegisterFrom(temp_location);
5856 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5857 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5858
5859 // Use the calling convention instead of the location of the receiver, as
5860 // intrinsics may have put the receiver in a different register. In the intrinsics
5861 // slow path, the arguments have been moved to the right place, so here we are
5862 // guaranteed that the receiver is the first register of the calling convention.
5863 InvokeDexCallingConventionARMVIXL calling_convention;
5864 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
5865 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5866 // /* HeapReference<Class> */ temp = receiver->klass_
5867 GetAssembler()->LoadFromOffset(kLoadWord, temp, receiver, class_offset);
5868 MaybeRecordImplicitNullCheck(invoke);
5869 // Instead of simply (possibly) unpoisoning `temp` here, we should
5870 // emit a read barrier for the previous class reference load.
5871 // However this is not required in practice, as this is an
5872 // intermediate/temporary reference and because the current
5873 // concurrent copying collector keeps the from-space memory
5874 // intact/accessible until the end of the marking phase (the
5875 // concurrent copying collector may not in the future).
5876 GetAssembler()->MaybeUnpoisonHeapReference(temp);
5877
5878 // temp = temp->GetMethodAt(method_offset);
5879 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5880 kArmPointerSize).Int32Value();
5881 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
5882 // LR = temp->GetEntryPoint();
5883 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
5884 // LR();
5885 __ Blx(lr);
5886}
5887
Artem Serov551b28f2016-10-18 19:11:30 +01005888void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5889 // Nothing to do, this should be removed during prepare for register allocator.
5890 LOG(FATAL) << "Unreachable";
5891}
5892
5893void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5894 // Nothing to do, this should be removed during prepare for register allocator.
5895 LOG(FATAL) << "Unreachable";
5896}
5897
5898// Simple implementation of packed switch - generate cascaded compare/jumps.
5899void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5900 LocationSummary* locations =
5901 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5902 locations->SetInAt(0, Location::RequiresRegister());
5903 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
5904 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
5905 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
5906 if (switch_instr->GetStartValue() != 0) {
5907 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
5908 }
5909 }
5910}
5911
5912// TODO(VIXL): Investigate and reach the parity with old arm codegen.
5913void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5914 int32_t lower_bound = switch_instr->GetStartValue();
5915 uint32_t num_entries = switch_instr->GetNumEntries();
5916 LocationSummary* locations = switch_instr->GetLocations();
5917 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
5918 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5919
5920 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
5921 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
5922 // Create a series of compare/jumps.
5923 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5924 vixl32::Register temp_reg = temps.Acquire();
5925 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
5926 // the immediate, because IP is used as the destination register. For the other
5927 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
5928 // and they can be encoded in the instruction without making use of IP register.
5929 __ Adds(temp_reg, value_reg, -lower_bound);
5930
5931 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5932 // Jump to successors[0] if value == lower_bound.
5933 __ B(eq, codegen_->GetLabelOf(successors[0]));
5934 int32_t last_index = 0;
5935 for (; num_entries - last_index > 2; last_index += 2) {
5936 __ Adds(temp_reg, temp_reg, -2);
5937 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5938 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5939 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5940 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5941 }
5942 if (num_entries - last_index == 2) {
5943 // The last missing case_value.
5944 __ Cmp(temp_reg, 1);
5945 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
5946 }
5947
5948 // And the default for any other value.
5949 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5950 __ B(codegen_->GetLabelOf(default_block));
5951 }
5952 } else {
5953 // Create a table lookup.
5954 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
5955
5956 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
5957
5958 // Remove the bias.
5959 vixl32::Register key_reg;
5960 if (lower_bound != 0) {
5961 key_reg = RegisterFrom(locations->GetTemp(1));
5962 __ Sub(key_reg, value_reg, lower_bound);
5963 } else {
5964 key_reg = value_reg;
5965 }
5966
5967 // Check whether the value is in the table, jump to default block if not.
5968 __ Cmp(key_reg, num_entries - 1);
5969 __ B(hi, codegen_->GetLabelOf(default_block));
5970
5971 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5972 vixl32::Register jump_offset = temps.Acquire();
5973
5974 // Load jump offset from the table.
5975 __ Adr(table_base, jump_table->GetTableStartLabel());
5976 __ Ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
5977
5978 // Jump to target block by branching to table_base(pc related) + offset.
5979 vixl32::Register target_address = table_base;
5980 __ Add(target_address, table_base, jump_offset);
5981 __ Bx(target_address);
5982 }
5983}
5984
Artem Serov02d37832016-10-25 15:25:33 +01005985// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005986void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5987 if (!trg.IsValid()) {
5988 DCHECK_EQ(type, Primitive::kPrimVoid);
5989 return;
5990 }
5991
5992 DCHECK_NE(type, Primitive::kPrimVoid);
5993
5994 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5995 if (return_loc.Equals(trg)) {
5996 return;
5997 }
5998
5999 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6000 // with the last branch.
6001 if (type == Primitive::kPrimLong) {
6002 TODO_VIXL32(FATAL);
6003 } else if (type == Primitive::kPrimDouble) {
6004 TODO_VIXL32(FATAL);
6005 } else {
6006 // Let the parallel move resolver take care of all of this.
6007 HParallelMove parallel_move(GetGraph()->GetArena());
6008 parallel_move.AddMove(return_loc, trg, type, nullptr);
6009 GetMoveResolver()->EmitNativeCode(&parallel_move);
6010 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006011}
Scott Wakelingfe885462016-09-22 10:24:38 +01006012
Artem Serov551b28f2016-10-18 19:11:30 +01006013void LocationsBuilderARMVIXL::VisitClassTableGet(
6014 HClassTableGet* instruction ATTRIBUTE_UNUSED) {
6015 TODO_VIXL32(FATAL);
6016}
6017
6018void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(
6019 HClassTableGet* instruction ATTRIBUTE_UNUSED) {
6020 TODO_VIXL32(FATAL);
6021}
6022
6023
Scott Wakelingfe885462016-09-22 10:24:38 +01006024#undef __
6025#undef QUICK_ENTRY_POINT
6026#undef TODO_VIXL32
6027
6028} // namespace arm
6029} // namespace art