blob: 3d6415de292b264f038ec14e70006af1f4d83039 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "common_arm.h"
23#include "compiled_method.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010026#include "intrinsics_arm_vixl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "thread.h"
30#include "utils/arm/assembler_arm_vixl.h"
31#include "utils/arm/managed_register_arm.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35namespace art {
36namespace arm {
37
38namespace vixl32 = vixl::aarch32;
39using namespace vixl32; // NOLINT(build/namespaces)
40
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010041using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010042using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010043using helpers::HighDRegisterFrom;
44using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010045using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010046using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010047using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputSRegisterAt;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010049using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000050using helpers::Int32ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010051using helpers::LocationFrom;
52using helpers::LowRegisterFrom;
53using helpers::LowSRegisterFrom;
54using helpers::OutputRegister;
55using helpers::OutputSRegister;
56using helpers::OutputVRegister;
57using helpers::RegisterFrom;
58using helpers::SRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010059
Artem Serov0fb37192016-12-06 18:13:40 +000060using vixl::ExactAssemblyScope;
61using vixl::CodeBufferCheckScope;
62
Scott Wakelingfe885462016-09-22 10:24:38 +010063using RegisterList = vixl32::RegisterList;
64
65static bool ExpectedPairLayout(Location location) {
66 // We expected this for both core and fpu register pairs.
67 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
68}
Artem Serovd4cc5b22016-11-04 11:19:09 +000069// Use a local definition to prevent copying mistakes.
70static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
71static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010072static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010073static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010074
75#ifdef __
76#error "ARM Codegen VIXL macro-assembler macro already defined."
77#endif
78
Scott Wakelingfe885462016-09-22 10:24:38 +010079// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
80#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
81#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
82
83// Marker that code is yet to be, and must, be implemented.
84#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
85
Scott Wakelinga7812ae2016-10-17 10:03:36 +010086// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
87// for each live D registers they treat two corresponding S registers as live ones.
88//
89// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
90// from a list of contiguous S registers a list of contiguous D registers (processing first/last
91// S registers corner cases) and save/restore this new list treating them as D registers.
92// - decreasing code size
93// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
94// restored and then used in regular non SlowPath code as D register.
95//
96// For the following example (v means the S register is live):
97// D names: | D0 | D1 | D2 | D4 | ...
98// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
99// Live? | | v | v | v | v | v | v | | ...
100//
101// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
102// as D registers.
103//
104// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
105// for lists of floating-point registers.
106static size_t SaveContiguousSRegisterList(size_t first,
107 size_t last,
108 CodeGenerator* codegen,
109 size_t stack_offset) {
110 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
111 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
112 DCHECK_LE(first, last);
113 if ((first == last) && (first == 0)) {
114 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
115 return stack_offset + kSRegSizeInBytes;
116 }
117 if (first % 2 == 1) {
118 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
119 stack_offset += kSRegSizeInBytes;
120 }
121
122 bool save_last = false;
123 if (last % 2 == 0) {
124 save_last = true;
125 --last;
126 }
127
128 if (first < last) {
129 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
130 DCHECK_EQ((last - first + 1) % 2, 0u);
131 size_t number_of_d_regs = (last - first + 1) / 2;
132
133 if (number_of_d_regs == 1) {
134 __ Vstr(d_reg, MemOperand(sp, stack_offset));
135 } else if (number_of_d_regs > 1) {
136 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
137 vixl32::Register base = sp;
138 if (stack_offset != 0) {
139 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000140 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100141 }
142 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
143 }
144 stack_offset += number_of_d_regs * kDRegSizeInBytes;
145 }
146
147 if (save_last) {
148 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
149 stack_offset += kSRegSizeInBytes;
150 }
151
152 return stack_offset;
153}
154
155static size_t RestoreContiguousSRegisterList(size_t first,
156 size_t last,
157 CodeGenerator* codegen,
158 size_t stack_offset) {
159 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
160 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
161 DCHECK_LE(first, last);
162 if ((first == last) && (first == 0)) {
163 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
164 return stack_offset + kSRegSizeInBytes;
165 }
166 if (first % 2 == 1) {
167 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
168 stack_offset += kSRegSizeInBytes;
169 }
170
171 bool restore_last = false;
172 if (last % 2 == 0) {
173 restore_last = true;
174 --last;
175 }
176
177 if (first < last) {
178 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
179 DCHECK_EQ((last - first + 1) % 2, 0u);
180 size_t number_of_d_regs = (last - first + 1) / 2;
181 if (number_of_d_regs == 1) {
182 __ Vldr(d_reg, MemOperand(sp, stack_offset));
183 } else if (number_of_d_regs > 1) {
184 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
185 vixl32::Register base = sp;
186 if (stack_offset != 0) {
187 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000188 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100189 }
190 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
191 }
192 stack_offset += number_of_d_regs * kDRegSizeInBytes;
193 }
194
195 if (restore_last) {
196 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
197 stack_offset += kSRegSizeInBytes;
198 }
199
200 return stack_offset;
201}
202
203void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
204 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
205 size_t orig_offset = stack_offset;
206
207 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
208 for (uint32_t i : LowToHighBits(core_spills)) {
209 // If the register holds an object, update the stack mask.
210 if (locations->RegisterContainsObject(i)) {
211 locations->SetStackBit(stack_offset / kVRegSize);
212 }
213 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
214 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
215 saved_core_stack_offsets_[i] = stack_offset;
216 stack_offset += kArmWordSize;
217 }
218
219 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
220 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
221
222 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
223 orig_offset = stack_offset;
224 for (uint32_t i : LowToHighBits(fp_spills)) {
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_fpu_stack_offsets_[i] = stack_offset;
227 stack_offset += kArmWordSize;
228 }
229
230 stack_offset = orig_offset;
231 while (fp_spills != 0u) {
232 uint32_t begin = CTZ(fp_spills);
233 uint32_t tmp = fp_spills + (1u << begin);
234 fp_spills &= tmp; // Clear the contiguous range of 1s.
235 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
236 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
237 }
238 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
239}
240
241void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
242 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
243 size_t orig_offset = stack_offset;
244
245 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
246 for (uint32_t i : LowToHighBits(core_spills)) {
247 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
248 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
249 stack_offset += kArmWordSize;
250 }
251
252 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
253 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
254 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
255
256 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
257 while (fp_spills != 0u) {
258 uint32_t begin = CTZ(fp_spills);
259 uint32_t tmp = fp_spills + (1u << begin);
260 fp_spills &= tmp; // Clear the contiguous range of 1s.
261 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
262 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
263 }
264 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
265}
266
267class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
268 public:
269 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
270
271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
273 __ Bind(GetEntryLabel());
274 if (instruction_->CanThrowIntoCatchBlock()) {
275 // Live registers will be restored in the catch block if caught.
276 SaveLiveRegisters(codegen, instruction_->GetLocations());
277 }
278 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
279 instruction_,
280 instruction_->GetDexPc(),
281 this);
282 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
283 }
284
285 bool IsFatal() const OVERRIDE { return true; }
286
287 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
288
289 private:
290 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
291};
292
Scott Wakelingfe885462016-09-22 10:24:38 +0100293class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
294 public:
295 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
296 : SlowPathCodeARMVIXL(instruction) {}
297
298 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100299 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100300 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100301 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100302 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
303 }
304
305 bool IsFatal() const OVERRIDE { return true; }
306
307 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
308
309 private:
310 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
311};
312
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100313class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
314 public:
315 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
316 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
319 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
320 __ Bind(GetEntryLabel());
321 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
322 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
323 if (successor_ == nullptr) {
324 __ B(GetReturnLabel());
325 } else {
326 __ B(arm_codegen->GetLabelOf(successor_));
327 }
328 }
329
330 vixl32::Label* GetReturnLabel() {
331 DCHECK(successor_ == nullptr);
332 return &return_label_;
333 }
334
335 HBasicBlock* GetSuccessor() const {
336 return successor_;
337 }
338
339 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
340
341 private:
342 // If not null, the block to branch to after the suspend check.
343 HBasicBlock* const successor_;
344
345 // If `successor_` is null, the label to branch to after the suspend check.
346 vixl32::Label return_label_;
347
348 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
349};
350
Scott Wakelingc34dba72016-10-03 10:14:44 +0100351class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
352 public:
353 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
354 : SlowPathCodeARMVIXL(instruction) {}
355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
358 LocationSummary* locations = instruction_->GetLocations();
359
360 __ Bind(GetEntryLabel());
361 if (instruction_->CanThrowIntoCatchBlock()) {
362 // Live registers will be restored in the catch block if caught.
363 SaveLiveRegisters(codegen, instruction_->GetLocations());
364 }
365 // We're moving two locations to locations that could overlap, so we need a parallel
366 // move resolver.
367 InvokeRuntimeCallingConventionARMVIXL calling_convention;
368 codegen->EmitParallelMoves(
369 locations->InAt(0),
370 LocationFrom(calling_convention.GetRegisterAt(0)),
371 Primitive::kPrimInt,
372 locations->InAt(1),
373 LocationFrom(calling_convention.GetRegisterAt(1)),
374 Primitive::kPrimInt);
375 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
376 ? kQuickThrowStringBounds
377 : kQuickThrowArrayBounds;
378 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
379 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
380 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
381 }
382
383 bool IsFatal() const OVERRIDE { return true; }
384
385 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
386
387 private:
388 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
389};
390
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100391class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
392 public:
393 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
394 : SlowPathCodeARMVIXL(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
395 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
396 }
397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 LocationSummary* locations = at_->GetLocations();
400
401 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
402 __ Bind(GetEntryLabel());
403 SaveLiveRegisters(codegen, locations);
404
405 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800406 __ Mov(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100407 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
408 : kQuickInitializeType;
409 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
410 if (do_clinit_) {
411 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
412 } else {
413 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
414 }
415
416 // Move the class to the desired location.
417 Location out = locations->Out();
418 if (out.IsValid()) {
419 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
420 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
421 }
422 RestoreLiveRegisters(codegen, locations);
423 __ B(GetExitLabel());
424 }
425
426 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
427
428 private:
429 // The class this slow path will load.
430 HLoadClass* const cls_;
431
432 // The instruction where this slow path is happening.
433 // (Might be the load class or an initialization check).
434 HInstruction* const at_;
435
436 // The dex PC of `at_`.
437 const uint32_t dex_pc_;
438
439 // Whether to initialize the class.
440 const bool do_clinit_;
441
442 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
443};
444
Artem Serovd4cc5b22016-11-04 11:19:09 +0000445class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
446 public:
447 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
448 : SlowPathCodeARMVIXL(instruction) {}
449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
451 LocationSummary* locations = instruction_->GetLocations();
452 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
453 HLoadString* load = instruction_->AsLoadString();
454 const uint32_t string_index = load->GetStringIndex().index_;
455 vixl32::Register out = OutputRegister(load);
456 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
457 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
458
459 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConventionARMVIXL calling_convention;
464 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
465 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
466 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
467 vixl32::Register entry_address = temp_is_r0 ? out : temp;
468 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
469 if (call_saves_everything_except_r0 && temp_is_r0) {
470 __ Mov(entry_address, temp);
471 }
472
473 __ Mov(calling_convention.GetRegisterAt(0), string_index);
474 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
475 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
476
477 // Store the resolved String to the .bss entry.
478 if (call_saves_everything_except_r0) {
479 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
480 __ Str(r0, MemOperand(entry_address));
481 } else {
482 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
483 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
484 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
485 arm_codegen->EmitMovwMovtPlaceholder(labels, out);
486 __ Str(r0, MemOperand(entry_address));
487 }
488
489 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
490 RestoreLiveRegisters(codegen, locations);
491
492 __ B(GetExitLabel());
493 }
494
495 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
496
497 private:
498 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
499};
500
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100501class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
502 public:
503 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
504 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100508 DCHECK(instruction_->IsCheckCast()
509 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
510
511 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
512 __ Bind(GetEntryLabel());
513
514 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100515 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100516 }
517
518 // We're moving two locations to locations that could overlap, so we need a parallel
519 // move resolver.
520 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100521
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800522 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800523 LocationFrom(calling_convention.GetRegisterAt(0)),
524 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800525 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800526 LocationFrom(calling_convention.GetRegisterAt(1)),
527 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100528 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100529 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
530 instruction_,
531 instruction_->GetDexPc(),
532 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800533 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100534 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100535 } else {
536 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800537 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
538 instruction_,
539 instruction_->GetDexPc(),
540 this);
541 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100542 }
543
544 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100545 RestoreLiveRegisters(codegen, locations);
546 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100547 }
548 }
549
550 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
551
552 bool IsFatal() const OVERRIDE { return is_fatal_; }
553
554 private:
555 const bool is_fatal_;
556
557 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
558};
559
Scott Wakelingc34dba72016-10-03 10:14:44 +0100560class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
561 public:
562 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
563 : SlowPathCodeARMVIXL(instruction) {}
564
565 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
566 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
567 __ Bind(GetEntryLabel());
568 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
569 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
570 }
571
572 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
573
574 private:
575 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
576};
577
578class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
579 public:
580 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
581
582 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
583 LocationSummary* locations = instruction_->GetLocations();
584 __ Bind(GetEntryLabel());
585 SaveLiveRegisters(codegen, locations);
586
587 InvokeRuntimeCallingConventionARMVIXL calling_convention;
588 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
589 parallel_move.AddMove(
590 locations->InAt(0),
591 LocationFrom(calling_convention.GetRegisterAt(0)),
592 Primitive::kPrimNot,
593 nullptr);
594 parallel_move.AddMove(
595 locations->InAt(1),
596 LocationFrom(calling_convention.GetRegisterAt(1)),
597 Primitive::kPrimInt,
598 nullptr);
599 parallel_move.AddMove(
600 locations->InAt(2),
601 LocationFrom(calling_convention.GetRegisterAt(2)),
602 Primitive::kPrimNot,
603 nullptr);
604 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
605
606 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
607 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
608 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
609 RestoreLiveRegisters(codegen, locations);
610 __ B(GetExitLabel());
611 }
612
613 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
614
615 private:
616 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
617};
618
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000619// Slow path marking an object reference `ref` during a read
620// barrier. The field `obj.field` in the object `obj` holding this
621// reference does not get updated by this slow path after marking (see
622// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
623//
624// This means that after the execution of this slow path, `ref` will
625// always be up-to-date, but `obj.field` may not; i.e., after the
626// flip, `ref` will be a to-space reference, but `obj.field` will
627// probably still be a from-space reference (unless it gets updated by
628// another thread, or if another thread installed another object
629// reference (different from `ref`) in `obj.field`).
630class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
631 public:
632 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
633 Location ref,
634 Location entrypoint = Location::NoLocation())
635 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
636 DCHECK(kEmitCompilerReadBarrier);
637 }
638
639 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
640
641 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
642 LocationSummary* locations = instruction_->GetLocations();
643 vixl32::Register ref_reg = RegisterFrom(ref_);
644 DCHECK(locations->CanCall());
645 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
646 DCHECK(instruction_->IsInstanceFieldGet() ||
647 instruction_->IsStaticFieldGet() ||
648 instruction_->IsArrayGet() ||
649 instruction_->IsArraySet() ||
650 instruction_->IsLoadClass() ||
651 instruction_->IsLoadString() ||
652 instruction_->IsInstanceOf() ||
653 instruction_->IsCheckCast() ||
654 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
655 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
656 << "Unexpected instruction in read barrier marking slow path: "
657 << instruction_->DebugName();
658 // The read barrier instrumentation of object ArrayGet
659 // instructions does not support the HIntermediateAddress
660 // instruction.
661 DCHECK(!(instruction_->IsArrayGet() &&
662 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
663
664 __ Bind(GetEntryLabel());
665 // No need to save live registers; it's taken care of by the
666 // entrypoint. Also, there is no need to update the stack mask,
667 // as this runtime call will not trigger a garbage collection.
668 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
669 DCHECK(!ref_reg.Is(sp));
670 DCHECK(!ref_reg.Is(lr));
671 DCHECK(!ref_reg.Is(pc));
672 // IP is used internally by the ReadBarrierMarkRegX entry point
673 // as a temporary, it cannot be the entry point's input/output.
674 DCHECK(!ref_reg.Is(ip));
675 DCHECK(ref_reg.IsRegister()) << ref_reg;
676 // "Compact" slow path, saving two moves.
677 //
678 // Instead of using the standard runtime calling convention (input
679 // and output in R0):
680 //
681 // R0 <- ref
682 // R0 <- ReadBarrierMark(R0)
683 // ref <- R0
684 //
685 // we just use rX (the register containing `ref`) as input and output
686 // of a dedicated entrypoint:
687 //
688 // rX <- ReadBarrierMarkRegX(rX)
689 //
690 if (entrypoint_.IsValid()) {
691 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
692 __ Blx(RegisterFrom(entrypoint_));
693 } else {
694 int32_t entry_point_offset =
695 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
696 // This runtime call does not require a stack map.
697 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
698 }
699 __ B(GetExitLabel());
700 }
701
702 private:
703 // The location (register) of the marked object reference.
704 const Location ref_;
705
706 // The location of the entrypoint if already loaded.
707 const Location entrypoint_;
708
709 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
710};
711
712// Slow path marking an object reference `ref` during a read barrier,
713// and if needed, atomically updating the field `obj.field` in the
714// object `obj` holding this reference after marking (contrary to
715// ReadBarrierMarkSlowPathARM above, which never tries to update
716// `obj.field`).
717//
718// This means that after the execution of this slow path, both `ref`
719// and `obj.field` will be up-to-date; i.e., after the flip, both will
720// hold the same to-space reference (unless another thread installed
721// another object reference (different from `ref`) in `obj.field`).
722class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
723 public:
724 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
725 Location ref,
726 vixl32::Register obj,
727 Location field_offset,
728 vixl32::Register temp1,
729 vixl32::Register temp2)
730 : SlowPathCodeARMVIXL(instruction),
731 ref_(ref),
732 obj_(obj),
733 field_offset_(field_offset),
734 temp1_(temp1),
735 temp2_(temp2) {
736 DCHECK(kEmitCompilerReadBarrier);
737 }
738
739 const char* GetDescription() const OVERRIDE {
740 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
741 }
742
743 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
744 LocationSummary* locations = instruction_->GetLocations();
745 vixl32::Register ref_reg = RegisterFrom(ref_);
746 DCHECK(locations->CanCall());
747 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
748 // This slow path is only used by the UnsafeCASObject intrinsic.
749 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
750 << "Unexpected instruction in read barrier marking and field updating slow path: "
751 << instruction_->DebugName();
752 DCHECK(instruction_->GetLocations()->Intrinsified());
753 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
754 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
755
756 __ Bind(GetEntryLabel());
757
758 // Save the old reference.
759 // Note that we cannot use IP to save the old reference, as IP is
760 // used internally by the ReadBarrierMarkRegX entry point, and we
761 // need the old reference after the call to that entry point.
762 DCHECK(!temp1_.Is(ip));
763 __ Mov(temp1_, ref_reg);
764
765 // No need to save live registers; it's taken care of by the
766 // entrypoint. Also, there is no need to update the stack mask,
767 // as this runtime call will not trigger a garbage collection.
768 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
769 DCHECK(!ref_reg.Is(sp));
770 DCHECK(!ref_reg.Is(lr));
771 DCHECK(!ref_reg.Is(pc));
772 // IP is used internally by the ReadBarrierMarkRegX entry point
773 // as a temporary, it cannot be the entry point's input/output.
774 DCHECK(!ref_reg.Is(ip));
775 DCHECK(ref_reg.IsRegister()) << ref_reg;
776 // "Compact" slow path, saving two moves.
777 //
778 // Instead of using the standard runtime calling convention (input
779 // and output in R0):
780 //
781 // R0 <- ref
782 // R0 <- ReadBarrierMark(R0)
783 // ref <- R0
784 //
785 // we just use rX (the register containing `ref`) as input and output
786 // of a dedicated entrypoint:
787 //
788 // rX <- ReadBarrierMarkRegX(rX)
789 //
790 int32_t entry_point_offset =
791 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
792 // This runtime call does not require a stack map.
793 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
794
795 // If the new reference is different from the old reference,
796 // update the field in the holder (`*(obj_ + field_offset_)`).
797 //
798 // Note that this field could also hold a different object, if
799 // another thread had concurrently changed it. In that case, the
800 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
801 // (CAS) operation below would abort the CAS, leaving the field
802 // as-is.
803 vixl32::Label done;
804 __ Cmp(temp1_, ref_reg);
805 __ B(eq, &done);
806
807 // Update the the holder's field atomically. This may fail if
808 // mutator updates before us, but it's OK. This is achieved
809 // using a strong compare-and-set (CAS) operation with relaxed
810 // memory synchronization ordering, where the expected value is
811 // the old reference and the desired value is the new reference.
812
813 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
814 // Convenience aliases.
815 vixl32::Register base = obj_;
816 // The UnsafeCASObject intrinsic uses a register pair as field
817 // offset ("long offset"), of which only the low part contains
818 // data.
819 vixl32::Register offset = LowRegisterFrom(field_offset_);
820 vixl32::Register expected = temp1_;
821 vixl32::Register value = ref_reg;
822 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
823 vixl32::Register tmp = temp2_; // Value in memory.
824
825 __ Add(tmp_ptr, base, offset);
826
827 if (kPoisonHeapReferences) {
828 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
829 if (value.Is(expected)) {
830 // Do not poison `value`, as it is the same register as
831 // `expected`, which has just been poisoned.
832 } else {
833 arm_codegen->GetAssembler()->PoisonHeapReference(value);
834 }
835 }
836
837 // do {
838 // tmp = [r_ptr] - expected;
839 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
840
841 vixl32::Label loop_head, exit_loop;
842 __ Bind(&loop_head);
843
844 __ Ldrex(tmp, MemOperand(tmp_ptr));
845
846 __ Subs(tmp, tmp, expected);
847
848 {
Artem Serov0fb37192016-12-06 18:13:40 +0000849 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
850 2 * kMaxInstructionSizeInBytes,
851 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000852
853 __ it(ne);
854 __ clrex(ne);
855 }
856
857 __ B(ne, &exit_loop);
858
859 __ Strex(tmp, value, MemOperand(tmp_ptr));
860 __ Cmp(tmp, 1);
861 __ B(eq, &loop_head);
862
863 __ Bind(&exit_loop);
864
865 if (kPoisonHeapReferences) {
866 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
867 if (value.Is(expected)) {
868 // Do not unpoison `value`, as it is the same register as
869 // `expected`, which has just been unpoisoned.
870 } else {
871 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
872 }
873 }
874
875 __ Bind(&done);
876 __ B(GetExitLabel());
877 }
878
879 private:
880 // The location (register) of the marked object reference.
881 const Location ref_;
882 // The register containing the object holding the marked object reference field.
883 const vixl32::Register obj_;
884 // The location of the offset of the marked reference field within `obj_`.
885 Location field_offset_;
886
887 const vixl32::Register temp1_;
888 const vixl32::Register temp2_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
891};
892
893// Slow path generating a read barrier for a heap reference.
894class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
895 public:
896 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
897 Location out,
898 Location ref,
899 Location obj,
900 uint32_t offset,
901 Location index)
902 : SlowPathCodeARMVIXL(instruction),
903 out_(out),
904 ref_(ref),
905 obj_(obj),
906 offset_(offset),
907 index_(index) {
908 DCHECK(kEmitCompilerReadBarrier);
909 // If `obj` is equal to `out` or `ref`, it means the initial object
910 // has been overwritten by (or after) the heap object reference load
911 // to be instrumented, e.g.:
912 //
913 // __ LoadFromOffset(kLoadWord, out, out, offset);
914 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
915 //
916 // In that case, we have lost the information about the original
917 // object, and the emitted read barrier cannot work properly.
918 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
919 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
920 }
921
922 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
923 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
924 LocationSummary* locations = instruction_->GetLocations();
925 vixl32::Register reg_out = RegisterFrom(out_);
926 DCHECK(locations->CanCall());
927 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
928 DCHECK(instruction_->IsInstanceFieldGet() ||
929 instruction_->IsStaticFieldGet() ||
930 instruction_->IsArrayGet() ||
931 instruction_->IsInstanceOf() ||
932 instruction_->IsCheckCast() ||
933 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
934 << "Unexpected instruction in read barrier for heap reference slow path: "
935 << instruction_->DebugName();
936 // The read barrier instrumentation of object ArrayGet
937 // instructions does not support the HIntermediateAddress
938 // instruction.
939 DCHECK(!(instruction_->IsArrayGet() &&
940 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
941
942 __ Bind(GetEntryLabel());
943 SaveLiveRegisters(codegen, locations);
944
945 // We may have to change the index's value, but as `index_` is a
946 // constant member (like other "inputs" of this slow path),
947 // introduce a copy of it, `index`.
948 Location index = index_;
949 if (index_.IsValid()) {
950 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
951 if (instruction_->IsArrayGet()) {
952 // Compute the actual memory offset and store it in `index`.
953 vixl32::Register index_reg = RegisterFrom(index_);
954 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
955 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
956 // We are about to change the value of `index_reg` (see the
957 // calls to art::arm::Thumb2Assembler::Lsl and
958 // art::arm::Thumb2Assembler::AddConstant below), but it has
959 // not been saved by the previous call to
960 // art::SlowPathCode::SaveLiveRegisters, as it is a
961 // callee-save register --
962 // art::SlowPathCode::SaveLiveRegisters does not consider
963 // callee-save registers, as it has been designed with the
964 // assumption that callee-save registers are supposed to be
965 // handled by the called function. So, as a callee-save
966 // register, `index_reg` _would_ eventually be saved onto
967 // the stack, but it would be too late: we would have
968 // changed its value earlier. Therefore, we manually save
969 // it here into another freely available register,
970 // `free_reg`, chosen of course among the caller-save
971 // registers (as a callee-save `free_reg` register would
972 // exhibit the same problem).
973 //
974 // Note we could have requested a temporary register from
975 // the register allocator instead; but we prefer not to, as
976 // this is a slow path, and we know we can find a
977 // caller-save register that is available.
978 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
979 __ Mov(free_reg, index_reg);
980 index_reg = free_reg;
981 index = LocationFrom(index_reg);
982 } else {
983 // The initial register stored in `index_` has already been
984 // saved in the call to art::SlowPathCode::SaveLiveRegisters
985 // (as it is not a callee-save register), so we can freely
986 // use it.
987 }
988 // Shifting the index value contained in `index_reg` by the scale
989 // factor (2) cannot overflow in practice, as the runtime is
990 // unable to allocate object arrays with a size larger than
991 // 2^26 - 1 (that is, 2^28 - 4 bytes).
992 __ Lsl(index_reg, index_reg, TIMES_4);
993 static_assert(
994 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
995 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
996 __ Add(index_reg, index_reg, offset_);
997 } else {
998 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
999 // intrinsics, `index_` is not shifted by a scale factor of 2
1000 // (as in the case of ArrayGet), as it is actually an offset
1001 // to an object field within an object.
1002 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1003 DCHECK(instruction_->GetLocations()->Intrinsified());
1004 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1005 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1006 << instruction_->AsInvoke()->GetIntrinsic();
1007 DCHECK_EQ(offset_, 0U);
1008 DCHECK(index_.IsRegisterPair());
1009 // UnsafeGet's offset location is a register pair, the low
1010 // part contains the correct offset.
1011 index = index_.ToLow();
1012 }
1013 }
1014
1015 // We're moving two or three locations to locations that could
1016 // overlap, so we need a parallel move resolver.
1017 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1018 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1019 parallel_move.AddMove(ref_,
1020 LocationFrom(calling_convention.GetRegisterAt(0)),
1021 Primitive::kPrimNot,
1022 nullptr);
1023 parallel_move.AddMove(obj_,
1024 LocationFrom(calling_convention.GetRegisterAt(1)),
1025 Primitive::kPrimNot,
1026 nullptr);
1027 if (index.IsValid()) {
1028 parallel_move.AddMove(index,
1029 LocationFrom(calling_convention.GetRegisterAt(2)),
1030 Primitive::kPrimInt,
1031 nullptr);
1032 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1033 } else {
1034 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1035 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1036 }
1037 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1038 CheckEntrypointTypes<
1039 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1040 arm_codegen->Move32(out_, LocationFrom(r0));
1041
1042 RestoreLiveRegisters(codegen, locations);
1043 __ B(GetExitLabel());
1044 }
1045
1046 const char* GetDescription() const OVERRIDE {
1047 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1048 }
1049
1050 private:
1051 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1052 uint32_t ref = RegisterFrom(ref_).GetCode();
1053 uint32_t obj = RegisterFrom(obj_).GetCode();
1054 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1055 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1056 return vixl32::Register(i);
1057 }
1058 }
1059 // We shall never fail to find a free caller-save register, as
1060 // there are more than two core caller-save registers on ARM
1061 // (meaning it is possible to find one which is different from
1062 // `ref` and `obj`).
1063 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1064 LOG(FATAL) << "Could not find a free caller-save register";
1065 UNREACHABLE();
1066 }
1067
1068 const Location out_;
1069 const Location ref_;
1070 const Location obj_;
1071 const uint32_t offset_;
1072 // An additional location containing an index to an array.
1073 // Only used for HArrayGet and the UnsafeGetObject &
1074 // UnsafeGetObjectVolatile intrinsics.
1075 const Location index_;
1076
1077 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1078};
1079
1080// Slow path generating a read barrier for a GC root.
1081class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1082 public:
1083 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1084 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1085 DCHECK(kEmitCompilerReadBarrier);
1086 }
1087
1088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1089 LocationSummary* locations = instruction_->GetLocations();
1090 vixl32::Register reg_out = RegisterFrom(out_);
1091 DCHECK(locations->CanCall());
1092 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1093 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1094 << "Unexpected instruction in read barrier for GC root slow path: "
1095 << instruction_->DebugName();
1096
1097 __ Bind(GetEntryLabel());
1098 SaveLiveRegisters(codegen, locations);
1099
1100 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1101 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1102 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1103 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1104 instruction_,
1105 instruction_->GetDexPc(),
1106 this);
1107 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1108 arm_codegen->Move32(out_, LocationFrom(r0));
1109
1110 RestoreLiveRegisters(codegen, locations);
1111 __ B(GetExitLabel());
1112 }
1113
1114 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1115
1116 private:
1117 const Location out_;
1118 const Location root_;
1119
1120 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1121};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001122
Scott Wakelingfe885462016-09-22 10:24:38 +01001123inline vixl32::Condition ARMCondition(IfCondition cond) {
1124 switch (cond) {
1125 case kCondEQ: return eq;
1126 case kCondNE: return ne;
1127 case kCondLT: return lt;
1128 case kCondLE: return le;
1129 case kCondGT: return gt;
1130 case kCondGE: return ge;
1131 case kCondB: return lo;
1132 case kCondBE: return ls;
1133 case kCondA: return hi;
1134 case kCondAE: return hs;
1135 }
1136 LOG(FATAL) << "Unreachable";
1137 UNREACHABLE();
1138}
1139
1140// Maps signed condition to unsigned condition.
1141inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1142 switch (cond) {
1143 case kCondEQ: return eq;
1144 case kCondNE: return ne;
1145 // Signed to unsigned.
1146 case kCondLT: return lo;
1147 case kCondLE: return ls;
1148 case kCondGT: return hi;
1149 case kCondGE: return hs;
1150 // Unsigned remain unchanged.
1151 case kCondB: return lo;
1152 case kCondBE: return ls;
1153 case kCondA: return hi;
1154 case kCondAE: return hs;
1155 }
1156 LOG(FATAL) << "Unreachable";
1157 UNREACHABLE();
1158}
1159
1160inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1161 // The ARM condition codes can express all the necessary branches, see the
1162 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1163 // There is no dex instruction or HIR that would need the missing conditions
1164 // "equal or unordered" or "not equal".
1165 switch (cond) {
1166 case kCondEQ: return eq;
1167 case kCondNE: return ne /* unordered */;
1168 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1169 case kCondLE: return gt_bias ? ls : le /* unordered */;
1170 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1171 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1172 default:
1173 LOG(FATAL) << "UNREACHABLE";
1174 UNREACHABLE();
1175 }
1176}
1177
Scott Wakelingfe885462016-09-22 10:24:38 +01001178void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1179 stream << vixl32::Register(reg);
1180}
1181
1182void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1183 stream << vixl32::SRegister(reg);
1184}
1185
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001186static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001187 uint32_t mask = 0;
1188 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1189 i <= regs.GetLastSRegister().GetCode();
1190 ++i) {
1191 mask |= (1 << i);
1192 }
1193 return mask;
1194}
1195
Artem Serovd4cc5b22016-11-04 11:19:09 +00001196// Saves the register in the stack. Returns the size taken on stack.
1197size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1198 uint32_t reg_id ATTRIBUTE_UNUSED) {
1199 TODO_VIXL32(FATAL);
1200 return 0;
1201}
1202
1203// Restores the register from the stack. Returns the size taken on stack.
1204size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1205 uint32_t reg_id ATTRIBUTE_UNUSED) {
1206 TODO_VIXL32(FATAL);
1207 return 0;
1208}
1209
1210size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1211 uint32_t reg_id ATTRIBUTE_UNUSED) {
1212 TODO_VIXL32(FATAL);
1213 return 0;
1214}
1215
1216size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1217 uint32_t reg_id ATTRIBUTE_UNUSED) {
1218 TODO_VIXL32(FATAL);
1219 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001220}
1221
Scott Wakelingfe885462016-09-22 10:24:38 +01001222#undef __
1223
1224CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1225 const ArmInstructionSetFeatures& isa_features,
1226 const CompilerOptions& compiler_options,
1227 OptimizingCompilerStats* stats)
1228 : CodeGenerator(graph,
1229 kNumberOfCoreRegisters,
1230 kNumberOfSRegisters,
1231 kNumberOfRegisterPairs,
1232 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001233 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001234 compiler_options,
1235 stats),
1236 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001237 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001238 location_builder_(graph, this),
1239 instruction_visitor_(graph, this),
1240 move_resolver_(graph->GetArena(), this),
1241 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001242 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001243 uint32_literals_(std::less<uint32_t>(),
1244 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1245 method_patches_(MethodReferenceComparator(),
1246 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1247 call_patches_(MethodReferenceComparator(),
1248 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001249 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1250 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001251 boot_image_string_patches_(StringReferenceValueComparator(),
1252 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001253 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001254 boot_image_type_patches_(TypeReferenceValueComparator(),
1255 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1256 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1257 boot_image_address_patches_(std::less<uint32_t>(),
1258 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1259 jit_string_patches_(StringReferenceValueComparator(),
1260 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1261 jit_class_patches_(TypeReferenceValueComparator(),
1262 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001263 // Always save the LR register to mimic Quick.
1264 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001265 // Give d14 and d15 as scratch registers to VIXL.
1266 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1267 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1268 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1269 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1270 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1271 // also want to investigate giving those 14 other D registers to the allocator.
1272 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1273 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001274}
1275
Artem Serov551b28f2016-10-18 19:11:30 +01001276void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1277 uint32_t num_entries = switch_instr_->GetNumEntries();
1278 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1279
1280 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001281 // underlying code buffer and we have generated a jump table of the right size, using
1282 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001283 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1284 num_entries * sizeof(int32_t),
1285 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001286 // TODO(VIXL): Check that using lower case bind is fine here.
1287 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001288 for (uint32_t i = 0; i < num_entries; i++) {
1289 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1290 }
1291}
1292
1293void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1294 uint32_t num_entries = switch_instr_->GetNumEntries();
1295 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1296
Artem Serov551b28f2016-10-18 19:11:30 +01001297 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1298 for (uint32_t i = 0; i < num_entries; i++) {
1299 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1300 DCHECK(target_label->IsBound());
1301 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1302 // When doing BX to address we need to have lower bit set to 1 in T32.
1303 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1304 jump_offset++;
1305 }
1306 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1307 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001308
Scott Wakelingb77051e2016-11-21 19:46:00 +00001309 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001310 }
1311}
1312
Artem Serov09a940d2016-11-11 16:15:11 +00001313void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001314 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001315 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001316 }
1317}
1318
Andreas Gampeca620d72016-11-08 08:09:33 -08001319#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001320
1321void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001322 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001323 GetAssembler()->FinalizeCode();
1324 CodeGenerator::Finalize(allocator);
1325}
1326
1327void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001328 // Stack register, LR and PC are always reserved.
1329 blocked_core_registers_[SP] = true;
1330 blocked_core_registers_[LR] = true;
1331 blocked_core_registers_[PC] = true;
1332
1333 // Reserve thread register.
1334 blocked_core_registers_[TR] = true;
1335
1336 // Reserve temp register.
1337 blocked_core_registers_[IP] = true;
1338
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001339 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1340 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1341 blocked_fpu_registers_[28] = true;
1342 blocked_fpu_registers_[29] = true;
1343 blocked_fpu_registers_[30] = true;
1344 blocked_fpu_registers_[31] = true;
1345
Scott Wakelingfe885462016-09-22 10:24:38 +01001346 if (GetGraph()->IsDebuggable()) {
1347 // Stubs do not save callee-save floating point registers. If the graph
1348 // is debuggable, we need to deal with these registers differently. For
1349 // now, just block them.
1350 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1351 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1352 ++i) {
1353 blocked_fpu_registers_[i] = true;
1354 }
1355 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001356}
1357
Scott Wakelingfe885462016-09-22 10:24:38 +01001358InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1359 CodeGeneratorARMVIXL* codegen)
1360 : InstructionCodeGenerator(graph, codegen),
1361 assembler_(codegen->GetAssembler()),
1362 codegen_(codegen) {}
1363
1364void CodeGeneratorARMVIXL::ComputeSpillMask() {
1365 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1366 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1367 // There is no easy instruction to restore just the PC on thumb2. We spill and
1368 // restore another arbitrary register.
1369 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1370 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1371 // We use vpush and vpop for saving and restoring floating point registers, which take
1372 // a SRegister and the number of registers to save/restore after that SRegister. We
1373 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1374 // but in the range.
1375 if (fpu_spill_mask_ != 0) {
1376 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1377 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1378 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1379 fpu_spill_mask_ |= (1 << i);
1380 }
1381 }
1382}
1383
1384void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1385 bool skip_overflow_check =
1386 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1387 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1388 __ Bind(&frame_entry_label_);
1389
1390 if (HasEmptyFrame()) {
1391 return;
1392 }
1393
Scott Wakelingfe885462016-09-22 10:24:38 +01001394 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001395 UseScratchRegisterScope temps(GetVIXLAssembler());
1396 vixl32::Register temp = temps.Acquire();
Scott Wakelingfe885462016-09-22 10:24:38 +01001397 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1398 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001399 ExactAssemblyScope aas(GetVIXLAssembler(),
1400 vixl32::kMaxInstructionSizeInBytes,
1401 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001402 __ ldr(temp, MemOperand(temp));
1403 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001404 }
1405
1406 __ Push(RegisterList(core_spill_mask_));
1407 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1408 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1409 0,
1410 core_spill_mask_,
1411 kArmWordSize);
1412 if (fpu_spill_mask_ != 0) {
1413 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1414
1415 // Check that list is contiguous.
1416 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1417
1418 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1419 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001420 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001421 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001422
1423 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1424 UseScratchRegisterScope temps(GetVIXLAssembler());
1425 vixl32::Register temp = temps.Acquire();
1426 // Initialize should_deoptimize flag to 0.
1427 __ Mov(temp, 0);
1428 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1429 }
1430
Scott Wakelingfe885462016-09-22 10:24:38 +01001431 int adjust = GetFrameSize() - FrameEntrySpillSize();
1432 __ Sub(sp, sp, adjust);
1433 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001434
1435 // Save the current method if we need it. Note that we do not
1436 // do this in HCurrentMethod, as the instruction might have been removed
1437 // in the SSA graph.
1438 if (RequiresCurrentMethod()) {
1439 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1440 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001441}
1442
1443void CodeGeneratorARMVIXL::GenerateFrameExit() {
1444 if (HasEmptyFrame()) {
1445 __ Bx(lr);
1446 return;
1447 }
1448 GetAssembler()->cfi().RememberState();
1449 int adjust = GetFrameSize() - FrameEntrySpillSize();
1450 __ Add(sp, sp, adjust);
1451 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1452 if (fpu_spill_mask_ != 0) {
1453 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1454
1455 // Check that list is contiguous.
1456 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1457
1458 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1459 GetAssembler()->cfi().AdjustCFAOffset(
1460 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001461 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001462 }
1463 // Pop LR into PC to return.
1464 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1465 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1466 __ Pop(RegisterList(pop_mask));
1467 GetAssembler()->cfi().RestoreState();
1468 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1469}
1470
1471void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1472 __ Bind(GetLabelOf(block));
1473}
1474
Artem Serovd4cc5b22016-11-04 11:19:09 +00001475Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1476 switch (type) {
1477 case Primitive::kPrimBoolean:
1478 case Primitive::kPrimByte:
1479 case Primitive::kPrimChar:
1480 case Primitive::kPrimShort:
1481 case Primitive::kPrimInt:
1482 case Primitive::kPrimNot: {
1483 uint32_t index = gp_index_++;
1484 uint32_t stack_index = stack_index_++;
1485 if (index < calling_convention.GetNumberOfRegisters()) {
1486 return LocationFrom(calling_convention.GetRegisterAt(index));
1487 } else {
1488 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1489 }
1490 }
1491
1492 case Primitive::kPrimLong: {
1493 uint32_t index = gp_index_;
1494 uint32_t stack_index = stack_index_;
1495 gp_index_ += 2;
1496 stack_index_ += 2;
1497 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1498 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1499 // Skip R1, and use R2_R3 instead.
1500 gp_index_++;
1501 index++;
1502 }
1503 }
1504 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1505 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1506 calling_convention.GetRegisterAt(index + 1).GetCode());
1507
1508 return LocationFrom(calling_convention.GetRegisterAt(index),
1509 calling_convention.GetRegisterAt(index + 1));
1510 } else {
1511 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1512 }
1513 }
1514
1515 case Primitive::kPrimFloat: {
1516 uint32_t stack_index = stack_index_++;
1517 if (float_index_ % 2 == 0) {
1518 float_index_ = std::max(double_index_, float_index_);
1519 }
1520 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1521 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1522 } else {
1523 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1524 }
1525 }
1526
1527 case Primitive::kPrimDouble: {
1528 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1529 uint32_t stack_index = stack_index_;
1530 stack_index_ += 2;
1531 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1532 uint32_t index = double_index_;
1533 double_index_ += 2;
1534 Location result = LocationFrom(
1535 calling_convention.GetFpuRegisterAt(index),
1536 calling_convention.GetFpuRegisterAt(index + 1));
1537 DCHECK(ExpectedPairLayout(result));
1538 return result;
1539 } else {
1540 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1541 }
1542 }
1543
1544 case Primitive::kPrimVoid:
1545 LOG(FATAL) << "Unexpected parameter type " << type;
1546 break;
1547 }
1548 return Location::NoLocation();
1549}
1550
1551Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1552 switch (type) {
1553 case Primitive::kPrimBoolean:
1554 case Primitive::kPrimByte:
1555 case Primitive::kPrimChar:
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimNot: {
1559 return LocationFrom(r0);
1560 }
1561
1562 case Primitive::kPrimFloat: {
1563 return LocationFrom(s0);
1564 }
1565
1566 case Primitive::kPrimLong: {
1567 return LocationFrom(r0, r1);
1568 }
1569
1570 case Primitive::kPrimDouble: {
1571 return LocationFrom(s0, s1);
1572 }
1573
1574 case Primitive::kPrimVoid:
1575 return Location::NoLocation();
1576 }
1577
1578 UNREACHABLE();
1579}
1580
1581Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1582 return LocationFrom(kMethodRegister);
1583}
1584
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001585void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1586 if (source.Equals(destination)) {
1587 return;
1588 }
1589 if (destination.IsRegister()) {
1590 if (source.IsRegister()) {
1591 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1592 } else if (source.IsFpuRegister()) {
1593 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1594 } else {
1595 GetAssembler()->LoadFromOffset(kLoadWord,
1596 RegisterFrom(destination),
1597 sp,
1598 source.GetStackIndex());
1599 }
1600 } else if (destination.IsFpuRegister()) {
1601 if (source.IsRegister()) {
1602 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1603 } else if (source.IsFpuRegister()) {
1604 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1605 } else {
1606 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1607 }
1608 } else {
1609 DCHECK(destination.IsStackSlot()) << destination;
1610 if (source.IsRegister()) {
1611 GetAssembler()->StoreToOffset(kStoreWord,
1612 RegisterFrom(source),
1613 sp,
1614 destination.GetStackIndex());
1615 } else if (source.IsFpuRegister()) {
1616 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1617 } else {
1618 DCHECK(source.IsStackSlot()) << source;
1619 UseScratchRegisterScope temps(GetVIXLAssembler());
1620 vixl32::Register temp = temps.Acquire();
1621 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1622 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1623 }
1624 }
1625}
1626
Artem Serovcfbe9132016-10-14 15:58:56 +01001627void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1628 DCHECK(location.IsRegister());
1629 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001630}
1631
1632void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001633 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1634 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1635 HParallelMove move(GetGraph()->GetArena());
1636 move.AddMove(src, dst, dst_type, nullptr);
1637 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001638}
1639
Artem Serovcfbe9132016-10-14 15:58:56 +01001640void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1641 if (location.IsRegister()) {
1642 locations->AddTemp(location);
1643 } else if (location.IsRegisterPair()) {
1644 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1645 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1646 } else {
1647 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1648 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001649}
1650
1651void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1652 HInstruction* instruction,
1653 uint32_t dex_pc,
1654 SlowPathCode* slow_path) {
1655 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001656 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1657 // Ensure the pc position is recorded immediately after the `blx` instruction.
1658 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00001659 ExactAssemblyScope aas(GetVIXLAssembler(),
1660 vixl32::k16BitT32InstructionSizeInBytes,
1661 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001662 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001663 if (EntrypointRequiresStackMap(entrypoint)) {
1664 RecordPcInfo(instruction, dex_pc, slow_path);
1665 }
1666}
1667
1668void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1669 HInstruction* instruction,
1670 SlowPathCode* slow_path) {
1671 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001672 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001673 __ Blx(lr);
1674}
1675
Scott Wakelingfe885462016-09-22 10:24:38 +01001676void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1677 DCHECK(!successor->IsExitBlock());
1678 HBasicBlock* block = got->GetBlock();
1679 HInstruction* previous = got->GetPrevious();
1680 HLoopInformation* info = block->GetLoopInformation();
1681
1682 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1683 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1684 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1685 return;
1686 }
1687 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1688 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1689 }
1690 if (!codegen_->GoesToNextBlock(block, successor)) {
1691 __ B(codegen_->GetLabelOf(successor));
1692 }
1693}
1694
1695void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1696 got->SetLocations(nullptr);
1697}
1698
1699void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1700 HandleGoto(got, got->GetSuccessor());
1701}
1702
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001703void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1704 try_boundary->SetLocations(nullptr);
1705}
1706
1707void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1708 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1709 if (!successor->IsExitBlock()) {
1710 HandleGoto(try_boundary, successor);
1711 }
1712}
1713
Scott Wakelingfe885462016-09-22 10:24:38 +01001714void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1715 exit->SetLocations(nullptr);
1716}
1717
1718void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1719}
1720
1721void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1722 Primitive::Type type = instruction->InputAt(0)->GetType();
1723 Location lhs_loc = instruction->GetLocations()->InAt(0);
1724 Location rhs_loc = instruction->GetLocations()->InAt(1);
1725 if (rhs_loc.IsConstant()) {
1726 // 0.0 is the only immediate that can be encoded directly in
1727 // a VCMP instruction.
1728 //
1729 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1730 // specify that in a floating-point comparison, positive zero
1731 // and negative zero are considered equal, so we can use the
1732 // literal 0.0 for both cases here.
1733 //
1734 // Note however that some methods (Float.equal, Float.compare,
1735 // Float.compareTo, Double.equal, Double.compare,
1736 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1737 // StrictMath.min) consider 0.0 to be (strictly) greater than
1738 // -0.0. So if we ever translate calls to these methods into a
1739 // HCompare instruction, we must handle the -0.0 case with
1740 // care here.
1741 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1742 if (type == Primitive::kPrimFloat) {
1743 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1744 } else {
1745 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001746 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001747 }
1748 } else {
1749 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001750 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001751 } else {
1752 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001753 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001754 }
1755 }
1756}
1757
1758void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1759 vixl32::Label* true_label,
1760 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1761 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1762 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1763 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1764}
1765
1766void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1767 vixl32::Label* true_label,
1768 vixl32::Label* false_label) {
1769 LocationSummary* locations = cond->GetLocations();
1770 Location left = locations->InAt(0);
1771 Location right = locations->InAt(1);
1772 IfCondition if_cond = cond->GetCondition();
1773
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001774 vixl32::Register left_high = HighRegisterFrom(left);
1775 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01001776 IfCondition true_high_cond = if_cond;
1777 IfCondition false_high_cond = cond->GetOppositeCondition();
1778 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
1779
1780 // Set the conditions for the test, remembering that == needs to be
1781 // decided using the low words.
1782 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1783 switch (if_cond) {
1784 case kCondEQ:
1785 case kCondNE:
1786 // Nothing to do.
1787 break;
1788 case kCondLT:
1789 false_high_cond = kCondGT;
1790 break;
1791 case kCondLE:
1792 true_high_cond = kCondLT;
1793 break;
1794 case kCondGT:
1795 false_high_cond = kCondLT;
1796 break;
1797 case kCondGE:
1798 true_high_cond = kCondGT;
1799 break;
1800 case kCondB:
1801 false_high_cond = kCondA;
1802 break;
1803 case kCondBE:
1804 true_high_cond = kCondB;
1805 break;
1806 case kCondA:
1807 false_high_cond = kCondB;
1808 break;
1809 case kCondAE:
1810 true_high_cond = kCondA;
1811 break;
1812 }
1813 if (right.IsConstant()) {
1814 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1815 int32_t val_low = Low32Bits(value);
1816 int32_t val_high = High32Bits(value);
1817
1818 __ Cmp(left_high, val_high);
1819 if (if_cond == kCondNE) {
1820 __ B(ARMCondition(true_high_cond), true_label);
1821 } else if (if_cond == kCondEQ) {
1822 __ B(ARMCondition(false_high_cond), false_label);
1823 } else {
1824 __ B(ARMCondition(true_high_cond), true_label);
1825 __ B(ARMCondition(false_high_cond), false_label);
1826 }
1827 // Must be equal high, so compare the lows.
1828 __ Cmp(left_low, val_low);
1829 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001830 vixl32::Register right_high = HighRegisterFrom(right);
1831 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001832
1833 __ Cmp(left_high, right_high);
1834 if (if_cond == kCondNE) {
1835 __ B(ARMCondition(true_high_cond), true_label);
1836 } else if (if_cond == kCondEQ) {
1837 __ B(ARMCondition(false_high_cond), false_label);
1838 } else {
1839 __ B(ARMCondition(true_high_cond), true_label);
1840 __ B(ARMCondition(false_high_cond), false_label);
1841 }
1842 // Must be equal high, so compare the lows.
1843 __ Cmp(left_low, right_low);
1844 }
1845 // The last comparison might be unsigned.
1846 // TODO: optimize cases where this is always true/false
1847 __ B(final_condition, true_label);
1848}
1849
1850void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1851 vixl32::Label* true_target_in,
1852 vixl32::Label* false_target_in) {
1853 // Generated branching requires both targets to be explicit. If either of the
1854 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1855 vixl32::Label fallthrough;
1856 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1857 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1858
1859 Primitive::Type type = condition->InputAt(0)->GetType();
1860 switch (type) {
1861 case Primitive::kPrimLong:
1862 GenerateLongComparesAndJumps(condition, true_target, false_target);
1863 break;
1864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble:
1866 GenerateVcmp(condition);
1867 GenerateFPJumps(condition, true_target, false_target);
1868 break;
1869 default:
1870 LOG(FATAL) << "Unexpected compare type " << type;
1871 }
1872
1873 if (false_target != &fallthrough) {
1874 __ B(false_target);
1875 }
1876
1877 if (true_target_in == nullptr || false_target_in == nullptr) {
1878 __ Bind(&fallthrough);
1879 }
1880}
1881
1882void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1883 size_t condition_input_index,
1884 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00001885 vixl32::Label* false_target,
1886 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001887 HInstruction* cond = instruction->InputAt(condition_input_index);
1888
1889 if (true_target == nullptr && false_target == nullptr) {
1890 // Nothing to do. The code always falls through.
1891 return;
1892 } else if (cond->IsIntConstant()) {
1893 // Constant condition, statically compared against "true" (integer value 1).
1894 if (cond->AsIntConstant()->IsTrue()) {
1895 if (true_target != nullptr) {
1896 __ B(true_target);
1897 }
1898 } else {
1899 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
1900 if (false_target != nullptr) {
1901 __ B(false_target);
1902 }
1903 }
1904 return;
1905 }
1906
1907 // The following code generates these patterns:
1908 // (1) true_target == nullptr && false_target != nullptr
1909 // - opposite condition true => branch to false_target
1910 // (2) true_target != nullptr && false_target == nullptr
1911 // - condition true => branch to true_target
1912 // (3) true_target != nullptr && false_target != nullptr
1913 // - condition true => branch to true_target
1914 // - branch to false_target
1915 if (IsBooleanValueOrMaterializedCondition(cond)) {
1916 // Condition has been materialized, compare the output to 0.
1917 if (kIsDebugBuild) {
1918 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1919 DCHECK(cond_val.IsRegister());
1920 }
1921 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001922 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
1923 false_target,
1924 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001925 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001926 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
1927 true_target,
1928 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001929 }
1930 } else {
1931 // Condition has not been materialized. Use its inputs as the comparison and
1932 // its condition as the branch condition.
1933 HCondition* condition = cond->AsCondition();
1934
1935 // If this is a long or FP comparison that has been folded into
1936 // the HCondition, generate the comparison directly.
1937 Primitive::Type type = condition->InputAt(0)->GetType();
1938 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1939 GenerateCompareTestAndBranch(condition, true_target, false_target);
1940 return;
1941 }
1942
1943 LocationSummary* locations = cond->GetLocations();
1944 DCHECK(locations->InAt(0).IsRegister());
1945 vixl32::Register left = InputRegisterAt(cond, 0);
1946 Location right = locations->InAt(1);
1947 if (right.IsRegister()) {
1948 __ Cmp(left, InputRegisterAt(cond, 1));
1949 } else {
1950 DCHECK(right.IsConstant());
1951 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1952 }
1953 if (true_target == nullptr) {
1954 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1955 } else {
1956 __ B(ARMCondition(condition->GetCondition()), true_target);
1957 }
1958 }
1959
1960 // If neither branch falls through (case 3), the conditional branch to `true_target`
1961 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1962 if (true_target != nullptr && false_target != nullptr) {
1963 __ B(false_target);
1964 }
1965}
1966
1967void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1969 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1970 locations->SetInAt(0, Location::RequiresRegister());
1971 }
1972}
1973
1974void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1975 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1976 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001977 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1978 nullptr : codegen_->GetLabelOf(true_successor);
1979 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1980 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001981 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1982}
1983
Scott Wakelingc34dba72016-10-03 10:14:44 +01001984void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1985 LocationSummary* locations = new (GetGraph()->GetArena())
1986 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1987 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1988 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 }
1991}
1992
1993void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1994 SlowPathCodeARMVIXL* slow_path =
1995 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
1996 GenerateTestAndBranch(deoptimize,
1997 /* condition_input_index */ 0,
1998 slow_path->GetEntryLabel(),
1999 /* false_target */ nullptr);
2000}
2001
Artem Serovd4cc5b22016-11-04 11:19:09 +00002002void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2003 LocationSummary* locations = new (GetGraph()->GetArena())
2004 LocationSummary(flag, LocationSummary::kNoCall);
2005 locations->SetOut(Location::RequiresRegister());
2006}
2007
2008void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2009 GetAssembler()->LoadFromOffset(kLoadWord,
2010 OutputRegister(flag),
2011 sp,
2012 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2013}
2014
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002015void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2017 if (Primitive::IsFloatingPointType(select->GetType())) {
2018 locations->SetInAt(0, Location::RequiresFpuRegister());
2019 locations->SetInAt(1, Location::RequiresFpuRegister());
2020 } else {
2021 locations->SetInAt(0, Location::RequiresRegister());
2022 locations->SetInAt(1, Location::RequiresRegister());
2023 }
2024 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2025 locations->SetInAt(2, Location::RequiresRegister());
2026 }
2027 locations->SetOut(Location::SameAsFirstInput());
2028}
2029
2030void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2031 LocationSummary* locations = select->GetLocations();
2032 vixl32::Label false_target;
2033 GenerateTestAndBranch(select,
2034 /* condition_input_index */ 2,
2035 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002036 &false_target,
2037 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002038 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2039 __ Bind(&false_target);
2040}
2041
Artem Serov551b28f2016-10-18 19:11:30 +01002042void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2043 new (GetGraph()->GetArena()) LocationSummary(info);
2044}
2045
2046void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2047 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2048}
2049
Scott Wakelingfe885462016-09-22 10:24:38 +01002050void CodeGeneratorARMVIXL::GenerateNop() {
2051 __ Nop();
2052}
2053
2054void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2057 // Handle the long/FP comparisons made in instruction simplification.
2058 switch (cond->InputAt(0)->GetType()) {
2059 case Primitive::kPrimLong:
2060 locations->SetInAt(0, Location::RequiresRegister());
2061 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2062 if (!cond->IsEmittedAtUseSite()) {
2063 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2064 }
2065 break;
2066
Scott Wakelingfe885462016-09-22 10:24:38 +01002067 case Primitive::kPrimFloat:
2068 case Primitive::kPrimDouble:
2069 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002070 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002071 if (!cond->IsEmittedAtUseSite()) {
2072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2073 }
2074 break;
2075
2076 default:
2077 locations->SetInAt(0, Location::RequiresRegister());
2078 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2079 if (!cond->IsEmittedAtUseSite()) {
2080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2081 }
2082 }
2083}
2084
2085void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2086 if (cond->IsEmittedAtUseSite()) {
2087 return;
2088 }
2089
Artem Serov657022c2016-11-23 14:19:38 +00002090 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002091 vixl32::Register out = OutputRegister(cond);
2092 vixl32::Label true_label, false_label;
2093
2094 switch (cond->InputAt(0)->GetType()) {
2095 default: {
2096 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002097 if (right.IsRegister()) {
2098 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2099 } else {
2100 DCHECK(right.IsConstant());
2101 __ Cmp(InputRegisterAt(cond, 0),
2102 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2103 }
Artem Serov0fb37192016-12-06 18:13:40 +00002104 ExactAssemblyScope aas(GetVIXLAssembler(),
2105 3 * vixl32::kMaxInstructionSizeInBytes,
2106 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002107 __ ite(ARMCondition(cond->GetCondition()));
2108 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2109 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002110 return;
2111 }
2112 case Primitive::kPrimLong:
2113 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2114 break;
2115 case Primitive::kPrimFloat:
2116 case Primitive::kPrimDouble:
2117 GenerateVcmp(cond);
2118 GenerateFPJumps(cond, &true_label, &false_label);
2119 break;
2120 }
2121
2122 // Convert the jumps into the result.
2123 vixl32::Label done_label;
2124
2125 // False case: result = 0.
2126 __ Bind(&false_label);
2127 __ Mov(out, 0);
2128 __ B(&done_label);
2129
2130 // True case: result = 1.
2131 __ Bind(&true_label);
2132 __ Mov(out, 1);
2133 __ Bind(&done_label);
2134}
2135
2136void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2137 HandleCondition(comp);
2138}
2139
2140void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2141 HandleCondition(comp);
2142}
2143
2144void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2145 HandleCondition(comp);
2146}
2147
2148void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2149 HandleCondition(comp);
2150}
2151
2152void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2153 HandleCondition(comp);
2154}
2155
2156void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2157 HandleCondition(comp);
2158}
2159
2160void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2161 HandleCondition(comp);
2162}
2163
2164void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2165 HandleCondition(comp);
2166}
2167
2168void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2169 HandleCondition(comp);
2170}
2171
2172void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2173 HandleCondition(comp);
2174}
2175
2176void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2177 HandleCondition(comp);
2178}
2179
2180void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2181 HandleCondition(comp);
2182}
2183
2184void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2185 HandleCondition(comp);
2186}
2187
2188void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2189 HandleCondition(comp);
2190}
2191
2192void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2193 HandleCondition(comp);
2194}
2195
2196void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2197 HandleCondition(comp);
2198}
2199
2200void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2201 HandleCondition(comp);
2202}
2203
2204void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2205 HandleCondition(comp);
2206}
2207
2208void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2209 HandleCondition(comp);
2210}
2211
2212void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2213 HandleCondition(comp);
2214}
2215
2216void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2217 LocationSummary* locations =
2218 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2219 locations->SetOut(Location::ConstantLocation(constant));
2220}
2221
2222void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2223 // Will be generated at use site.
2224}
2225
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002226void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2227 LocationSummary* locations =
2228 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2229 locations->SetOut(Location::ConstantLocation(constant));
2230}
2231
2232void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2233 // Will be generated at use site.
2234}
2235
Scott Wakelingfe885462016-09-22 10:24:38 +01002236void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2237 LocationSummary* locations =
2238 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2239 locations->SetOut(Location::ConstantLocation(constant));
2240}
2241
2242void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2243 // Will be generated at use site.
2244}
2245
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002246void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2247 LocationSummary* locations =
2248 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2249 locations->SetOut(Location::ConstantLocation(constant));
2250}
2251
Scott Wakelingc34dba72016-10-03 10:14:44 +01002252void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2253 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002254 // Will be generated at use site.
2255}
2256
2257void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2258 LocationSummary* locations =
2259 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2260 locations->SetOut(Location::ConstantLocation(constant));
2261}
2262
Scott Wakelingc34dba72016-10-03 10:14:44 +01002263void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2264 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002265 // Will be generated at use site.
2266}
2267
Scott Wakelingfe885462016-09-22 10:24:38 +01002268void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2269 memory_barrier->SetLocations(nullptr);
2270}
2271
2272void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2273 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2274}
2275
2276void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2277 ret->SetLocations(nullptr);
2278}
2279
2280void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2281 codegen_->GenerateFrameExit();
2282}
2283
2284void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2285 LocationSummary* locations =
2286 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2287 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2288}
2289
2290void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2291 codegen_->GenerateFrameExit();
2292}
2293
Artem Serovcfbe9132016-10-14 15:58:56 +01002294void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2295 // The trampoline uses the same calling convention as dex calling conventions,
2296 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2297 // the method_idx.
2298 HandleInvoke(invoke);
2299}
2300
2301void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2303}
2304
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002305void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2306 // Explicit clinit checks triggered by static invokes must have been pruned by
2307 // art::PrepareForRegisterAllocation.
2308 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2309
Anton Kirilov5ec62182016-10-13 20:16:02 +01002310 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2311 if (intrinsic.TryDispatch(invoke)) {
2312 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2313 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2314 }
2315 return;
2316 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002317
2318 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002319
Artem Serovd4cc5b22016-11-04 11:19:09 +00002320 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2321 if (invoke->HasPcRelativeDexCache()) {
2322 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2323 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002324}
2325
Anton Kirilov5ec62182016-10-13 20:16:02 +01002326static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2327 if (invoke->GetLocations()->Intrinsified()) {
2328 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2329 intrinsic.Dispatch(invoke);
2330 return true;
2331 }
2332 return false;
2333}
2334
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002335void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2336 // Explicit clinit checks triggered by static invokes must have been pruned by
2337 // art::PrepareForRegisterAllocation.
2338 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2339
Anton Kirilov5ec62182016-10-13 20:16:02 +01002340 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2341 return;
2342 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002343
2344 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002345 codegen_->GenerateStaticOrDirectCall(
2346 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002347 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2348}
2349
2350void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002351 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002352 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2353}
2354
2355void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002356 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2357 if (intrinsic.TryDispatch(invoke)) {
2358 return;
2359 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002360
2361 HandleInvoke(invoke);
2362}
2363
2364void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002365 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2366 return;
2367 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002368
2369 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002370 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002371 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002372}
2373
Artem Serovcfbe9132016-10-14 15:58:56 +01002374void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2375 HandleInvoke(invoke);
2376 // Add the hidden argument.
2377 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2378}
2379
2380void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2381 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2382 LocationSummary* locations = invoke->GetLocations();
2383 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2384 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2385 Location receiver = locations->InAt(0);
2386 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2387
2388 DCHECK(!receiver.IsStackSlot());
2389
Alexandre Rames374ddf32016-11-04 10:40:49 +00002390 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2391 {
Artem Serov0fb37192016-12-06 18:13:40 +00002392 ExactAssemblyScope aas(GetVIXLAssembler(),
2393 vixl32::kMaxInstructionSizeInBytes,
2394 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002395 // /* HeapReference<Class> */ temp = receiver->klass_
2396 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2397 codegen_->MaybeRecordImplicitNullCheck(invoke);
2398 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002399 // Instead of simply (possibly) unpoisoning `temp` here, we should
2400 // emit a read barrier for the previous class reference load.
2401 // However this is not required in practice, as this is an
2402 // intermediate/temporary reference and because the current
2403 // concurrent copying collector keeps the from-space memory
2404 // intact/accessible until the end of the marking phase (the
2405 // concurrent copying collector may not in the future).
2406 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2407 GetAssembler()->LoadFromOffset(kLoadWord,
2408 temp,
2409 temp,
2410 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2411 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2412 invoke->GetImtIndex(), kArmPointerSize));
2413 // temp = temp->GetImtEntryAt(method_offset);
2414 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2415 uint32_t entry_point =
2416 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2417 // LR = temp->GetEntryPoint();
2418 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2419
2420 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2421 // instruction from clobbering it as they might use r12 as a scratch register.
2422 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002423
2424 {
2425 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2426 // so it checks if the application is using them (by passing them to the macro assembler
2427 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2428 // what is available, and is the opposite of the standard usage: Instead of requesting a
2429 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2430 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2431 // (to materialize the constant), since the destination register becomes available for such use
2432 // internally for the duration of the macro instruction.
2433 UseScratchRegisterScope temps(GetVIXLAssembler());
2434 temps.Exclude(hidden_reg);
2435 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2436 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002437 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002438 // Ensure the pc position is recorded immediately after the `blx` instruction.
2439 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002440 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002441 vixl32::k16BitT32InstructionSizeInBytes,
2442 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002443 // LR();
2444 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002445 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002446 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002447 }
2448}
2449
Artem Serov02109dd2016-09-23 17:17:54 +01002450void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2451 LocationSummary* locations =
2452 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2453 switch (neg->GetResultType()) {
2454 case Primitive::kPrimInt: {
2455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2457 break;
2458 }
2459 case Primitive::kPrimLong: {
2460 locations->SetInAt(0, Location::RequiresRegister());
2461 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2462 break;
2463 }
2464
2465 case Primitive::kPrimFloat:
2466 case Primitive::kPrimDouble:
2467 locations->SetInAt(0, Location::RequiresFpuRegister());
2468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2473 }
2474}
2475
2476void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2477 LocationSummary* locations = neg->GetLocations();
2478 Location out = locations->Out();
2479 Location in = locations->InAt(0);
2480 switch (neg->GetResultType()) {
2481 case Primitive::kPrimInt:
2482 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2483 break;
2484
2485 case Primitive::kPrimLong:
2486 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2487 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2488 // We cannot emit an RSC (Reverse Subtract with Carry)
2489 // instruction here, as it does not exist in the Thumb-2
2490 // instruction set. We use the following approach
2491 // using SBC and SUB instead.
2492 //
2493 // out.hi = -C
2494 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2495 // out.hi = out.hi - in.hi
2496 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2497 break;
2498
2499 case Primitive::kPrimFloat:
2500 case Primitive::kPrimDouble:
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002501 // TODO(VIXL): Consider introducing an InputVRegister()
2502 // helper function (equivalent to InputRegister()).
Artem Serov02109dd2016-09-23 17:17:54 +01002503 __ Vneg(OutputVRegister(neg), InputVRegisterAt(neg, 0));
2504 break;
2505
2506 default:
2507 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2508 }
2509}
2510
Scott Wakelingfe885462016-09-22 10:24:38 +01002511void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2512 Primitive::Type result_type = conversion->GetResultType();
2513 Primitive::Type input_type = conversion->GetInputType();
2514 DCHECK_NE(result_type, input_type);
2515
2516 // The float-to-long, double-to-long and long-to-float type conversions
2517 // rely on a call to the runtime.
2518 LocationSummary::CallKind call_kind =
2519 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2520 && result_type == Primitive::kPrimLong)
2521 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2522 ? LocationSummary::kCallOnMainOnly
2523 : LocationSummary::kNoCall;
2524 LocationSummary* locations =
2525 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2526
2527 // The Java language does not allow treating boolean as an integral type but
2528 // our bit representation makes it safe.
2529
2530 switch (result_type) {
2531 case Primitive::kPrimByte:
2532 switch (input_type) {
2533 case Primitive::kPrimLong:
2534 // Type conversion from long to byte is a result of code transformations.
2535 case Primitive::kPrimBoolean:
2536 // Boolean input is a result of code transformations.
2537 case Primitive::kPrimShort:
2538 case Primitive::kPrimInt:
2539 case Primitive::kPrimChar:
2540 // Processing a Dex `int-to-byte' instruction.
2541 locations->SetInAt(0, Location::RequiresRegister());
2542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2543 break;
2544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 }
2549 break;
2550
2551 case Primitive::kPrimShort:
2552 switch (input_type) {
2553 case Primitive::kPrimLong:
2554 // Type conversion from long to short is a result of code transformations.
2555 case Primitive::kPrimBoolean:
2556 // Boolean input is a result of code transformations.
2557 case Primitive::kPrimByte:
2558 case Primitive::kPrimInt:
2559 case Primitive::kPrimChar:
2560 // Processing a Dex `int-to-short' instruction.
2561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
2571 case Primitive::kPrimInt:
2572 switch (input_type) {
2573 case Primitive::kPrimLong:
2574 // Processing a Dex `long-to-int' instruction.
2575 locations->SetInAt(0, Location::Any());
2576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2577 break;
2578
2579 case Primitive::kPrimFloat:
2580 // Processing a Dex `float-to-int' instruction.
2581 locations->SetInAt(0, Location::RequiresFpuRegister());
2582 locations->SetOut(Location::RequiresRegister());
2583 locations->AddTemp(Location::RequiresFpuRegister());
2584 break;
2585
2586 case Primitive::kPrimDouble:
2587 // Processing a Dex `double-to-int' instruction.
2588 locations->SetInAt(0, Location::RequiresFpuRegister());
2589 locations->SetOut(Location::RequiresRegister());
2590 locations->AddTemp(Location::RequiresFpuRegister());
2591 break;
2592
2593 default:
2594 LOG(FATAL) << "Unexpected type conversion from " << input_type
2595 << " to " << result_type;
2596 }
2597 break;
2598
2599 case Primitive::kPrimLong:
2600 switch (input_type) {
2601 case Primitive::kPrimBoolean:
2602 // Boolean input is a result of code transformations.
2603 case Primitive::kPrimByte:
2604 case Primitive::kPrimShort:
2605 case Primitive::kPrimInt:
2606 case Primitive::kPrimChar:
2607 // Processing a Dex `int-to-long' instruction.
2608 locations->SetInAt(0, Location::RequiresRegister());
2609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2610 break;
2611
2612 case Primitive::kPrimFloat: {
2613 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002614 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2615 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2616 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002617 break;
2618 }
2619
2620 case Primitive::kPrimDouble: {
2621 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002622 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2623 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2624 calling_convention.GetFpuRegisterAt(1)));
2625 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002626 break;
2627 }
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
2632 }
2633 break;
2634
2635 case Primitive::kPrimChar:
2636 switch (input_type) {
2637 case Primitive::kPrimLong:
2638 // Type conversion from long to char is a result of code transformations.
2639 case Primitive::kPrimBoolean:
2640 // Boolean input is a result of code transformations.
2641 case Primitive::kPrimByte:
2642 case Primitive::kPrimShort:
2643 case Primitive::kPrimInt:
2644 // Processing a Dex `int-to-char' instruction.
2645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2647 break;
2648
2649 default:
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type
2651 << " to " << result_type;
2652 }
2653 break;
2654
2655 case Primitive::kPrimFloat:
2656 switch (input_type) {
2657 case Primitive::kPrimBoolean:
2658 // Boolean input is a result of code transformations.
2659 case Primitive::kPrimByte:
2660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
2662 case Primitive::kPrimChar:
2663 // Processing a Dex `int-to-float' instruction.
2664 locations->SetInAt(0, Location::RequiresRegister());
2665 locations->SetOut(Location::RequiresFpuRegister());
2666 break;
2667
2668 case Primitive::kPrimLong: {
2669 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002670 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2671 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2672 calling_convention.GetRegisterAt(1)));
2673 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002674 break;
2675 }
2676
2677 case Primitive::kPrimDouble:
2678 // Processing a Dex `double-to-float' instruction.
2679 locations->SetInAt(0, Location::RequiresFpuRegister());
2680 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2681 break;
2682
2683 default:
2684 LOG(FATAL) << "Unexpected type conversion from " << input_type
2685 << " to " << result_type;
2686 };
2687 break;
2688
2689 case Primitive::kPrimDouble:
2690 switch (input_type) {
2691 case Primitive::kPrimBoolean:
2692 // Boolean input is a result of code transformations.
2693 case Primitive::kPrimByte:
2694 case Primitive::kPrimShort:
2695 case Primitive::kPrimInt:
2696 case Primitive::kPrimChar:
2697 // Processing a Dex `int-to-double' instruction.
2698 locations->SetInAt(0, Location::RequiresRegister());
2699 locations->SetOut(Location::RequiresFpuRegister());
2700 break;
2701
2702 case Primitive::kPrimLong:
2703 // Processing a Dex `long-to-double' instruction.
2704 locations->SetInAt(0, Location::RequiresRegister());
2705 locations->SetOut(Location::RequiresFpuRegister());
2706 locations->AddTemp(Location::RequiresFpuRegister());
2707 locations->AddTemp(Location::RequiresFpuRegister());
2708 break;
2709
2710 case Primitive::kPrimFloat:
2711 // Processing a Dex `float-to-double' instruction.
2712 locations->SetInAt(0, Location::RequiresFpuRegister());
2713 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected type conversion from " << input_type
2718 << " to " << result_type;
2719 };
2720 break;
2721
2722 default:
2723 LOG(FATAL) << "Unexpected type conversion from " << input_type
2724 << " to " << result_type;
2725 }
2726}
2727
2728void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2729 LocationSummary* locations = conversion->GetLocations();
2730 Location out = locations->Out();
2731 Location in = locations->InAt(0);
2732 Primitive::Type result_type = conversion->GetResultType();
2733 Primitive::Type input_type = conversion->GetInputType();
2734 DCHECK_NE(result_type, input_type);
2735 switch (result_type) {
2736 case Primitive::kPrimByte:
2737 switch (input_type) {
2738 case Primitive::kPrimLong:
2739 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002740 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002741 break;
2742 case Primitive::kPrimBoolean:
2743 // Boolean input is a result of code transformations.
2744 case Primitive::kPrimShort:
2745 case Primitive::kPrimInt:
2746 case Primitive::kPrimChar:
2747 // Processing a Dex `int-to-byte' instruction.
2748 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 }
2755 break;
2756
2757 case Primitive::kPrimShort:
2758 switch (input_type) {
2759 case Primitive::kPrimLong:
2760 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002761 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002762 break;
2763 case Primitive::kPrimBoolean:
2764 // Boolean input is a result of code transformations.
2765 case Primitive::kPrimByte:
2766 case Primitive::kPrimInt:
2767 case Primitive::kPrimChar:
2768 // Processing a Dex `int-to-short' instruction.
2769 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2770 break;
2771
2772 default:
2773 LOG(FATAL) << "Unexpected type conversion from " << input_type
2774 << " to " << result_type;
2775 }
2776 break;
2777
2778 case Primitive::kPrimInt:
2779 switch (input_type) {
2780 case Primitive::kPrimLong:
2781 // Processing a Dex `long-to-int' instruction.
2782 DCHECK(out.IsRegister());
2783 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002784 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002785 } else if (in.IsDoubleStackSlot()) {
2786 GetAssembler()->LoadFromOffset(kLoadWord,
2787 OutputRegister(conversion),
2788 sp,
2789 in.GetStackIndex());
2790 } else {
2791 DCHECK(in.IsConstant());
2792 DCHECK(in.GetConstant()->IsLongConstant());
2793 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2794 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
2795 }
2796 break;
2797
2798 case Primitive::kPrimFloat: {
2799 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002800 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002801 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002802 __ Vmov(OutputRegister(conversion), temp);
2803 break;
2804 }
2805
2806 case Primitive::kPrimDouble: {
2807 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002808 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002809 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002810 __ Vmov(OutputRegister(conversion), temp_s);
2811 break;
2812 }
2813
2814 default:
2815 LOG(FATAL) << "Unexpected type conversion from " << input_type
2816 << " to " << result_type;
2817 }
2818 break;
2819
2820 case Primitive::kPrimLong:
2821 switch (input_type) {
2822 case Primitive::kPrimBoolean:
2823 // Boolean input is a result of code transformations.
2824 case Primitive::kPrimByte:
2825 case Primitive::kPrimShort:
2826 case Primitive::kPrimInt:
2827 case Primitive::kPrimChar:
2828 // Processing a Dex `int-to-long' instruction.
2829 DCHECK(out.IsRegisterPair());
2830 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002831 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002832 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002833 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01002834 break;
2835
2836 case Primitive::kPrimFloat:
2837 // Processing a Dex `float-to-long' instruction.
2838 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
2839 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2840 break;
2841
2842 case Primitive::kPrimDouble:
2843 // Processing a Dex `double-to-long' instruction.
2844 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
2845 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2846 break;
2847
2848 default:
2849 LOG(FATAL) << "Unexpected type conversion from " << input_type
2850 << " to " << result_type;
2851 }
2852 break;
2853
2854 case Primitive::kPrimChar:
2855 switch (input_type) {
2856 case Primitive::kPrimLong:
2857 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002858 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002859 break;
2860 case Primitive::kPrimBoolean:
2861 // Boolean input is a result of code transformations.
2862 case Primitive::kPrimByte:
2863 case Primitive::kPrimShort:
2864 case Primitive::kPrimInt:
2865 // Processing a Dex `int-to-char' instruction.
2866 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2867 break;
2868
2869 default:
2870 LOG(FATAL) << "Unexpected type conversion from " << input_type
2871 << " to " << result_type;
2872 }
2873 break;
2874
2875 case Primitive::kPrimFloat:
2876 switch (input_type) {
2877 case Primitive::kPrimBoolean:
2878 // Boolean input is a result of code transformations.
2879 case Primitive::kPrimByte:
2880 case Primitive::kPrimShort:
2881 case Primitive::kPrimInt:
2882 case Primitive::kPrimChar: {
2883 // Processing a Dex `int-to-float' instruction.
2884 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002885 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01002886 break;
2887 }
2888
2889 case Primitive::kPrimLong:
2890 // Processing a Dex `long-to-float' instruction.
2891 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
2892 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2893 break;
2894
2895 case Primitive::kPrimDouble:
2896 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002897 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002898 break;
2899
2900 default:
2901 LOG(FATAL) << "Unexpected type conversion from " << input_type
2902 << " to " << result_type;
2903 };
2904 break;
2905
2906 case Primitive::kPrimDouble:
2907 switch (input_type) {
2908 case Primitive::kPrimBoolean:
2909 // Boolean input is a result of code transformations.
2910 case Primitive::kPrimByte:
2911 case Primitive::kPrimShort:
2912 case Primitive::kPrimInt:
2913 case Primitive::kPrimChar: {
2914 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002915 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002916 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01002917 break;
2918 }
2919
2920 case Primitive::kPrimLong: {
2921 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002922 vixl32::Register low = LowRegisterFrom(in);
2923 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002924 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002925 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002926 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002927 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002928 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002929
2930 // temp_d = int-to-double(high)
2931 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002932 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01002933 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002934 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002935 // out_d = unsigned-to-double(low)
2936 __ Vmov(out_s, low);
2937 __ Vcvt(F64, U32, out_d, out_s);
2938 // out_d += temp_d * constant_d
2939 __ Vmla(F64, out_d, temp_d, constant_d);
2940 break;
2941 }
2942
2943 case Primitive::kPrimFloat:
2944 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002945 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002946 break;
2947
2948 default:
2949 LOG(FATAL) << "Unexpected type conversion from " << input_type
2950 << " to " << result_type;
2951 };
2952 break;
2953
2954 default:
2955 LOG(FATAL) << "Unexpected type conversion from " << input_type
2956 << " to " << result_type;
2957 }
2958}
2959
2960void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2963 switch (add->GetResultType()) {
2964 case Primitive::kPrimInt: {
2965 locations->SetInAt(0, Location::RequiresRegister());
2966 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2967 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2968 break;
2969 }
2970
Scott Wakelingfe885462016-09-22 10:24:38 +01002971 case Primitive::kPrimLong: {
2972 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00002973 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01002974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2975 break;
2976 }
2977
2978 case Primitive::kPrimFloat:
2979 case Primitive::kPrimDouble: {
2980 locations->SetInAt(0, Location::RequiresFpuRegister());
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
2982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2983 break;
2984 }
2985
2986 default:
2987 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2988 }
2989}
2990
2991void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
2992 LocationSummary* locations = add->GetLocations();
2993 Location out = locations->Out();
2994 Location first = locations->InAt(0);
2995 Location second = locations->InAt(1);
2996
2997 switch (add->GetResultType()) {
2998 case Primitive::kPrimInt: {
2999 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3000 }
3001 break;
3002
Scott Wakelingfe885462016-09-22 10:24:38 +01003003 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003004 if (second.IsConstant()) {
3005 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3006 GenerateAddLongConst(out, first, value);
3007 } else {
3008 DCHECK(second.IsRegisterPair());
3009 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3010 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3011 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003012 break;
3013 }
3014
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003015 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003016 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003017 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003018 break;
3019
3020 default:
3021 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3022 }
3023}
3024
3025void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3026 LocationSummary* locations =
3027 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3028 switch (sub->GetResultType()) {
3029 case Primitive::kPrimInt: {
3030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3033 break;
3034 }
3035
Scott Wakelingfe885462016-09-22 10:24:38 +01003036 case Primitive::kPrimLong: {
3037 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003038 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3040 break;
3041 }
3042 case Primitive::kPrimFloat:
3043 case Primitive::kPrimDouble: {
3044 locations->SetInAt(0, Location::RequiresFpuRegister());
3045 locations->SetInAt(1, Location::RequiresFpuRegister());
3046 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3047 break;
3048 }
3049 default:
3050 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3051 }
3052}
3053
3054void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3055 LocationSummary* locations = sub->GetLocations();
3056 Location out = locations->Out();
3057 Location first = locations->InAt(0);
3058 Location second = locations->InAt(1);
3059 switch (sub->GetResultType()) {
3060 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003061 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003062 break;
3063 }
3064
Scott Wakelingfe885462016-09-22 10:24:38 +01003065 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003066 if (second.IsConstant()) {
3067 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3068 GenerateAddLongConst(out, first, -value);
3069 } else {
3070 DCHECK(second.IsRegisterPair());
3071 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3072 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3073 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003074 break;
3075 }
3076
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003077 case Primitive::kPrimFloat:
3078 case Primitive::kPrimDouble:
3079 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003080 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003081
3082 default:
3083 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3084 }
3085}
3086
3087void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3088 LocationSummary* locations =
3089 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3090 switch (mul->GetResultType()) {
3091 case Primitive::kPrimInt:
3092 case Primitive::kPrimLong: {
3093 locations->SetInAt(0, Location::RequiresRegister());
3094 locations->SetInAt(1, Location::RequiresRegister());
3095 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3096 break;
3097 }
3098
3099 case Primitive::kPrimFloat:
3100 case Primitive::kPrimDouble: {
3101 locations->SetInAt(0, Location::RequiresFpuRegister());
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
3103 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3104 break;
3105 }
3106
3107 default:
3108 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3109 }
3110}
3111
3112void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3113 LocationSummary* locations = mul->GetLocations();
3114 Location out = locations->Out();
3115 Location first = locations->InAt(0);
3116 Location second = locations->InAt(1);
3117 switch (mul->GetResultType()) {
3118 case Primitive::kPrimInt: {
3119 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3120 break;
3121 }
3122 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003123 vixl32::Register out_hi = HighRegisterFrom(out);
3124 vixl32::Register out_lo = LowRegisterFrom(out);
3125 vixl32::Register in1_hi = HighRegisterFrom(first);
3126 vixl32::Register in1_lo = LowRegisterFrom(first);
3127 vixl32::Register in2_hi = HighRegisterFrom(second);
3128 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003129
3130 // Extra checks to protect caused by the existence of R1_R2.
3131 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3132 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3133 DCHECK_NE(out_hi.GetCode(), in1_lo.GetCode());
3134 DCHECK_NE(out_hi.GetCode(), in2_lo.GetCode());
3135
3136 // input: in1 - 64 bits, in2 - 64 bits
3137 // output: out
3138 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3139 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3140 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3141
3142 UseScratchRegisterScope temps(GetVIXLAssembler());
3143 vixl32::Register temp = temps.Acquire();
3144 // temp <- in1.lo * in2.hi
3145 __ Mul(temp, in1_lo, in2_hi);
3146 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3147 __ Mla(out_hi, in1_hi, in2_lo, temp);
3148 // out.lo <- (in1.lo * in2.lo)[31:0];
3149 __ Umull(out_lo, temp, in1_lo, in2_lo);
3150 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003151 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003152 break;
3153 }
3154
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155 case Primitive::kPrimFloat:
3156 case Primitive::kPrimDouble:
3157 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003158 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003159
3160 default:
3161 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3162 }
3163}
3164
Scott Wakelingfe885462016-09-22 10:24:38 +01003165void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3166 DCHECK(instruction->IsDiv() || instruction->IsRem());
3167 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3168
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003169 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003170 DCHECK(second.IsConstant());
3171
3172 vixl32::Register out = OutputRegister(instruction);
3173 vixl32::Register dividend = InputRegisterAt(instruction, 0);
3174 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3175 DCHECK(imm == 1 || imm == -1);
3176
3177 if (instruction->IsRem()) {
3178 __ Mov(out, 0);
3179 } else {
3180 if (imm == 1) {
3181 __ Mov(out, dividend);
3182 } else {
3183 __ Rsb(out, dividend, 0);
3184 }
3185 }
3186}
3187
3188void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3189 DCHECK(instruction->IsDiv() || instruction->IsRem());
3190 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3191
3192 LocationSummary* locations = instruction->GetLocations();
3193 Location second = locations->InAt(1);
3194 DCHECK(second.IsConstant());
3195
3196 vixl32::Register out = OutputRegister(instruction);
3197 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003198 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003199 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3200 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3201 int ctz_imm = CTZ(abs_imm);
3202
3203 if (ctz_imm == 1) {
3204 __ Lsr(temp, dividend, 32 - ctz_imm);
3205 } else {
3206 __ Asr(temp, dividend, 31);
3207 __ Lsr(temp, temp, 32 - ctz_imm);
3208 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003209 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003210
3211 if (instruction->IsDiv()) {
3212 __ Asr(out, out, ctz_imm);
3213 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003214 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003215 }
3216 } else {
3217 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003218 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003219 }
3220}
3221
3222void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3223 DCHECK(instruction->IsDiv() || instruction->IsRem());
3224 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3225
3226 LocationSummary* locations = instruction->GetLocations();
3227 Location second = locations->InAt(1);
3228 DCHECK(second.IsConstant());
3229
3230 vixl32::Register out = OutputRegister(instruction);
3231 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003232 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3233 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003234 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003235
3236 int64_t magic;
3237 int shift;
3238 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3239
Anton Kirilovdda43962016-11-21 19:55:20 +00003240 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3241 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003242 __ Smull(temp2, temp1, dividend, temp1);
3243
3244 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003245 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003246 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003247 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003248 }
3249
3250 if (shift != 0) {
3251 __ Asr(temp1, temp1, shift);
3252 }
3253
3254 if (instruction->IsDiv()) {
3255 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3256 } else {
3257 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3258 // TODO: Strength reduction for mls.
3259 __ Mov(temp2, imm);
3260 __ Mls(out, temp1, temp2, dividend);
3261 }
3262}
3263
3264void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3265 HBinaryOperation* instruction) {
3266 DCHECK(instruction->IsDiv() || instruction->IsRem());
3267 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3268
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003269 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003270 DCHECK(second.IsConstant());
3271
3272 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3273 if (imm == 0) {
3274 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3275 } else if (imm == 1 || imm == -1) {
3276 DivRemOneOrMinusOne(instruction);
3277 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3278 DivRemByPowerOfTwo(instruction);
3279 } else {
3280 DCHECK(imm <= -2 || imm >= 2);
3281 GenerateDivRemWithAnyConstant(instruction);
3282 }
3283}
3284
3285void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3286 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3287 if (div->GetResultType() == Primitive::kPrimLong) {
3288 // pLdiv runtime call.
3289 call_kind = LocationSummary::kCallOnMainOnly;
3290 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3291 // sdiv will be replaced by other instruction sequence.
3292 } else if (div->GetResultType() == Primitive::kPrimInt &&
3293 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3294 // pIdivmod runtime call.
3295 call_kind = LocationSummary::kCallOnMainOnly;
3296 }
3297
3298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3299
3300 switch (div->GetResultType()) {
3301 case Primitive::kPrimInt: {
3302 if (div->InputAt(1)->IsConstant()) {
3303 locations->SetInAt(0, Location::RequiresRegister());
3304 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3307 if (value == 1 || value == 0 || value == -1) {
3308 // No temp register required.
3309 } else {
3310 locations->AddTemp(Location::RequiresRegister());
3311 if (!IsPowerOfTwo(AbsOrMin(value))) {
3312 locations->AddTemp(Location::RequiresRegister());
3313 }
3314 }
3315 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3316 locations->SetInAt(0, Location::RequiresRegister());
3317 locations->SetInAt(1, Location::RequiresRegister());
3318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3319 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003320 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3321 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3322 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3323 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3324 // we only need the former.
3325 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003326 }
3327 break;
3328 }
3329 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003330 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3331 locations->SetInAt(0, LocationFrom(
3332 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3333 locations->SetInAt(1, LocationFrom(
3334 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3335 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003336 break;
3337 }
3338 case Primitive::kPrimFloat:
3339 case Primitive::kPrimDouble: {
3340 locations->SetInAt(0, Location::RequiresFpuRegister());
3341 locations->SetInAt(1, Location::RequiresFpuRegister());
3342 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3343 break;
3344 }
3345
3346 default:
3347 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3348 }
3349}
3350
3351void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003352 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003353 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003354
3355 switch (div->GetResultType()) {
3356 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003357 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003358 GenerateDivRemConstantIntegral(div);
3359 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3360 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3361 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003362 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3363 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3364 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3365 DCHECK(r0.Is(OutputRegister(div)));
3366
3367 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3368 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003369 }
3370 break;
3371 }
3372
3373 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003374 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3375 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3376 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3377 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3378 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3379 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3380 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3381
3382 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3383 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003384 break;
3385 }
3386
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003387 case Primitive::kPrimFloat:
3388 case Primitive::kPrimDouble:
3389 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003390 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003391
3392 default:
3393 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3394 }
3395}
3396
Artem Serov551b28f2016-10-18 19:11:30 +01003397void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3398 Primitive::Type type = rem->GetResultType();
3399
3400 // Most remainders are implemented in the runtime.
3401 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3402 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3403 // sdiv will be replaced by other instruction sequence.
3404 call_kind = LocationSummary::kNoCall;
3405 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3406 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3407 // Have hardware divide instruction for int, do it with three instructions.
3408 call_kind = LocationSummary::kNoCall;
3409 }
3410
3411 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3412
3413 switch (type) {
3414 case Primitive::kPrimInt: {
3415 if (rem->InputAt(1)->IsConstant()) {
3416 locations->SetInAt(0, Location::RequiresRegister());
3417 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3419 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3420 if (value == 1 || value == 0 || value == -1) {
3421 // No temp register required.
3422 } else {
3423 locations->AddTemp(Location::RequiresRegister());
3424 if (!IsPowerOfTwo(AbsOrMin(value))) {
3425 locations->AddTemp(Location::RequiresRegister());
3426 }
3427 }
3428 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3429 locations->SetInAt(0, Location::RequiresRegister());
3430 locations->SetInAt(1, Location::RequiresRegister());
3431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3432 locations->AddTemp(Location::RequiresRegister());
3433 } else {
3434 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3435 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3436 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3437 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3438 // we only need the latter.
3439 locations->SetOut(LocationFrom(r1));
3440 }
3441 break;
3442 }
3443 case Primitive::kPrimLong: {
3444 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3445 locations->SetInAt(0, LocationFrom(
3446 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3447 locations->SetInAt(1, LocationFrom(
3448 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3449 // The runtime helper puts the output in R2,R3.
3450 locations->SetOut(LocationFrom(r2, r3));
3451 break;
3452 }
3453 case Primitive::kPrimFloat: {
3454 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3455 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3456 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3457 locations->SetOut(LocationFrom(s0));
3458 break;
3459 }
3460
3461 case Primitive::kPrimDouble: {
3462 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3463 locations->SetInAt(0, LocationFrom(
3464 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3465 locations->SetInAt(1, LocationFrom(
3466 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3467 locations->SetOut(LocationFrom(s0, s1));
3468 break;
3469 }
3470
3471 default:
3472 LOG(FATAL) << "Unexpected rem type " << type;
3473 }
3474}
3475
3476void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3477 LocationSummary* locations = rem->GetLocations();
3478 Location second = locations->InAt(1);
3479
3480 Primitive::Type type = rem->GetResultType();
3481 switch (type) {
3482 case Primitive::kPrimInt: {
3483 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3484 vixl32::Register out_reg = OutputRegister(rem);
3485 if (second.IsConstant()) {
3486 GenerateDivRemConstantIntegral(rem);
3487 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3488 vixl32::Register reg2 = RegisterFrom(second);
3489 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3490
3491 // temp = reg1 / reg2 (integer division)
3492 // dest = reg1 - temp * reg2
3493 __ Sdiv(temp, reg1, reg2);
3494 __ Mls(out_reg, temp, reg2, reg1);
3495 } else {
3496 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3497 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3498 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3499 DCHECK(out_reg.Is(r1));
3500
3501 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3502 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3503 }
3504 break;
3505 }
3506
3507 case Primitive::kPrimLong: {
3508 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3509 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3510 break;
3511 }
3512
3513 case Primitive::kPrimFloat: {
3514 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3515 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3516 break;
3517 }
3518
3519 case Primitive::kPrimDouble: {
3520 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3521 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3522 break;
3523 }
3524
3525 default:
3526 LOG(FATAL) << "Unexpected rem type " << type;
3527 }
3528}
3529
3530
Scott Wakelingfe885462016-09-22 10:24:38 +01003531void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003532 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003533 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003534}
3535
3536void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3537 DivZeroCheckSlowPathARMVIXL* slow_path =
3538 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3539 codegen_->AddSlowPath(slow_path);
3540
3541 LocationSummary* locations = instruction->GetLocations();
3542 Location value = locations->InAt(0);
3543
3544 switch (instruction->GetType()) {
3545 case Primitive::kPrimBoolean:
3546 case Primitive::kPrimByte:
3547 case Primitive::kPrimChar:
3548 case Primitive::kPrimShort:
3549 case Primitive::kPrimInt: {
3550 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003551 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003552 } else {
3553 DCHECK(value.IsConstant()) << value;
3554 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3555 __ B(slow_path->GetEntryLabel());
3556 }
3557 }
3558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 if (value.IsRegisterPair()) {
3562 UseScratchRegisterScope temps(GetVIXLAssembler());
3563 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003564 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003565 __ B(eq, slow_path->GetEntryLabel());
3566 } else {
3567 DCHECK(value.IsConstant()) << value;
3568 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3569 __ B(slow_path->GetEntryLabel());
3570 }
3571 }
3572 break;
3573 }
3574 default:
3575 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3576 }
3577}
3578
Artem Serov02109dd2016-09-23 17:17:54 +01003579void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3580 LocationSummary* locations = ror->GetLocations();
3581 vixl32::Register in = InputRegisterAt(ror, 0);
3582 Location rhs = locations->InAt(1);
3583 vixl32::Register out = OutputRegister(ror);
3584
3585 if (rhs.IsConstant()) {
3586 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3587 // so map all rotations to a +ve. equivalent in that range.
3588 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3589 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3590 if (rot) {
3591 // Rotate, mapping left rotations to right equivalents if necessary.
3592 // (e.g. left by 2 bits == right by 30.)
3593 __ Ror(out, in, rot);
3594 } else if (!out.Is(in)) {
3595 __ Mov(out, in);
3596 }
3597 } else {
3598 __ Ror(out, in, RegisterFrom(rhs));
3599 }
3600}
3601
3602// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3603// rotates by swapping input regs (effectively rotating by the first 32-bits of
3604// a larger rotation) or flipping direction (thus treating larger right/left
3605// rotations as sub-word sized rotations in the other direction) as appropriate.
3606void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3607 LocationSummary* locations = ror->GetLocations();
3608 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3609 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3610 Location rhs = locations->InAt(1);
3611 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3612 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3613
3614 if (rhs.IsConstant()) {
3615 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3616 // Map all rotations to +ve. equivalents on the interval [0,63].
3617 rot &= kMaxLongShiftDistance;
3618 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3619 // logic below to a simple pair of binary orr.
3620 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3621 if (rot >= kArmBitsPerWord) {
3622 rot -= kArmBitsPerWord;
3623 std::swap(in_reg_hi, in_reg_lo);
3624 }
3625 // Rotate, or mov to out for zero or word size rotations.
3626 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003627 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003628 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003629 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003630 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3631 } else {
3632 __ Mov(out_reg_lo, in_reg_lo);
3633 __ Mov(out_reg_hi, in_reg_hi);
3634 }
3635 } else {
3636 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3637 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3638 vixl32::Label end;
3639 vixl32::Label shift_by_32_plus_shift_right;
3640
3641 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3642 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003643 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov02109dd2016-09-23 17:17:54 +01003644 __ B(cc, &shift_by_32_plus_shift_right);
3645
3646 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3647 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3648 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3649 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3650 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3651 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3652 __ Lsr(shift_left, in_reg_hi, shift_right);
3653 __ Add(out_reg_lo, out_reg_lo, shift_left);
3654 __ B(&end);
3655
3656 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3657 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3658 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3659 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3660 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3661 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3662 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3663 __ Lsl(shift_right, in_reg_hi, shift_left);
3664 __ Add(out_reg_lo, out_reg_lo, shift_right);
3665
3666 __ Bind(&end);
3667 }
3668}
3669
3670void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3671 LocationSummary* locations =
3672 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3673 switch (ror->GetResultType()) {
3674 case Primitive::kPrimInt: {
3675 locations->SetInAt(0, Location::RequiresRegister());
3676 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3678 break;
3679 }
3680 case Primitive::kPrimLong: {
3681 locations->SetInAt(0, Location::RequiresRegister());
3682 if (ror->InputAt(1)->IsConstant()) {
3683 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3684 } else {
3685 locations->SetInAt(1, Location::RequiresRegister());
3686 locations->AddTemp(Location::RequiresRegister());
3687 locations->AddTemp(Location::RequiresRegister());
3688 }
3689 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3690 break;
3691 }
3692 default:
3693 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3694 }
3695}
3696
3697void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3698 Primitive::Type type = ror->GetResultType();
3699 switch (type) {
3700 case Primitive::kPrimInt: {
3701 HandleIntegerRotate(ror);
3702 break;
3703 }
3704 case Primitive::kPrimLong: {
3705 HandleLongRotate(ror);
3706 break;
3707 }
3708 default:
3709 LOG(FATAL) << "Unexpected operation type " << type;
3710 UNREACHABLE();
3711 }
3712}
3713
Artem Serov02d37832016-10-25 15:25:33 +01003714void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3715 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3716
3717 LocationSummary* locations =
3718 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3719
3720 switch (op->GetResultType()) {
3721 case Primitive::kPrimInt: {
3722 locations->SetInAt(0, Location::RequiresRegister());
3723 if (op->InputAt(1)->IsConstant()) {
3724 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3726 } else {
3727 locations->SetInAt(1, Location::RequiresRegister());
3728 // Make the output overlap, as it will be used to hold the masked
3729 // second input.
3730 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3731 }
3732 break;
3733 }
3734 case Primitive::kPrimLong: {
3735 locations->SetInAt(0, Location::RequiresRegister());
3736 if (op->InputAt(1)->IsConstant()) {
3737 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3738 // For simplicity, use kOutputOverlap even though we only require that low registers
3739 // don't clash with high registers which the register allocator currently guarantees.
3740 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3741 } else {
3742 locations->SetInAt(1, Location::RequiresRegister());
3743 locations->AddTemp(Location::RequiresRegister());
3744 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3745 }
3746 break;
3747 }
3748 default:
3749 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3750 }
3751}
3752
3753void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3754 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3755
3756 LocationSummary* locations = op->GetLocations();
3757 Location out = locations->Out();
3758 Location first = locations->InAt(0);
3759 Location second = locations->InAt(1);
3760
3761 Primitive::Type type = op->GetResultType();
3762 switch (type) {
3763 case Primitive::kPrimInt: {
3764 vixl32::Register out_reg = OutputRegister(op);
3765 vixl32::Register first_reg = InputRegisterAt(op, 0);
3766 if (second.IsRegister()) {
3767 vixl32::Register second_reg = RegisterFrom(second);
3768 // ARM doesn't mask the shift count so we need to do it ourselves.
3769 __ And(out_reg, second_reg, kMaxIntShiftDistance);
3770 if (op->IsShl()) {
3771 __ Lsl(out_reg, first_reg, out_reg);
3772 } else if (op->IsShr()) {
3773 __ Asr(out_reg, first_reg, out_reg);
3774 } else {
3775 __ Lsr(out_reg, first_reg, out_reg);
3776 }
3777 } else {
3778 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3779 uint32_t shift_value = cst & kMaxIntShiftDistance;
3780 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
3781 __ Mov(out_reg, first_reg);
3782 } else if (op->IsShl()) {
3783 __ Lsl(out_reg, first_reg, shift_value);
3784 } else if (op->IsShr()) {
3785 __ Asr(out_reg, first_reg, shift_value);
3786 } else {
3787 __ Lsr(out_reg, first_reg, shift_value);
3788 }
3789 }
3790 break;
3791 }
3792 case Primitive::kPrimLong: {
3793 vixl32::Register o_h = HighRegisterFrom(out);
3794 vixl32::Register o_l = LowRegisterFrom(out);
3795
3796 vixl32::Register high = HighRegisterFrom(first);
3797 vixl32::Register low = LowRegisterFrom(first);
3798
3799 if (second.IsRegister()) {
3800 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3801
3802 vixl32::Register second_reg = RegisterFrom(second);
3803
3804 if (op->IsShl()) {
3805 __ And(o_l, second_reg, kMaxLongShiftDistance);
3806 // Shift the high part
3807 __ Lsl(o_h, high, o_l);
3808 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003809 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003810 __ Lsr(temp, low, temp);
3811 __ Orr(o_h, o_h, temp);
3812 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003813 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003814 {
Artem Serov0fb37192016-12-06 18:13:40 +00003815 ExactAssemblyScope guard(GetVIXLAssembler(),
3816 2 * vixl32::kMaxInstructionSizeInBytes,
3817 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003818 __ it(pl);
3819 __ lsl(pl, o_h, low, temp);
3820 }
3821 // Shift the low part
3822 __ Lsl(o_l, low, o_l);
3823 } else if (op->IsShr()) {
3824 __ And(o_h, second_reg, kMaxLongShiftDistance);
3825 // Shift the low part
3826 __ Lsr(o_l, low, o_h);
3827 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003828 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003829 __ Lsl(temp, high, temp);
3830 __ Orr(o_l, o_l, temp);
3831 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003832 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003833 {
Artem Serov0fb37192016-12-06 18:13:40 +00003834 ExactAssemblyScope guard(GetVIXLAssembler(),
3835 2 * vixl32::kMaxInstructionSizeInBytes,
3836 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003837 __ it(pl);
3838 __ asr(pl, o_l, high, temp);
3839 }
3840 // Shift the high part
3841 __ Asr(o_h, high, o_h);
3842 } else {
3843 __ And(o_h, second_reg, kMaxLongShiftDistance);
3844 // same as Shr except we use `Lsr`s and not `Asr`s
3845 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003846 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003847 __ Lsl(temp, high, temp);
3848 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003849 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003850 {
Artem Serov0fb37192016-12-06 18:13:40 +00003851 ExactAssemblyScope guard(GetVIXLAssembler(),
3852 2 * vixl32::kMaxInstructionSizeInBytes,
3853 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003854 __ it(pl);
3855 __ lsr(pl, o_l, high, temp);
3856 }
3857 __ Lsr(o_h, high, o_h);
3858 }
3859 } else {
3860 // Register allocator doesn't create partial overlap.
3861 DCHECK(!o_l.Is(high));
3862 DCHECK(!o_h.Is(low));
3863 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3864 uint32_t shift_value = cst & kMaxLongShiftDistance;
3865 if (shift_value > 32) {
3866 if (op->IsShl()) {
3867 __ Lsl(o_h, low, shift_value - 32);
3868 __ Mov(o_l, 0);
3869 } else if (op->IsShr()) {
3870 __ Asr(o_l, high, shift_value - 32);
3871 __ Asr(o_h, high, 31);
3872 } else {
3873 __ Lsr(o_l, high, shift_value - 32);
3874 __ Mov(o_h, 0);
3875 }
3876 } else if (shift_value == 32) {
3877 if (op->IsShl()) {
3878 __ Mov(o_h, low);
3879 __ Mov(o_l, 0);
3880 } else if (op->IsShr()) {
3881 __ Mov(o_l, high);
3882 __ Asr(o_h, high, 31);
3883 } else {
3884 __ Mov(o_l, high);
3885 __ Mov(o_h, 0);
3886 }
3887 } else if (shift_value == 1) {
3888 if (op->IsShl()) {
3889 __ Lsls(o_l, low, 1);
3890 __ Adc(o_h, high, high);
3891 } else if (op->IsShr()) {
3892 __ Asrs(o_h, high, 1);
3893 __ Rrx(o_l, low);
3894 } else {
3895 __ Lsrs(o_h, high, 1);
3896 __ Rrx(o_l, low);
3897 }
3898 } else {
3899 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3900 if (op->IsShl()) {
3901 __ Lsl(o_h, high, shift_value);
3902 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
3903 __ Lsl(o_l, low, shift_value);
3904 } else if (op->IsShr()) {
3905 __ Lsr(o_l, low, shift_value);
3906 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3907 __ Asr(o_h, high, shift_value);
3908 } else {
3909 __ Lsr(o_l, low, shift_value);
3910 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3911 __ Lsr(o_h, high, shift_value);
3912 }
3913 }
3914 }
3915 break;
3916 }
3917 default:
3918 LOG(FATAL) << "Unexpected operation type " << type;
3919 UNREACHABLE();
3920 }
3921}
3922
3923void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
3924 HandleShift(shl);
3925}
3926
3927void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
3928 HandleShift(shl);
3929}
3930
3931void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
3932 HandleShift(shr);
3933}
3934
3935void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
3936 HandleShift(shr);
3937}
3938
3939void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
3940 HandleShift(ushr);
3941}
3942
3943void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
3944 HandleShift(ushr);
3945}
3946
3947void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3950 if (instruction->IsStringAlloc()) {
3951 locations->AddTemp(LocationFrom(kMethodRegister));
3952 } else {
3953 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3954 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3955 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3956 }
3957 locations->SetOut(LocationFrom(r0));
3958}
3959
3960void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3961 // Note: if heap poisoning is enabled, the entry point takes cares
3962 // of poisoning the reference.
3963 if (instruction->IsStringAlloc()) {
3964 // String is allocated through StringFactory. Call NewEmptyString entry point.
3965 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
3966 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3967 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
3968 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003969 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00003970 ExactAssemblyScope aas(GetVIXLAssembler(),
3971 vixl32::k16BitT32InstructionSizeInBytes,
3972 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01003973 __ blx(lr);
3974 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3975 } else {
3976 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3977 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3978 }
3979}
3980
3981void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
3982 LocationSummary* locations =
3983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3984 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3985 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
3986 locations->SetOut(LocationFrom(r0));
3987 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
3988 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
3989}
3990
3991void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
3992 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08003993 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01003994 // Note: if heap poisoning is enabled, the entry point takes cares
3995 // of poisoning the reference.
3996 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3997 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3998}
3999
4000void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4003 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4004 if (location.IsStackSlot()) {
4005 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4006 } else if (location.IsDoubleStackSlot()) {
4007 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4008 }
4009 locations->SetOut(location);
4010}
4011
4012void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4013 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4014 // Nothing to do, the parameter is already at its location.
4015}
4016
4017void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4018 LocationSummary* locations =
4019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4020 locations->SetOut(LocationFrom(kMethodRegister));
4021}
4022
4023void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4024 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4025 // Nothing to do, the method is already at its location.
4026}
4027
4028void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4031 locations->SetInAt(0, Location::RequiresRegister());
4032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4033}
4034
4035void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4036 LocationSummary* locations = not_->GetLocations();
4037 Location out = locations->Out();
4038 Location in = locations->InAt(0);
4039 switch (not_->GetResultType()) {
4040 case Primitive::kPrimInt:
4041 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4042 break;
4043
4044 case Primitive::kPrimLong:
4045 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4046 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4047 break;
4048
4049 default:
4050 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4051 }
4052}
4053
Scott Wakelingc34dba72016-10-03 10:14:44 +01004054void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4055 LocationSummary* locations =
4056 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4057 locations->SetInAt(0, Location::RequiresRegister());
4058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4059}
4060
4061void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4062 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4063}
4064
Artem Serov02d37832016-10-25 15:25:33 +01004065void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4066 LocationSummary* locations =
4067 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4068 switch (compare->InputAt(0)->GetType()) {
4069 case Primitive::kPrimBoolean:
4070 case Primitive::kPrimByte:
4071 case Primitive::kPrimShort:
4072 case Primitive::kPrimChar:
4073 case Primitive::kPrimInt:
4074 case Primitive::kPrimLong: {
4075 locations->SetInAt(0, Location::RequiresRegister());
4076 locations->SetInAt(1, Location::RequiresRegister());
4077 // Output overlaps because it is written before doing the low comparison.
4078 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4079 break;
4080 }
4081 case Primitive::kPrimFloat:
4082 case Primitive::kPrimDouble: {
4083 locations->SetInAt(0, Location::RequiresFpuRegister());
4084 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4085 locations->SetOut(Location::RequiresRegister());
4086 break;
4087 }
4088 default:
4089 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4090 }
4091}
4092
4093void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4094 LocationSummary* locations = compare->GetLocations();
4095 vixl32::Register out = OutputRegister(compare);
4096 Location left = locations->InAt(0);
4097 Location right = locations->InAt(1);
4098
4099 vixl32::Label less, greater, done;
4100 Primitive::Type type = compare->InputAt(0)->GetType();
4101 vixl32::Condition less_cond = vixl32::Condition(kNone);
4102 switch (type) {
4103 case Primitive::kPrimBoolean:
4104 case Primitive::kPrimByte:
4105 case Primitive::kPrimShort:
4106 case Primitive::kPrimChar:
4107 case Primitive::kPrimInt: {
4108 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4109 __ Mov(out, 0);
4110 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4111 less_cond = lt;
4112 break;
4113 }
4114 case Primitive::kPrimLong: {
4115 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
4116 __ B(lt, &less);
4117 __ B(gt, &greater);
4118 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4119 __ Mov(out, 0);
4120 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4121 less_cond = lo;
4122 break;
4123 }
4124 case Primitive::kPrimFloat:
4125 case Primitive::kPrimDouble: {
4126 __ Mov(out, 0);
4127 GenerateVcmp(compare);
4128 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4129 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4130 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4131 break;
4132 }
4133 default:
4134 LOG(FATAL) << "Unexpected compare type " << type;
4135 UNREACHABLE();
4136 }
4137
4138 __ B(eq, &done);
4139 __ B(less_cond, &less);
4140
4141 __ Bind(&greater);
4142 __ Mov(out, 1);
4143 __ B(&done);
4144
4145 __ Bind(&less);
4146 __ Mov(out, -1);
4147
4148 __ Bind(&done);
4149}
4150
4151void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4154 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4155 locations->SetInAt(i, Location::Any());
4156 }
4157 locations->SetOut(Location::Any());
4158}
4159
4160void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4161 LOG(FATAL) << "Unreachable";
4162}
4163
4164void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4165 // TODO (ported from quick): revisit ARM barrier kinds.
4166 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4167 switch (kind) {
4168 case MemBarrierKind::kAnyStore:
4169 case MemBarrierKind::kLoadAny:
4170 case MemBarrierKind::kAnyAny: {
4171 flavor = DmbOptions::ISH;
4172 break;
4173 }
4174 case MemBarrierKind::kStoreStore: {
4175 flavor = DmbOptions::ISHST;
4176 break;
4177 }
4178 default:
4179 LOG(FATAL) << "Unexpected memory barrier " << kind;
4180 }
4181 __ Dmb(flavor);
4182}
4183
4184void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4185 uint32_t offset,
4186 vixl32::Register out_lo,
4187 vixl32::Register out_hi) {
4188 UseScratchRegisterScope temps(GetVIXLAssembler());
4189 if (offset != 0) {
4190 vixl32::Register temp = temps.Acquire();
4191 __ Add(temp, addr, offset);
4192 addr = temp;
4193 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004194 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004195}
4196
4197void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4198 uint32_t offset,
4199 vixl32::Register value_lo,
4200 vixl32::Register value_hi,
4201 vixl32::Register temp1,
4202 vixl32::Register temp2,
4203 HInstruction* instruction) {
4204 UseScratchRegisterScope temps(GetVIXLAssembler());
4205 vixl32::Label fail;
4206 if (offset != 0) {
4207 vixl32::Register temp = temps.Acquire();
4208 __ Add(temp, addr, offset);
4209 addr = temp;
4210 }
4211 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004212 {
4213 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004214 ExactAssemblyScope aas(GetVIXLAssembler(),
4215 vixl32::kMaxInstructionSizeInBytes,
4216 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004217 // We need a load followed by store. (The address used in a STREX instruction must
4218 // be the same as the address in the most recently executed LDREX instruction.)
4219 __ ldrexd(temp1, temp2, MemOperand(addr));
4220 codegen_->MaybeRecordImplicitNullCheck(instruction);
4221 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004222 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004223 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004224}
Artem Serov02109dd2016-09-23 17:17:54 +01004225
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004226void LocationsBuilderARMVIXL::HandleFieldSet(
4227 HInstruction* instruction, const FieldInfo& field_info) {
4228 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4229
4230 LocationSummary* locations =
4231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4232 locations->SetInAt(0, Location::RequiresRegister());
4233
4234 Primitive::Type field_type = field_info.GetFieldType();
4235 if (Primitive::IsFloatingPointType(field_type)) {
4236 locations->SetInAt(1, Location::RequiresFpuRegister());
4237 } else {
4238 locations->SetInAt(1, Location::RequiresRegister());
4239 }
4240
4241 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4242 bool generate_volatile = field_info.IsVolatile()
4243 && is_wide
4244 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4245 bool needs_write_barrier =
4246 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4247 // Temporary registers for the write barrier.
4248 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4249 if (needs_write_barrier) {
4250 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4251 locations->AddTemp(Location::RequiresRegister());
4252 } else if (generate_volatile) {
4253 // ARM encoding have some additional constraints for ldrexd/strexd:
4254 // - registers need to be consecutive
4255 // - the first register should be even but not R14.
4256 // We don't test for ARM yet, and the assertion makes sure that we
4257 // revisit this if we ever enable ARM encoding.
4258 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4259
4260 locations->AddTemp(Location::RequiresRegister());
4261 locations->AddTemp(Location::RequiresRegister());
4262 if (field_type == Primitive::kPrimDouble) {
4263 // For doubles we need two more registers to copy the value.
4264 locations->AddTemp(LocationFrom(r2));
4265 locations->AddTemp(LocationFrom(r3));
4266 }
4267 }
4268}
4269
4270void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4271 const FieldInfo& field_info,
4272 bool value_can_be_null) {
4273 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4274
4275 LocationSummary* locations = instruction->GetLocations();
4276 vixl32::Register base = InputRegisterAt(instruction, 0);
4277 Location value = locations->InAt(1);
4278
4279 bool is_volatile = field_info.IsVolatile();
4280 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4281 Primitive::Type field_type = field_info.GetFieldType();
4282 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4283 bool needs_write_barrier =
4284 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4285
4286 if (is_volatile) {
4287 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4288 }
4289
4290 switch (field_type) {
4291 case Primitive::kPrimBoolean:
4292 case Primitive::kPrimByte: {
4293 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4294 break;
4295 }
4296
4297 case Primitive::kPrimShort:
4298 case Primitive::kPrimChar: {
4299 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4300 break;
4301 }
4302
4303 case Primitive::kPrimInt:
4304 case Primitive::kPrimNot: {
4305 if (kPoisonHeapReferences && needs_write_barrier) {
4306 // Note that in the case where `value` is a null reference,
4307 // we do not enter this block, as a null reference does not
4308 // need poisoning.
4309 DCHECK_EQ(field_type, Primitive::kPrimNot);
4310 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4311 __ Mov(temp, RegisterFrom(value));
4312 GetAssembler()->PoisonHeapReference(temp);
4313 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4314 } else {
4315 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4316 }
4317 break;
4318 }
4319
4320 case Primitive::kPrimLong: {
4321 if (is_volatile && !atomic_ldrd_strd) {
4322 GenerateWideAtomicStore(base,
4323 offset,
4324 LowRegisterFrom(value),
4325 HighRegisterFrom(value),
4326 RegisterFrom(locations->GetTemp(0)),
4327 RegisterFrom(locations->GetTemp(1)),
4328 instruction);
4329 } else {
4330 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4331 codegen_->MaybeRecordImplicitNullCheck(instruction);
4332 }
4333 break;
4334 }
4335
4336 case Primitive::kPrimFloat: {
4337 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4338 break;
4339 }
4340
4341 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004342 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004343 if (is_volatile && !atomic_ldrd_strd) {
4344 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4345 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4346
4347 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4348
4349 GenerateWideAtomicStore(base,
4350 offset,
4351 value_reg_lo,
4352 value_reg_hi,
4353 RegisterFrom(locations->GetTemp(2)),
4354 RegisterFrom(locations->GetTemp(3)),
4355 instruction);
4356 } else {
4357 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4358 codegen_->MaybeRecordImplicitNullCheck(instruction);
4359 }
4360 break;
4361 }
4362
4363 case Primitive::kPrimVoid:
4364 LOG(FATAL) << "Unreachable type " << field_type;
4365 UNREACHABLE();
4366 }
4367
4368 // Longs and doubles are handled in the switch.
4369 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004370 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4371 // should use a scope and the assembler to emit the store instruction to guarantee that we
4372 // record the pc at the correct position. But the `Assembler` does not automatically handle
4373 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4374 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004375 codegen_->MaybeRecordImplicitNullCheck(instruction);
4376 }
4377
4378 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4379 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4380 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4381 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4382 }
4383
4384 if (is_volatile) {
4385 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4386 }
4387}
4388
Artem Serov02d37832016-10-25 15:25:33 +01004389void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4390 const FieldInfo& field_info) {
4391 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4392
4393 bool object_field_get_with_read_barrier =
4394 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4395 LocationSummary* locations =
4396 new (GetGraph()->GetArena()) LocationSummary(instruction,
4397 object_field_get_with_read_barrier ?
4398 LocationSummary::kCallOnSlowPath :
4399 LocationSummary::kNoCall);
4400 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4401 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4402 }
4403 locations->SetInAt(0, Location::RequiresRegister());
4404
4405 bool volatile_for_double = field_info.IsVolatile()
4406 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4407 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4408 // The output overlaps in case of volatile long: we don't want the
4409 // code generated by GenerateWideAtomicLoad to overwrite the
4410 // object's location. Likewise, in the case of an object field get
4411 // with read barriers enabled, we do not want the load to overwrite
4412 // the object's location, as we need it to emit the read barrier.
4413 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4414 object_field_get_with_read_barrier;
4415
4416 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4417 locations->SetOut(Location::RequiresFpuRegister());
4418 } else {
4419 locations->SetOut(Location::RequiresRegister(),
4420 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4421 }
4422 if (volatile_for_double) {
4423 // ARM encoding have some additional constraints for ldrexd/strexd:
4424 // - registers need to be consecutive
4425 // - the first register should be even but not R14.
4426 // We don't test for ARM yet, and the assertion makes sure that we
4427 // revisit this if we ever enable ARM encoding.
4428 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4429 locations->AddTemp(Location::RequiresRegister());
4430 locations->AddTemp(Location::RequiresRegister());
4431 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4432 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004433 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004434 locations->AddTemp(Location::RequiresRegister());
4435 }
4436}
4437
4438Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4439 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4440 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4441 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4442 return Location::ConstantLocation(input->AsConstant());
4443 } else {
4444 return Location::RequiresFpuRegister();
4445 }
4446}
4447
Artem Serov02109dd2016-09-23 17:17:54 +01004448Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4449 Opcode opcode) {
4450 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4451 if (constant->IsConstant() &&
4452 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4453 return Location::ConstantLocation(constant->AsConstant());
4454 }
4455 return Location::RequiresRegister();
4456}
4457
4458bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4459 Opcode opcode) {
4460 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4461 if (Primitive::Is64BitType(input_cst->GetType())) {
4462 Opcode high_opcode = opcode;
4463 SetCc low_set_cc = kCcDontCare;
4464 switch (opcode) {
4465 case SUB:
4466 // Flip the operation to an ADD.
4467 value = -value;
4468 opcode = ADD;
4469 FALLTHROUGH_INTENDED;
4470 case ADD:
4471 if (Low32Bits(value) == 0u) {
4472 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4473 }
4474 high_opcode = ADC;
4475 low_set_cc = kCcSet;
4476 break;
4477 default:
4478 break;
4479 }
4480 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4481 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4482 } else {
4483 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4484 }
4485}
4486
4487// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4488// enabled.
4489bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4490 Opcode opcode,
4491 SetCc set_cc) {
4492 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4493 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4494 return true;
4495 }
4496 Opcode neg_opcode = kNoOperand;
4497 switch (opcode) {
4498 case AND: neg_opcode = BIC; value = ~value; break;
4499 case ORR: neg_opcode = ORN; value = ~value; break;
4500 case ADD: neg_opcode = SUB; value = -value; break;
4501 case ADC: neg_opcode = SBC; value = ~value; break;
4502 case SUB: neg_opcode = ADD; value = -value; break;
4503 case SBC: neg_opcode = ADC; value = ~value; break;
4504 default:
4505 return false;
4506 }
4507 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4508}
4509
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004510void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4511 const FieldInfo& field_info) {
4512 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4513
4514 LocationSummary* locations = instruction->GetLocations();
4515 vixl32::Register base = InputRegisterAt(instruction, 0);
4516 Location out = locations->Out();
4517 bool is_volatile = field_info.IsVolatile();
4518 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4519 Primitive::Type field_type = field_info.GetFieldType();
4520 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4521
4522 switch (field_type) {
4523 case Primitive::kPrimBoolean:
4524 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4525 break;
4526
4527 case Primitive::kPrimByte:
4528 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4529 break;
4530
4531 case Primitive::kPrimShort:
4532 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4533 break;
4534
4535 case Primitive::kPrimChar:
4536 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4537 break;
4538
4539 case Primitive::kPrimInt:
4540 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4541 break;
4542
4543 case Primitive::kPrimNot: {
4544 // /* HeapReference<Object> */ out = *(base + offset)
4545 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004546 Location temp_loc = locations->GetTemp(0);
4547 // Note that a potential implicit null check is handled in this
4548 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4549 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4550 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4551 if (is_volatile) {
4552 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4553 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004554 } else {
4555 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004556 codegen_->MaybeRecordImplicitNullCheck(instruction);
4557 if (is_volatile) {
4558 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4559 }
4560 // If read barriers are enabled, emit read barriers other than
4561 // Baker's using a slow path (and also unpoison the loaded
4562 // reference, if heap poisoning is enabled).
4563 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4564 }
4565 break;
4566 }
4567
4568 case Primitive::kPrimLong:
4569 if (is_volatile && !atomic_ldrd_strd) {
4570 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4571 } else {
4572 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4573 }
4574 break;
4575
4576 case Primitive::kPrimFloat:
4577 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4578 break;
4579
4580 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004581 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004582 if (is_volatile && !atomic_ldrd_strd) {
4583 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4584 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4585 GenerateWideAtomicLoad(base, offset, lo, hi);
4586 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4587 // scope.
4588 codegen_->MaybeRecordImplicitNullCheck(instruction);
4589 __ Vmov(out_dreg, lo, hi);
4590 } else {
4591 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004592 codegen_->MaybeRecordImplicitNullCheck(instruction);
4593 }
4594 break;
4595 }
4596
4597 case Primitive::kPrimVoid:
4598 LOG(FATAL) << "Unreachable type " << field_type;
4599 UNREACHABLE();
4600 }
4601
4602 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4603 // Potential implicit null checks, in the case of reference or
4604 // double fields, are handled in the previous switch statement.
4605 } else {
4606 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004607 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4608 // should use a scope and the assembler to emit the load instruction to guarantee that we
4609 // record the pc at the correct position. But the `Assembler` does not automatically handle
4610 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4611 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004612 codegen_->MaybeRecordImplicitNullCheck(instruction);
4613 }
4614
4615 if (is_volatile) {
4616 if (field_type == Primitive::kPrimNot) {
4617 // Memory barriers, in the case of references, are also handled
4618 // in the previous switch statement.
4619 } else {
4620 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4621 }
4622 }
4623}
4624
4625void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4626 HandleFieldSet(instruction, instruction->GetFieldInfo());
4627}
4628
4629void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4630 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4631}
4632
4633void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4634 HandleFieldGet(instruction, instruction->GetFieldInfo());
4635}
4636
4637void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4638 HandleFieldGet(instruction, instruction->GetFieldInfo());
4639}
4640
4641void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4642 HandleFieldGet(instruction, instruction->GetFieldInfo());
4643}
4644
4645void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4646 HandleFieldGet(instruction, instruction->GetFieldInfo());
4647}
4648
Scott Wakelingc34dba72016-10-03 10:14:44 +01004649void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4650 HandleFieldSet(instruction, instruction->GetFieldInfo());
4651}
4652
4653void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4654 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4655}
4656
Artem Serovcfbe9132016-10-14 15:58:56 +01004657void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4658 HUnresolvedInstanceFieldGet* instruction) {
4659 FieldAccessCallingConventionARMVIXL calling_convention;
4660 codegen_->CreateUnresolvedFieldLocationSummary(
4661 instruction, instruction->GetFieldType(), calling_convention);
4662}
4663
4664void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4665 HUnresolvedInstanceFieldGet* instruction) {
4666 FieldAccessCallingConventionARMVIXL calling_convention;
4667 codegen_->GenerateUnresolvedFieldAccess(instruction,
4668 instruction->GetFieldType(),
4669 instruction->GetFieldIndex(),
4670 instruction->GetDexPc(),
4671 calling_convention);
4672}
4673
4674void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4675 HUnresolvedInstanceFieldSet* instruction) {
4676 FieldAccessCallingConventionARMVIXL calling_convention;
4677 codegen_->CreateUnresolvedFieldLocationSummary(
4678 instruction, instruction->GetFieldType(), calling_convention);
4679}
4680
4681void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4682 HUnresolvedInstanceFieldSet* instruction) {
4683 FieldAccessCallingConventionARMVIXL calling_convention;
4684 codegen_->GenerateUnresolvedFieldAccess(instruction,
4685 instruction->GetFieldType(),
4686 instruction->GetFieldIndex(),
4687 instruction->GetDexPc(),
4688 calling_convention);
4689}
4690
4691void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4692 HUnresolvedStaticFieldGet* instruction) {
4693 FieldAccessCallingConventionARMVIXL calling_convention;
4694 codegen_->CreateUnresolvedFieldLocationSummary(
4695 instruction, instruction->GetFieldType(), calling_convention);
4696}
4697
4698void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4699 HUnresolvedStaticFieldGet* instruction) {
4700 FieldAccessCallingConventionARMVIXL calling_convention;
4701 codegen_->GenerateUnresolvedFieldAccess(instruction,
4702 instruction->GetFieldType(),
4703 instruction->GetFieldIndex(),
4704 instruction->GetDexPc(),
4705 calling_convention);
4706}
4707
4708void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4709 HUnresolvedStaticFieldSet* instruction) {
4710 FieldAccessCallingConventionARMVIXL calling_convention;
4711 codegen_->CreateUnresolvedFieldLocationSummary(
4712 instruction, instruction->GetFieldType(), calling_convention);
4713}
4714
4715void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4716 HUnresolvedStaticFieldSet* instruction) {
4717 FieldAccessCallingConventionARMVIXL calling_convention;
4718 codegen_->GenerateUnresolvedFieldAccess(instruction,
4719 instruction->GetFieldType(),
4720 instruction->GetFieldIndex(),
4721 instruction->GetDexPc(),
4722 calling_convention);
4723}
4724
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004725void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004726 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004727 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004728}
4729
4730void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4731 if (CanMoveNullCheckToUser(instruction)) {
4732 return;
4733 }
4734
4735 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004736 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004737 ExactAssemblyScope aas(GetVIXLAssembler(),
4738 vixl32::kMaxInstructionSizeInBytes,
4739 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004740 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4741 RecordPcInfo(instruction, instruction->GetDexPc());
4742}
4743
4744void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4745 NullCheckSlowPathARMVIXL* slow_path =
4746 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4747 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004748 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004749}
4750
4751void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4752 codegen_->GenerateNullCheck(instruction);
4753}
4754
Scott Wakelingc34dba72016-10-03 10:14:44 +01004755static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4756 switch (type) {
4757 case Primitive::kPrimNot:
4758 return kLoadWord;
4759 case Primitive::kPrimBoolean:
4760 return kLoadUnsignedByte;
4761 case Primitive::kPrimByte:
4762 return kLoadSignedByte;
4763 case Primitive::kPrimChar:
4764 return kLoadUnsignedHalfword;
4765 case Primitive::kPrimShort:
4766 return kLoadSignedHalfword;
4767 case Primitive::kPrimInt:
4768 return kLoadWord;
4769 case Primitive::kPrimLong:
4770 return kLoadWordPair;
4771 case Primitive::kPrimFloat:
4772 return kLoadSWord;
4773 case Primitive::kPrimDouble:
4774 return kLoadDWord;
4775 default:
4776 LOG(FATAL) << "Unreachable type " << type;
4777 UNREACHABLE();
4778 }
4779}
4780
4781static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4782 switch (type) {
4783 case Primitive::kPrimNot:
4784 return kStoreWord;
4785 case Primitive::kPrimBoolean:
4786 case Primitive::kPrimByte:
4787 return kStoreByte;
4788 case Primitive::kPrimChar:
4789 case Primitive::kPrimShort:
4790 return kStoreHalfword;
4791 case Primitive::kPrimInt:
4792 return kStoreWord;
4793 case Primitive::kPrimLong:
4794 return kStoreWordPair;
4795 case Primitive::kPrimFloat:
4796 return kStoreSWord;
4797 case Primitive::kPrimDouble:
4798 return kStoreDWord;
4799 default:
4800 LOG(FATAL) << "Unreachable type " << type;
4801 UNREACHABLE();
4802 }
4803}
4804
4805void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
4806 Location out_loc,
4807 vixl32::Register base,
4808 vixl32::Register reg_index,
4809 vixl32::Condition cond) {
4810 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4811 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4812
4813 switch (type) {
4814 case Primitive::kPrimByte:
4815 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
4816 break;
4817 case Primitive::kPrimBoolean:
4818 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
4819 break;
4820 case Primitive::kPrimShort:
4821 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
4822 break;
4823 case Primitive::kPrimChar:
4824 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
4825 break;
4826 case Primitive::kPrimNot:
4827 case Primitive::kPrimInt:
4828 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
4829 break;
4830 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4831 case Primitive::kPrimLong:
4832 case Primitive::kPrimFloat:
4833 case Primitive::kPrimDouble:
4834 default:
4835 LOG(FATAL) << "Unreachable type " << type;
4836 UNREACHABLE();
4837 }
4838}
4839
4840void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
4841 Location loc,
4842 vixl32::Register base,
4843 vixl32::Register reg_index,
4844 vixl32::Condition cond) {
4845 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4846 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4847
4848 switch (type) {
4849 case Primitive::kPrimByte:
4850 case Primitive::kPrimBoolean:
4851 __ Strb(cond, RegisterFrom(loc), mem_address);
4852 break;
4853 case Primitive::kPrimShort:
4854 case Primitive::kPrimChar:
4855 __ Strh(cond, RegisterFrom(loc), mem_address);
4856 break;
4857 case Primitive::kPrimNot:
4858 case Primitive::kPrimInt:
4859 __ Str(cond, RegisterFrom(loc), mem_address);
4860 break;
4861 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4862 case Primitive::kPrimLong:
4863 case Primitive::kPrimFloat:
4864 case Primitive::kPrimDouble:
4865 default:
4866 LOG(FATAL) << "Unreachable type " << type;
4867 UNREACHABLE();
4868 }
4869}
4870
4871void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4872 bool object_array_get_with_read_barrier =
4873 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4874 LocationSummary* locations =
4875 new (GetGraph()->GetArena()) LocationSummary(instruction,
4876 object_array_get_with_read_barrier ?
4877 LocationSummary::kCallOnSlowPath :
4878 LocationSummary::kNoCall);
4879 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004880 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004881 }
4882 locations->SetInAt(0, Location::RequiresRegister());
4883 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4884 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4886 } else {
4887 // The output overlaps in the case of an object array get with
4888 // read barriers enabled: we do not want the move to overwrite the
4889 // array's location, as we need it to emit the read barrier.
4890 locations->SetOut(
4891 Location::RequiresRegister(),
4892 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4893 }
4894 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004895 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004896 // Also need for String compression feature.
4897 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4898 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004899 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004900 }
4901}
4902
4903void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004904 LocationSummary* locations = instruction->GetLocations();
4905 Location obj_loc = locations->InAt(0);
4906 vixl32::Register obj = InputRegisterAt(instruction, 0);
4907 Location index = locations->InAt(1);
4908 Location out_loc = locations->Out();
4909 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4910 Primitive::Type type = instruction->GetType();
4911 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4912 instruction->IsStringCharAt();
4913 HInstruction* array_instr = instruction->GetArray();
4914 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01004915
4916 switch (type) {
4917 case Primitive::kPrimBoolean:
4918 case Primitive::kPrimByte:
4919 case Primitive::kPrimShort:
4920 case Primitive::kPrimChar:
4921 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004922 vixl32::Register length;
4923 if (maybe_compressed_char_at) {
4924 length = RegisterFrom(locations->GetTemp(0));
4925 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4926 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
4927 codegen_->MaybeRecordImplicitNullCheck(instruction);
4928 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01004929 if (index.IsConstant()) {
4930 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4931 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004932 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004933 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4934 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4935 "Expecting 0=compressed, 1=uncompressed");
4936 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004937 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
4938 RegisterFrom(out_loc),
4939 obj,
4940 data_offset + const_index);
4941 __ B(&done);
4942 __ Bind(&uncompressed_load);
4943 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4944 RegisterFrom(out_loc),
4945 obj,
4946 data_offset + (const_index << 1));
4947 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004948 } else {
4949 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4950
4951 LoadOperandType load_type = GetLoadOperandType(type);
4952 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
4953 }
4954 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004955 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004956 vixl32::Register temp = temps.Acquire();
4957
4958 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01004959 // We do not need to compute the intermediate address from the array: the
4960 // input instruction has done it already. See the comment in
4961 // `TryExtractArrayAccessAddress()`.
4962 if (kIsDebugBuild) {
4963 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4964 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4965 }
4966 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01004967 } else {
4968 __ Add(temp, obj, data_offset);
4969 }
4970 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004971 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004972 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4973 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4974 "Expecting 0=compressed, 1=uncompressed");
4975 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004976 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
4977 __ B(&done);
4978 __ Bind(&uncompressed_load);
4979 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
4980 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004981 } else {
4982 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4983 }
4984 }
4985 break;
4986 }
4987
4988 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004989 // The read barrier instrumentation of object ArrayGet
4990 // instructions does not support the HIntermediateAddress
4991 // instruction.
4992 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4993
Scott Wakelingc34dba72016-10-03 10:14:44 +01004994 static_assert(
4995 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4996 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4997 // /* HeapReference<Object> */ out =
4998 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4999 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005000 Location temp = locations->GetTemp(0);
5001 // Note that a potential implicit null check is handled in this
5002 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5003 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5004 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005005 } else {
5006 vixl32::Register out = OutputRegister(instruction);
5007 if (index.IsConstant()) {
5008 size_t offset =
5009 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5010 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005011 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5012 // we should use a scope and the assembler to emit the load instruction to guarantee that
5013 // we record the pc at the correct position. But the `Assembler` does not automatically
5014 // handle unencodable offsets. Practically, everything is fine because the helper and
5015 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005016 codegen_->MaybeRecordImplicitNullCheck(instruction);
5017 // If read barriers are enabled, emit read barriers other than
5018 // Baker's using a slow path (and also unpoison the loaded
5019 // reference, if heap poisoning is enabled).
5020 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5021 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005022 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005023 vixl32::Register temp = temps.Acquire();
5024
5025 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005026 // We do not need to compute the intermediate address from the array: the
5027 // input instruction has done it already. See the comment in
5028 // `TryExtractArrayAccessAddress()`.
5029 if (kIsDebugBuild) {
5030 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5031 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5032 }
5033 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005034 } else {
5035 __ Add(temp, obj, data_offset);
5036 }
5037 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005038 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005039 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5040 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5041 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005042 codegen_->MaybeRecordImplicitNullCheck(instruction);
5043 // If read barriers are enabled, emit read barriers other than
5044 // Baker's using a slow path (and also unpoison the loaded
5045 // reference, if heap poisoning is enabled).
5046 codegen_->MaybeGenerateReadBarrierSlow(
5047 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5048 }
5049 }
5050 break;
5051 }
5052
5053 case Primitive::kPrimLong: {
5054 if (index.IsConstant()) {
5055 size_t offset =
5056 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
5057 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5058 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005059 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005060 vixl32::Register temp = temps.Acquire();
5061 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5062 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5063 }
5064 break;
5065 }
5066
5067 case Primitive::kPrimFloat: {
5068 vixl32::SRegister out = SRegisterFrom(out_loc);
5069 if (index.IsConstant()) {
5070 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5071 GetAssembler()->LoadSFromOffset(out, obj, offset);
5072 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005073 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005074 vixl32::Register temp = temps.Acquire();
5075 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5076 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5077 }
5078 break;
5079 }
5080
5081 case Primitive::kPrimDouble: {
5082 if (index.IsConstant()) {
5083 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
5084 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5085 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005086 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005087 vixl32::Register temp = temps.Acquire();
5088 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5089 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5090 }
5091 break;
5092 }
5093
5094 case Primitive::kPrimVoid:
5095 LOG(FATAL) << "Unreachable type " << type;
5096 UNREACHABLE();
5097 }
5098
5099 if (type == Primitive::kPrimNot) {
5100 // Potential implicit null checks, in the case of reference
5101 // arrays, are handled in the previous switch statement.
5102 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005103 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5104 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005105 codegen_->MaybeRecordImplicitNullCheck(instruction);
5106 }
5107}
5108
5109void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5110 Primitive::Type value_type = instruction->GetComponentType();
5111
5112 bool needs_write_barrier =
5113 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5114 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5115
5116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5117 instruction,
5118 may_need_runtime_call_for_type_check ?
5119 LocationSummary::kCallOnSlowPath :
5120 LocationSummary::kNoCall);
5121
5122 locations->SetInAt(0, Location::RequiresRegister());
5123 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5124 if (Primitive::IsFloatingPointType(value_type)) {
5125 locations->SetInAt(2, Location::RequiresFpuRegister());
5126 } else {
5127 locations->SetInAt(2, Location::RequiresRegister());
5128 }
5129 if (needs_write_barrier) {
5130 // Temporary registers for the write barrier.
5131 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5132 locations->AddTemp(Location::RequiresRegister());
5133 }
5134}
5135
5136void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005137 LocationSummary* locations = instruction->GetLocations();
5138 vixl32::Register array = InputRegisterAt(instruction, 0);
5139 Location index = locations->InAt(1);
5140 Primitive::Type value_type = instruction->GetComponentType();
5141 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5142 bool needs_write_barrier =
5143 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5144 uint32_t data_offset =
5145 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5146 Location value_loc = locations->InAt(2);
5147 HInstruction* array_instr = instruction->GetArray();
5148 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005149
5150 switch (value_type) {
5151 case Primitive::kPrimBoolean:
5152 case Primitive::kPrimByte:
5153 case Primitive::kPrimShort:
5154 case Primitive::kPrimChar:
5155 case Primitive::kPrimInt: {
5156 if (index.IsConstant()) {
5157 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5158 uint32_t full_offset =
5159 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5160 StoreOperandType store_type = GetStoreOperandType(value_type);
5161 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5162 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005163 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005164 vixl32::Register temp = temps.Acquire();
5165
5166 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005167 // We do not need to compute the intermediate address from the array: the
5168 // input instruction has done it already. See the comment in
5169 // `TryExtractArrayAccessAddress()`.
5170 if (kIsDebugBuild) {
5171 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5172 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5173 }
5174 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005175 } else {
5176 __ Add(temp, array, data_offset);
5177 }
5178 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5179 }
5180 break;
5181 }
5182
5183 case Primitive::kPrimNot: {
5184 vixl32::Register value = RegisterFrom(value_loc);
5185 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5186 // See the comment in instruction_simplifier_shared.cc.
5187 DCHECK(!has_intermediate_address);
5188
5189 if (instruction->InputAt(2)->IsNullConstant()) {
5190 // Just setting null.
5191 if (index.IsConstant()) {
5192 size_t offset =
5193 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5194 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5195 } else {
5196 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005197 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005198 vixl32::Register temp = temps.Acquire();
5199 __ Add(temp, array, data_offset);
5200 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5201 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005202 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5203 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005204 codegen_->MaybeRecordImplicitNullCheck(instruction);
5205 DCHECK(!needs_write_barrier);
5206 DCHECK(!may_need_runtime_call_for_type_check);
5207 break;
5208 }
5209
5210 DCHECK(needs_write_barrier);
5211 Location temp1_loc = locations->GetTemp(0);
5212 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5213 Location temp2_loc = locations->GetTemp(1);
5214 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5216 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5217 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5218 vixl32::Label done;
5219 SlowPathCodeARMVIXL* slow_path = nullptr;
5220
5221 if (may_need_runtime_call_for_type_check) {
5222 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5223 codegen_->AddSlowPath(slow_path);
5224 if (instruction->GetValueCanBeNull()) {
5225 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005226 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005227 if (index.IsConstant()) {
5228 size_t offset =
5229 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5230 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5231 } else {
5232 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005233 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005234 vixl32::Register temp = temps.Acquire();
5235 __ Add(temp, array, data_offset);
5236 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5237 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005238 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5239 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005240 codegen_->MaybeRecordImplicitNullCheck(instruction);
5241 __ B(&done);
5242 __ Bind(&non_zero);
5243 }
5244
5245 // Note that when read barriers are enabled, the type checks
5246 // are performed without read barriers. This is fine, even in
5247 // the case where a class object is in the from-space after
5248 // the flip, as a comparison involving such a type would not
5249 // produce a false positive; it may of course produce a false
5250 // negative, in which case we would take the ArraySet slow
5251 // path.
5252
Alexandre Rames374ddf32016-11-04 10:40:49 +00005253 {
5254 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005255 ExactAssemblyScope aas(GetVIXLAssembler(),
5256 vixl32::kMaxInstructionSizeInBytes,
5257 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005258 // /* HeapReference<Class> */ temp1 = array->klass_
5259 __ ldr(temp1, MemOperand(array, class_offset));
5260 codegen_->MaybeRecordImplicitNullCheck(instruction);
5261 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005262 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5263
5264 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5265 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5266 // /* HeapReference<Class> */ temp2 = value->klass_
5267 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5268 // If heap poisoning is enabled, no need to unpoison `temp1`
5269 // nor `temp2`, as we are comparing two poisoned references.
5270 __ Cmp(temp1, temp2);
5271
5272 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5273 vixl32::Label do_put;
5274 __ B(eq, &do_put);
5275 // If heap poisoning is enabled, the `temp1` reference has
5276 // not been unpoisoned yet; unpoison it now.
5277 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5278
5279 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5280 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5281 // If heap poisoning is enabled, no need to unpoison
5282 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005283 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005284 __ Bind(&do_put);
5285 } else {
5286 __ B(ne, slow_path->GetEntryLabel());
5287 }
5288 }
5289
5290 vixl32::Register source = value;
5291 if (kPoisonHeapReferences) {
5292 // Note that in the case where `value` is a null reference,
5293 // we do not enter this block, as a null reference does not
5294 // need poisoning.
5295 DCHECK_EQ(value_type, Primitive::kPrimNot);
5296 __ Mov(temp1, value);
5297 GetAssembler()->PoisonHeapReference(temp1);
5298 source = temp1;
5299 }
5300
5301 if (index.IsConstant()) {
5302 size_t offset =
5303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5304 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5305 } else {
5306 DCHECK(index.IsRegister()) << index;
5307
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005308 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005309 vixl32::Register temp = temps.Acquire();
5310 __ Add(temp, array, data_offset);
5311 codegen_->StoreToShiftedRegOffset(value_type,
5312 LocationFrom(source),
5313 temp,
5314 RegisterFrom(index));
5315 }
5316
5317 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005318 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5319 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005320 codegen_->MaybeRecordImplicitNullCheck(instruction);
5321 }
5322
5323 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5324
5325 if (done.IsReferenced()) {
5326 __ Bind(&done);
5327 }
5328
5329 if (slow_path != nullptr) {
5330 __ Bind(slow_path->GetExitLabel());
5331 }
5332
5333 break;
5334 }
5335
5336 case Primitive::kPrimLong: {
5337 Location value = locations->InAt(2);
5338 if (index.IsConstant()) {
5339 size_t offset =
5340 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
5341 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5342 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005343 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005344 vixl32::Register temp = temps.Acquire();
5345 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5346 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5347 }
5348 break;
5349 }
5350
5351 case Primitive::kPrimFloat: {
5352 Location value = locations->InAt(2);
5353 DCHECK(value.IsFpuRegister());
5354 if (index.IsConstant()) {
5355 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5356 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5357 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005358 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005359 vixl32::Register temp = temps.Acquire();
5360 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5361 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5362 }
5363 break;
5364 }
5365
5366 case Primitive::kPrimDouble: {
5367 Location value = locations->InAt(2);
5368 DCHECK(value.IsFpuRegisterPair());
5369 if (index.IsConstant()) {
5370 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
5371 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5372 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005373 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005374 vixl32::Register temp = temps.Acquire();
5375 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5376 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5377 }
5378 break;
5379 }
5380
5381 case Primitive::kPrimVoid:
5382 LOG(FATAL) << "Unreachable type " << value_type;
5383 UNREACHABLE();
5384 }
5385
5386 // Objects are handled in the switch.
5387 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005388 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5389 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005390 codegen_->MaybeRecordImplicitNullCheck(instruction);
5391 }
5392}
5393
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005394void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5395 LocationSummary* locations =
5396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5397 locations->SetInAt(0, Location::RequiresRegister());
5398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5399}
5400
5401void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5402 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5403 vixl32::Register obj = InputRegisterAt(instruction, 0);
5404 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005405 {
Artem Serov0fb37192016-12-06 18:13:40 +00005406 ExactAssemblyScope aas(GetVIXLAssembler(),
5407 vixl32::kMaxInstructionSizeInBytes,
5408 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005409 __ ldr(out, MemOperand(obj, offset));
5410 codegen_->MaybeRecordImplicitNullCheck(instruction);
5411 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005412 // Mask out compression flag from String's array length.
5413 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005414 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005415 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005416}
5417
Artem Serov2bbc9532016-10-21 11:51:50 +01005418void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005419 LocationSummary* locations =
5420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5421
5422 locations->SetInAt(0, Location::RequiresRegister());
5423 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5425}
5426
5427void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5428 vixl32::Register out = OutputRegister(instruction);
5429 vixl32::Register first = InputRegisterAt(instruction, 0);
5430 Location second = instruction->GetLocations()->InAt(1);
5431
Artem Serov2bbc9532016-10-21 11:51:50 +01005432 if (second.IsRegister()) {
5433 __ Add(out, first, RegisterFrom(second));
5434 } else {
5435 __ Add(out, first, second.GetConstant()->AsIntConstant()->GetValue());
5436 }
5437}
5438
Scott Wakelingc34dba72016-10-03 10:14:44 +01005439void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5440 RegisterSet caller_saves = RegisterSet::Empty();
5441 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5442 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5443 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5444 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5445 locations->SetInAt(0, Location::RequiresRegister());
5446 locations->SetInAt(1, Location::RequiresRegister());
5447}
5448
5449void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5450 SlowPathCodeARMVIXL* slow_path =
5451 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5452 codegen_->AddSlowPath(slow_path);
5453
5454 vixl32::Register index = InputRegisterAt(instruction, 0);
5455 vixl32::Register length = InputRegisterAt(instruction, 1);
5456
5457 __ Cmp(index, length);
5458 __ B(hs, slow_path->GetEntryLabel());
5459}
5460
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005461void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5462 vixl32::Register card,
5463 vixl32::Register object,
5464 vixl32::Register value,
5465 bool can_be_null) {
5466 vixl32::Label is_null;
5467 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005468 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005469 }
5470 GetAssembler()->LoadFromOffset(
5471 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005472 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005473 __ Strb(card, MemOperand(card, temp));
5474 if (can_be_null) {
5475 __ Bind(&is_null);
5476 }
5477}
5478
Scott Wakelingfe885462016-09-22 10:24:38 +01005479void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5480 LOG(FATAL) << "Unreachable";
5481}
5482
5483void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5484 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5485}
5486
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005487void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005488 LocationSummary* locations =
5489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5490 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005491}
5492
5493void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5494 HBasicBlock* block = instruction->GetBlock();
5495 if (block->GetLoopInformation() != nullptr) {
5496 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5497 // The back edge will generate the suspend check.
5498 return;
5499 }
5500 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5501 // The goto will generate the suspend check.
5502 return;
5503 }
5504 GenerateSuspendCheck(instruction, nullptr);
5505}
5506
5507void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5508 HBasicBlock* successor) {
5509 SuspendCheckSlowPathARMVIXL* slow_path =
5510 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5511 if (slow_path == nullptr) {
5512 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5513 instruction->SetSlowPath(slow_path);
5514 codegen_->AddSlowPath(slow_path);
5515 if (successor != nullptr) {
5516 DCHECK(successor->IsLoopHeader());
5517 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5518 }
5519 } else {
5520 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5521 }
5522
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005523 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005524 vixl32::Register temp = temps.Acquire();
5525 GetAssembler()->LoadFromOffset(
5526 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5527 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005528 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005529 __ Bind(slow_path->GetReturnLabel());
5530 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005531 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005532 __ B(slow_path->GetEntryLabel());
5533 }
5534}
5535
Scott Wakelingfe885462016-09-22 10:24:38 +01005536ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5537 return codegen_->GetAssembler();
5538}
5539
5540void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005541 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005542 MoveOperands* move = moves_[index];
5543 Location source = move->GetSource();
5544 Location destination = move->GetDestination();
5545
5546 if (source.IsRegister()) {
5547 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005548 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005549 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005550 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005551 } else {
5552 DCHECK(destination.IsStackSlot());
5553 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005554 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005555 sp,
5556 destination.GetStackIndex());
5557 }
5558 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005559 if (destination.IsRegister()) {
5560 GetAssembler()->LoadFromOffset(kLoadWord,
5561 RegisterFrom(destination),
5562 sp,
5563 source.GetStackIndex());
5564 } else if (destination.IsFpuRegister()) {
5565 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5566 } else {
5567 DCHECK(destination.IsStackSlot());
5568 vixl32::Register temp = temps.Acquire();
5569 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5570 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5571 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005572 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005573 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005574 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005575 } else if (destination.IsFpuRegister()) {
5576 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5577 } else {
5578 DCHECK(destination.IsStackSlot());
5579 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5580 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005581 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005582 if (destination.IsDoubleStackSlot()) {
5583 vixl32::DRegister temp = temps.AcquireD();
5584 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5585 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5586 } else if (destination.IsRegisterPair()) {
5587 DCHECK(ExpectedPairLayout(destination));
5588 GetAssembler()->LoadFromOffset(
5589 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5590 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005591 DCHECK(destination.IsFpuRegisterPair()) << destination;
5592 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005593 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005594 } else if (source.IsRegisterPair()) {
5595 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005596 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5597 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005598 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005599 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005600 } else {
5601 DCHECK(destination.IsDoubleStackSlot()) << destination;
5602 DCHECK(ExpectedPairLayout(source));
5603 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005604 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005605 sp,
5606 destination.GetStackIndex());
5607 }
5608 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005609 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005610 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005611 } else if (destination.IsFpuRegisterPair()) {
5612 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5613 } else {
5614 DCHECK(destination.IsDoubleStackSlot()) << destination;
5615 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5616 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005617 } else {
5618 DCHECK(source.IsConstant()) << source;
5619 HConstant* constant = source.GetConstant();
5620 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5621 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5622 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005623 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005624 } else {
5625 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005626 vixl32::Register temp = temps.Acquire();
5627 __ Mov(temp, value);
5628 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5629 }
5630 } else if (constant->IsLongConstant()) {
5631 int64_t value = constant->AsLongConstant()->GetValue();
5632 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005633 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5634 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005635 } else {
5636 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005637 vixl32::Register temp = temps.Acquire();
5638 __ Mov(temp, Low32Bits(value));
5639 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5640 __ Mov(temp, High32Bits(value));
5641 GetAssembler()->StoreToOffset(kStoreWord,
5642 temp,
5643 sp,
5644 destination.GetHighStackIndex(kArmWordSize));
5645 }
5646 } else if (constant->IsDoubleConstant()) {
5647 double value = constant->AsDoubleConstant()->GetValue();
5648 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005649 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005650 } else {
5651 DCHECK(destination.IsDoubleStackSlot()) << destination;
5652 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005653 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005654 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005655 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005656 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005657 GetAssembler()->StoreToOffset(kStoreWord,
5658 temp,
5659 sp,
5660 destination.GetHighStackIndex(kArmWordSize));
5661 }
5662 } else {
5663 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5664 float value = constant->AsFloatConstant()->GetValue();
5665 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005667 } else {
5668 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005669 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005670 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005671 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5672 }
5673 }
5674 }
5675}
5676
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005677void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5678 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5679 vixl32::Register temp = temps.Acquire();
5680 __ Mov(temp, reg);
5681 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5682 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005683}
5684
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005685void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5686 // TODO(VIXL32): Double check the performance of this implementation.
5687 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5688 vixl32::Register temp = temps.Acquire();
5689 vixl32::SRegister temp_s = temps.AcquireS();
5690
5691 __ Ldr(temp, MemOperand(sp, mem1));
5692 __ Vldr(temp_s, MemOperand(sp, mem2));
5693 __ Str(temp, MemOperand(sp, mem2));
5694 __ Vstr(temp_s, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005695}
5696
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005697void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5698 MoveOperands* move = moves_[index];
5699 Location source = move->GetSource();
5700 Location destination = move->GetDestination();
5701 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5702
5703 if (source.IsRegister() && destination.IsRegister()) {
5704 vixl32::Register temp = temps.Acquire();
5705 DCHECK(!RegisterFrom(source).Is(temp));
5706 DCHECK(!RegisterFrom(destination).Is(temp));
5707 __ Mov(temp, RegisterFrom(destination));
5708 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5709 __ Mov(RegisterFrom(source), temp);
5710 } else if (source.IsRegister() && destination.IsStackSlot()) {
5711 Exchange(RegisterFrom(source), destination.GetStackIndex());
5712 } else if (source.IsStackSlot() && destination.IsRegister()) {
5713 Exchange(RegisterFrom(destination), source.GetStackIndex());
5714 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005715 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005716 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005717 vixl32::SRegister temp = temps.AcquireS();
5718 __ Vmov(temp, SRegisterFrom(source));
5719 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5720 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005721 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5722 vixl32::DRegister temp = temps.AcquireD();
5723 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5724 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5725 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5726 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5727 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5728 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5729 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5730 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5731 vixl32::DRegister temp = temps.AcquireD();
5732 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5733 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5734 GetAssembler()->StoreDToOffset(temp, sp, mem);
5735 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005736 vixl32::DRegister first = DRegisterFrom(source);
5737 vixl32::DRegister second = DRegisterFrom(destination);
5738 vixl32::DRegister temp = temps.AcquireD();
5739 __ Vmov(temp, first);
5740 __ Vmov(first, second);
5741 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005742 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005743 vixl32::DRegister reg = source.IsFpuRegisterPair()
5744 ? DRegisterFrom(source)
5745 : DRegisterFrom(destination);
5746 int mem = source.IsFpuRegisterPair()
5747 ? destination.GetStackIndex()
5748 : source.GetStackIndex();
5749 vixl32::DRegister temp = temps.AcquireD();
5750 __ Vmov(temp, reg);
5751 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5752 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005753 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005754 vixl32::SRegister reg = source.IsFpuRegister()
5755 ? SRegisterFrom(source)
5756 : SRegisterFrom(destination);
5757 int mem = source.IsFpuRegister()
5758 ? destination.GetStackIndex()
5759 : source.GetStackIndex();
5760 vixl32::Register temp = temps.Acquire();
5761 __ Vmov(temp, reg);
5762 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5763 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005764 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5765 vixl32::DRegister temp1 = temps.AcquireD();
5766 vixl32::DRegister temp2 = temps.AcquireD();
5767 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
5768 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
5769 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
5770 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
5771 } else {
5772 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
5773 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005774}
5775
5776void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
5777 TODO_VIXL32(FATAL);
5778}
5779
5780void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
5781 TODO_VIXL32(FATAL);
5782}
5783
Artem Serov02d37832016-10-25 15:25:33 +01005784HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005785 HLoadClass::LoadKind desired_class_load_kind) {
5786 switch (desired_class_load_kind) {
5787 case HLoadClass::LoadKind::kReferrersClass:
5788 break;
5789 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005790 DCHECK(!GetCompilerOptions().GetCompilePic());
5791 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005792 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5793 DCHECK(GetCompilerOptions().GetCompilePic());
5794 break;
5795 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005796 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005797 case HLoadClass::LoadKind::kJitTableAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005798 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005799 case HLoadClass::LoadKind::kDexCachePcRelative:
5800 DCHECK(!Runtime::Current()->UseJitCompilation());
5801 // We disable pc-relative load when there is an irreducible loop, as the optimization
5802 // is incompatible with it.
5803 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5804 // with irreducible loops.
5805 if (GetGraph()->HasIrreducibleLoops()) {
5806 return HLoadClass::LoadKind::kDexCacheViaMethod;
5807 }
5808 break;
5809 case HLoadClass::LoadKind::kDexCacheViaMethod:
5810 break;
5811 }
5812 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005813}
5814
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005815void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
5816 if (cls->NeedsAccessCheck()) {
5817 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5818 CodeGenerator::CreateLoadClassLocationSummary(
5819 cls,
5820 LocationFrom(calling_convention.GetRegisterAt(0)),
5821 LocationFrom(r0),
5822 /* code_generator_supports_read_barrier */ true);
5823 return;
5824 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005825
Artem Serovd4cc5b22016-11-04 11:19:09 +00005826 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5827 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005828 ? LocationSummary::kCallOnSlowPath
5829 : LocationSummary::kNoCall;
5830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005831 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005832 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00005833 }
5834
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005835 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5836 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5837 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5838 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5839 locations->SetInAt(0, Location::RequiresRegister());
5840 }
5841 locations->SetOut(Location::RequiresRegister());
5842}
5843
5844void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
5845 LocationSummary* locations = cls->GetLocations();
5846 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005847 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005848 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
5849 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
5850 return;
5851 }
5852
5853 Location out_loc = locations->Out();
5854 vixl32::Register out = OutputRegister(cls);
5855
Artem Serovd4cc5b22016-11-04 11:19:09 +00005856 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5857 ? kWithoutReadBarrier
5858 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005859 bool generate_null_check = false;
5860 switch (cls->GetLoadKind()) {
5861 case HLoadClass::LoadKind::kReferrersClass: {
5862 DCHECK(!cls->CanCallRuntime());
5863 DCHECK(!cls->MustGenerateClinitCheck());
5864 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5865 vixl32::Register current_method = InputRegisterAt(cls, 0);
5866 GenerateGcRootFieldLoad(cls,
5867 out_loc,
5868 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01005869 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00005870 read_barrier_option);
5871 break;
5872 }
5873 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005874 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5875 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5876 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005877 break;
5878 }
5879 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5880 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5881 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
5882 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5883 codegen_->EmitMovwMovtPlaceholder(labels, out);
5884 break;
5885 }
5886 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005887 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5888 DCHECK_NE(cls->GetAddress(), 0u);
5889 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5890 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005891 break;
5892 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005893 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005894 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5895 cls->GetTypeIndex(),
5896 cls->GetAddress()));
5897 // /* GcRoot<mirror::Class> */ out = *out
5898 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005899 break;
5900 }
5901 case HLoadClass::LoadKind::kDexCachePcRelative: {
5902 vixl32::Register base_reg = InputRegisterAt(cls, 0);
5903 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5904 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5905 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5906 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, read_barrier_option);
5907 generate_null_check = !cls->IsInDexCache();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005908 break;
5909 }
5910 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5911 // /* GcRoot<mirror::Class>[] */ out =
5912 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5913 vixl32::Register current_method = InputRegisterAt(cls, 0);
5914 const int32_t resolved_types_offset =
5915 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value();
5916 GetAssembler()->LoadFromOffset(kLoadWord, out, current_method, resolved_types_offset);
5917 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005918 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005919 GenerateGcRootFieldLoad(cls, out_loc, out, offset, read_barrier_option);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005920 generate_null_check = !cls->IsInDexCache();
5921 break;
5922 }
5923 default:
5924 TODO_VIXL32(FATAL);
5925 }
5926
5927 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5928 DCHECK(cls->CanCallRuntime());
5929 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
5930 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5931 codegen_->AddSlowPath(slow_path);
5932 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005933 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005934 }
5935 if (cls->MustGenerateClinitCheck()) {
5936 GenerateClassInitializationCheck(slow_path, out);
5937 } else {
5938 __ Bind(slow_path->GetExitLabel());
5939 }
5940 }
5941}
5942
Artem Serov02d37832016-10-25 15:25:33 +01005943void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5944 LocationSummary* locations =
5945 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5946 locations->SetInAt(0, Location::RequiresRegister());
5947 if (check->HasUses()) {
5948 locations->SetOut(Location::SameAsFirstInput());
5949 }
5950}
5951
5952void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5953 // We assume the class is not null.
5954 LoadClassSlowPathARMVIXL* slow_path =
5955 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
5956 check,
5957 check->GetDexPc(),
5958 /* do_clinit */ true);
5959 codegen_->AddSlowPath(slow_path);
5960 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
5961}
5962
5963void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
5964 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
5965 UseScratchRegisterScope temps(GetVIXLAssembler());
5966 vixl32::Register temp = temps.Acquire();
5967 GetAssembler()->LoadFromOffset(kLoadWord,
5968 temp,
5969 class_reg,
5970 mirror::Class::StatusOffset().Int32Value());
5971 __ Cmp(temp, mirror::Class::kStatusInitialized);
5972 __ B(lt, slow_path->GetEntryLabel());
5973 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5974 // properly. Therefore, we do a memory fence.
5975 __ Dmb(ISH);
5976 __ Bind(slow_path->GetExitLabel());
5977}
5978
Artem Serov02d37832016-10-25 15:25:33 +01005979HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005980 HLoadString::LoadKind desired_string_load_kind) {
5981 switch (desired_string_load_kind) {
5982 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005983 DCHECK(!GetCompilerOptions().GetCompilePic());
5984 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005985 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5986 DCHECK(GetCompilerOptions().GetCompilePic());
5987 break;
5988 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005989 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005990 case HLoadString::LoadKind::kBssEntry:
5991 DCHECK(!Runtime::Current()->UseJitCompilation());
5992 break;
5993 case HLoadString::LoadKind::kJitTableAddress:
5994 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005995 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005996 case HLoadString::LoadKind::kDexCacheViaMethod:
5997 break;
5998 }
5999 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006000}
6001
6002void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006003 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006005 HLoadString::LoadKind load_kind = load->GetLoadKind();
6006 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006007 locations->SetOut(LocationFrom(r0));
6008 } else {
6009 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006010 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6011 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6012 // Rely on the pResolveString and/or marking to save everything, including temps.
6013 // Note that IP may theoretically be clobbered by saving/restoring the live register
6014 // (only one thanks to the custom calling convention), so we request a different temp.
6015 locations->AddTemp(Location::RequiresRegister());
6016 RegisterSet caller_saves = RegisterSet::Empty();
6017 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6018 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6019 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6020 // that the the kPrimNot result register is the same as the first argument register.
6021 locations->SetCustomSlowPathCallerSaves(caller_saves);
6022 } else {
6023 // For non-Baker read barrier we have a temp-clobbering call.
6024 }
6025 }
Artem Serov02d37832016-10-25 15:25:33 +01006026 }
6027}
6028
6029void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006030 LocationSummary* locations = load->GetLocations();
6031 Location out_loc = locations->Out();
6032 vixl32::Register out = OutputRegister(load);
6033 HLoadString::LoadKind load_kind = load->GetLoadKind();
6034
6035 switch (load_kind) {
6036 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006037 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6038 load->GetStringIndex()));
6039 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006040 }
6041 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6042 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6043 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6044 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
6045 codegen_->EmitMovwMovtPlaceholder(labels, out);
6046 return; // No dex cache slow path.
6047 }
6048 case HLoadString::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006049 DCHECK_NE(load->GetAddress(), 0u);
6050 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6051 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6052 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006053 }
6054 case HLoadString::LoadKind::kBssEntry: {
6055 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6056 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
6057 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6058 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
6059 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6060 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6061 LoadStringSlowPathARMVIXL* slow_path =
6062 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6063 codegen_->AddSlowPath(slow_path);
6064 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6065 __ Bind(slow_path->GetExitLabel());
6066 return;
6067 }
6068 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006069 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6070 load->GetStringIndex()));
6071 // /* GcRoot<mirror::String> */ out = *out
6072 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6073 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006074 }
6075 default:
6076 break;
6077 }
Artem Serov02d37832016-10-25 15:25:33 +01006078
6079 // TODO: Re-add the compiler code to do string dex cache lookup again.
6080 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6081 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006082 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006083 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6084 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6085}
6086
6087static int32_t GetExceptionTlsOffset() {
6088 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6089}
6090
6091void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6092 LocationSummary* locations =
6093 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6094 locations->SetOut(Location::RequiresRegister());
6095}
6096
6097void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6098 vixl32::Register out = OutputRegister(load);
6099 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6100}
6101
6102
6103void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6104 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6105}
6106
6107void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6108 UseScratchRegisterScope temps(GetVIXLAssembler());
6109 vixl32::Register temp = temps.Acquire();
6110 __ Mov(temp, 0);
6111 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6112}
6113
6114void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6115 LocationSummary* locations =
6116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6117 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6118 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6119}
6120
6121void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6122 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6123 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6124}
6125
Artem Serov657022c2016-11-23 14:19:38 +00006126// Temp is used for read barrier.
6127static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6128 if (kEmitCompilerReadBarrier &&
6129 (kUseBakerReadBarrier ||
6130 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6131 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6132 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6133 return 1;
6134 }
6135 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006136}
6137
Artem Serov657022c2016-11-23 14:19:38 +00006138// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6139// interface pointer, one for loading the current interface.
6140// The other checks have one temp for loading the object's class.
6141static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6142 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6143 return 3;
6144 }
6145 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6146}
Artem Serovcfbe9132016-10-14 15:58:56 +01006147
6148void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6149 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6150 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6151 bool baker_read_barrier_slow_path = false;
6152 switch (type_check_kind) {
6153 case TypeCheckKind::kExactCheck:
6154 case TypeCheckKind::kAbstractClassCheck:
6155 case TypeCheckKind::kClassHierarchyCheck:
6156 case TypeCheckKind::kArrayObjectCheck:
6157 call_kind =
6158 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6159 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6160 break;
6161 case TypeCheckKind::kArrayCheck:
6162 case TypeCheckKind::kUnresolvedCheck:
6163 case TypeCheckKind::kInterfaceCheck:
6164 call_kind = LocationSummary::kCallOnSlowPath;
6165 break;
6166 }
6167
6168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6169 if (baker_read_barrier_slow_path) {
6170 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6171 }
6172 locations->SetInAt(0, Location::RequiresRegister());
6173 locations->SetInAt(1, Location::RequiresRegister());
6174 // The "out" register is used as a temporary, so it overlaps with the inputs.
6175 // Note that TypeCheckSlowPathARM uses this register too.
6176 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006177 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006178}
6179
6180void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6181 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6182 LocationSummary* locations = instruction->GetLocations();
6183 Location obj_loc = locations->InAt(0);
6184 vixl32::Register obj = InputRegisterAt(instruction, 0);
6185 vixl32::Register cls = InputRegisterAt(instruction, 1);
6186 Location out_loc = locations->Out();
6187 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006188 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6189 DCHECK_LE(num_temps, 1u);
6190 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006191 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6192 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6193 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6194 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6195 vixl32::Label done, zero;
6196 SlowPathCodeARMVIXL* slow_path = nullptr;
6197
6198 // Return 0 if `obj` is null.
6199 // avoid null check if we know obj is not null.
6200 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006201 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006202 }
6203
Artem Serovcfbe9132016-10-14 15:58:56 +01006204 switch (type_check_kind) {
6205 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006206 // /* HeapReference<Class> */ out = obj->klass_
6207 GenerateReferenceLoadTwoRegisters(instruction,
6208 out_loc,
6209 obj_loc,
6210 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006211 maybe_temp_loc,
6212 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006213 __ Cmp(out, cls);
6214 // Classes must be equal for the instanceof to succeed.
6215 __ B(ne, &zero);
6216 __ Mov(out, 1);
6217 __ B(&done);
6218 break;
6219 }
6220
6221 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006222 // /* HeapReference<Class> */ out = obj->klass_
6223 GenerateReferenceLoadTwoRegisters(instruction,
6224 out_loc,
6225 obj_loc,
6226 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006227 maybe_temp_loc,
6228 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006229 // If the class is abstract, we eagerly fetch the super class of the
6230 // object to avoid doing a comparison we know will fail.
6231 vixl32::Label loop;
6232 __ Bind(&loop);
6233 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006234 GenerateReferenceLoadOneRegister(instruction,
6235 out_loc,
6236 super_offset,
6237 maybe_temp_loc,
6238 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006239 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006240 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006241 __ Cmp(out, cls);
6242 __ B(ne, &loop);
6243 __ Mov(out, 1);
6244 if (zero.IsReferenced()) {
6245 __ B(&done);
6246 }
6247 break;
6248 }
6249
6250 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006251 // /* HeapReference<Class> */ out = obj->klass_
6252 GenerateReferenceLoadTwoRegisters(instruction,
6253 out_loc,
6254 obj_loc,
6255 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006256 maybe_temp_loc,
6257 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006258 // Walk over the class hierarchy to find a match.
6259 vixl32::Label loop, success;
6260 __ Bind(&loop);
6261 __ Cmp(out, cls);
6262 __ B(eq, &success);
6263 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006264 GenerateReferenceLoadOneRegister(instruction,
6265 out_loc,
6266 super_offset,
6267 maybe_temp_loc,
6268 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006269 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006270 // If `out` is null, we use it for the result, and jump to `done`.
6271 __ B(&done);
6272 __ Bind(&success);
6273 __ Mov(out, 1);
6274 if (zero.IsReferenced()) {
6275 __ B(&done);
6276 }
6277 break;
6278 }
6279
6280 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006281 // /* HeapReference<Class> */ out = obj->klass_
6282 GenerateReferenceLoadTwoRegisters(instruction,
6283 out_loc,
6284 obj_loc,
6285 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006286 maybe_temp_loc,
6287 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006288 // Do an exact check.
6289 vixl32::Label exact_check;
6290 __ Cmp(out, cls);
6291 __ B(eq, &exact_check);
6292 // Otherwise, we need to check that the object's class is a non-primitive array.
6293 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006294 GenerateReferenceLoadOneRegister(instruction,
6295 out_loc,
6296 component_offset,
6297 maybe_temp_loc,
6298 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006299 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006300 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006301 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6302 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006303 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006304 __ Bind(&exact_check);
6305 __ Mov(out, 1);
6306 __ B(&done);
6307 break;
6308 }
6309
6310 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006311 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006312 // /* HeapReference<Class> */ out = obj->klass_
6313 GenerateReferenceLoadTwoRegisters(instruction,
6314 out_loc,
6315 obj_loc,
6316 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006317 maybe_temp_loc,
6318 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006319 __ Cmp(out, cls);
6320 DCHECK(locations->OnlyCallsOnSlowPath());
6321 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6322 /* is_fatal */ false);
6323 codegen_->AddSlowPath(slow_path);
6324 __ B(ne, slow_path->GetEntryLabel());
6325 __ Mov(out, 1);
6326 if (zero.IsReferenced()) {
6327 __ B(&done);
6328 }
6329 break;
6330 }
6331
6332 case TypeCheckKind::kUnresolvedCheck:
6333 case TypeCheckKind::kInterfaceCheck: {
6334 // Note that we indeed only call on slow path, but we always go
6335 // into the slow path for the unresolved and interface check
6336 // cases.
6337 //
6338 // We cannot directly call the InstanceofNonTrivial runtime
6339 // entry point without resorting to a type checking slow path
6340 // here (i.e. by calling InvokeRuntime directly), as it would
6341 // require to assign fixed registers for the inputs of this
6342 // HInstanceOf instruction (following the runtime calling
6343 // convention), which might be cluttered by the potential first
6344 // read barrier emission at the beginning of this method.
6345 //
6346 // TODO: Introduce a new runtime entry point taking the object
6347 // to test (instead of its class) as argument, and let it deal
6348 // with the read barrier issues. This will let us refactor this
6349 // case of the `switch` code as it was previously (with a direct
6350 // call to the runtime not using a type checking slow path).
6351 // This should also be beneficial for the other cases above.
6352 DCHECK(locations->OnlyCallsOnSlowPath());
6353 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6354 /* is_fatal */ false);
6355 codegen_->AddSlowPath(slow_path);
6356 __ B(slow_path->GetEntryLabel());
6357 if (zero.IsReferenced()) {
6358 __ B(&done);
6359 }
6360 break;
6361 }
6362 }
6363
6364 if (zero.IsReferenced()) {
6365 __ Bind(&zero);
6366 __ Mov(out, 0);
6367 }
6368
6369 if (done.IsReferenced()) {
6370 __ Bind(&done);
6371 }
6372
6373 if (slow_path != nullptr) {
6374 __ Bind(slow_path->GetExitLabel());
6375 }
6376}
6377
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006378void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6379 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6380 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6381
6382 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6383 switch (type_check_kind) {
6384 case TypeCheckKind::kExactCheck:
6385 case TypeCheckKind::kAbstractClassCheck:
6386 case TypeCheckKind::kClassHierarchyCheck:
6387 case TypeCheckKind::kArrayObjectCheck:
6388 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6389 LocationSummary::kCallOnSlowPath :
6390 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6391 break;
6392 case TypeCheckKind::kArrayCheck:
6393 case TypeCheckKind::kUnresolvedCheck:
6394 case TypeCheckKind::kInterfaceCheck:
6395 call_kind = LocationSummary::kCallOnSlowPath;
6396 break;
6397 }
6398
6399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6400 locations->SetInAt(0, Location::RequiresRegister());
6401 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006402 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006403}
6404
6405void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6406 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6407 LocationSummary* locations = instruction->GetLocations();
6408 Location obj_loc = locations->InAt(0);
6409 vixl32::Register obj = InputRegisterAt(instruction, 0);
6410 vixl32::Register cls = InputRegisterAt(instruction, 1);
6411 Location temp_loc = locations->GetTemp(0);
6412 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006413 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6414 DCHECK_LE(num_temps, 3u);
6415 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6416 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6417 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6418 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6419 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6420 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6421 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6422 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6423 const uint32_t object_array_data_offset =
6424 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006425
Artem Serov657022c2016-11-23 14:19:38 +00006426 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6427 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6428 // read barriers is done for performance and code size reasons.
6429 bool is_type_check_slow_path_fatal = false;
6430 if (!kEmitCompilerReadBarrier) {
6431 is_type_check_slow_path_fatal =
6432 (type_check_kind == TypeCheckKind::kExactCheck ||
6433 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6434 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6435 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6436 !instruction->CanThrowIntoCatchBlock();
6437 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006438 SlowPathCodeARMVIXL* type_check_slow_path =
6439 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6440 is_type_check_slow_path_fatal);
6441 codegen_->AddSlowPath(type_check_slow_path);
6442
6443 vixl32::Label done;
6444 // Avoid null check if we know obj is not null.
6445 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006446 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006447 }
6448
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006449 switch (type_check_kind) {
6450 case TypeCheckKind::kExactCheck:
6451 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006452 // /* HeapReference<Class> */ temp = obj->klass_
6453 GenerateReferenceLoadTwoRegisters(instruction,
6454 temp_loc,
6455 obj_loc,
6456 class_offset,
6457 maybe_temp2_loc,
6458 kWithoutReadBarrier);
6459
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006460 __ Cmp(temp, cls);
6461 // Jump to slow path for throwing the exception or doing a
6462 // more involved array check.
6463 __ B(ne, type_check_slow_path->GetEntryLabel());
6464 break;
6465 }
6466
6467 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006468 // /* HeapReference<Class> */ temp = obj->klass_
6469 GenerateReferenceLoadTwoRegisters(instruction,
6470 temp_loc,
6471 obj_loc,
6472 class_offset,
6473 maybe_temp2_loc,
6474 kWithoutReadBarrier);
6475
Artem Serovcfbe9132016-10-14 15:58:56 +01006476 // If the class is abstract, we eagerly fetch the super class of the
6477 // object to avoid doing a comparison we know will fail.
6478 vixl32::Label loop;
6479 __ Bind(&loop);
6480 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006481 GenerateReferenceLoadOneRegister(instruction,
6482 temp_loc,
6483 super_offset,
6484 maybe_temp2_loc,
6485 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006486
6487 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6488 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006489 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006490
6491 // Otherwise, compare the classes.
6492 __ Cmp(temp, cls);
6493 __ B(ne, &loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006494 break;
6495 }
6496
6497 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006498 // /* HeapReference<Class> */ temp = obj->klass_
6499 GenerateReferenceLoadTwoRegisters(instruction,
6500 temp_loc,
6501 obj_loc,
6502 class_offset,
6503 maybe_temp2_loc,
6504 kWithoutReadBarrier);
6505
Artem Serovcfbe9132016-10-14 15:58:56 +01006506 // Walk over the class hierarchy to find a match.
6507 vixl32::Label loop;
6508 __ Bind(&loop);
6509 __ Cmp(temp, cls);
6510 __ B(eq, &done);
6511
6512 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006513 GenerateReferenceLoadOneRegister(instruction,
6514 temp_loc,
6515 super_offset,
6516 maybe_temp2_loc,
6517 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006518
6519 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6520 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006521 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006522 // Otherwise, jump to the beginning of the loop.
6523 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006524 break;
6525 }
6526
Artem Serovcfbe9132016-10-14 15:58:56 +01006527 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006528 // /* HeapReference<Class> */ temp = obj->klass_
6529 GenerateReferenceLoadTwoRegisters(instruction,
6530 temp_loc,
6531 obj_loc,
6532 class_offset,
6533 maybe_temp2_loc,
6534 kWithoutReadBarrier);
6535
Artem Serovcfbe9132016-10-14 15:58:56 +01006536 // Do an exact check.
6537 __ Cmp(temp, cls);
6538 __ B(eq, &done);
6539
6540 // Otherwise, we need to check that the object's class is a non-primitive array.
6541 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006542 GenerateReferenceLoadOneRegister(instruction,
6543 temp_loc,
6544 component_offset,
6545 maybe_temp2_loc,
6546 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006547 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006548 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006549 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6550 // to further check that this component type is not a primitive type.
6551 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6552 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006553 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006554 break;
6555 }
6556
6557 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006558 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006559 // We cannot directly call the CheckCast runtime entry point
6560 // without resorting to a type checking slow path here (i.e. by
6561 // calling InvokeRuntime directly), as it would require to
6562 // assign fixed registers for the inputs of this HInstanceOf
6563 // instruction (following the runtime calling convention), which
6564 // might be cluttered by the potential first read barrier
6565 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006566
Artem Serovcfbe9132016-10-14 15:58:56 +01006567 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006568 break;
Artem Serov657022c2016-11-23 14:19:38 +00006569
6570 case TypeCheckKind::kInterfaceCheck: {
6571 // Avoid read barriers to improve performance of the fast path. We can not get false
6572 // positives by doing this.
6573 // /* HeapReference<Class> */ temp = obj->klass_
6574 GenerateReferenceLoadTwoRegisters(instruction,
6575 temp_loc,
6576 obj_loc,
6577 class_offset,
6578 maybe_temp2_loc,
6579 kWithoutReadBarrier);
6580
6581 // /* HeapReference<Class> */ temp = temp->iftable_
6582 GenerateReferenceLoadTwoRegisters(instruction,
6583 temp_loc,
6584 temp_loc,
6585 iftable_offset,
6586 maybe_temp2_loc,
6587 kWithoutReadBarrier);
6588 // Iftable is never null.
6589 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6590 // Loop through the iftable and check if any class matches.
6591 vixl32::Label start_loop;
6592 __ Bind(&start_loop);
6593 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6594 type_check_slow_path->GetEntryLabel());
6595 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6596 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6597 // Go to next interface.
6598 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6599 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6600 // Compare the classes and continue the loop if they do not match.
6601 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
6602 __ B(ne, &start_loop);
6603 break;
6604 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006605 }
6606 __ Bind(&done);
6607
6608 __ Bind(type_check_slow_path->GetExitLabel());
6609}
6610
Artem Serov551b28f2016-10-18 19:11:30 +01006611void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6612 LocationSummary* locations =
6613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6614 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6615 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6616}
6617
6618void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6619 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6620 instruction,
6621 instruction->GetDexPc());
6622 if (instruction->IsEnter()) {
6623 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6624 } else {
6625 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6626 }
6627}
6628
Artem Serov02109dd2016-09-23 17:17:54 +01006629void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6630 HandleBitwiseOperation(instruction, AND);
6631}
6632
6633void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6634 HandleBitwiseOperation(instruction, ORR);
6635}
6636
6637void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6638 HandleBitwiseOperation(instruction, EOR);
6639}
6640
6641void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6642 LocationSummary* locations =
6643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6644 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6645 || instruction->GetResultType() == Primitive::kPrimLong);
6646 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6647 locations->SetInAt(0, Location::RequiresRegister());
6648 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6650}
6651
6652void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6653 HandleBitwiseOperation(instruction);
6654}
6655
6656void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6657 HandleBitwiseOperation(instruction);
6658}
6659
6660void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6661 HandleBitwiseOperation(instruction);
6662}
6663
Artem Serov2bbc9532016-10-21 11:51:50 +01006664void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6665 LocationSummary* locations =
6666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6667 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6668 || instruction->GetResultType() == Primitive::kPrimLong);
6669
6670 locations->SetInAt(0, Location::RequiresRegister());
6671 locations->SetInAt(1, Location::RequiresRegister());
6672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6673}
6674
6675void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6676 LocationSummary* locations = instruction->GetLocations();
6677 Location first = locations->InAt(0);
6678 Location second = locations->InAt(1);
6679 Location out = locations->Out();
6680
6681 if (instruction->GetResultType() == Primitive::kPrimInt) {
6682 vixl32::Register first_reg = RegisterFrom(first);
6683 vixl32::Register second_reg = RegisterFrom(second);
6684 vixl32::Register out_reg = RegisterFrom(out);
6685
6686 switch (instruction->GetOpKind()) {
6687 case HInstruction::kAnd:
6688 __ Bic(out_reg, first_reg, second_reg);
6689 break;
6690 case HInstruction::kOr:
6691 __ Orn(out_reg, first_reg, second_reg);
6692 break;
6693 // There is no EON on arm.
6694 case HInstruction::kXor:
6695 default:
6696 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6697 UNREACHABLE();
6698 }
6699 return;
6700
6701 } else {
6702 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6703 vixl32::Register first_low = LowRegisterFrom(first);
6704 vixl32::Register first_high = HighRegisterFrom(first);
6705 vixl32::Register second_low = LowRegisterFrom(second);
6706 vixl32::Register second_high = HighRegisterFrom(second);
6707 vixl32::Register out_low = LowRegisterFrom(out);
6708 vixl32::Register out_high = HighRegisterFrom(out);
6709
6710 switch (instruction->GetOpKind()) {
6711 case HInstruction::kAnd:
6712 __ Bic(out_low, first_low, second_low);
6713 __ Bic(out_high, first_high, second_high);
6714 break;
6715 case HInstruction::kOr:
6716 __ Orn(out_low, first_low, second_low);
6717 __ Orn(out_high, first_high, second_high);
6718 break;
6719 // There is no EON on arm.
6720 case HInstruction::kXor:
6721 default:
6722 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6723 UNREACHABLE();
6724 }
6725 }
6726}
6727
Artem Serov02109dd2016-09-23 17:17:54 +01006728// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6729void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
6730 vixl32::Register first,
6731 uint32_t value) {
6732 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6733 if (value == 0xffffffffu) {
6734 if (!out.Is(first)) {
6735 __ Mov(out, first);
6736 }
6737 return;
6738 }
6739 if (value == 0u) {
6740 __ Mov(out, 0);
6741 return;
6742 }
6743 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
6744 __ And(out, first, value);
6745 } else {
6746 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
6747 __ Bic(out, first, ~value);
6748 }
6749}
6750
6751// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6752void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
6753 vixl32::Register first,
6754 uint32_t value) {
6755 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6756 if (value == 0u) {
6757 if (!out.Is(first)) {
6758 __ Mov(out, first);
6759 }
6760 return;
6761 }
6762 if (value == 0xffffffffu) {
6763 __ Mvn(out, 0);
6764 return;
6765 }
6766 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
6767 __ Orr(out, first, value);
6768 } else {
6769 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
6770 __ Orn(out, first, ~value);
6771 }
6772}
6773
6774// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6775void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
6776 vixl32::Register first,
6777 uint32_t value) {
6778 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6779 if (value == 0u) {
6780 if (!out.Is(first)) {
6781 __ Mov(out, first);
6782 }
6783 return;
6784 }
6785 __ Eor(out, first, value);
6786}
6787
Anton Kirilovdda43962016-11-21 19:55:20 +00006788void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
6789 Location first,
6790 uint64_t value) {
6791 vixl32::Register out_low = LowRegisterFrom(out);
6792 vixl32::Register out_high = HighRegisterFrom(out);
6793 vixl32::Register first_low = LowRegisterFrom(first);
6794 vixl32::Register first_high = HighRegisterFrom(first);
6795 uint32_t value_low = Low32Bits(value);
6796 uint32_t value_high = High32Bits(value);
6797 if (value_low == 0u) {
6798 if (!out_low.Is(first_low)) {
6799 __ Mov(out_low, first_low);
6800 }
6801 __ Add(out_high, first_high, value_high);
6802 return;
6803 }
6804 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006805 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006806 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006807 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006808 __ Sbc(out_high, first_high, ~value_high);
6809 } else {
6810 LOG(FATAL) << "Unexpected constant " << value_high;
6811 UNREACHABLE();
6812 }
6813}
6814
Artem Serov02109dd2016-09-23 17:17:54 +01006815void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
6816 LocationSummary* locations = instruction->GetLocations();
6817 Location first = locations->InAt(0);
6818 Location second = locations->InAt(1);
6819 Location out = locations->Out();
6820
6821 if (second.IsConstant()) {
6822 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6823 uint32_t value_low = Low32Bits(value);
6824 if (instruction->GetResultType() == Primitive::kPrimInt) {
6825 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6826 vixl32::Register out_reg = OutputRegister(instruction);
6827 if (instruction->IsAnd()) {
6828 GenerateAndConst(out_reg, first_reg, value_low);
6829 } else if (instruction->IsOr()) {
6830 GenerateOrrConst(out_reg, first_reg, value_low);
6831 } else {
6832 DCHECK(instruction->IsXor());
6833 GenerateEorConst(out_reg, first_reg, value_low);
6834 }
6835 } else {
6836 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6837 uint32_t value_high = High32Bits(value);
6838 vixl32::Register first_low = LowRegisterFrom(first);
6839 vixl32::Register first_high = HighRegisterFrom(first);
6840 vixl32::Register out_low = LowRegisterFrom(out);
6841 vixl32::Register out_high = HighRegisterFrom(out);
6842 if (instruction->IsAnd()) {
6843 GenerateAndConst(out_low, first_low, value_low);
6844 GenerateAndConst(out_high, first_high, value_high);
6845 } else if (instruction->IsOr()) {
6846 GenerateOrrConst(out_low, first_low, value_low);
6847 GenerateOrrConst(out_high, first_high, value_high);
6848 } else {
6849 DCHECK(instruction->IsXor());
6850 GenerateEorConst(out_low, first_low, value_low);
6851 GenerateEorConst(out_high, first_high, value_high);
6852 }
6853 }
6854 return;
6855 }
6856
6857 if (instruction->GetResultType() == Primitive::kPrimInt) {
6858 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6859 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
6860 vixl32::Register out_reg = OutputRegister(instruction);
6861 if (instruction->IsAnd()) {
6862 __ And(out_reg, first_reg, second_reg);
6863 } else if (instruction->IsOr()) {
6864 __ Orr(out_reg, first_reg, second_reg);
6865 } else {
6866 DCHECK(instruction->IsXor());
6867 __ Eor(out_reg, first_reg, second_reg);
6868 }
6869 } else {
6870 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6871 vixl32::Register first_low = LowRegisterFrom(first);
6872 vixl32::Register first_high = HighRegisterFrom(first);
6873 vixl32::Register second_low = LowRegisterFrom(second);
6874 vixl32::Register second_high = HighRegisterFrom(second);
6875 vixl32::Register out_low = LowRegisterFrom(out);
6876 vixl32::Register out_high = HighRegisterFrom(out);
6877 if (instruction->IsAnd()) {
6878 __ And(out_low, first_low, second_low);
6879 __ And(out_high, first_high, second_high);
6880 } else if (instruction->IsOr()) {
6881 __ Orr(out_low, first_low, second_low);
6882 __ Orr(out_high, first_high, second_high);
6883 } else {
6884 DCHECK(instruction->IsXor());
6885 __ Eor(out_low, first_low, second_low);
6886 __ Eor(out_high, first_high, second_high);
6887 }
6888 }
6889}
6890
Artem Serovcfbe9132016-10-14 15:58:56 +01006891void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006892 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01006893 Location out,
6894 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006895 Location maybe_temp,
6896 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01006897 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006898 if (read_barrier_option == kWithReadBarrier) {
6899 CHECK(kEmitCompilerReadBarrier);
6900 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6901 if (kUseBakerReadBarrier) {
6902 // Load with fast path based Baker's read barrier.
6903 // /* HeapReference<Object> */ out = *(out + offset)
6904 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6905 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
6906 } else {
6907 // Load with slow path based read barrier.
6908 // Save the value of `out` into `maybe_temp` before overwriting it
6909 // in the following move operation, as we will need it for the
6910 // read barrier below.
6911 __ Mov(RegisterFrom(maybe_temp), out_reg);
6912 // /* HeapReference<Object> */ out = *(out + offset)
6913 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6914 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6915 }
Artem Serovcfbe9132016-10-14 15:58:56 +01006916 } else {
6917 // Plain load with no read barrier.
6918 // /* HeapReference<Object> */ out = *(out + offset)
6919 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6920 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6921 }
6922}
6923
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006924void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006925 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006926 Location out,
6927 Location obj,
6928 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006929 Location maybe_temp,
6930 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006931 vixl32::Register out_reg = RegisterFrom(out);
6932 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006933 if (read_barrier_option == kWithReadBarrier) {
6934 CHECK(kEmitCompilerReadBarrier);
6935 if (kUseBakerReadBarrier) {
6936 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6937 // Load with fast path based Baker's read barrier.
6938 // /* HeapReference<Object> */ out = *(obj + offset)
6939 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6940 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
6941 } else {
6942 // Load with slow path based read barrier.
6943 // /* HeapReference<Object> */ out = *(obj + offset)
6944 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6945 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6946 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006947 } else {
6948 // Plain load with no read barrier.
6949 // /* HeapReference<Object> */ out = *(obj + offset)
6950 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6951 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6952 }
6953}
6954
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006955void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006956 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006957 Location root,
6958 vixl32::Register obj,
6959 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00006960 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006961 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006962 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006963 DCHECK(kEmitCompilerReadBarrier);
6964 if (kUseBakerReadBarrier) {
6965 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6966 // Baker's read barrier are used:
6967 //
6968 // root = obj.field;
6969 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6970 // if (temp != null) {
6971 // root = temp(root)
6972 // }
6973
6974 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6975 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
6976 static_assert(
6977 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6978 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6979 "have different sizes.");
6980 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6981 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6982 "have different sizes.");
6983
6984 // Slow path marking the GC root `root`.
6985 Location temp = LocationFrom(lr);
6986 SlowPathCodeARMVIXL* slow_path =
6987 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
6988 instruction,
6989 root,
6990 /*entrypoint*/ temp);
6991 codegen_->AddSlowPath(slow_path);
6992
6993 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6994 const int32_t entry_point_offset =
6995 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6996 // Loading the entrypoint does not require a load acquire since it is only changed when
6997 // threads are suspended or running a checkpoint.
6998 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
6999 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7000 // checking GetIsGcMarking.
7001 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7002 __ Bind(slow_path->GetExitLabel());
7003 } else {
7004 // GC root loaded through a slow path for read barriers other
7005 // than Baker's.
7006 // /* GcRoot<mirror::Object>* */ root = obj + offset
7007 __ Add(root_reg, obj, offset);
7008 // /* mirror::Object* */ root = root->Read()
7009 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7010 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007011 } else {
7012 // Plain GC root load with no read barrier.
7013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7014 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7015 // Note that GC roots are not affected by heap poisoning, thus we
7016 // do not have to unpoison `root_reg` here.
7017 }
7018}
7019
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007020void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7021 Location ref,
7022 vixl32::Register obj,
7023 uint32_t offset,
7024 Location temp,
7025 bool needs_null_check) {
7026 DCHECK(kEmitCompilerReadBarrier);
7027 DCHECK(kUseBakerReadBarrier);
7028
7029 // /* HeapReference<Object> */ ref = *(obj + offset)
7030 Location no_index = Location::NoLocation();
7031 ScaleFactor no_scale_factor = TIMES_1;
7032 GenerateReferenceLoadWithBakerReadBarrier(
7033 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007034}
7035
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007036void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7037 Location ref,
7038 vixl32::Register obj,
7039 uint32_t data_offset,
7040 Location index,
7041 Location temp,
7042 bool needs_null_check) {
7043 DCHECK(kEmitCompilerReadBarrier);
7044 DCHECK(kUseBakerReadBarrier);
7045
7046 static_assert(
7047 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7048 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7049 // /* HeapReference<Object> */ ref =
7050 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7051 ScaleFactor scale_factor = TIMES_4;
7052 GenerateReferenceLoadWithBakerReadBarrier(
7053 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007054}
7055
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007056void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7057 Location ref,
7058 vixl32::Register obj,
7059 uint32_t offset,
7060 Location index,
7061 ScaleFactor scale_factor,
7062 Location temp,
7063 bool needs_null_check,
7064 bool always_update_field,
7065 vixl32::Register* temp2) {
7066 DCHECK(kEmitCompilerReadBarrier);
7067 DCHECK(kUseBakerReadBarrier);
7068
7069 // In slow path based read barriers, the read barrier call is
7070 // inserted after the original load. However, in fast path based
7071 // Baker's read barriers, we need to perform the load of
7072 // mirror::Object::monitor_ *before* the original reference load.
7073 // This load-load ordering is required by the read barrier.
7074 // The fast path/slow path (for Baker's algorithm) should look like:
7075 //
7076 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7077 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7078 // HeapReference<Object> ref = *src; // Original reference load.
7079 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7080 // if (is_gray) {
7081 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7082 // }
7083 //
7084 // Note: the original implementation in ReadBarrier::Barrier is
7085 // slightly more complex as it performs additional checks that we do
7086 // not do here for performance reasons.
7087
7088 vixl32::Register ref_reg = RegisterFrom(ref);
7089 vixl32::Register temp_reg = RegisterFrom(temp);
7090 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7091
7092 // /* int32_t */ monitor = obj->monitor_
7093 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7094 if (needs_null_check) {
7095 MaybeRecordImplicitNullCheck(instruction);
7096 }
7097 // /* LockWord */ lock_word = LockWord(monitor)
7098 static_assert(sizeof(LockWord) == sizeof(int32_t),
7099 "art::LockWord and int32_t have different sizes.");
7100
7101 // Introduce a dependency on the lock_word including the rb_state,
7102 // which shall prevent load-load reordering without using
7103 // a memory barrier (which would be more expensive).
7104 // `obj` is unchanged by this operation, but its value now depends
7105 // on `temp_reg`.
7106 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7107
7108 // The actual reference load.
7109 if (index.IsValid()) {
7110 // Load types involving an "index": ArrayGet,
7111 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7112 // intrinsics.
7113 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7114 if (index.IsConstant()) {
7115 size_t computed_offset =
7116 (Int32ConstantFrom(index) << scale_factor) + offset;
7117 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7118 } else {
7119 // Handle the special case of the
7120 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7121 // intrinsics, which use a register pair as index ("long
7122 // offset"), of which only the low part contains data.
7123 vixl32::Register index_reg = index.IsRegisterPair()
7124 ? LowRegisterFrom(index)
7125 : RegisterFrom(index);
7126 UseScratchRegisterScope temps(GetVIXLAssembler());
7127 const vixl32::Register temp3 = temps.Acquire();
7128 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7129 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7130 }
7131 } else {
7132 // /* HeapReference<Object> */ ref = *(obj + offset)
7133 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7134 }
7135
7136 // Object* ref = ref_addr->AsMirrorPtr()
7137 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7138
7139 // Slow path marking the object `ref` when it is gray.
7140 SlowPathCodeARMVIXL* slow_path;
7141 if (always_update_field) {
7142 DCHECK(temp2 != nullptr);
7143 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7144 // of the form `obj + field_offset`, where `obj` is a register and
7145 // `field_offset` is a register pair (of which only the lower half
7146 // is used). Thus `offset` and `scale_factor` above are expected
7147 // to be null in this code path.
7148 DCHECK_EQ(offset, 0u);
7149 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7150 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
7151 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7152 } else {
7153 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
7154 }
7155 AddSlowPath(slow_path);
7156
7157 // if (rb_state == ReadBarrier::GrayState())
7158 // ref = ReadBarrier::Mark(ref);
7159 // Given the numeric representation, it's enough to check the low bit of the
7160 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7161 // which can be a 16-bit instruction unlike the TST immediate.
7162 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7163 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7164 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7165 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
7166 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007167}
7168
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007169void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7170 Location out,
7171 Location ref,
7172 Location obj,
7173 uint32_t offset,
7174 Location index) {
7175 DCHECK(kEmitCompilerReadBarrier);
7176
7177 // Insert a slow path based read barrier *after* the reference load.
7178 //
7179 // If heap poisoning is enabled, the unpoisoning of the loaded
7180 // reference will be carried out by the runtime within the slow
7181 // path.
7182 //
7183 // Note that `ref` currently does not get unpoisoned (when heap
7184 // poisoning is enabled), which is alright as the `ref` argument is
7185 // not used by the artReadBarrierSlow entry point.
7186 //
7187 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7188 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7189 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7190 AddSlowPath(slow_path);
7191
7192 __ B(slow_path->GetEntryLabel());
7193 __ Bind(slow_path->GetExitLabel());
7194}
7195
7196void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007197 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007198 Location ref,
7199 Location obj,
7200 uint32_t offset,
7201 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007202 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007203 // Baker's read barriers shall be handled by the fast path
7204 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007205 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007206 // If heap poisoning is enabled, unpoisoning will be taken care of
7207 // by the runtime within the slow path.
7208 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007209 } else if (kPoisonHeapReferences) {
7210 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7211 }
7212}
7213
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007214void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7215 Location out,
7216 Location root) {
7217 DCHECK(kEmitCompilerReadBarrier);
7218
7219 // Insert a slow path based read barrier *after* the GC root load.
7220 //
7221 // Note that GC roots are not affected by heap poisoning, so we do
7222 // not need to do anything special for this here.
7223 SlowPathCodeARMVIXL* slow_path =
7224 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7225 AddSlowPath(slow_path);
7226
7227 __ B(slow_path->GetEntryLabel());
7228 __ Bind(slow_path->GetExitLabel());
7229}
7230
Artem Serov02d37832016-10-25 15:25:33 +01007231// Check if the desired_dispatch_info is supported. If it is, return it,
7232// otherwise return a fall-back info that should be used instead.
7233HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007234 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
7235 HInvokeStaticOrDirect* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007236 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7237 // We disable pc-relative load when there is an irreducible loop, as the optimization
7238 // is incompatible with it.
7239 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7240 // with irreducible loops.
7241 if (GetGraph()->HasIrreducibleLoops() &&
7242 (dispatch_info.method_load_kind ==
7243 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7244 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7245 }
7246
7247 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
7248 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
7249 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
7250 // Calls across dex files are more likely to exceed the available BL range,
7251 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7252 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7253 (desired_dispatch_info.method_load_kind ==
7254 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7255 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7256 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7257 return HInvokeStaticOrDirect::DispatchInfo {
7258 dispatch_info.method_load_kind,
7259 code_ptr_location,
7260 dispatch_info.method_load_data,
7261 0u
7262 };
7263 }
7264 }
7265 return dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007266}
7267
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007268vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7269 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7270 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7271 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7272 if (!invoke->GetLocations()->Intrinsified()) {
7273 return RegisterFrom(location);
7274 }
7275 // For intrinsics we allow any location, so it may be on the stack.
7276 if (!location.IsRegister()) {
7277 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7278 return temp;
7279 }
7280 // For register locations, check if the register was saved. If so, get it from the stack.
7281 // Note: There is a chance that the register was saved but not overwritten, so we could
7282 // save one load. However, since this is just an intrinsic slow path we prefer this
7283 // simple and more robust approach rather that trying to determine if that's the case.
7284 SlowPathCode* slow_path = GetCurrentSlowPath();
7285 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7286 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
7287 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7288 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7289 return temp;
7290 }
7291 return RegisterFrom(location);
7292}
7293
7294void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
7295 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007296 // For better instruction scheduling we load the direct code pointer before the method pointer.
7297 switch (invoke->GetCodePtrLocation()) {
7298 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7299 // LR = code address from literal pool with link-time patch.
Artem Serovc5fcb442016-12-02 19:19:58 +00007300 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007301 break;
7302 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7303 // LR = invoke->GetDirectCodePtr();
7304 __ Mov(lr, Operand::From(invoke->GetDirectCodePtr()));
7305 break;
7306 default:
7307 break;
7308 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007309
Artem Serovd4cc5b22016-11-04 11:19:09 +00007310 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007311 switch (invoke->GetMethodLoadKind()) {
7312 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7313 uint32_t offset =
7314 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7315 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007316 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7317 break;
7318 }
7319 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7320 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7321 break;
7322 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7323 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7324 break;
7325 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Artem Serovc5fcb442016-12-02 19:19:58 +00007326 __ Ldr(RegisterFrom(temp), DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 break;
7328 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7329 HArmDexCacheArraysBase* base =
7330 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7331 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7332 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7333 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007334 break;
7335 }
7336 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7337 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7338 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007339 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007340 if (current_method.IsRegister()) {
7341 method_reg = RegisterFrom(current_method);
7342 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007343 DCHECK(invoke->GetLocations()->Intrinsified());
7344 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007345 method_reg = reg;
7346 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007347 }
7348 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7349 GetAssembler()->LoadFromOffset(
7350 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007351 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007352 method_reg,
7353 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7354 // temp = temp[index_in_cache];
7355 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7356 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7357 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007358 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007359 break;
7360 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007361 }
7362
Artem Serovd4cc5b22016-11-04 11:19:09 +00007363 switch (invoke->GetCodePtrLocation()) {
7364 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7365 __ Bl(GetFrameEntryLabel());
7366 break;
7367 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
7368 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7369 invoke->GetTargetMethod().dex_method_index);
7370 {
Artem Serov0fb37192016-12-06 18:13:40 +00007371 ExactAssemblyScope aas(GetVIXLAssembler(),
7372 vixl32::kMaxInstructionSizeInBytes,
7373 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007374 __ bind(&relative_call_patches_.back().label);
7375 // Arbitrarily branch to the BL itself, override at link time.
7376 __ bl(&relative_call_patches_.back().label);
7377 }
7378 break;
7379 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7380 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7381 // LR prepared above for better instruction scheduling.
7382 // LR()
Alexandre Rames374ddf32016-11-04 10:40:49 +00007383 {
7384 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00007385 ExactAssemblyScope aas(GetVIXLAssembler(),
7386 vixl32::k16BitT32InstructionSizeInBytes,
7387 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007388 __ blx(lr);
7389 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007390 break;
7391 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7392 // LR = callee_method->entry_point_from_quick_compiled_code_
7393 GetAssembler()->LoadFromOffset(
7394 kLoadWord,
7395 lr,
7396 RegisterFrom(callee_method),
7397 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007398 {
7399 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00007400 ExactAssemblyScope aas(GetVIXLAssembler(),
7401 vixl32::k16BitT32InstructionSizeInBytes,
7402 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007403 // LR()
7404 __ blx(lr);
7405 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007406 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007407 }
7408
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007409 DCHECK(!IsLeafMethod());
7410}
7411
7412void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7413 vixl32::Register temp = RegisterFrom(temp_location);
7414 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7415 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7416
7417 // Use the calling convention instead of the location of the receiver, as
7418 // intrinsics may have put the receiver in a different register. In the intrinsics
7419 // slow path, the arguments have been moved to the right place, so here we are
7420 // guaranteed that the receiver is the first register of the calling convention.
7421 InvokeDexCallingConventionARMVIXL calling_convention;
7422 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007424 {
7425 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007426 ExactAssemblyScope aas(GetVIXLAssembler(),
7427 vixl32::kMaxInstructionSizeInBytes,
7428 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007429 // /* HeapReference<Class> */ temp = receiver->klass_
7430 __ ldr(temp, MemOperand(receiver, class_offset));
7431 MaybeRecordImplicitNullCheck(invoke);
7432 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007433 // Instead of simply (possibly) unpoisoning `temp` here, we should
7434 // emit a read barrier for the previous class reference load.
7435 // However this is not required in practice, as this is an
7436 // intermediate/temporary reference and because the current
7437 // concurrent copying collector keeps the from-space memory
7438 // intact/accessible until the end of the marking phase (the
7439 // concurrent copying collector may not in the future).
7440 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7441
7442 // temp = temp->GetMethodAt(method_offset);
7443 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7444 kArmPointerSize).Int32Value();
7445 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7446 // LR = temp->GetEntryPoint();
7447 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7448 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007449 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7450 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7451 // that.
7452 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00007453 ExactAssemblyScope aas(GetVIXLAssembler(),
7454 vixl32::k16BitT32InstructionSizeInBytes,
7455 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007456 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007457}
7458
Artem Serovd4cc5b22016-11-04 11:19:09 +00007459CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
7460 const DexFile& dex_file, uint32_t string_index) {
7461 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7462}
7463
7464CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7465 const DexFile& dex_file, dex::TypeIndex type_index) {
7466 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7467}
7468
7469CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7470 const DexFile& dex_file, uint32_t element_offset) {
7471 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7472}
7473
7474CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7475 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7476 patches->emplace_back(dex_file, offset_or_index);
7477 return &patches->back();
7478}
7479
Artem Serovc5fcb442016-12-02 19:19:58 +00007480VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7481 const DexFile& dex_file,
7482 dex::StringIndex string_index) {
7483 return boot_image_string_patches_.GetOrCreate(
7484 StringReference(&dex_file, string_index),
7485 [this]() {
7486 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7487 });
7488}
7489
7490VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7491 const DexFile& dex_file,
7492 dex::TypeIndex type_index) {
7493 return boot_image_type_patches_.GetOrCreate(
7494 TypeReference(&dex_file, type_index),
7495 [this]() {
7496 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7497 });
7498}
7499
7500VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
7501 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7502 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7503 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7504}
7505
7506VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7507 return DeduplicateUint32Literal(address, &uint32_literals_);
7508}
7509
7510VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(const DexFile& dex_file,
7511 dex::StringIndex string_index) {
7512 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
7513 return jit_string_patches_.GetOrCreate(
7514 StringReference(&dex_file, string_index),
7515 [this]() {
7516 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7517 });
7518}
7519
7520VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7521 dex::TypeIndex type_index,
7522 uint64_t address) {
7523 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
7524 return jit_class_patches_.GetOrCreate(
7525 TypeReference(&dex_file, type_index),
7526 [this]() {
7527 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7528 });
7529}
7530
Artem Serovd4cc5b22016-11-04 11:19:09 +00007531template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7532inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7533 const ArenaDeque<PcRelativePatchInfo>& infos,
7534 ArenaVector<LinkerPatch>* linker_patches) {
7535 for (const PcRelativePatchInfo& info : infos) {
7536 const DexFile& dex_file = info.target_dex_file;
7537 size_t offset_or_index = info.offset_or_index;
7538 DCHECK(info.add_pc_label.IsBound());
7539 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7540 // Add MOVW patch.
7541 DCHECK(info.movw_label.IsBound());
7542 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7543 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7544 // Add MOVT patch.
7545 DCHECK(info.movt_label.IsBound());
7546 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7547 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7548 }
7549}
7550
7551void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7552 DCHECK(linker_patches->empty());
7553 size_t size =
Artem Serovc5fcb442016-12-02 19:19:58 +00007554 method_patches_.size() +
7555 call_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007556 relative_call_patches_.size() +
7557 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007558 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007559 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007560 boot_image_type_patches_.size() +
7561 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
7562 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007563 linker_patches->reserve(size);
Artem Serovc5fcb442016-12-02 19:19:58 +00007564 for (const auto& entry : method_patches_) {
7565 const MethodReference& target_method = entry.first;
7566 VIXLUInt32Literal* literal = entry.second;
7567 DCHECK(literal->IsBound());
7568 uint32_t literal_offset = literal->GetLocation();
7569 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7570 target_method.dex_file,
7571 target_method.dex_method_index));
7572 }
7573 for (const auto& entry : call_patches_) {
7574 const MethodReference& target_method = entry.first;
7575 VIXLUInt32Literal* literal = entry.second;
7576 DCHECK(literal->IsBound());
7577 uint32_t literal_offset = literal->GetLocation();
7578 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7579 target_method.dex_file,
7580 target_method.dex_method_index));
7581 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007582 for (const PatchInfo<vixl32::Label>& info : relative_call_patches_) {
7583 uint32_t literal_offset = info.label.GetLocation();
7584 linker_patches->push_back(
7585 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
7586 }
7587 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7588 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007589 for (const auto& entry : boot_image_string_patches_) {
7590 const StringReference& target_string = entry.first;
7591 VIXLUInt32Literal* literal = entry.second;
7592 DCHECK(literal->IsBound());
7593 uint32_t literal_offset = literal->GetLocation();
7594 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7595 target_string.dex_file,
7596 target_string.string_index.index_));
7597 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007598 if (!GetCompilerOptions().IsBootImage()) {
7599 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7600 linker_patches);
7601 } else {
7602 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7603 linker_patches);
7604 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007605 for (const auto& entry : boot_image_type_patches_) {
7606 const TypeReference& target_type = entry.first;
7607 VIXLUInt32Literal* literal = entry.second;
7608 DCHECK(literal->IsBound());
7609 uint32_t literal_offset = literal->GetLocation();
7610 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7611 target_type.dex_file,
7612 target_type.type_index.index_));
7613 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007614 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7615 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007616 for (const auto& entry : boot_image_address_patches_) {
7617 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7618 VIXLUInt32Literal* literal = entry.second;
7619 DCHECK(literal->IsBound());
7620 uint32_t literal_offset = literal->GetLocation();
7621 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7622 }
7623}
7624
7625VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7626 uint32_t value,
7627 Uint32ToLiteralMap* map) {
7628 return map->GetOrCreate(
7629 value,
7630 [this, value]() {
7631 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7632 });
7633}
7634
7635VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7636 MethodReference target_method,
7637 MethodToLiteralMap* map) {
7638 return map->GetOrCreate(
7639 target_method,
7640 [this]() {
7641 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7642 });
7643}
7644
7645VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodAddressLiteral(
7646 MethodReference target_method) {
7647 return DeduplicateMethodLiteral(target_method, &method_patches_);
7648}
7649
7650VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodCodeLiteral(
7651 MethodReference target_method) {
7652 return DeduplicateMethodLiteral(target_method, &call_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007653}
7654
Artem Serov2bbc9532016-10-21 11:51:50 +01007655void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7656 LocationSummary* locations =
7657 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7658 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7659 Location::RequiresRegister());
7660 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7661 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7663}
7664
7665void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7666 vixl32::Register res = OutputRegister(instr);
7667 vixl32::Register accumulator =
7668 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7669 vixl32::Register mul_left =
7670 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7671 vixl32::Register mul_right =
7672 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7673
7674 if (instr->GetOpKind() == HInstruction::kAdd) {
7675 __ Mla(res, mul_left, mul_right, accumulator);
7676 } else {
7677 __ Mls(res, mul_left, mul_right, accumulator);
7678 }
7679}
7680
Artem Serov551b28f2016-10-18 19:11:30 +01007681void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7682 // Nothing to do, this should be removed during prepare for register allocator.
7683 LOG(FATAL) << "Unreachable";
7684}
7685
7686void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7687 // Nothing to do, this should be removed during prepare for register allocator.
7688 LOG(FATAL) << "Unreachable";
7689}
7690
7691// Simple implementation of packed switch - generate cascaded compare/jumps.
7692void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7693 LocationSummary* locations =
7694 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7695 locations->SetInAt(0, Location::RequiresRegister());
7696 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7697 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7698 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7699 if (switch_instr->GetStartValue() != 0) {
7700 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7701 }
7702 }
7703}
7704
7705// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7706void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7707 int32_t lower_bound = switch_instr->GetStartValue();
7708 uint32_t num_entries = switch_instr->GetNumEntries();
7709 LocationSummary* locations = switch_instr->GetLocations();
7710 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7711 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7712
7713 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7714 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7715 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007716 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007717 vixl32::Register temp_reg = temps.Acquire();
7718 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7719 // the immediate, because IP is used as the destination register. For the other
7720 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7721 // and they can be encoded in the instruction without making use of IP register.
7722 __ Adds(temp_reg, value_reg, -lower_bound);
7723
7724 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7725 // Jump to successors[0] if value == lower_bound.
7726 __ B(eq, codegen_->GetLabelOf(successors[0]));
7727 int32_t last_index = 0;
7728 for (; num_entries - last_index > 2; last_index += 2) {
7729 __ Adds(temp_reg, temp_reg, -2);
7730 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7731 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7732 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7733 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7734 }
7735 if (num_entries - last_index == 2) {
7736 // The last missing case_value.
7737 __ Cmp(temp_reg, 1);
7738 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7739 }
7740
7741 // And the default for any other value.
7742 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7743 __ B(codegen_->GetLabelOf(default_block));
7744 }
7745 } else {
7746 // Create a table lookup.
7747 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7748
7749 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7750
7751 // Remove the bias.
7752 vixl32::Register key_reg;
7753 if (lower_bound != 0) {
7754 key_reg = RegisterFrom(locations->GetTemp(1));
7755 __ Sub(key_reg, value_reg, lower_bound);
7756 } else {
7757 key_reg = value_reg;
7758 }
7759
7760 // Check whether the value is in the table, jump to default block if not.
7761 __ Cmp(key_reg, num_entries - 1);
7762 __ B(hi, codegen_->GetLabelOf(default_block));
7763
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007764 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007765 vixl32::Register jump_offset = temps.Acquire();
7766
7767 // Load jump offset from the table.
7768 __ Adr(table_base, jump_table->GetTableStartLabel());
7769 __ Ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
7770
7771 // Jump to target block by branching to table_base(pc related) + offset.
7772 vixl32::Register target_address = table_base;
7773 __ Add(target_address, table_base, jump_offset);
7774 __ Bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007775
7776 jump_table->EmitTable(codegen_);
Artem Serov551b28f2016-10-18 19:11:30 +01007777 }
7778}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007779void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7780 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7781 locations->SetOut(Location::RequiresRegister());
7782}
7783
7784void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7785 vixl32::Register base_reg = OutputRegister(base);
7786 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7787 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7788 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
7789}
Artem Serov551b28f2016-10-18 19:11:30 +01007790
Artem Serov02d37832016-10-25 15:25:33 +01007791// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007792void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7793 if (!trg.IsValid()) {
7794 DCHECK_EQ(type, Primitive::kPrimVoid);
7795 return;
7796 }
7797
7798 DCHECK_NE(type, Primitive::kPrimVoid);
7799
Artem Serovd4cc5b22016-11-04 11:19:09 +00007800 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007801 if (return_loc.Equals(trg)) {
7802 return;
7803 }
7804
7805 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7806 // with the last branch.
7807 if (type == Primitive::kPrimLong) {
7808 TODO_VIXL32(FATAL);
7809 } else if (type == Primitive::kPrimDouble) {
7810 TODO_VIXL32(FATAL);
7811 } else {
7812 // Let the parallel move resolver take care of all of this.
7813 HParallelMove parallel_move(GetGraph()->GetArena());
7814 parallel_move.AddMove(return_loc, trg, type, nullptr);
7815 GetMoveResolver()->EmitNativeCode(&parallel_move);
7816 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007817}
Scott Wakelingfe885462016-09-22 10:24:38 +01007818
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007819void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7820 LocationSummary* locations =
7821 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7822 locations->SetInAt(0, Location::RequiresRegister());
7823 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01007824}
7825
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007826void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7827 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7828 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7829 instruction->GetIndex(), kArmPointerSize).SizeValue();
7830 GetAssembler()->LoadFromOffset(kLoadWord,
7831 OutputRegister(instruction),
7832 InputRegisterAt(instruction, 0),
7833 method_offset);
7834 } else {
7835 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7836 instruction->GetIndex(), kArmPointerSize));
7837 GetAssembler()->LoadFromOffset(kLoadWord,
7838 OutputRegister(instruction),
7839 InputRegisterAt(instruction, 0),
7840 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7841 GetAssembler()->LoadFromOffset(kLoadWord,
7842 OutputRegister(instruction),
7843 OutputRegister(instruction),
7844 method_offset);
7845 }
Artem Serov551b28f2016-10-18 19:11:30 +01007846}
7847
Artem Serovc5fcb442016-12-02 19:19:58 +00007848static void PatchJitRootUse(uint8_t* code,
7849 const uint8_t* roots_data,
7850 VIXLUInt32Literal* literal,
7851 uint64_t index_in_table) {
7852 DCHECK(literal->IsBound());
7853 uint32_t literal_offset = literal->GetLocation();
7854 uintptr_t address =
7855 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7856 uint8_t* data = code + literal_offset;
7857 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7858}
7859
7860void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7861 for (const auto& entry : jit_string_patches_) {
7862 const auto& it = jit_string_roots_.find(entry.first);
7863 DCHECK(it != jit_string_roots_.end());
7864 PatchJitRootUse(code, roots_data, entry.second, it->second);
7865 }
7866 for (const auto& entry : jit_class_patches_) {
7867 const auto& it = jit_class_roots_.find(entry.first);
7868 DCHECK(it != jit_class_roots_.end());
7869 PatchJitRootUse(code, roots_data, entry.second, it->second);
7870 }
7871}
7872
Artem Serovd4cc5b22016-11-04 11:19:09 +00007873void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
7874 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
7875 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00007876 ExactAssemblyScope aas(GetVIXLAssembler(),
7877 3 * vixl32::kMaxInstructionSizeInBytes,
7878 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007879 // TODO(VIXL): Think about using mov instead of movw.
7880 __ bind(&labels->movw_label);
7881 __ movw(out, /* placeholder */ 0u);
7882 __ bind(&labels->movt_label);
7883 __ movt(out, /* placeholder */ 0u);
7884 __ bind(&labels->add_pc_label);
7885 __ add(out, out, pc);
7886}
7887
Scott Wakelingfe885462016-09-22 10:24:38 +01007888#undef __
7889#undef QUICK_ENTRY_POINT
7890#undef TODO_VIXL32
7891
7892} // namespace arm
7893} // namespace art