blob: 1c6e56470fa67104bd5b88c94a7090d0e8caca77 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "common_arm.h"
23#include "compiled_method.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
26#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "thread.h"
29#include "utils/arm/assembler_arm_vixl.h"
30#include "utils/arm/managed_register_arm.h"
31#include "utils/assembler.h"
32#include "utils/stack_checks.h"
33
34namespace art {
35namespace arm {
36
37namespace vixl32 = vixl::aarch32;
38using namespace vixl32; // NOLINT(build/namespaces)
39
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010040using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010041using helpers::DWARFReg;
42using helpers::FromLowSToD;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010043using helpers::HighDRegisterFrom;
44using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010045using helpers::InputOperandAt;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010046using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010047using helpers::InputSRegisterAt;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048using helpers::InputVRegisterAt;
49using helpers::LocationFrom;
50using helpers::LowRegisterFrom;
51using helpers::LowSRegisterFrom;
52using helpers::OutputRegister;
53using helpers::OutputSRegister;
54using helpers::OutputVRegister;
55using helpers::RegisterFrom;
56using helpers::SRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010057
58using RegisterList = vixl32::RegisterList;
59
60static bool ExpectedPairLayout(Location location) {
61 // We expected this for both core and fpu register pairs.
62 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
63}
64
65static constexpr size_t kArmInstrMaxSizeInBytes = 4u;
66
67#ifdef __
68#error "ARM Codegen VIXL macro-assembler macro already defined."
69#endif
70
Scott Wakelingfe885462016-09-22 10:24:38 +010071// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
72#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
73#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
74
75// Marker that code is yet to be, and must, be implemented.
76#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
77
Scott Wakelinga7812ae2016-10-17 10:03:36 +010078// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
79// for each live D registers they treat two corresponding S registers as live ones.
80//
81// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
82// from a list of contiguous S registers a list of contiguous D registers (processing first/last
83// S registers corner cases) and save/restore this new list treating them as D registers.
84// - decreasing code size
85// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
86// restored and then used in regular non SlowPath code as D register.
87//
88// For the following example (v means the S register is live):
89// D names: | D0 | D1 | D2 | D4 | ...
90// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
91// Live? | | v | v | v | v | v | v | | ...
92//
93// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
94// as D registers.
95//
96// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
97// for lists of floating-point registers.
98static size_t SaveContiguousSRegisterList(size_t first,
99 size_t last,
100 CodeGenerator* codegen,
101 size_t stack_offset) {
102 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
103 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
104 DCHECK_LE(first, last);
105 if ((first == last) && (first == 0)) {
106 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
107 return stack_offset + kSRegSizeInBytes;
108 }
109 if (first % 2 == 1) {
110 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
111 stack_offset += kSRegSizeInBytes;
112 }
113
114 bool save_last = false;
115 if (last % 2 == 0) {
116 save_last = true;
117 --last;
118 }
119
120 if (first < last) {
121 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
122 DCHECK_EQ((last - first + 1) % 2, 0u);
123 size_t number_of_d_regs = (last - first + 1) / 2;
124
125 if (number_of_d_regs == 1) {
126 __ Vstr(d_reg, MemOperand(sp, stack_offset));
127 } else if (number_of_d_regs > 1) {
128 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
129 vixl32::Register base = sp;
130 if (stack_offset != 0) {
131 base = temps.Acquire();
132 __ Add(base, sp, stack_offset);
133 }
134 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
135 }
136 stack_offset += number_of_d_regs * kDRegSizeInBytes;
137 }
138
139 if (save_last) {
140 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
141 stack_offset += kSRegSizeInBytes;
142 }
143
144 return stack_offset;
145}
146
147static size_t RestoreContiguousSRegisterList(size_t first,
148 size_t last,
149 CodeGenerator* codegen,
150 size_t stack_offset) {
151 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
152 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
153 DCHECK_LE(first, last);
154 if ((first == last) && (first == 0)) {
155 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
156 return stack_offset + kSRegSizeInBytes;
157 }
158 if (first % 2 == 1) {
159 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
160 stack_offset += kSRegSizeInBytes;
161 }
162
163 bool restore_last = false;
164 if (last % 2 == 0) {
165 restore_last = true;
166 --last;
167 }
168
169 if (first < last) {
170 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
171 DCHECK_EQ((last - first + 1) % 2, 0u);
172 size_t number_of_d_regs = (last - first + 1) / 2;
173 if (number_of_d_regs == 1) {
174 __ Vldr(d_reg, MemOperand(sp, stack_offset));
175 } else if (number_of_d_regs > 1) {
176 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
177 vixl32::Register base = sp;
178 if (stack_offset != 0) {
179 base = temps.Acquire();
180 __ Add(base, sp, stack_offset);
181 }
182 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
183 }
184 stack_offset += number_of_d_regs * kDRegSizeInBytes;
185 }
186
187 if (restore_last) {
188 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
189 stack_offset += kSRegSizeInBytes;
190 }
191
192 return stack_offset;
193}
194
195void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
196 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
197 size_t orig_offset = stack_offset;
198
199 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
200 for (uint32_t i : LowToHighBits(core_spills)) {
201 // If the register holds an object, update the stack mask.
202 if (locations->RegisterContainsObject(i)) {
203 locations->SetStackBit(stack_offset / kVRegSize);
204 }
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_core_stack_offsets_[i] = stack_offset;
208 stack_offset += kArmWordSize;
209 }
210
211 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
212 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
213
214 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
215 orig_offset = stack_offset;
216 for (uint32_t i : LowToHighBits(fp_spills)) {
217 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
218 saved_fpu_stack_offsets_[i] = stack_offset;
219 stack_offset += kArmWordSize;
220 }
221
222 stack_offset = orig_offset;
223 while (fp_spills != 0u) {
224 uint32_t begin = CTZ(fp_spills);
225 uint32_t tmp = fp_spills + (1u << begin);
226 fp_spills &= tmp; // Clear the contiguous range of 1s.
227 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
228 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
229 }
230 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231}
232
233void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
234 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
235 size_t orig_offset = stack_offset;
236
237 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
238 for (uint32_t i : LowToHighBits(core_spills)) {
239 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
240 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
241 stack_offset += kArmWordSize;
242 }
243
244 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
245 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
246 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
247
248 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
249 while (fp_spills != 0u) {
250 uint32_t begin = CTZ(fp_spills);
251 uint32_t tmp = fp_spills + (1u << begin);
252 fp_spills &= tmp; // Clear the contiguous range of 1s.
253 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
254 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
255 }
256 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
257}
258
259class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
260 public:
261 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
262
263 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
264 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
265 __ Bind(GetEntryLabel());
266 if (instruction_->CanThrowIntoCatchBlock()) {
267 // Live registers will be restored in the catch block if caught.
268 SaveLiveRegisters(codegen, instruction_->GetLocations());
269 }
270 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
271 instruction_,
272 instruction_->GetDexPc(),
273 this);
274 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
275 }
276
277 bool IsFatal() const OVERRIDE { return true; }
278
279 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
280
281 private:
282 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
283};
284
Scott Wakelingfe885462016-09-22 10:24:38 +0100285class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
286 public:
287 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
288 : SlowPathCodeARMVIXL(instruction) {}
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100291 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100292 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100293 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100294 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
295 }
296
297 bool IsFatal() const OVERRIDE { return true; }
298
299 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
300
301 private:
302 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
303};
304
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100305class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
306 public:
307 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
308 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
312 __ Bind(GetEntryLabel());
313 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
314 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
315 if (successor_ == nullptr) {
316 __ B(GetReturnLabel());
317 } else {
318 __ B(arm_codegen->GetLabelOf(successor_));
319 }
320 }
321
322 vixl32::Label* GetReturnLabel() {
323 DCHECK(successor_ == nullptr);
324 return &return_label_;
325 }
326
327 HBasicBlock* GetSuccessor() const {
328 return successor_;
329 }
330
331 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
332
333 private:
334 // If not null, the block to branch to after the suspend check.
335 HBasicBlock* const successor_;
336
337 // If `successor_` is null, the label to branch to after the suspend check.
338 vixl32::Label return_label_;
339
340 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
341};
342
343class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
344 public:
345 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
346 : SlowPathCodeARMVIXL(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
347 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
348 }
349
350 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
351 LocationSummary* locations = at_->GetLocations();
352
353 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
354 __ Bind(GetEntryLabel());
355 SaveLiveRegisters(codegen, locations);
356
357 InvokeRuntimeCallingConventionARMVIXL calling_convention;
358 __ Mov(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
359 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
360 : kQuickInitializeType;
361 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
362 if (do_clinit_) {
363 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
364 } else {
365 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
366 }
367
368 // Move the class to the desired location.
369 Location out = locations->Out();
370 if (out.IsValid()) {
371 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
372 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
373 }
374 RestoreLiveRegisters(codegen, locations);
375 __ B(GetExitLabel());
376 }
377
378 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
379
380 private:
381 // The class this slow path will load.
382 HLoadClass* const cls_;
383
384 // The instruction where this slow path is happening.
385 // (Might be the load class or an initialization check).
386 HInstruction* const at_;
387
388 // The dex PC of `at_`.
389 const uint32_t dex_pc_;
390
391 // Whether to initialize the class.
392 const bool do_clinit_;
393
394 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
395};
396
Scott Wakelingfe885462016-09-22 10:24:38 +0100397inline vixl32::Condition ARMCondition(IfCondition cond) {
398 switch (cond) {
399 case kCondEQ: return eq;
400 case kCondNE: return ne;
401 case kCondLT: return lt;
402 case kCondLE: return le;
403 case kCondGT: return gt;
404 case kCondGE: return ge;
405 case kCondB: return lo;
406 case kCondBE: return ls;
407 case kCondA: return hi;
408 case kCondAE: return hs;
409 }
410 LOG(FATAL) << "Unreachable";
411 UNREACHABLE();
412}
413
414// Maps signed condition to unsigned condition.
415inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
416 switch (cond) {
417 case kCondEQ: return eq;
418 case kCondNE: return ne;
419 // Signed to unsigned.
420 case kCondLT: return lo;
421 case kCondLE: return ls;
422 case kCondGT: return hi;
423 case kCondGE: return hs;
424 // Unsigned remain unchanged.
425 case kCondB: return lo;
426 case kCondBE: return ls;
427 case kCondA: return hi;
428 case kCondAE: return hs;
429 }
430 LOG(FATAL) << "Unreachable";
431 UNREACHABLE();
432}
433
434inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
435 // The ARM condition codes can express all the necessary branches, see the
436 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
437 // There is no dex instruction or HIR that would need the missing conditions
438 // "equal or unordered" or "not equal".
439 switch (cond) {
440 case kCondEQ: return eq;
441 case kCondNE: return ne /* unordered */;
442 case kCondLT: return gt_bias ? cc : lt /* unordered */;
443 case kCondLE: return gt_bias ? ls : le /* unordered */;
444 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
445 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
446 default:
447 LOG(FATAL) << "UNREACHABLE";
448 UNREACHABLE();
449 }
450}
451
Scott Wakelingfe885462016-09-22 10:24:38 +0100452void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
453 stream << vixl32::Register(reg);
454}
455
456void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
457 stream << vixl32::SRegister(reg);
458}
459
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100460static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100461 uint32_t mask = 0;
462 for (uint32_t i = regs.GetFirstSRegister().GetCode();
463 i <= regs.GetLastSRegister().GetCode();
464 ++i) {
465 mask |= (1 << i);
466 }
467 return mask;
468}
469
470#undef __
471
472CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
473 const ArmInstructionSetFeatures& isa_features,
474 const CompilerOptions& compiler_options,
475 OptimizingCompilerStats* stats)
476 : CodeGenerator(graph,
477 kNumberOfCoreRegisters,
478 kNumberOfSRegisters,
479 kNumberOfRegisterPairs,
480 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100481 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +0100482 compiler_options,
483 stats),
484 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
485 location_builder_(graph, this),
486 instruction_visitor_(graph, this),
487 move_resolver_(graph->GetArena(), this),
488 assembler_(graph->GetArena()),
489 isa_features_(isa_features) {
490 // Always save the LR register to mimic Quick.
491 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100492 // Give d14 and d15 as scratch registers to VIXL.
493 // They are removed from the register allocator in `SetupBlockedRegisters()`.
494 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
495 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
496 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
497 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
498 // also want to investigate giving those 14 other D registers to the allocator.
499 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
500 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +0100501}
502
503#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()->
504
505void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
506 GetAssembler()->FinalizeCode();
507 CodeGenerator::Finalize(allocator);
508}
509
510void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +0100511 // Stack register, LR and PC are always reserved.
512 blocked_core_registers_[SP] = true;
513 blocked_core_registers_[LR] = true;
514 blocked_core_registers_[PC] = true;
515
516 // Reserve thread register.
517 blocked_core_registers_[TR] = true;
518
519 // Reserve temp register.
520 blocked_core_registers_[IP] = true;
521
Alexandre Rames9c19bd62016-10-24 11:50:32 +0100522 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
523 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
524 blocked_fpu_registers_[28] = true;
525 blocked_fpu_registers_[29] = true;
526 blocked_fpu_registers_[30] = true;
527 blocked_fpu_registers_[31] = true;
528
Scott Wakelingfe885462016-09-22 10:24:38 +0100529 if (GetGraph()->IsDebuggable()) {
530 // Stubs do not save callee-save floating point registers. If the graph
531 // is debuggable, we need to deal with these registers differently. For
532 // now, just block them.
533 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
534 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
535 ++i) {
536 blocked_fpu_registers_[i] = true;
537 }
538 }
Scott Wakelingfe885462016-09-22 10:24:38 +0100539}
540
Scott Wakelingfe885462016-09-22 10:24:38 +0100541InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
542 CodeGeneratorARMVIXL* codegen)
543 : InstructionCodeGenerator(graph, codegen),
544 assembler_(codegen->GetAssembler()),
545 codegen_(codegen) {}
546
547void CodeGeneratorARMVIXL::ComputeSpillMask() {
548 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
549 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
550 // There is no easy instruction to restore just the PC on thumb2. We spill and
551 // restore another arbitrary register.
552 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
553 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
554 // We use vpush and vpop for saving and restoring floating point registers, which take
555 // a SRegister and the number of registers to save/restore after that SRegister. We
556 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
557 // but in the range.
558 if (fpu_spill_mask_ != 0) {
559 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
560 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
561 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
562 fpu_spill_mask_ |= (1 << i);
563 }
564 }
565}
566
567void CodeGeneratorARMVIXL::GenerateFrameEntry() {
568 bool skip_overflow_check =
569 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
570 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
571 __ Bind(&frame_entry_label_);
572
573 if (HasEmptyFrame()) {
574 return;
575 }
576
Scott Wakelingfe885462016-09-22 10:24:38 +0100577 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100578 UseScratchRegisterScope temps(GetVIXLAssembler());
579 vixl32::Register temp = temps.Acquire();
Scott Wakelingfe885462016-09-22 10:24:38 +0100580 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
581 // The load must immediately precede RecordPcInfo.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100582 AssemblerAccurateScope aas(GetVIXLAssembler(),
583 kArmInstrMaxSizeInBytes,
584 CodeBufferCheckScope::kMaximumSize);
585 __ ldr(temp, MemOperand(temp));
586 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +0100587 }
588
589 __ Push(RegisterList(core_spill_mask_));
590 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
591 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
592 0,
593 core_spill_mask_,
594 kArmWordSize);
595 if (fpu_spill_mask_ != 0) {
596 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
597
598 // Check that list is contiguous.
599 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
600
601 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
602 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100603 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +0100604 }
605 int adjust = GetFrameSize() - FrameEntrySpillSize();
606 __ Sub(sp, sp, adjust);
607 GetAssembler()->cfi().AdjustCFAOffset(adjust);
608 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
609}
610
611void CodeGeneratorARMVIXL::GenerateFrameExit() {
612 if (HasEmptyFrame()) {
613 __ Bx(lr);
614 return;
615 }
616 GetAssembler()->cfi().RememberState();
617 int adjust = GetFrameSize() - FrameEntrySpillSize();
618 __ Add(sp, sp, adjust);
619 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
620 if (fpu_spill_mask_ != 0) {
621 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
622
623 // Check that list is contiguous.
624 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
625
626 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
627 GetAssembler()->cfi().AdjustCFAOffset(
628 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100629 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +0100630 }
631 // Pop LR into PC to return.
632 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
633 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
634 __ Pop(RegisterList(pop_mask));
635 GetAssembler()->cfi().RestoreState();
636 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
637}
638
639void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
640 __ Bind(GetLabelOf(block));
641}
642
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100643void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
644 if (source.Equals(destination)) {
645 return;
646 }
647 if (destination.IsRegister()) {
648 if (source.IsRegister()) {
649 __ Mov(RegisterFrom(destination), RegisterFrom(source));
650 } else if (source.IsFpuRegister()) {
651 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
652 } else {
653 GetAssembler()->LoadFromOffset(kLoadWord,
654 RegisterFrom(destination),
655 sp,
656 source.GetStackIndex());
657 }
658 } else if (destination.IsFpuRegister()) {
659 if (source.IsRegister()) {
660 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
661 } else if (source.IsFpuRegister()) {
662 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
663 } else {
664 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
665 }
666 } else {
667 DCHECK(destination.IsStackSlot()) << destination;
668 if (source.IsRegister()) {
669 GetAssembler()->StoreToOffset(kStoreWord,
670 RegisterFrom(source),
671 sp,
672 destination.GetStackIndex());
673 } else if (source.IsFpuRegister()) {
674 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
675 } else {
676 DCHECK(source.IsStackSlot()) << source;
677 UseScratchRegisterScope temps(GetVIXLAssembler());
678 vixl32::Register temp = temps.Acquire();
679 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
680 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
681 }
682 }
683}
684
685void CodeGeneratorARMVIXL::MoveConstant(Location destination ATTRIBUTE_UNUSED,
686 int32_t value ATTRIBUTE_UNUSED) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100687 TODO_VIXL32(FATAL);
688}
689
690void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100691 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
692 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
693 HParallelMove move(GetGraph()->GetArena());
694 move.AddMove(src, dst, dst_type, nullptr);
695 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +0100696}
697
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100698void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location ATTRIBUTE_UNUSED,
699 LocationSummary* locations ATTRIBUTE_UNUSED) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100700 TODO_VIXL32(FATAL);
701}
702
703void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
704 HInstruction* instruction,
705 uint32_t dex_pc,
706 SlowPathCode* slow_path) {
707 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
708 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
709 if (EntrypointRequiresStackMap(entrypoint)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100710 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
711 // previous instruction.
Scott Wakelingfe885462016-09-22 10:24:38 +0100712 RecordPcInfo(instruction, dex_pc, slow_path);
713 }
714}
715
716void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
717 HInstruction* instruction,
718 SlowPathCode* slow_path) {
719 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
720 GenerateInvokeRuntime(entry_point_offset);
721}
722
723void CodeGeneratorARMVIXL::GenerateInvokeRuntime(int32_t entry_point_offset) {
724 GetAssembler()->LoadFromOffset(kLoadWord, lr, tr, entry_point_offset);
725 __ Blx(lr);
726}
727
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100728void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
729 LocationSummary* locations =
730 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
731 locations->SetInAt(0, Location::RequiresRegister());
732 if (check->HasUses()) {
733 locations->SetOut(Location::SameAsFirstInput());
734 }
735}
736
737void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
738 // We assume the class is not null.
739 LoadClassSlowPathARMVIXL* slow_path =
740 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
741 check,
742 check->GetDexPc(),
743 /* do_clinit */ true);
744 codegen_->AddSlowPath(slow_path);
745 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
746}
747
748void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
749 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
750 UseScratchRegisterScope temps(GetVIXLAssembler());
751 vixl32::Register temp = temps.Acquire();
752 GetAssembler()->LoadFromOffset(kLoadWord,
753 temp,
754 class_reg,
755 mirror::Class::StatusOffset().Int32Value());
756 __ Cmp(temp, mirror::Class::kStatusInitialized);
757 __ B(lt, slow_path->GetEntryLabel());
758 // Even if the initialized flag is set, we may be in a situation where caches are not synced
759 // properly. Therefore, we do a memory fence.
760 __ Dmb(ISH);
761 __ Bind(slow_path->GetExitLabel());
762}
763
Scott Wakelingfe885462016-09-22 10:24:38 +0100764// Check if the desired_string_load_kind is supported. If it is, return it,
765// otherwise return a fall-back kind that should be used instead.
766HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100767 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
768 // TODO(VIXL): Implement optimized code paths. For now we always use the simpler fallback code.
769 return HLoadString::LoadKind::kDexCacheViaMethod;
770}
771
772void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
773 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
774 ? LocationSummary::kCallOnMainOnly
775 : LocationSummary::kNoCall;
776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
777
778 // TODO(VIXL): Implement optimized code paths.
779 // See InstructionCodeGeneratorARMVIXL::VisitLoadString.
780 HLoadString::LoadKind load_kind = load->GetLoadKind();
781 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
782 locations->SetInAt(0, Location::RequiresRegister());
783 // TODO(VIXL): Use InvokeRuntimeCallingConventionARMVIXL instead.
784 locations->SetOut(LocationFrom(r0));
785 } else {
786 locations->SetOut(Location::RequiresRegister());
787 }
788}
789
790void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) {
791 // TODO(VIXL): Implement optimized code paths.
792 // We implemented the simplest solution to get first ART tests passing, we deferred the
793 // optimized path until later, we should implement it using ARM64 implementation as a
794 // reference. The same related to LocationsBuilderARMVIXL::VisitLoadString.
795
796 // TODO: Re-add the compiler code to do string dex cache lookup again.
797 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
798 InvokeRuntimeCallingConventionARMVIXL calling_convention;
799 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex());
800 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
801 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +0100802}
803
804// Check if the desired_class_load_kind is supported. If it is, return it,
805// otherwise return a fall-back kind that should be used instead.
806HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100807 HLoadClass::LoadKind desired_class_load_kind ATTRIBUTE_UNUSED) {
808 // TODO(VIXL): Implement optimized code paths.
809 return HLoadClass::LoadKind::kDexCacheViaMethod;
Scott Wakelingfe885462016-09-22 10:24:38 +0100810}
811
812// Check if the desired_dispatch_info is supported. If it is, return it,
813// otherwise return a fall-back info that should be used instead.
814HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100815 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info ATTRIBUTE_UNUSED,
816 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
817 // TODO(VIXL): Implement optimized code paths.
818 return {
819 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
820 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
821 0u,
822 0u
823 };
Scott Wakelingfe885462016-09-22 10:24:38 +0100824}
825
826// Copy the result of a call into the given target.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100827void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
828 Primitive::Type type ATTRIBUTE_UNUSED) {
Scott Wakelingfe885462016-09-22 10:24:38 +0100829 TODO_VIXL32(FATAL);
830}
831
832void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
833 DCHECK(!successor->IsExitBlock());
834 HBasicBlock* block = got->GetBlock();
835 HInstruction* previous = got->GetPrevious();
836 HLoopInformation* info = block->GetLoopInformation();
837
838 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
839 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
840 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
841 return;
842 }
843 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
844 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
845 }
846 if (!codegen_->GoesToNextBlock(block, successor)) {
847 __ B(codegen_->GetLabelOf(successor));
848 }
849}
850
851void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
852 got->SetLocations(nullptr);
853}
854
855void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
856 HandleGoto(got, got->GetSuccessor());
857}
858
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100859void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
860 try_boundary->SetLocations(nullptr);
861}
862
863void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
864 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
865 if (!successor->IsExitBlock()) {
866 HandleGoto(try_boundary, successor);
867 }
868}
869
Scott Wakelingfe885462016-09-22 10:24:38 +0100870void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
871 exit->SetLocations(nullptr);
872}
873
874void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
875}
876
877void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
878 Primitive::Type type = instruction->InputAt(0)->GetType();
879 Location lhs_loc = instruction->GetLocations()->InAt(0);
880 Location rhs_loc = instruction->GetLocations()->InAt(1);
881 if (rhs_loc.IsConstant()) {
882 // 0.0 is the only immediate that can be encoded directly in
883 // a VCMP instruction.
884 //
885 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
886 // specify that in a floating-point comparison, positive zero
887 // and negative zero are considered equal, so we can use the
888 // literal 0.0 for both cases here.
889 //
890 // Note however that some methods (Float.equal, Float.compare,
891 // Float.compareTo, Double.equal, Double.compare,
892 // Double.compareTo, Math.max, Math.min, StrictMath.max,
893 // StrictMath.min) consider 0.0 to be (strictly) greater than
894 // -0.0. So if we ever translate calls to these methods into a
895 // HCompare instruction, we must handle the -0.0 case with
896 // care here.
897 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
898 if (type == Primitive::kPrimFloat) {
899 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
900 } else {
901 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100902 __ Vcmp(F64, FromLowSToD(LowSRegisterFrom(lhs_loc)), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +0100903 }
904 } else {
905 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100906 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +0100907 } else {
908 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100909 __ Vcmp(FromLowSToD(LowSRegisterFrom(lhs_loc)), FromLowSToD(LowSRegisterFrom(rhs_loc)));
Scott Wakelingfe885462016-09-22 10:24:38 +0100910 }
911 }
912}
913
914void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
915 vixl32::Label* true_label,
916 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
917 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
918 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
919 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
920}
921
922void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
923 vixl32::Label* true_label,
924 vixl32::Label* false_label) {
925 LocationSummary* locations = cond->GetLocations();
926 Location left = locations->InAt(0);
927 Location right = locations->InAt(1);
928 IfCondition if_cond = cond->GetCondition();
929
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100930 vixl32::Register left_high = HighRegisterFrom(left);
931 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +0100932 IfCondition true_high_cond = if_cond;
933 IfCondition false_high_cond = cond->GetOppositeCondition();
934 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
935
936 // Set the conditions for the test, remembering that == needs to be
937 // decided using the low words.
938 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
939 switch (if_cond) {
940 case kCondEQ:
941 case kCondNE:
942 // Nothing to do.
943 break;
944 case kCondLT:
945 false_high_cond = kCondGT;
946 break;
947 case kCondLE:
948 true_high_cond = kCondLT;
949 break;
950 case kCondGT:
951 false_high_cond = kCondLT;
952 break;
953 case kCondGE:
954 true_high_cond = kCondGT;
955 break;
956 case kCondB:
957 false_high_cond = kCondA;
958 break;
959 case kCondBE:
960 true_high_cond = kCondB;
961 break;
962 case kCondA:
963 false_high_cond = kCondB;
964 break;
965 case kCondAE:
966 true_high_cond = kCondA;
967 break;
968 }
969 if (right.IsConstant()) {
970 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
971 int32_t val_low = Low32Bits(value);
972 int32_t val_high = High32Bits(value);
973
974 __ Cmp(left_high, val_high);
975 if (if_cond == kCondNE) {
976 __ B(ARMCondition(true_high_cond), true_label);
977 } else if (if_cond == kCondEQ) {
978 __ B(ARMCondition(false_high_cond), false_label);
979 } else {
980 __ B(ARMCondition(true_high_cond), true_label);
981 __ B(ARMCondition(false_high_cond), false_label);
982 }
983 // Must be equal high, so compare the lows.
984 __ Cmp(left_low, val_low);
985 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100986 vixl32::Register right_high = HighRegisterFrom(right);
987 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +0100988
989 __ Cmp(left_high, right_high);
990 if (if_cond == kCondNE) {
991 __ B(ARMCondition(true_high_cond), true_label);
992 } else if (if_cond == kCondEQ) {
993 __ B(ARMCondition(false_high_cond), false_label);
994 } else {
995 __ B(ARMCondition(true_high_cond), true_label);
996 __ B(ARMCondition(false_high_cond), false_label);
997 }
998 // Must be equal high, so compare the lows.
999 __ Cmp(left_low, right_low);
1000 }
1001 // The last comparison might be unsigned.
1002 // TODO: optimize cases where this is always true/false
1003 __ B(final_condition, true_label);
1004}
1005
1006void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1007 vixl32::Label* true_target_in,
1008 vixl32::Label* false_target_in) {
1009 // Generated branching requires both targets to be explicit. If either of the
1010 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1011 vixl32::Label fallthrough;
1012 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1013 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1014
1015 Primitive::Type type = condition->InputAt(0)->GetType();
1016 switch (type) {
1017 case Primitive::kPrimLong:
1018 GenerateLongComparesAndJumps(condition, true_target, false_target);
1019 break;
1020 case Primitive::kPrimFloat:
1021 case Primitive::kPrimDouble:
1022 GenerateVcmp(condition);
1023 GenerateFPJumps(condition, true_target, false_target);
1024 break;
1025 default:
1026 LOG(FATAL) << "Unexpected compare type " << type;
1027 }
1028
1029 if (false_target != &fallthrough) {
1030 __ B(false_target);
1031 }
1032
1033 if (true_target_in == nullptr || false_target_in == nullptr) {
1034 __ Bind(&fallthrough);
1035 }
1036}
1037
1038void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1039 size_t condition_input_index,
1040 vixl32::Label* true_target,
1041 vixl32::Label* false_target) {
1042 HInstruction* cond = instruction->InputAt(condition_input_index);
1043
1044 if (true_target == nullptr && false_target == nullptr) {
1045 // Nothing to do. The code always falls through.
1046 return;
1047 } else if (cond->IsIntConstant()) {
1048 // Constant condition, statically compared against "true" (integer value 1).
1049 if (cond->AsIntConstant()->IsTrue()) {
1050 if (true_target != nullptr) {
1051 __ B(true_target);
1052 }
1053 } else {
1054 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
1055 if (false_target != nullptr) {
1056 __ B(false_target);
1057 }
1058 }
1059 return;
1060 }
1061
1062 // The following code generates these patterns:
1063 // (1) true_target == nullptr && false_target != nullptr
1064 // - opposite condition true => branch to false_target
1065 // (2) true_target != nullptr && false_target == nullptr
1066 // - condition true => branch to true_target
1067 // (3) true_target != nullptr && false_target != nullptr
1068 // - condition true => branch to true_target
1069 // - branch to false_target
1070 if (IsBooleanValueOrMaterializedCondition(cond)) {
1071 // Condition has been materialized, compare the output to 0.
1072 if (kIsDebugBuild) {
1073 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1074 DCHECK(cond_val.IsRegister());
1075 }
1076 if (true_target == nullptr) {
1077 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
1078 } else {
1079 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
1080 }
1081 } else {
1082 // Condition has not been materialized. Use its inputs as the comparison and
1083 // its condition as the branch condition.
1084 HCondition* condition = cond->AsCondition();
1085
1086 // If this is a long or FP comparison that has been folded into
1087 // the HCondition, generate the comparison directly.
1088 Primitive::Type type = condition->InputAt(0)->GetType();
1089 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1090 GenerateCompareTestAndBranch(condition, true_target, false_target);
1091 return;
1092 }
1093
1094 LocationSummary* locations = cond->GetLocations();
1095 DCHECK(locations->InAt(0).IsRegister());
1096 vixl32::Register left = InputRegisterAt(cond, 0);
1097 Location right = locations->InAt(1);
1098 if (right.IsRegister()) {
1099 __ Cmp(left, InputRegisterAt(cond, 1));
1100 } else {
1101 DCHECK(right.IsConstant());
1102 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1103 }
1104 if (true_target == nullptr) {
1105 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1106 } else {
1107 __ B(ARMCondition(condition->GetCondition()), true_target);
1108 }
1109 }
1110
1111 // If neither branch falls through (case 3), the conditional branch to `true_target`
1112 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1113 if (true_target != nullptr && false_target != nullptr) {
1114 __ B(false_target);
1115 }
1116}
1117
1118void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1119 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1120 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1121 locations->SetInAt(0, Location::RequiresRegister());
1122 }
1123}
1124
1125void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1126 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1127 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001128 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1129 nullptr : codegen_->GetLabelOf(true_successor);
1130 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1131 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001132 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1133}
1134
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001135void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
1136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1137 if (Primitive::IsFloatingPointType(select->GetType())) {
1138 locations->SetInAt(0, Location::RequiresFpuRegister());
1139 locations->SetInAt(1, Location::RequiresFpuRegister());
1140 } else {
1141 locations->SetInAt(0, Location::RequiresRegister());
1142 locations->SetInAt(1, Location::RequiresRegister());
1143 }
1144 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1145 locations->SetInAt(2, Location::RequiresRegister());
1146 }
1147 locations->SetOut(Location::SameAsFirstInput());
1148}
1149
1150void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
1151 LocationSummary* locations = select->GetLocations();
1152 vixl32::Label false_target;
1153 GenerateTestAndBranch(select,
1154 /* condition_input_index */ 2,
1155 /* true_target */ nullptr,
1156 &false_target);
1157 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1158 __ Bind(&false_target);
1159}
1160
Scott Wakelingfe885462016-09-22 10:24:38 +01001161void CodeGeneratorARMVIXL::GenerateNop() {
1162 __ Nop();
1163}
1164
1165void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
1166 LocationSummary* locations =
1167 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
1168 // Handle the long/FP comparisons made in instruction simplification.
1169 switch (cond->InputAt(0)->GetType()) {
1170 case Primitive::kPrimLong:
1171 locations->SetInAt(0, Location::RequiresRegister());
1172 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1173 if (!cond->IsEmittedAtUseSite()) {
1174 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1175 }
1176 break;
1177
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001178 // TODO(VIXL): https://android-review.googlesource.com/#/c/252265/
Scott Wakelingfe885462016-09-22 10:24:38 +01001179 case Primitive::kPrimFloat:
1180 case Primitive::kPrimDouble:
1181 locations->SetInAt(0, Location::RequiresFpuRegister());
1182 locations->SetInAt(1, Location::RequiresFpuRegister());
1183 if (!cond->IsEmittedAtUseSite()) {
1184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1185 }
1186 break;
1187
1188 default:
1189 locations->SetInAt(0, Location::RequiresRegister());
1190 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1191 if (!cond->IsEmittedAtUseSite()) {
1192 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1193 }
1194 }
1195}
1196
1197void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
1198 if (cond->IsEmittedAtUseSite()) {
1199 return;
1200 }
1201
Scott Wakelingfe885462016-09-22 10:24:38 +01001202 vixl32::Register out = OutputRegister(cond);
1203 vixl32::Label true_label, false_label;
1204
1205 switch (cond->InputAt(0)->GetType()) {
1206 default: {
1207 // Integer case.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001208 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
1209 AssemblerAccurateScope aas(GetVIXLAssembler(),
1210 kArmInstrMaxSizeInBytes * 3u,
1211 CodeBufferCheckScope::kMaximumSize);
1212 __ ite(ARMCondition(cond->GetCondition()));
1213 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
1214 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001215 return;
1216 }
1217 case Primitive::kPrimLong:
1218 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1219 break;
1220 case Primitive::kPrimFloat:
1221 case Primitive::kPrimDouble:
1222 GenerateVcmp(cond);
1223 GenerateFPJumps(cond, &true_label, &false_label);
1224 break;
1225 }
1226
1227 // Convert the jumps into the result.
1228 vixl32::Label done_label;
1229
1230 // False case: result = 0.
1231 __ Bind(&false_label);
1232 __ Mov(out, 0);
1233 __ B(&done_label);
1234
1235 // True case: result = 1.
1236 __ Bind(&true_label);
1237 __ Mov(out, 1);
1238 __ Bind(&done_label);
1239}
1240
1241void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
1242 HandleCondition(comp);
1243}
1244
1245void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
1246 HandleCondition(comp);
1247}
1248
1249void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
1250 HandleCondition(comp);
1251}
1252
1253void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
1254 HandleCondition(comp);
1255}
1256
1257void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
1258 HandleCondition(comp);
1259}
1260
1261void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
1262 HandleCondition(comp);
1263}
1264
1265void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1266 HandleCondition(comp);
1267}
1268
1269void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1270 HandleCondition(comp);
1271}
1272
1273void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1274 HandleCondition(comp);
1275}
1276
1277void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
1278 HandleCondition(comp);
1279}
1280
1281void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1282 HandleCondition(comp);
1283}
1284
1285void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1286 HandleCondition(comp);
1287}
1288
1289void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
1290 HandleCondition(comp);
1291}
1292
1293void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
1294 HandleCondition(comp);
1295}
1296
1297void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1298 HandleCondition(comp);
1299}
1300
1301void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
1302 HandleCondition(comp);
1303}
1304
1305void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
1306 HandleCondition(comp);
1307}
1308
1309void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
1310 HandleCondition(comp);
1311}
1312
1313void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1314 HandleCondition(comp);
1315}
1316
1317void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
1318 HandleCondition(comp);
1319}
1320
1321void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
1322 LocationSummary* locations =
1323 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1324 locations->SetOut(Location::ConstantLocation(constant));
1325}
1326
1327void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
1328 // Will be generated at use site.
1329}
1330
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001331void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
1332 LocationSummary* locations =
1333 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1334 locations->SetOut(Location::ConstantLocation(constant));
1335}
1336
1337void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
1338 // Will be generated at use site.
1339}
1340
Scott Wakelingfe885462016-09-22 10:24:38 +01001341void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
1342 LocationSummary* locations =
1343 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1344 locations->SetOut(Location::ConstantLocation(constant));
1345}
1346
1347void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
1348 // Will be generated at use site.
1349}
1350
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01001351void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
1352 LocationSummary* locations =
1353 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1354 locations->SetOut(Location::ConstantLocation(constant));
1355}
1356
1357void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
1358 // Will be generated at use site.
1359}
1360
1361void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
1362 LocationSummary* locations =
1363 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1364 locations->SetOut(Location::ConstantLocation(constant));
1365}
1366
1367void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
1368 // Will be generated at use site.
1369}
1370
Scott Wakelingfe885462016-09-22 10:24:38 +01001371void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1372 memory_barrier->SetLocations(nullptr);
1373}
1374
1375void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1376 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1377}
1378
1379void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
1380 ret->SetLocations(nullptr);
1381}
1382
1383void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
1384 codegen_->GenerateFrameExit();
1385}
1386
1387void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
1388 LocationSummary* locations =
1389 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
1390 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
1391}
1392
1393void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
1394 codegen_->GenerateFrameExit();
1395}
1396
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001397void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1398 // Explicit clinit checks triggered by static invokes must have been pruned by
1399 // art::PrepareForRegisterAllocation.
1400 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1401
1402 // TODO(VIXL): TryDispatch
1403
1404 HandleInvoke(invoke);
1405}
1406
1407void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1408 // Explicit clinit checks triggered by static invokes must have been pruned by
1409 // art::PrepareForRegisterAllocation.
1410 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1411
1412 // TODO(VIXL): TryGenerateIntrinsicCode
1413
1414 LocationSummary* locations = invoke->GetLocations();
1415 DCHECK(locations->HasTemps());
1416 codegen_->GenerateStaticOrDirectCall(invoke, locations->GetTemp(0));
1417 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1418 // previous instruction.
1419 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1420}
1421
1422void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
1423 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
1424 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
1425}
1426
1427void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1428 // TODO(VIXL): TryDispatch
1429
1430 HandleInvoke(invoke);
1431}
1432
1433void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1434 // TODO(VIXL): TryGenerateIntrinsicCode
1435
1436 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
1437 DCHECK(!codegen_->IsLeafMethod());
1438 // TODO(VIXL): If necessary, use a scope to ensure we record the pc info immediately after the
1439 // previous instruction.
1440 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1441}
1442
Artem Serov02109dd2016-09-23 17:17:54 +01001443void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
1444 LocationSummary* locations =
1445 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1446 switch (neg->GetResultType()) {
1447 case Primitive::kPrimInt: {
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1450 break;
1451 }
1452 case Primitive::kPrimLong: {
1453 locations->SetInAt(0, Location::RequiresRegister());
1454 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1455 break;
1456 }
1457
1458 case Primitive::kPrimFloat:
1459 case Primitive::kPrimDouble:
1460 locations->SetInAt(0, Location::RequiresFpuRegister());
1461 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1462 break;
1463
1464 default:
1465 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1466 }
1467}
1468
1469void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
1470 LocationSummary* locations = neg->GetLocations();
1471 Location out = locations->Out();
1472 Location in = locations->InAt(0);
1473 switch (neg->GetResultType()) {
1474 case Primitive::kPrimInt:
1475 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
1476 break;
1477
1478 case Primitive::kPrimLong:
1479 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1480 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
1481 // We cannot emit an RSC (Reverse Subtract with Carry)
1482 // instruction here, as it does not exist in the Thumb-2
1483 // instruction set. We use the following approach
1484 // using SBC and SUB instead.
1485 //
1486 // out.hi = -C
1487 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
1488 // out.hi = out.hi - in.hi
1489 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
1490 break;
1491
1492 case Primitive::kPrimFloat:
1493 case Primitive::kPrimDouble:
1494 __ Vneg(OutputVRegister(neg), InputVRegisterAt(neg, 0));
1495 break;
1496
1497 default:
1498 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1499 }
1500}
1501
Scott Wakelingfe885462016-09-22 10:24:38 +01001502void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1503 Primitive::Type result_type = conversion->GetResultType();
1504 Primitive::Type input_type = conversion->GetInputType();
1505 DCHECK_NE(result_type, input_type);
1506
1507 // The float-to-long, double-to-long and long-to-float type conversions
1508 // rely on a call to the runtime.
1509 LocationSummary::CallKind call_kind =
1510 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1511 && result_type == Primitive::kPrimLong)
1512 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
1513 ? LocationSummary::kCallOnMainOnly
1514 : LocationSummary::kNoCall;
1515 LocationSummary* locations =
1516 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1517
1518 // The Java language does not allow treating boolean as an integral type but
1519 // our bit representation makes it safe.
1520
1521 switch (result_type) {
1522 case Primitive::kPrimByte:
1523 switch (input_type) {
1524 case Primitive::kPrimLong:
1525 // Type conversion from long to byte is a result of code transformations.
1526 case Primitive::kPrimBoolean:
1527 // Boolean input is a result of code transformations.
1528 case Primitive::kPrimShort:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimChar:
1531 // Processing a Dex `int-to-byte' instruction.
1532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540 break;
1541
1542 case Primitive::kPrimShort:
1543 switch (input_type) {
1544 case Primitive::kPrimLong:
1545 // Type conversion from long to short is a result of code transformations.
1546 case Primitive::kPrimBoolean:
1547 // Boolean input is a result of code transformations.
1548 case Primitive::kPrimByte:
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimChar:
1551 // Processing a Dex `int-to-short' instruction.
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1554 break;
1555
1556 default:
1557 LOG(FATAL) << "Unexpected type conversion from " << input_type
1558 << " to " << result_type;
1559 }
1560 break;
1561
1562 case Primitive::kPrimInt:
1563 switch (input_type) {
1564 case Primitive::kPrimLong:
1565 // Processing a Dex `long-to-int' instruction.
1566 locations->SetInAt(0, Location::Any());
1567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1568 break;
1569
1570 case Primitive::kPrimFloat:
1571 // Processing a Dex `float-to-int' instruction.
1572 locations->SetInAt(0, Location::RequiresFpuRegister());
1573 locations->SetOut(Location::RequiresRegister());
1574 locations->AddTemp(Location::RequiresFpuRegister());
1575 break;
1576
1577 case Primitive::kPrimDouble:
1578 // Processing a Dex `double-to-int' instruction.
1579 locations->SetInAt(0, Location::RequiresFpuRegister());
1580 locations->SetOut(Location::RequiresRegister());
1581 locations->AddTemp(Location::RequiresFpuRegister());
1582 break;
1583
1584 default:
1585 LOG(FATAL) << "Unexpected type conversion from " << input_type
1586 << " to " << result_type;
1587 }
1588 break;
1589
1590 case Primitive::kPrimLong:
1591 switch (input_type) {
1592 case Primitive::kPrimBoolean:
1593 // Boolean input is a result of code transformations.
1594 case Primitive::kPrimByte:
1595 case Primitive::kPrimShort:
1596 case Primitive::kPrimInt:
1597 case Primitive::kPrimChar:
1598 // Processing a Dex `int-to-long' instruction.
1599 locations->SetInAt(0, Location::RequiresRegister());
1600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1601 break;
1602
1603 case Primitive::kPrimFloat: {
1604 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1606 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1607 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001608 break;
1609 }
1610
1611 case Primitive::kPrimDouble: {
1612 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001613 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1614 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
1615 calling_convention.GetFpuRegisterAt(1)));
1616 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001617 break;
1618 }
1619
1620 default:
1621 LOG(FATAL) << "Unexpected type conversion from " << input_type
1622 << " to " << result_type;
1623 }
1624 break;
1625
1626 case Primitive::kPrimChar:
1627 switch (input_type) {
1628 case Primitive::kPrimLong:
1629 // Type conversion from long to char is a result of code transformations.
1630 case Primitive::kPrimBoolean:
1631 // Boolean input is a result of code transformations.
1632 case Primitive::kPrimByte:
1633 case Primitive::kPrimShort:
1634 case Primitive::kPrimInt:
1635 // Processing a Dex `int-to-char' instruction.
1636 locations->SetInAt(0, Location::RequiresRegister());
1637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1638 break;
1639
1640 default:
1641 LOG(FATAL) << "Unexpected type conversion from " << input_type
1642 << " to " << result_type;
1643 }
1644 break;
1645
1646 case Primitive::kPrimFloat:
1647 switch (input_type) {
1648 case Primitive::kPrimBoolean:
1649 // Boolean input is a result of code transformations.
1650 case Primitive::kPrimByte:
1651 case Primitive::kPrimShort:
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimChar:
1654 // Processing a Dex `int-to-float' instruction.
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 locations->SetOut(Location::RequiresFpuRegister());
1657 break;
1658
1659 case Primitive::kPrimLong: {
1660 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001661 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1662 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
1663 calling_convention.GetRegisterAt(1)));
1664 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001665 break;
1666 }
1667
1668 case Primitive::kPrimDouble:
1669 // Processing a Dex `double-to-float' instruction.
1670 locations->SetInAt(0, Location::RequiresFpuRegister());
1671 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1672 break;
1673
1674 default:
1675 LOG(FATAL) << "Unexpected type conversion from " << input_type
1676 << " to " << result_type;
1677 };
1678 break;
1679
1680 case Primitive::kPrimDouble:
1681 switch (input_type) {
1682 case Primitive::kPrimBoolean:
1683 // Boolean input is a result of code transformations.
1684 case Primitive::kPrimByte:
1685 case Primitive::kPrimShort:
1686 case Primitive::kPrimInt:
1687 case Primitive::kPrimChar:
1688 // Processing a Dex `int-to-double' instruction.
1689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetOut(Location::RequiresFpuRegister());
1691 break;
1692
1693 case Primitive::kPrimLong:
1694 // Processing a Dex `long-to-double' instruction.
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetOut(Location::RequiresFpuRegister());
1697 locations->AddTemp(Location::RequiresFpuRegister());
1698 locations->AddTemp(Location::RequiresFpuRegister());
1699 break;
1700
1701 case Primitive::kPrimFloat:
1702 // Processing a Dex `float-to-double' instruction.
1703 locations->SetInAt(0, Location::RequiresFpuRegister());
1704 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1705 break;
1706
1707 default:
1708 LOG(FATAL) << "Unexpected type conversion from " << input_type
1709 << " to " << result_type;
1710 };
1711 break;
1712
1713 default:
1714 LOG(FATAL) << "Unexpected type conversion from " << input_type
1715 << " to " << result_type;
1716 }
1717}
1718
1719void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
1720 LocationSummary* locations = conversion->GetLocations();
1721 Location out = locations->Out();
1722 Location in = locations->InAt(0);
1723 Primitive::Type result_type = conversion->GetResultType();
1724 Primitive::Type input_type = conversion->GetInputType();
1725 DCHECK_NE(result_type, input_type);
1726 switch (result_type) {
1727 case Primitive::kPrimByte:
1728 switch (input_type) {
1729 case Primitive::kPrimLong:
1730 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001731 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01001732 break;
1733 case Primitive::kPrimBoolean:
1734 // Boolean input is a result of code transformations.
1735 case Primitive::kPrimShort:
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimChar:
1738 // Processing a Dex `int-to-byte' instruction.
1739 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
1740 break;
1741
1742 default:
1743 LOG(FATAL) << "Unexpected type conversion from " << input_type
1744 << " to " << result_type;
1745 }
1746 break;
1747
1748 case Primitive::kPrimShort:
1749 switch (input_type) {
1750 case Primitive::kPrimLong:
1751 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001752 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01001753 break;
1754 case Primitive::kPrimBoolean:
1755 // Boolean input is a result of code transformations.
1756 case Primitive::kPrimByte:
1757 case Primitive::kPrimInt:
1758 case Primitive::kPrimChar:
1759 // Processing a Dex `int-to-short' instruction.
1760 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
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::kPrimInt:
1770 switch (input_type) {
1771 case Primitive::kPrimLong:
1772 // Processing a Dex `long-to-int' instruction.
1773 DCHECK(out.IsRegister());
1774 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001775 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01001776 } else if (in.IsDoubleStackSlot()) {
1777 GetAssembler()->LoadFromOffset(kLoadWord,
1778 OutputRegister(conversion),
1779 sp,
1780 in.GetStackIndex());
1781 } else {
1782 DCHECK(in.IsConstant());
1783 DCHECK(in.GetConstant()->IsLongConstant());
1784 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1785 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
1786 }
1787 break;
1788
1789 case Primitive::kPrimFloat: {
1790 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001791 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001792 __ Vcvt(I32, F32, temp, InputSRegisterAt(conversion, 0));
1793 __ Vmov(OutputRegister(conversion), temp);
1794 break;
1795 }
1796
1797 case Primitive::kPrimDouble: {
1798 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001799 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
1800 __ Vcvt(I32, F64, temp_s, FromLowSToD(LowSRegisterFrom(in)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001801 __ Vmov(OutputRegister(conversion), temp_s);
1802 break;
1803 }
1804
1805 default:
1806 LOG(FATAL) << "Unexpected type conversion from " << input_type
1807 << " to " << result_type;
1808 }
1809 break;
1810
1811 case Primitive::kPrimLong:
1812 switch (input_type) {
1813 case Primitive::kPrimBoolean:
1814 // Boolean input is a result of code transformations.
1815 case Primitive::kPrimByte:
1816 case Primitive::kPrimShort:
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimChar:
1819 // Processing a Dex `int-to-long' instruction.
1820 DCHECK(out.IsRegisterPair());
1821 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001822 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001823 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001824 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01001825 break;
1826
1827 case Primitive::kPrimFloat:
1828 // Processing a Dex `float-to-long' instruction.
1829 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
1830 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
1831 break;
1832
1833 case Primitive::kPrimDouble:
1834 // Processing a Dex `double-to-long' instruction.
1835 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
1836 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
1837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unexpected type conversion from " << input_type
1841 << " to " << result_type;
1842 }
1843 break;
1844
1845 case Primitive::kPrimChar:
1846 switch (input_type) {
1847 case Primitive::kPrimLong:
1848 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001849 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01001850 break;
1851 case Primitive::kPrimBoolean:
1852 // Boolean input is a result of code transformations.
1853 case Primitive::kPrimByte:
1854 case Primitive::kPrimShort:
1855 case Primitive::kPrimInt:
1856 // Processing a Dex `int-to-char' instruction.
1857 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
1858 break;
1859
1860 default:
1861 LOG(FATAL) << "Unexpected type conversion from " << input_type
1862 << " to " << result_type;
1863 }
1864 break;
1865
1866 case Primitive::kPrimFloat:
1867 switch (input_type) {
1868 case Primitive::kPrimBoolean:
1869 // Boolean input is a result of code transformations.
1870 case Primitive::kPrimByte:
1871 case Primitive::kPrimShort:
1872 case Primitive::kPrimInt:
1873 case Primitive::kPrimChar: {
1874 // Processing a Dex `int-to-float' instruction.
1875 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
1876 __ Vcvt(F32, I32, OutputSRegister(conversion), OutputSRegister(conversion));
1877 break;
1878 }
1879
1880 case Primitive::kPrimLong:
1881 // Processing a Dex `long-to-float' instruction.
1882 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
1883 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
1884 break;
1885
1886 case Primitive::kPrimDouble:
1887 // Processing a Dex `double-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001888 __ Vcvt(F32, F64, OutputSRegister(conversion), FromLowSToD(LowSRegisterFrom(in)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001889 break;
1890
1891 default:
1892 LOG(FATAL) << "Unexpected type conversion from " << input_type
1893 << " to " << result_type;
1894 };
1895 break;
1896
1897 case Primitive::kPrimDouble:
1898 switch (input_type) {
1899 case Primitive::kPrimBoolean:
1900 // Boolean input is a result of code transformations.
1901 case Primitive::kPrimByte:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimChar: {
1905 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001906 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
1907 __ Vcvt(F64, I32, FromLowSToD(LowSRegisterFrom(out)), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01001908 break;
1909 }
1910
1911 case Primitive::kPrimLong: {
1912 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001913 vixl32::Register low = LowRegisterFrom(in);
1914 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelingfe885462016-09-22 10:24:38 +01001915
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001916 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingfe885462016-09-22 10:24:38 +01001917 vixl32::DRegister out_d = FromLowSToD(out_s);
1918
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001919 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001920 vixl32::DRegister temp_d = FromLowSToD(temp_s);
1921
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001922 vixl32::SRegister constant_s = LowSRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001923 vixl32::DRegister constant_d = FromLowSToD(constant_s);
1924
1925 // temp_d = int-to-double(high)
1926 __ Vmov(temp_s, high);
1927 __ Vcvt(F64, I32, temp_d, temp_s);
1928 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001929 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01001930 // out_d = unsigned-to-double(low)
1931 __ Vmov(out_s, low);
1932 __ Vcvt(F64, U32, out_d, out_s);
1933 // out_d += temp_d * constant_d
1934 __ Vmla(F64, out_d, temp_d, constant_d);
1935 break;
1936 }
1937
1938 case Primitive::kPrimFloat:
1939 // Processing a Dex `float-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001940 __ Vcvt(F64, F32, FromLowSToD(LowSRegisterFrom(out)), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01001941 break;
1942
1943 default:
1944 LOG(FATAL) << "Unexpected type conversion from " << input_type
1945 << " to " << result_type;
1946 };
1947 break;
1948
1949 default:
1950 LOG(FATAL) << "Unexpected type conversion from " << input_type
1951 << " to " << result_type;
1952 }
1953}
1954
1955void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
1956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
1958 switch (add->GetResultType()) {
1959 case Primitive::kPrimInt: {
1960 locations->SetInAt(0, Location::RequiresRegister());
1961 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1963 break;
1964 }
1965
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001966 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01001967 case Primitive::kPrimLong: {
1968 locations->SetInAt(0, Location::RequiresRegister());
1969 locations->SetInAt(1, Location::RequiresRegister());
1970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1971 break;
1972 }
1973
1974 case Primitive::kPrimFloat:
1975 case Primitive::kPrimDouble: {
1976 locations->SetInAt(0, Location::RequiresFpuRegister());
1977 locations->SetInAt(1, Location::RequiresFpuRegister());
1978 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1979 break;
1980 }
1981
1982 default:
1983 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1984 }
1985}
1986
1987void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
1988 LocationSummary* locations = add->GetLocations();
1989 Location out = locations->Out();
1990 Location first = locations->InAt(0);
1991 Location second = locations->InAt(1);
1992
1993 switch (add->GetResultType()) {
1994 case Primitive::kPrimInt: {
1995 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
1996 }
1997 break;
1998
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001999 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002000 case Primitive::kPrimLong: {
2001 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002002 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2003 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002004 break;
2005 }
2006
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002007 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01002008 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002009 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002010 break;
2011
2012 default:
2013 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2014 }
2015}
2016
2017void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
2018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
2020 switch (sub->GetResultType()) {
2021 case Primitive::kPrimInt: {
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
2024 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2025 break;
2026 }
2027
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002028 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002029 case Primitive::kPrimLong: {
2030 locations->SetInAt(0, Location::RequiresRegister());
2031 locations->SetInAt(1, Location::RequiresRegister());
2032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2033 break;
2034 }
2035 case Primitive::kPrimFloat:
2036 case Primitive::kPrimDouble: {
2037 locations->SetInAt(0, Location::RequiresFpuRegister());
2038 locations->SetInAt(1, Location::RequiresFpuRegister());
2039 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2040 break;
2041 }
2042 default:
2043 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2044 }
2045}
2046
2047void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
2048 LocationSummary* locations = sub->GetLocations();
2049 Location out = locations->Out();
2050 Location first = locations->InAt(0);
2051 Location second = locations->InAt(1);
2052 switch (sub->GetResultType()) {
2053 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002054 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002055 break;
2056 }
2057
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002058 // TODO(VIXL): https://android-review.googlesource.com/#/c/254144/
Scott Wakelingfe885462016-09-22 10:24:38 +01002059 case Primitive::kPrimLong: {
2060 DCHECK(second.IsRegisterPair());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002061 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
2062 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
Scott Wakelingfe885462016-09-22 10:24:38 +01002063 break;
2064 }
2065
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002066 case Primitive::kPrimFloat:
2067 case Primitive::kPrimDouble:
2068 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002069 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002070
2071 default:
2072 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2073 }
2074}
2075
2076void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
2077 LocationSummary* locations =
2078 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2079 switch (mul->GetResultType()) {
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimLong: {
2082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetInAt(1, Location::RequiresRegister());
2084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2085 break;
2086 }
2087
2088 case Primitive::kPrimFloat:
2089 case Primitive::kPrimDouble: {
2090 locations->SetInAt(0, Location::RequiresFpuRegister());
2091 locations->SetInAt(1, Location::RequiresFpuRegister());
2092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2093 break;
2094 }
2095
2096 default:
2097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2098 }
2099}
2100
2101void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
2102 LocationSummary* locations = mul->GetLocations();
2103 Location out = locations->Out();
2104 Location first = locations->InAt(0);
2105 Location second = locations->InAt(1);
2106 switch (mul->GetResultType()) {
2107 case Primitive::kPrimInt: {
2108 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2109 break;
2110 }
2111 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002112 vixl32::Register out_hi = HighRegisterFrom(out);
2113 vixl32::Register out_lo = LowRegisterFrom(out);
2114 vixl32::Register in1_hi = HighRegisterFrom(first);
2115 vixl32::Register in1_lo = LowRegisterFrom(first);
2116 vixl32::Register in2_hi = HighRegisterFrom(second);
2117 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01002118
2119 // Extra checks to protect caused by the existence of R1_R2.
2120 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2121 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2122 DCHECK_NE(out_hi.GetCode(), in1_lo.GetCode());
2123 DCHECK_NE(out_hi.GetCode(), in2_lo.GetCode());
2124
2125 // input: in1 - 64 bits, in2 - 64 bits
2126 // output: out
2127 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2128 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2129 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2130
2131 UseScratchRegisterScope temps(GetVIXLAssembler());
2132 vixl32::Register temp = temps.Acquire();
2133 // temp <- in1.lo * in2.hi
2134 __ Mul(temp, in1_lo, in2_hi);
2135 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2136 __ Mla(out_hi, in1_hi, in2_lo, temp);
2137 // out.lo <- (in1.lo * in2.lo)[31:0];
2138 __ Umull(out_lo, temp, in1_lo, in2_lo);
2139 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002140 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002141 break;
2142 }
2143
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002144 case Primitive::kPrimFloat:
2145 case Primitive::kPrimDouble:
2146 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002147 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002148
2149 default:
2150 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2151 }
2152}
2153
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002154void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
2155 LocationSummary* locations =
2156 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
2157 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2158 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2159 locations->SetOut(LocationFrom(r0));
2160 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2161 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
2162}
2163
2164void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
2165 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2166 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
2167 // Note: if heap poisoning is enabled, the entry point takes cares
2168 // of poisoning the reference.
2169 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
2170 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
2171}
2172
Artem Serov02109dd2016-09-23 17:17:54 +01002173void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
2174 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2175
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2178
2179 switch (op->GetResultType()) {
2180 case Primitive::kPrimInt: {
2181 locations->SetInAt(0, Location::RequiresRegister());
2182 if (op->InputAt(1)->IsConstant()) {
2183 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2185 } else {
2186 locations->SetInAt(1, Location::RequiresRegister());
2187 // Make the output overlap, as it will be used to hold the masked
2188 // second input.
2189 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2190 }
2191 break;
2192 }
2193 case Primitive::kPrimLong: {
2194 locations->SetInAt(0, Location::RequiresRegister());
2195 if (op->InputAt(1)->IsConstant()) {
2196 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2197 // For simplicity, use kOutputOverlap even though we only require that low registers
2198 // don't clash with high registers which the register allocator currently guarantees.
2199 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2200 } else {
2201 locations->SetInAt(1, Location::RequiresRegister());
2202 locations->AddTemp(Location::RequiresRegister());
2203 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2204 }
2205 break;
2206 }
2207 default:
2208 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2209 }
2210}
2211
2212void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
2213 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2214
2215 LocationSummary* locations = op->GetLocations();
2216 Location out = locations->Out();
2217 Location first = locations->InAt(0);
2218 Location second = locations->InAt(1);
2219
2220 Primitive::Type type = op->GetResultType();
2221 switch (type) {
2222 case Primitive::kPrimInt: {
2223 vixl32::Register out_reg = OutputRegister(op);
2224 vixl32::Register first_reg = InputRegisterAt(op, 0);
2225 if (second.IsRegister()) {
2226 vixl32::Register second_reg = RegisterFrom(second);
2227 // ARM doesn't mask the shift count so we need to do it ourselves.
2228 __ And(out_reg, second_reg, kMaxIntShiftDistance);
2229 if (op->IsShl()) {
2230 __ Lsl(out_reg, first_reg, out_reg);
2231 } else if (op->IsShr()) {
2232 __ Asr(out_reg, first_reg, out_reg);
2233 } else {
2234 __ Lsr(out_reg, first_reg, out_reg);
2235 }
2236 } else {
2237 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2238 uint32_t shift_value = cst & kMaxIntShiftDistance;
2239 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
2240 __ Mov(out_reg, first_reg);
2241 } else if (op->IsShl()) {
2242 __ Lsl(out_reg, first_reg, shift_value);
2243 } else if (op->IsShr()) {
2244 __ Asr(out_reg, first_reg, shift_value);
2245 } else {
2246 __ Lsr(out_reg, first_reg, shift_value);
2247 }
2248 }
2249 break;
2250 }
2251 case Primitive::kPrimLong: {
2252 vixl32::Register o_h = HighRegisterFrom(out);
2253 vixl32::Register o_l = LowRegisterFrom(out);
2254
2255 vixl32::Register high = HighRegisterFrom(first);
2256 vixl32::Register low = LowRegisterFrom(first);
2257
2258 if (second.IsRegister()) {
2259 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2260
2261 vixl32::Register second_reg = RegisterFrom(second);
2262
2263 if (op->IsShl()) {
2264 __ And(o_l, second_reg, kMaxLongShiftDistance);
2265 // Shift the high part
2266 __ Lsl(o_h, high, o_l);
2267 // Shift the low part and `or` what overflew on the high part
2268 __ Rsb(temp, o_l, kArmBitsPerWord);
2269 __ Lsr(temp, low, temp);
2270 __ Orr(o_h, o_h, temp);
2271 // If the shift is > 32 bits, override the high part
2272 __ Subs(temp, o_l, kArmBitsPerWord);
2273 {
2274 AssemblerAccurateScope guard(GetVIXLAssembler(),
2275 3 * kArmInstrMaxSizeInBytes,
2276 CodeBufferCheckScope::kMaximumSize);
2277 __ it(pl);
2278 __ lsl(pl, o_h, low, temp);
2279 }
2280 // Shift the low part
2281 __ Lsl(o_l, low, o_l);
2282 } else if (op->IsShr()) {
2283 __ And(o_h, second_reg, kMaxLongShiftDistance);
2284 // Shift the low part
2285 __ Lsr(o_l, low, o_h);
2286 // Shift the high part and `or` what underflew on the low part
2287 __ Rsb(temp, o_h, kArmBitsPerWord);
2288 __ Lsl(temp, high, temp);
2289 __ Orr(o_l, o_l, temp);
2290 // If the shift is > 32 bits, override the low part
2291 __ Subs(temp, o_h, kArmBitsPerWord);
2292 {
2293 AssemblerAccurateScope guard(GetVIXLAssembler(),
2294 3 * kArmInstrMaxSizeInBytes,
2295 CodeBufferCheckScope::kMaximumSize);
2296 __ it(pl);
2297 __ asr(pl, o_l, high, temp);
2298 }
2299 // Shift the high part
2300 __ Asr(o_h, high, o_h);
2301 } else {
2302 __ And(o_h, second_reg, kMaxLongShiftDistance);
2303 // same as Shr except we use `Lsr`s and not `Asr`s
2304 __ Lsr(o_l, low, o_h);
2305 __ Rsb(temp, o_h, kArmBitsPerWord);
2306 __ Lsl(temp, high, temp);
2307 __ Orr(o_l, o_l, temp);
2308 __ Subs(temp, o_h, kArmBitsPerWord);
2309 {
2310 AssemblerAccurateScope guard(GetVIXLAssembler(),
2311 3 * kArmInstrMaxSizeInBytes,
2312 CodeBufferCheckScope::kMaximumSize);
2313 __ it(pl);
2314 __ lsr(pl, o_l, high, temp);
2315 }
2316 __ Lsr(o_h, high, o_h);
2317 }
2318 } else {
2319 // Register allocator doesn't create partial overlap.
2320 DCHECK(!o_l.Is(high));
2321 DCHECK(!o_h.Is(low));
2322 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2323 uint32_t shift_value = cst & kMaxLongShiftDistance;
2324 if (shift_value > 32) {
2325 if (op->IsShl()) {
2326 __ Lsl(o_h, low, shift_value - 32);
2327 __ Mov(o_l, 0);
2328 } else if (op->IsShr()) {
2329 __ Asr(o_l, high, shift_value - 32);
2330 __ Asr(o_h, high, 31);
2331 } else {
2332 __ Lsr(o_l, high, shift_value - 32);
2333 __ Mov(o_h, 0);
2334 }
2335 } else if (shift_value == 32) {
2336 if (op->IsShl()) {
2337 __ Mov(o_h, low);
2338 __ Mov(o_l, 0);
2339 } else if (op->IsShr()) {
2340 __ Mov(o_l, high);
2341 __ Asr(o_h, high, 31);
2342 } else {
2343 __ Mov(o_l, high);
2344 __ Mov(o_h, 0);
2345 }
2346 } else if (shift_value == 1) {
2347 if (op->IsShl()) {
2348 __ Lsls(o_l, low, 1);
2349 __ Adc(o_h, high, high);
2350 } else if (op->IsShr()) {
2351 __ Asrs(o_h, high, 1);
2352 __ Rrx(o_l, low);
2353 } else {
2354 __ Lsrs(o_h, high, 1);
2355 __ Rrx(o_l, low);
2356 }
2357 } else {
2358 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
2359 if (op->IsShl()) {
2360 __ Lsl(o_h, high, shift_value);
2361 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
2362 __ Lsl(o_l, low, shift_value);
2363 } else if (op->IsShr()) {
2364 __ Lsr(o_l, low, shift_value);
2365 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
2366 __ Asr(o_h, high, shift_value);
2367 } else {
2368 __ Lsr(o_l, low, shift_value);
2369 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
2370 __ Lsr(o_h, high, shift_value);
2371 }
2372 }
2373 }
2374 break;
2375 }
2376 default:
2377 LOG(FATAL) << "Unexpected operation type " << type;
2378 UNREACHABLE();
2379 }
2380}
2381
2382void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
2383 HandleShift(shl);
2384}
2385
2386void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
2387 HandleShift(shl);
2388}
2389
2390void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
2391 HandleShift(shr);
2392}
2393
2394void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
2395 HandleShift(shr);
2396}
2397
2398void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
2399 HandleShift(ushr);
2400}
2401
2402void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
2403 HandleShift(ushr);
2404}
2405
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002406void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
2407 LocationSummary* locations =
2408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
2409 if (instruction->IsStringAlloc()) {
2410 locations->AddTemp(LocationFrom(kMethodRegister));
2411 } else {
2412 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2413 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2414 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2415 }
2416 locations->SetOut(LocationFrom(r0));
2417}
2418
2419void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
2420 // Note: if heap poisoning is enabled, the entry point takes cares
2421 // of poisoning the reference.
2422 if (instruction->IsStringAlloc()) {
2423 // String is allocated through StringFactory. Call NewEmptyString entry point.
2424 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
2425 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
2426 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
2427 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
2428 AssemblerAccurateScope aas(GetVIXLAssembler(),
2429 kArmInstrMaxSizeInBytes,
2430 CodeBufferCheckScope::kMaximumSize);
2431 __ blx(lr);
2432 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2433 } else {
2434 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
2435 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
2436 }
2437}
2438
2439void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
2440 LocationSummary* locations =
2441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2442 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2443 if (location.IsStackSlot()) {
2444 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2445 } else if (location.IsDoubleStackSlot()) {
2446 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2447 }
2448 locations->SetOut(location);
2449}
2450
2451void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
2452 HParameterValue* instruction ATTRIBUTE_UNUSED) {
2453 // Nothing to do, the parameter is already at its location.
2454}
2455
2456void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
2457 LocationSummary* locations =
2458 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2459 locations->SetOut(LocationFrom(kMethodRegister));
2460}
2461
2462void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
2463 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2464 // Nothing to do, the method is already at its location.
2465}
2466
Scott Wakelingfe885462016-09-22 10:24:38 +01002467void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
2468 LocationSummary* locations =
2469 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
2470 locations->SetInAt(0, Location::RequiresRegister());
2471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2472}
2473
2474void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
2475 LocationSummary* locations = not_->GetLocations();
2476 Location out = locations->Out();
2477 Location in = locations->InAt(0);
2478 switch (not_->GetResultType()) {
2479 case Primitive::kPrimInt:
2480 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
2481 break;
2482
2483 case Primitive::kPrimLong:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002484 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
2485 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002486 break;
2487
2488 default:
2489 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2490 }
2491}
2492
Alexandre Rames9c19bd62016-10-24 11:50:32 +01002493void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
2494 LocationSummary* locations =
2495 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2496 switch (compare->InputAt(0)->GetType()) {
2497 case Primitive::kPrimBoolean:
2498 case Primitive::kPrimByte:
2499 case Primitive::kPrimShort:
2500 case Primitive::kPrimChar:
2501 case Primitive::kPrimInt:
2502 case Primitive::kPrimLong: {
2503 locations->SetInAt(0, Location::RequiresRegister());
2504 locations->SetInAt(1, Location::RequiresRegister());
2505 // Output overlaps because it is written before doing the low comparison.
2506 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2507 break;
2508 }
2509 case Primitive::kPrimFloat:
2510 case Primitive::kPrimDouble: {
2511 locations->SetInAt(0, Location::RequiresFpuRegister());
2512 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
2513 locations->SetOut(Location::RequiresRegister());
2514 break;
2515 }
2516 default:
2517 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2518 }
2519}
2520
2521void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
2522 LocationSummary* locations = compare->GetLocations();
2523 vixl32::Register out = OutputRegister(compare);
2524 Location left = locations->InAt(0);
2525 Location right = locations->InAt(1);
2526
2527 vixl32::Label less, greater, done;
2528 Primitive::Type type = compare->InputAt(0)->GetType();
2529 vixl32::Condition less_cond = vixl32::Condition(kNone);
2530 switch (type) {
2531 case Primitive::kPrimBoolean:
2532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimChar:
2535 case Primitive::kPrimInt: {
2536 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
2537 __ Mov(out, 0);
2538 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
2539 less_cond = lt;
2540 break;
2541 }
2542 case Primitive::kPrimLong: {
2543 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
2544 __ B(lt, &less);
2545 __ B(gt, &greater);
2546 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
2547 __ Mov(out, 0);
2548 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
2549 less_cond = lo;
2550 break;
2551 }
2552 case Primitive::kPrimFloat:
2553 case Primitive::kPrimDouble: {
2554 __ Mov(out, 0);
2555 GenerateVcmp(compare);
2556 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
2557 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2558 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
2559 break;
2560 }
2561 default:
2562 LOG(FATAL) << "Unexpected compare type " << type;
2563 UNREACHABLE();
2564 }
2565
2566 __ B(eq, &done);
2567 __ B(less_cond, &less);
2568
2569 __ Bind(&greater);
2570 __ Mov(out, 1);
2571 __ B(&done);
2572
2573 __ Bind(&less);
2574 __ Mov(out, -1);
2575
2576 __ Bind(&done);
2577}
2578
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002579void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
2580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2582 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
2583 locations->SetInAt(i, Location::Any());
2584 }
2585 locations->SetOut(Location::Any());
2586}
2587
2588void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
2589 LOG(FATAL) << "Unreachable";
2590}
2591
Scott Wakelingfe885462016-09-22 10:24:38 +01002592void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
2593 // TODO (ported from quick): revisit ARM barrier kinds.
2594 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
2595 switch (kind) {
2596 case MemBarrierKind::kAnyStore:
2597 case MemBarrierKind::kLoadAny:
2598 case MemBarrierKind::kAnyAny: {
2599 flavor = DmbOptions::ISH;
2600 break;
2601 }
2602 case MemBarrierKind::kStoreStore: {
2603 flavor = DmbOptions::ISHST;
2604 break;
2605 }
2606 default:
2607 LOG(FATAL) << "Unexpected memory barrier " << kind;
2608 }
2609 __ Dmb(flavor);
2610}
2611
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002612void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
2613 uint32_t offset,
2614 vixl32::Register out_lo,
2615 vixl32::Register out_hi) {
2616 UseScratchRegisterScope temps(GetVIXLAssembler());
2617 if (offset != 0) {
2618 vixl32::Register temp = temps.Acquire();
2619 __ Add(temp, addr, offset);
2620 addr = temp;
2621 }
2622 __ Ldrexd(out_lo, out_hi, addr);
2623}
2624
2625void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
2626 uint32_t offset,
2627 vixl32::Register value_lo,
2628 vixl32::Register value_hi,
2629 vixl32::Register temp1,
2630 vixl32::Register temp2,
2631 HInstruction* instruction) {
2632 UseScratchRegisterScope temps(GetVIXLAssembler());
2633 vixl32::Label fail;
2634 if (offset != 0) {
2635 vixl32::Register temp = temps.Acquire();
2636 __ Add(temp, addr, offset);
2637 addr = temp;
2638 }
2639 __ Bind(&fail);
2640 // We need a load followed by store. (The address used in a STREX instruction must
2641 // be the same as the address in the most recently executed LDREX instruction.)
2642 __ Ldrexd(temp1, temp2, addr);
2643 codegen_->MaybeRecordImplicitNullCheck(instruction);
2644 __ Strexd(temp1, value_lo, value_hi, addr);
2645 __ Cbnz(temp1, &fail);
2646}
2647
Scott Wakelingfe885462016-09-22 10:24:38 +01002648void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2649 DCHECK(instruction->IsDiv() || instruction->IsRem());
2650 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2651
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002652 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002653 DCHECK(second.IsConstant());
2654
2655 vixl32::Register out = OutputRegister(instruction);
2656 vixl32::Register dividend = InputRegisterAt(instruction, 0);
2657 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2658 DCHECK(imm == 1 || imm == -1);
2659
2660 if (instruction->IsRem()) {
2661 __ Mov(out, 0);
2662 } else {
2663 if (imm == 1) {
2664 __ Mov(out, dividend);
2665 } else {
2666 __ Rsb(out, dividend, 0);
2667 }
2668 }
2669}
2670
2671void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2672 DCHECK(instruction->IsDiv() || instruction->IsRem());
2673 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2674
2675 LocationSummary* locations = instruction->GetLocations();
2676 Location second = locations->InAt(1);
2677 DCHECK(second.IsConstant());
2678
2679 vixl32::Register out = OutputRegister(instruction);
2680 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002681 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002682 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2683 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
2684 int ctz_imm = CTZ(abs_imm);
2685
2686 if (ctz_imm == 1) {
2687 __ Lsr(temp, dividend, 32 - ctz_imm);
2688 } else {
2689 __ Asr(temp, dividend, 31);
2690 __ Lsr(temp, temp, 32 - ctz_imm);
2691 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002692 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002693
2694 if (instruction->IsDiv()) {
2695 __ Asr(out, out, ctz_imm);
2696 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002697 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002698 }
2699 } else {
2700 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002701 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01002702 }
2703}
2704
2705void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2706 DCHECK(instruction->IsDiv() || instruction->IsRem());
2707 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2708
2709 LocationSummary* locations = instruction->GetLocations();
2710 Location second = locations->InAt(1);
2711 DCHECK(second.IsConstant());
2712
2713 vixl32::Register out = OutputRegister(instruction);
2714 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002715 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
2716 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002717 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2718
2719 int64_t magic;
2720 int shift;
2721 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2722
2723 __ Mov(temp1, magic);
2724 __ Smull(temp2, temp1, dividend, temp1);
2725
2726 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002727 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002728 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002729 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01002730 }
2731
2732 if (shift != 0) {
2733 __ Asr(temp1, temp1, shift);
2734 }
2735
2736 if (instruction->IsDiv()) {
2737 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2738 } else {
2739 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
2740 // TODO: Strength reduction for mls.
2741 __ Mov(temp2, imm);
2742 __ Mls(out, temp1, temp2, dividend);
2743 }
2744}
2745
2746void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
2747 HBinaryOperation* instruction) {
2748 DCHECK(instruction->IsDiv() || instruction->IsRem());
2749 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2750
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002751 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002752 DCHECK(second.IsConstant());
2753
2754 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2755 if (imm == 0) {
2756 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2757 } else if (imm == 1 || imm == -1) {
2758 DivRemOneOrMinusOne(instruction);
2759 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
2760 DivRemByPowerOfTwo(instruction);
2761 } else {
2762 DCHECK(imm <= -2 || imm >= 2);
2763 GenerateDivRemWithAnyConstant(instruction);
2764 }
2765}
2766
2767void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
2768 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2769 if (div->GetResultType() == Primitive::kPrimLong) {
2770 // pLdiv runtime call.
2771 call_kind = LocationSummary::kCallOnMainOnly;
2772 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2773 // sdiv will be replaced by other instruction sequence.
2774 } else if (div->GetResultType() == Primitive::kPrimInt &&
2775 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2776 // pIdivmod runtime call.
2777 call_kind = LocationSummary::kCallOnMainOnly;
2778 }
2779
2780 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2781
2782 switch (div->GetResultType()) {
2783 case Primitive::kPrimInt: {
2784 if (div->InputAt(1)->IsConstant()) {
2785 locations->SetInAt(0, Location::RequiresRegister());
2786 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
2787 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2788 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2789 if (value == 1 || value == 0 || value == -1) {
2790 // No temp register required.
2791 } else {
2792 locations->AddTemp(Location::RequiresRegister());
2793 if (!IsPowerOfTwo(AbsOrMin(value))) {
2794 locations->AddTemp(Location::RequiresRegister());
2795 }
2796 }
2797 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2798 locations->SetInAt(0, Location::RequiresRegister());
2799 locations->SetInAt(1, Location::RequiresRegister());
2800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2801 } else {
2802 TODO_VIXL32(FATAL);
2803 }
2804 break;
2805 }
2806 case Primitive::kPrimLong: {
2807 TODO_VIXL32(FATAL);
2808 break;
2809 }
2810 case Primitive::kPrimFloat:
2811 case Primitive::kPrimDouble: {
2812 locations->SetInAt(0, Location::RequiresFpuRegister());
2813 locations->SetInAt(1, Location::RequiresFpuRegister());
2814 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2815 break;
2816 }
2817
2818 default:
2819 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2820 }
2821}
2822
2823void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002824 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002825
2826 switch (div->GetResultType()) {
2827 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002828 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002829 GenerateDivRemConstantIntegral(div);
2830 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2831 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
2832 } else {
2833 TODO_VIXL32(FATAL);
2834 }
2835 break;
2836 }
2837
2838 case Primitive::kPrimLong: {
2839 TODO_VIXL32(FATAL);
2840 break;
2841 }
2842
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002843 case Primitive::kPrimFloat:
2844 case Primitive::kPrimDouble:
2845 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002846 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01002847
2848 default:
2849 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2850 }
2851}
2852
2853void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002854 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
Scott Wakelingfe885462016-09-22 10:24:38 +01002855 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2856 ? LocationSummary::kCallOnSlowPath
2857 : LocationSummary::kNoCall;
2858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2859 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2860 if (instruction->HasUses()) {
2861 locations->SetOut(Location::SameAsFirstInput());
2862 }
2863}
2864
2865void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2866 DivZeroCheckSlowPathARMVIXL* slow_path =
2867 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
2868 codegen_->AddSlowPath(slow_path);
2869
2870 LocationSummary* locations = instruction->GetLocations();
2871 Location value = locations->InAt(0);
2872
2873 switch (instruction->GetType()) {
2874 case Primitive::kPrimBoolean:
2875 case Primitive::kPrimByte:
2876 case Primitive::kPrimChar:
2877 case Primitive::kPrimShort:
2878 case Primitive::kPrimInt: {
2879 if (value.IsRegister()) {
2880 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2881 } else {
2882 DCHECK(value.IsConstant()) << value;
2883 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2884 __ B(slow_path->GetEntryLabel());
2885 }
2886 }
2887 break;
2888 }
2889 case Primitive::kPrimLong: {
2890 if (value.IsRegisterPair()) {
2891 UseScratchRegisterScope temps(GetVIXLAssembler());
2892 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002893 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01002894 __ B(eq, slow_path->GetEntryLabel());
2895 } else {
2896 DCHECK(value.IsConstant()) << value;
2897 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2898 __ B(slow_path->GetEntryLabel());
2899 }
2900 }
2901 break;
2902 }
2903 default:
2904 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2905 }
2906}
2907
Artem Serov02109dd2016-09-23 17:17:54 +01002908void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
2909 LocationSummary* locations = ror->GetLocations();
2910 vixl32::Register in = InputRegisterAt(ror, 0);
2911 Location rhs = locations->InAt(1);
2912 vixl32::Register out = OutputRegister(ror);
2913
2914 if (rhs.IsConstant()) {
2915 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
2916 // so map all rotations to a +ve. equivalent in that range.
2917 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
2918 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
2919 if (rot) {
2920 // Rotate, mapping left rotations to right equivalents if necessary.
2921 // (e.g. left by 2 bits == right by 30.)
2922 __ Ror(out, in, rot);
2923 } else if (!out.Is(in)) {
2924 __ Mov(out, in);
2925 }
2926 } else {
2927 __ Ror(out, in, RegisterFrom(rhs));
2928 }
2929}
2930
2931// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
2932// rotates by swapping input regs (effectively rotating by the first 32-bits of
2933// a larger rotation) or flipping direction (thus treating larger right/left
2934// rotations as sub-word sized rotations in the other direction) as appropriate.
2935void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
2936 LocationSummary* locations = ror->GetLocations();
2937 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2938 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2939 Location rhs = locations->InAt(1);
2940 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2941 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2942
2943 if (rhs.IsConstant()) {
2944 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
2945 // Map all rotations to +ve. equivalents on the interval [0,63].
2946 rot &= kMaxLongShiftDistance;
2947 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
2948 // logic below to a simple pair of binary orr.
2949 // (e.g. 34 bits == in_reg swap + 2 bits right.)
2950 if (rot >= kArmBitsPerWord) {
2951 rot -= kArmBitsPerWord;
2952 std::swap(in_reg_hi, in_reg_lo);
2953 }
2954 // Rotate, or mov to out for zero or word size rotations.
2955 if (rot != 0u) {
2956 __ Lsr(out_reg_hi, in_reg_hi, rot);
2957 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
2958 __ Lsr(out_reg_lo, in_reg_lo, rot);
2959 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
2960 } else {
2961 __ Mov(out_reg_lo, in_reg_lo);
2962 __ Mov(out_reg_hi, in_reg_hi);
2963 }
2964 } else {
2965 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
2966 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
2967 vixl32::Label end;
2968 vixl32::Label shift_by_32_plus_shift_right;
2969
2970 __ And(shift_right, RegisterFrom(rhs), 0x1F);
2971 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
2972 // TODO(VIXL): Check that flags are kept after "vixl32::LeaveFlags" enabled.
2973 __ Rsb(shift_left, shift_right, kArmBitsPerWord);
2974 __ B(cc, &shift_by_32_plus_shift_right);
2975
2976 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
2977 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
2978 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
2979 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2980 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2981 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2982 __ Lsr(shift_left, in_reg_hi, shift_right);
2983 __ Add(out_reg_lo, out_reg_lo, shift_left);
2984 __ B(&end);
2985
2986 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
2987 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
2988 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
2989 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
2990 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
2991 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
2992 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
2993 __ Lsl(shift_right, in_reg_hi, shift_left);
2994 __ Add(out_reg_lo, out_reg_lo, shift_right);
2995
2996 __ Bind(&end);
2997 }
2998}
2999
3000void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3001 LocationSummary* locations =
3002 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3003 switch (ror->GetResultType()) {
3004 case Primitive::kPrimInt: {
3005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3008 break;
3009 }
3010 case Primitive::kPrimLong: {
3011 locations->SetInAt(0, Location::RequiresRegister());
3012 if (ror->InputAt(1)->IsConstant()) {
3013 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3014 } else {
3015 locations->SetInAt(1, Location::RequiresRegister());
3016 locations->AddTemp(Location::RequiresRegister());
3017 locations->AddTemp(Location::RequiresRegister());
3018 }
3019 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3020 break;
3021 }
3022 default:
3023 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3024 }
3025}
3026
3027void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3028 Primitive::Type type = ror->GetResultType();
3029 switch (type) {
3030 case Primitive::kPrimInt: {
3031 HandleIntegerRotate(ror);
3032 break;
3033 }
3034 case Primitive::kPrimLong: {
3035 HandleLongRotate(ror);
3036 break;
3037 }
3038 default:
3039 LOG(FATAL) << "Unexpected operation type " << type;
3040 UNREACHABLE();
3041 }
3042}
3043
3044
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003045void LocationsBuilderARMVIXL::HandleFieldSet(
3046 HInstruction* instruction, const FieldInfo& field_info) {
3047 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3048
3049 LocationSummary* locations =
3050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3051 locations->SetInAt(0, Location::RequiresRegister());
3052
3053 Primitive::Type field_type = field_info.GetFieldType();
3054 if (Primitive::IsFloatingPointType(field_type)) {
3055 locations->SetInAt(1, Location::RequiresFpuRegister());
3056 } else {
3057 locations->SetInAt(1, Location::RequiresRegister());
3058 }
3059
3060 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
3061 bool generate_volatile = field_info.IsVolatile()
3062 && is_wide
3063 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3064 bool needs_write_barrier =
3065 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3066 // Temporary registers for the write barrier.
3067 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
3068 if (needs_write_barrier) {
3069 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
3070 locations->AddTemp(Location::RequiresRegister());
3071 } else if (generate_volatile) {
3072 // ARM encoding have some additional constraints for ldrexd/strexd:
3073 // - registers need to be consecutive
3074 // - the first register should be even but not R14.
3075 // We don't test for ARM yet, and the assertion makes sure that we
3076 // revisit this if we ever enable ARM encoding.
3077 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3078
3079 locations->AddTemp(Location::RequiresRegister());
3080 locations->AddTemp(Location::RequiresRegister());
3081 if (field_type == Primitive::kPrimDouble) {
3082 // For doubles we need two more registers to copy the value.
3083 locations->AddTemp(LocationFrom(r2));
3084 locations->AddTemp(LocationFrom(r3));
3085 }
3086 }
3087}
3088
3089void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
3090 const FieldInfo& field_info,
3091 bool value_can_be_null) {
3092 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3093
3094 LocationSummary* locations = instruction->GetLocations();
3095 vixl32::Register base = InputRegisterAt(instruction, 0);
3096 Location value = locations->InAt(1);
3097
3098 bool is_volatile = field_info.IsVolatile();
3099 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3100 Primitive::Type field_type = field_info.GetFieldType();
3101 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3102 bool needs_write_barrier =
3103 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3104
3105 if (is_volatile) {
3106 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3107 }
3108
3109 switch (field_type) {
3110 case Primitive::kPrimBoolean:
3111 case Primitive::kPrimByte: {
3112 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
3113 break;
3114 }
3115
3116 case Primitive::kPrimShort:
3117 case Primitive::kPrimChar: {
3118 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
3119 break;
3120 }
3121
3122 case Primitive::kPrimInt:
3123 case Primitive::kPrimNot: {
3124 if (kPoisonHeapReferences && needs_write_barrier) {
3125 // Note that in the case where `value` is a null reference,
3126 // we do not enter this block, as a null reference does not
3127 // need poisoning.
3128 DCHECK_EQ(field_type, Primitive::kPrimNot);
3129 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3130 __ Mov(temp, RegisterFrom(value));
3131 GetAssembler()->PoisonHeapReference(temp);
3132 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
3133 } else {
3134 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
3135 }
3136 break;
3137 }
3138
3139 case Primitive::kPrimLong: {
3140 if (is_volatile && !atomic_ldrd_strd) {
3141 GenerateWideAtomicStore(base,
3142 offset,
3143 LowRegisterFrom(value),
3144 HighRegisterFrom(value),
3145 RegisterFrom(locations->GetTemp(0)),
3146 RegisterFrom(locations->GetTemp(1)),
3147 instruction);
3148 } else {
3149 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
3150 codegen_->MaybeRecordImplicitNullCheck(instruction);
3151 }
3152 break;
3153 }
3154
3155 case Primitive::kPrimFloat: {
3156 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
3157 break;
3158 }
3159
3160 case Primitive::kPrimDouble: {
3161 vixl32::DRegister value_reg = FromLowSToD(LowSRegisterFrom(value));
3162 if (is_volatile && !atomic_ldrd_strd) {
3163 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
3164 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
3165
3166 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
3167
3168 GenerateWideAtomicStore(base,
3169 offset,
3170 value_reg_lo,
3171 value_reg_hi,
3172 RegisterFrom(locations->GetTemp(2)),
3173 RegisterFrom(locations->GetTemp(3)),
3174 instruction);
3175 } else {
3176 GetAssembler()->StoreDToOffset(value_reg, base, offset);
3177 codegen_->MaybeRecordImplicitNullCheck(instruction);
3178 }
3179 break;
3180 }
3181
3182 case Primitive::kPrimVoid:
3183 LOG(FATAL) << "Unreachable type " << field_type;
3184 UNREACHABLE();
3185 }
3186
3187 // Longs and doubles are handled in the switch.
3188 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3189 codegen_->MaybeRecordImplicitNullCheck(instruction);
3190 }
3191
3192 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3193 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3194 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
3195 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
3196 }
3197
3198 if (is_volatile) {
3199 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3200 }
3201}
3202
Artem Serov02109dd2016-09-23 17:17:54 +01003203Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
3204 Opcode opcode) {
3205 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3206 if (constant->IsConstant() &&
3207 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3208 return Location::ConstantLocation(constant->AsConstant());
3209 }
3210 return Location::RequiresRegister();
3211}
3212
3213bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
3214 Opcode opcode) {
3215 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3216 if (Primitive::Is64BitType(input_cst->GetType())) {
3217 Opcode high_opcode = opcode;
3218 SetCc low_set_cc = kCcDontCare;
3219 switch (opcode) {
3220 case SUB:
3221 // Flip the operation to an ADD.
3222 value = -value;
3223 opcode = ADD;
3224 FALLTHROUGH_INTENDED;
3225 case ADD:
3226 if (Low32Bits(value) == 0u) {
3227 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
3228 }
3229 high_opcode = ADC;
3230 low_set_cc = kCcSet;
3231 break;
3232 default:
3233 break;
3234 }
3235 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
3236 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
3237 } else {
3238 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3239 }
3240}
3241
3242// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
3243// enabled.
3244bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
3245 Opcode opcode,
3246 SetCc set_cc) {
3247 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
3248 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
3249 return true;
3250 }
3251 Opcode neg_opcode = kNoOperand;
3252 switch (opcode) {
3253 case AND: neg_opcode = BIC; value = ~value; break;
3254 case ORR: neg_opcode = ORN; value = ~value; break;
3255 case ADD: neg_opcode = SUB; value = -value; break;
3256 case ADC: neg_opcode = SBC; value = ~value; break;
3257 case SUB: neg_opcode = ADD; value = -value; break;
3258 case SBC: neg_opcode = ADC; value = ~value; break;
3259 default:
3260 return false;
3261 }
3262 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
3263}
3264
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003265Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
3266 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
3267 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
3268 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
3269 return Location::ConstantLocation(input->AsConstant());
3270 } else {
3271 return Location::RequiresFpuRegister();
3272 }
3273}
3274
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003275void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
3276 const FieldInfo& field_info) {
3277 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3278
3279 bool object_field_get_with_read_barrier =
3280 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
3281 LocationSummary* locations =
3282 new (GetGraph()->GetArena()) LocationSummary(instruction,
3283 object_field_get_with_read_barrier ?
3284 LocationSummary::kCallOnSlowPath :
3285 LocationSummary::kNoCall);
3286 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3287 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3288 }
3289 locations->SetInAt(0, Location::RequiresRegister());
3290
3291 bool volatile_for_double = field_info.IsVolatile()
3292 && (field_info.GetFieldType() == Primitive::kPrimDouble)
3293 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3294 // The output overlaps in case of volatile long: we don't want the
3295 // code generated by GenerateWideAtomicLoad to overwrite the
3296 // object's location. Likewise, in the case of an object field get
3297 // with read barriers enabled, we do not want the load to overwrite
3298 // the object's location, as we need it to emit the read barrier.
3299 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3300 object_field_get_with_read_barrier;
3301
3302 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3303 locations->SetOut(Location::RequiresFpuRegister());
3304 } else {
3305 locations->SetOut(Location::RequiresRegister(),
3306 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3307 }
3308 if (volatile_for_double) {
3309 // ARM encoding have some additional constraints for ldrexd/strexd:
3310 // - registers need to be consecutive
3311 // - the first register should be even but not R14.
3312 // We don't test for ARM yet, and the assertion makes sure that we
3313 // revisit this if we ever enable ARM encoding.
3314 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3315 locations->AddTemp(Location::RequiresRegister());
3316 locations->AddTemp(Location::RequiresRegister());
3317 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3318 // We need a temporary register for the read barrier marking slow
3319 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3320 locations->AddTemp(Location::RequiresRegister());
3321 }
3322}
3323
3324void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
3325 const FieldInfo& field_info) {
3326 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3327
3328 LocationSummary* locations = instruction->GetLocations();
3329 vixl32::Register base = InputRegisterAt(instruction, 0);
3330 Location out = locations->Out();
3331 bool is_volatile = field_info.IsVolatile();
3332 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3333 Primitive::Type field_type = field_info.GetFieldType();
3334 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3335
3336 switch (field_type) {
3337 case Primitive::kPrimBoolean:
3338 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
3339 break;
3340
3341 case Primitive::kPrimByte:
3342 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
3343 break;
3344
3345 case Primitive::kPrimShort:
3346 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
3347 break;
3348
3349 case Primitive::kPrimChar:
3350 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
3351 break;
3352
3353 case Primitive::kPrimInt:
3354 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3355 break;
3356
3357 case Primitive::kPrimNot: {
3358 // /* HeapReference<Object> */ out = *(base + offset)
3359 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3360 TODO_VIXL32(FATAL);
3361 } else {
3362 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
3363 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3364 codegen_->MaybeRecordImplicitNullCheck(instruction);
3365 if (is_volatile) {
3366 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3367 }
3368 // If read barriers are enabled, emit read barriers other than
3369 // Baker's using a slow path (and also unpoison the loaded
3370 // reference, if heap poisoning is enabled).
3371 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
3372 }
3373 break;
3374 }
3375
3376 case Primitive::kPrimLong:
3377 if (is_volatile && !atomic_ldrd_strd) {
3378 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
3379 } else {
3380 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
3381 }
3382 break;
3383
3384 case Primitive::kPrimFloat:
3385 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
3386 break;
3387
3388 case Primitive::kPrimDouble: {
3389 vixl32::DRegister out_dreg = FromLowSToD(LowSRegisterFrom(out));
3390 if (is_volatile && !atomic_ldrd_strd) {
3391 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
3392 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
3393 GenerateWideAtomicLoad(base, offset, lo, hi);
3394 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
3395 // scope.
3396 codegen_->MaybeRecordImplicitNullCheck(instruction);
3397 __ Vmov(out_dreg, lo, hi);
3398 } else {
3399 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
3400 // TODO(VIXL): Scope to guarantee the position immediately after the load.
3401 codegen_->MaybeRecordImplicitNullCheck(instruction);
3402 }
3403 break;
3404 }
3405
3406 case Primitive::kPrimVoid:
3407 LOG(FATAL) << "Unreachable type " << field_type;
3408 UNREACHABLE();
3409 }
3410
3411 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
3412 // Potential implicit null checks, in the case of reference or
3413 // double fields, are handled in the previous switch statement.
3414 } else {
3415 // Address cases other than reference and double that may require an implicit null check.
3416 codegen_->MaybeRecordImplicitNullCheck(instruction);
3417 }
3418
3419 if (is_volatile) {
3420 if (field_type == Primitive::kPrimNot) {
3421 // Memory barriers, in the case of references, are also handled
3422 // in the previous switch statement.
3423 } else {
3424 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3425 }
3426 }
3427}
3428
3429void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3430 HandleFieldSet(instruction, instruction->GetFieldInfo());
3431}
3432
3433void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3434 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3435}
3436
3437void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3438 HandleFieldGet(instruction, instruction->GetFieldInfo());
3439}
3440
3441void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3442 HandleFieldGet(instruction, instruction->GetFieldInfo());
3443}
3444
3445void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3446 HandleFieldGet(instruction, instruction->GetFieldInfo());
3447}
3448
3449void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3450 HandleFieldGet(instruction, instruction->GetFieldInfo());
3451}
3452
3453void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3454 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/
3455 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3456 ? LocationSummary::kCallOnSlowPath
3457 : LocationSummary::kNoCall;
3458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3459 locations->SetInAt(0, Location::RequiresRegister());
3460 if (instruction->HasUses()) {
3461 locations->SetOut(Location::SameAsFirstInput());
3462 }
3463}
3464
3465void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
3466 if (CanMoveNullCheckToUser(instruction)) {
3467 return;
3468 }
3469
3470 UseScratchRegisterScope temps(GetVIXLAssembler());
3471 AssemblerAccurateScope aas(GetVIXLAssembler(),
3472 kArmInstrMaxSizeInBytes,
3473 CodeBufferCheckScope::kMaximumSize);
3474 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
3475 RecordPcInfo(instruction, instruction->GetDexPc());
3476}
3477
3478void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
3479 NullCheckSlowPathARMVIXL* slow_path =
3480 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
3481 AddSlowPath(slow_path);
3482 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3483}
3484
3485void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
3486 codegen_->GenerateNullCheck(instruction);
3487}
3488
3489void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
3490 LocationSummary* locations =
3491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3492 locations->SetInAt(0, Location::RequiresRegister());
3493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3494}
3495
3496void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
3497 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
3498 vixl32::Register obj = InputRegisterAt(instruction, 0);
3499 vixl32::Register out = OutputRegister(instruction);
3500 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
3501 codegen_->MaybeRecordImplicitNullCheck(instruction);
3502 // TODO(VIXL): https://android-review.googlesource.com/#/c/272625/
3503}
3504
3505void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
3506 vixl32::Register card,
3507 vixl32::Register object,
3508 vixl32::Register value,
3509 bool can_be_null) {
3510 vixl32::Label is_null;
3511 if (can_be_null) {
3512 __ Cbz(value, &is_null);
3513 }
3514 GetAssembler()->LoadFromOffset(
3515 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
3516 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3517 __ Strb(card, MemOperand(card, temp));
3518 if (can_be_null) {
3519 __ Bind(&is_null);
3520 }
3521}
3522
Scott Wakelingfe885462016-09-22 10:24:38 +01003523void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3524 LOG(FATAL) << "Unreachable";
3525}
3526
3527void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
3528 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3529}
3530
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003531void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
3532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3533 // TODO(VIXL): https://android-review.googlesource.com/#/c/275337/ and related.
3534}
3535
3536void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
3537 HBasicBlock* block = instruction->GetBlock();
3538 if (block->GetLoopInformation() != nullptr) {
3539 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3540 // The back edge will generate the suspend check.
3541 return;
3542 }
3543 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3544 // The goto will generate the suspend check.
3545 return;
3546 }
3547 GenerateSuspendCheck(instruction, nullptr);
3548}
3549
3550void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
3551 HBasicBlock* successor) {
3552 SuspendCheckSlowPathARMVIXL* slow_path =
3553 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
3554 if (slow_path == nullptr) {
3555 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
3556 instruction->SetSlowPath(slow_path);
3557 codegen_->AddSlowPath(slow_path);
3558 if (successor != nullptr) {
3559 DCHECK(successor->IsLoopHeader());
3560 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3561 }
3562 } else {
3563 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3564 }
3565
3566 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3567 vixl32::Register temp = temps.Acquire();
3568 GetAssembler()->LoadFromOffset(
3569 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
3570 if (successor == nullptr) {
3571 __ Cbnz(temp, slow_path->GetEntryLabel());
3572 __ Bind(slow_path->GetReturnLabel());
3573 } else {
3574 __ Cbz(temp, codegen_->GetLabelOf(successor));
3575 __ B(slow_path->GetEntryLabel());
3576 }
3577}
3578
Scott Wakelingfe885462016-09-22 10:24:38 +01003579ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
3580 return codegen_->GetAssembler();
3581}
3582
3583void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003584 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01003585 MoveOperands* move = moves_[index];
3586 Location source = move->GetSource();
3587 Location destination = move->GetDestination();
3588
3589 if (source.IsRegister()) {
3590 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003591 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01003592 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003593 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01003594 } else {
3595 DCHECK(destination.IsStackSlot());
3596 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003597 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01003598 sp,
3599 destination.GetStackIndex());
3600 }
3601 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003602 if (destination.IsRegister()) {
3603 GetAssembler()->LoadFromOffset(kLoadWord,
3604 RegisterFrom(destination),
3605 sp,
3606 source.GetStackIndex());
3607 } else if (destination.IsFpuRegister()) {
3608 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
3609 } else {
3610 DCHECK(destination.IsStackSlot());
3611 vixl32::Register temp = temps.Acquire();
3612 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
3613 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
3614 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003615 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003616 if (destination.IsRegister()) {
3617 TODO_VIXL32(FATAL);
3618 } else if (destination.IsFpuRegister()) {
3619 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
3620 } else {
3621 DCHECK(destination.IsStackSlot());
3622 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
3623 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003624 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003625 if (destination.IsDoubleStackSlot()) {
3626 vixl32::DRegister temp = temps.AcquireD();
3627 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
3628 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
3629 } else if (destination.IsRegisterPair()) {
3630 DCHECK(ExpectedPairLayout(destination));
3631 GetAssembler()->LoadFromOffset(
3632 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
3633 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003634 DCHECK(destination.IsFpuRegisterPair()) << destination;
3635 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003636 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003637 } else if (source.IsRegisterPair()) {
3638 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003639 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
3640 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01003641 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003642 __ Vmov(FromLowSToD(LowSRegisterFrom(destination)),
3643 LowRegisterFrom(source),
3644 HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01003645 } else {
3646 DCHECK(destination.IsDoubleStackSlot()) << destination;
3647 DCHECK(ExpectedPairLayout(source));
3648 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003649 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01003650 sp,
3651 destination.GetStackIndex());
3652 }
3653 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003654 if (destination.IsRegisterPair()) {
3655 TODO_VIXL32(FATAL);
3656 } else if (destination.IsFpuRegisterPair()) {
3657 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
3658 } else {
3659 DCHECK(destination.IsDoubleStackSlot()) << destination;
3660 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
3661 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003662 } else {
3663 DCHECK(source.IsConstant()) << source;
3664 HConstant* constant = source.GetConstant();
3665 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3666 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
3667 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003668 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003669 } else {
3670 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01003671 vixl32::Register temp = temps.Acquire();
3672 __ Mov(temp, value);
3673 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
3674 }
3675 } else if (constant->IsLongConstant()) {
3676 int64_t value = constant->AsLongConstant()->GetValue();
3677 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003678 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
3679 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003680 } else {
3681 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01003682 vixl32::Register temp = temps.Acquire();
3683 __ Mov(temp, Low32Bits(value));
3684 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
3685 __ Mov(temp, High32Bits(value));
3686 GetAssembler()->StoreToOffset(kStoreWord,
3687 temp,
3688 sp,
3689 destination.GetHighStackIndex(kArmWordSize));
3690 }
3691 } else if (constant->IsDoubleConstant()) {
3692 double value = constant->AsDoubleConstant()->GetValue();
3693 if (destination.IsFpuRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003694 __ Vmov(FromLowSToD(LowSRegisterFrom(destination)), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003695 } else {
3696 DCHECK(destination.IsDoubleStackSlot()) << destination;
3697 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003698 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003699 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003700 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003701 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003702 GetAssembler()->StoreToOffset(kStoreWord,
3703 temp,
3704 sp,
3705 destination.GetHighStackIndex(kArmWordSize));
3706 }
3707 } else {
3708 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
3709 float value = constant->AsFloatConstant()->GetValue();
3710 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003711 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003712 } else {
3713 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01003714 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003715 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003716 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
3717 }
3718 }
3719 }
3720}
3721
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003722void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
3723 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3724 vixl32::Register temp = temps.Acquire();
3725 __ Mov(temp, reg);
3726 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
3727 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01003728}
3729
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003730void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
3731 // TODO(VIXL32): Double check the performance of this implementation.
3732 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3733 vixl32::Register temp = temps.Acquire();
3734 vixl32::SRegister temp_s = temps.AcquireS();
3735
3736 __ Ldr(temp, MemOperand(sp, mem1));
3737 __ Vldr(temp_s, MemOperand(sp, mem2));
3738 __ Str(temp, MemOperand(sp, mem2));
3739 __ Vstr(temp_s, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003740}
3741
Alexandre Rames9c19bd62016-10-24 11:50:32 +01003742void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
3743 MoveOperands* move = moves_[index];
3744 Location source = move->GetSource();
3745 Location destination = move->GetDestination();
3746 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
3747
3748 if (source.IsRegister() && destination.IsRegister()) {
3749 vixl32::Register temp = temps.Acquire();
3750 DCHECK(!RegisterFrom(source).Is(temp));
3751 DCHECK(!RegisterFrom(destination).Is(temp));
3752 __ Mov(temp, RegisterFrom(destination));
3753 __ Mov(RegisterFrom(destination), RegisterFrom(source));
3754 __ Mov(RegisterFrom(source), temp);
3755 } else if (source.IsRegister() && destination.IsStackSlot()) {
3756 Exchange(RegisterFrom(source), destination.GetStackIndex());
3757 } else if (source.IsStackSlot() && destination.IsRegister()) {
3758 Exchange(RegisterFrom(destination), source.GetStackIndex());
3759 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3760 TODO_VIXL32(FATAL);
3761 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3762 TODO_VIXL32(FATAL);
3763 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
3764 vixl32::DRegister temp = temps.AcquireD();
3765 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
3766 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
3767 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
3768 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
3769 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
3770 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
3771 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
3772 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
3773 vixl32::DRegister temp = temps.AcquireD();
3774 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
3775 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
3776 GetAssembler()->StoreDToOffset(temp, sp, mem);
3777 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
3778 TODO_VIXL32(FATAL);
3779 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
3780 TODO_VIXL32(FATAL);
3781 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3782 TODO_VIXL32(FATAL);
3783 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3784 vixl32::DRegister temp1 = temps.AcquireD();
3785 vixl32::DRegister temp2 = temps.AcquireD();
3786 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
3787 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
3788 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
3789 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
3790 } else {
3791 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
3792 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003793}
3794
3795void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
3796 TODO_VIXL32(FATAL);
3797}
3798
3799void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
3800 TODO_VIXL32(FATAL);
3801}
3802
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003803void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
3804 if (cls->NeedsAccessCheck()) {
3805 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3806 CodeGenerator::CreateLoadClassLocationSummary(
3807 cls,
3808 LocationFrom(calling_convention.GetRegisterAt(0)),
3809 LocationFrom(r0),
3810 /* code_generator_supports_read_barrier */ true);
3811 return;
3812 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003813
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003814 // TODO(VIXL): read barrier code.
3815 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3816 ? LocationSummary::kCallOnSlowPath
3817 : LocationSummary::kNoCall;
3818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3819 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3820 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3821 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
3822 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
3823 locations->SetInAt(0, Location::RequiresRegister());
3824 }
3825 locations->SetOut(Location::RequiresRegister());
3826}
3827
3828void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
3829 LocationSummary* locations = cls->GetLocations();
3830 if (cls->NeedsAccessCheck()) {
3831 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3832 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
3833 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
3834 return;
3835 }
3836
3837 Location out_loc = locations->Out();
3838 vixl32::Register out = OutputRegister(cls);
3839
3840 // TODO(VIXL): read barrier code.
3841 bool generate_null_check = false;
3842 switch (cls->GetLoadKind()) {
3843 case HLoadClass::LoadKind::kReferrersClass: {
3844 DCHECK(!cls->CanCallRuntime());
3845 DCHECK(!cls->MustGenerateClinitCheck());
3846 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3847 vixl32::Register current_method = InputRegisterAt(cls, 0);
3848 GenerateGcRootFieldLoad(cls,
3849 out_loc,
3850 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01003851 ArtMethod::DeclaringClassOffset().Int32Value(),
3852 kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003853 break;
3854 }
3855 case HLoadClass::LoadKind::kDexCacheViaMethod: {
3856 // /* GcRoot<mirror::Class>[] */ out =
3857 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
3858 vixl32::Register current_method = InputRegisterAt(cls, 0);
3859 const int32_t resolved_types_offset =
3860 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value();
3861 GetAssembler()->LoadFromOffset(kLoadWord, out, current_method, resolved_types_offset);
3862 // /* GcRoot<mirror::Class> */ out = out[type_index]
3863 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Roland Levillain00468f32016-10-27 18:02:48 +01003864 GenerateGcRootFieldLoad(cls, out_loc, out, offset, kEmitCompilerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003865 generate_null_check = !cls->IsInDexCache();
3866 break;
3867 }
3868 default:
3869 TODO_VIXL32(FATAL);
3870 }
3871
3872 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3873 DCHECK(cls->CanCallRuntime());
3874 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
3875 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3876 codegen_->AddSlowPath(slow_path);
3877 if (generate_null_check) {
3878 __ Cbz(out, slow_path->GetEntryLabel());
3879 }
3880 if (cls->MustGenerateClinitCheck()) {
3881 GenerateClassInitializationCheck(slow_path, out);
3882 } else {
3883 __ Bind(slow_path->GetExitLabel());
3884 }
3885 }
3886}
3887
Artem Serov02109dd2016-09-23 17:17:54 +01003888void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
3889 HandleBitwiseOperation(instruction, AND);
3890}
3891
3892void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
3893 HandleBitwiseOperation(instruction, ORR);
3894}
3895
3896void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
3897 HandleBitwiseOperation(instruction, EOR);
3898}
3899
3900void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
3901 LocationSummary* locations =
3902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3903 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3904 || instruction->GetResultType() == Primitive::kPrimLong);
3905 // Note: GVN reorders commutative operations to have the constant on the right hand side.
3906 locations->SetInAt(0, Location::RequiresRegister());
3907 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
3908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3909}
3910
3911void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
3912 HandleBitwiseOperation(instruction);
3913}
3914
3915void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
3916 HandleBitwiseOperation(instruction);
3917}
3918
3919void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
3920 HandleBitwiseOperation(instruction);
3921}
3922
3923// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
3924void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
3925 vixl32::Register first,
3926 uint32_t value) {
3927 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
3928 if (value == 0xffffffffu) {
3929 if (!out.Is(first)) {
3930 __ Mov(out, first);
3931 }
3932 return;
3933 }
3934 if (value == 0u) {
3935 __ Mov(out, 0);
3936 return;
3937 }
3938 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
3939 __ And(out, first, value);
3940 } else {
3941 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
3942 __ Bic(out, first, ~value);
3943 }
3944}
3945
3946// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
3947void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
3948 vixl32::Register first,
3949 uint32_t value) {
3950 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
3951 if (value == 0u) {
3952 if (!out.Is(first)) {
3953 __ Mov(out, first);
3954 }
3955 return;
3956 }
3957 if (value == 0xffffffffu) {
3958 __ Mvn(out, 0);
3959 return;
3960 }
3961 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
3962 __ Orr(out, first, value);
3963 } else {
3964 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
3965 __ Orn(out, first, ~value);
3966 }
3967}
3968
3969// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
3970void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
3971 vixl32::Register first,
3972 uint32_t value) {
3973 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
3974 if (value == 0u) {
3975 if (!out.Is(first)) {
3976 __ Mov(out, first);
3977 }
3978 return;
3979 }
3980 __ Eor(out, first, value);
3981}
3982
3983void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
3984 LocationSummary* locations = instruction->GetLocations();
3985 Location first = locations->InAt(0);
3986 Location second = locations->InAt(1);
3987 Location out = locations->Out();
3988
3989 if (second.IsConstant()) {
3990 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3991 uint32_t value_low = Low32Bits(value);
3992 if (instruction->GetResultType() == Primitive::kPrimInt) {
3993 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
3994 vixl32::Register out_reg = OutputRegister(instruction);
3995 if (instruction->IsAnd()) {
3996 GenerateAndConst(out_reg, first_reg, value_low);
3997 } else if (instruction->IsOr()) {
3998 GenerateOrrConst(out_reg, first_reg, value_low);
3999 } else {
4000 DCHECK(instruction->IsXor());
4001 GenerateEorConst(out_reg, first_reg, value_low);
4002 }
4003 } else {
4004 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4005 uint32_t value_high = High32Bits(value);
4006 vixl32::Register first_low = LowRegisterFrom(first);
4007 vixl32::Register first_high = HighRegisterFrom(first);
4008 vixl32::Register out_low = LowRegisterFrom(out);
4009 vixl32::Register out_high = HighRegisterFrom(out);
4010 if (instruction->IsAnd()) {
4011 GenerateAndConst(out_low, first_low, value_low);
4012 GenerateAndConst(out_high, first_high, value_high);
4013 } else if (instruction->IsOr()) {
4014 GenerateOrrConst(out_low, first_low, value_low);
4015 GenerateOrrConst(out_high, first_high, value_high);
4016 } else {
4017 DCHECK(instruction->IsXor());
4018 GenerateEorConst(out_low, first_low, value_low);
4019 GenerateEorConst(out_high, first_high, value_high);
4020 }
4021 }
4022 return;
4023 }
4024
4025 if (instruction->GetResultType() == Primitive::kPrimInt) {
4026 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
4027 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
4028 vixl32::Register out_reg = OutputRegister(instruction);
4029 if (instruction->IsAnd()) {
4030 __ And(out_reg, first_reg, second_reg);
4031 } else if (instruction->IsOr()) {
4032 __ Orr(out_reg, first_reg, second_reg);
4033 } else {
4034 DCHECK(instruction->IsXor());
4035 __ Eor(out_reg, first_reg, second_reg);
4036 }
4037 } else {
4038 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4039 vixl32::Register first_low = LowRegisterFrom(first);
4040 vixl32::Register first_high = HighRegisterFrom(first);
4041 vixl32::Register second_low = LowRegisterFrom(second);
4042 vixl32::Register second_high = HighRegisterFrom(second);
4043 vixl32::Register out_low = LowRegisterFrom(out);
4044 vixl32::Register out_high = HighRegisterFrom(out);
4045 if (instruction->IsAnd()) {
4046 __ And(out_low, first_low, second_low);
4047 __ And(out_high, first_high, second_high);
4048 } else if (instruction->IsOr()) {
4049 __ Orr(out_low, first_low, second_low);
4050 __ Orr(out_high, first_high, second_high);
4051 } else {
4052 DCHECK(instruction->IsXor());
4053 __ Eor(out_low, first_low, second_low);
4054 __ Eor(out_high, first_high, second_high);
4055 }
4056 }
4057}
4058
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004059void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
4060 HInstruction* instruction ATTRIBUTE_UNUSED,
4061 Location root,
4062 vixl32::Register obj,
4063 uint32_t offset,
4064 bool requires_read_barrier) {
4065 vixl32::Register root_reg = RegisterFrom(root);
4066 if (requires_read_barrier) {
4067 TODO_VIXL32(FATAL);
4068 } else {
4069 // Plain GC root load with no read barrier.
4070 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4071 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
4072 // Note that GC roots are not affected by heap poisoning, thus we
4073 // do not have to unpoison `root_reg` here.
4074 }
4075}
4076
4077vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
4078 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
4079 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4080 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4081 if (!invoke->GetLocations()->Intrinsified()) {
4082 return RegisterFrom(location);
4083 }
4084 // For intrinsics we allow any location, so it may be on the stack.
4085 if (!location.IsRegister()) {
4086 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
4087 return temp;
4088 }
4089 // For register locations, check if the register was saved. If so, get it from the stack.
4090 // Note: There is a chance that the register was saved but not overwritten, so we could
4091 // save one load. However, since this is just an intrinsic slow path we prefer this
4092 // simple and more robust approach rather that trying to determine if that's the case.
4093 SlowPathCode* slow_path = GetCurrentSlowPath();
4094 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4095 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
4096 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
4097 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
4098 return temp;
4099 }
4100 return RegisterFrom(location);
4101}
4102
4103void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
4104 HInvokeStaticOrDirect* invoke, Location temp) {
4105 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4106 vixl32::Register temp_reg = RegisterFrom(temp);
4107
4108 switch (invoke->GetMethodLoadKind()) {
4109 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4110 uint32_t offset =
4111 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4112 // temp = thread->string_init_entrypoint
4113 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, tr, offset);
4114 break;
4115 }
4116 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4117 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4118 vixl32::Register method_reg;
4119 if (current_method.IsRegister()) {
4120 method_reg = RegisterFrom(current_method);
4121 } else {
4122 TODO_VIXL32(FATAL);
4123 }
4124 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
4125 GetAssembler()->LoadFromOffset(
4126 kLoadWord,
4127 temp_reg,
4128 method_reg,
4129 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
4130 // temp = temp[index_in_cache];
4131 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4132 uint32_t index_in_cache = invoke->GetDexMethodIndex();
4133 GetAssembler()->LoadFromOffset(
4134 kLoadWord, temp_reg, temp_reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
4135 break;
4136 }
4137 default:
4138 TODO_VIXL32(FATAL);
4139 }
4140
4141 // TODO(VIXL): Support `CodePtrLocation` values other than `kCallArtMethod`.
4142 if (invoke->GetCodePtrLocation() != HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod) {
4143 TODO_VIXL32(FATAL);
4144 }
4145
4146 // LR = callee_method->entry_point_from_quick_compiled_code_
4147 GetAssembler()->LoadFromOffset(
4148 kLoadWord,
4149 lr,
4150 RegisterFrom(callee_method),
4151 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
4152 // LR()
4153 __ Blx(lr);
4154
4155 DCHECK(!IsLeafMethod());
4156}
4157
4158void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
4159 vixl32::Register temp = RegisterFrom(temp_location);
4160 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4161 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
4162
4163 // Use the calling convention instead of the location of the receiver, as
4164 // intrinsics may have put the receiver in a different register. In the intrinsics
4165 // slow path, the arguments have been moved to the right place, so here we are
4166 // guaranteed that the receiver is the first register of the calling convention.
4167 InvokeDexCallingConventionARMVIXL calling_convention;
4168 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
4169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4170 // /* HeapReference<Class> */ temp = receiver->klass_
4171 GetAssembler()->LoadFromOffset(kLoadWord, temp, receiver, class_offset);
4172 MaybeRecordImplicitNullCheck(invoke);
4173 // Instead of simply (possibly) unpoisoning `temp` here, we should
4174 // emit a read barrier for the previous class reference load.
4175 // However this is not required in practice, as this is an
4176 // intermediate/temporary reference and because the current
4177 // concurrent copying collector keeps the from-space memory
4178 // intact/accessible until the end of the marking phase (the
4179 // concurrent copying collector may not in the future).
4180 GetAssembler()->MaybeUnpoisonHeapReference(temp);
4181
4182 // temp = temp->GetMethodAt(method_offset);
4183 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4184 kArmPointerSize).Int32Value();
4185 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
4186 // LR = temp->GetEntryPoint();
4187 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
4188 // LR();
4189 __ Blx(lr);
4190}
4191
4192static int32_t GetExceptionTlsOffset() {
4193 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
4194}
4195
4196void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
4197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4199 locations->SetOut(Location::RequiresRegister());
4200}
4201
4202void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
4203 vixl32::Register out = OutputRegister(load);
4204 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
4205}
4206
4207void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
4208 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4209}
4210
4211void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4212 UseScratchRegisterScope temps(GetVIXLAssembler());
4213 vixl32::Register temp = temps.Acquire();
4214 __ Mov(temp, 0);
4215 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
4216}
4217
4218void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
4219 LocationSummary* locations =
4220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4221 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4222 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4223}
4224
4225void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
4226 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
4227 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4228}
4229
4230void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction ATTRIBUTE_UNUSED,
4231 Location out,
4232 Location ref ATTRIBUTE_UNUSED,
4233 Location obj ATTRIBUTE_UNUSED,
4234 uint32_t offset ATTRIBUTE_UNUSED,
4235 Location index ATTRIBUTE_UNUSED) {
4236 if (kEmitCompilerReadBarrier) {
4237 DCHECK(!kUseBakerReadBarrier);
4238 TODO_VIXL32(FATAL);
4239 } else if (kPoisonHeapReferences) {
4240 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
4241 }
4242}
Scott Wakelingfe885462016-09-22 10:24:38 +01004243
4244#undef __
4245#undef QUICK_ENTRY_POINT
4246#undef TODO_VIXL32
4247
4248} // namespace arm
4249} // namespace art