blob: 1df00fff9a79cef442cc7573f2b4fb127184cbd0 [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;
Anton Kirilov644032c2016-12-06 17:51:43 +000049using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000051using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000052using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::LocationFrom;
54using helpers::LowRegisterFrom;
55using helpers::LowSRegisterFrom;
56using helpers::OutputRegister;
57using helpers::OutputSRegister;
58using helpers::OutputVRegister;
59using helpers::RegisterFrom;
60using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000061using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010062
Artem Serov0fb37192016-12-06 18:13:40 +000063using vixl::ExactAssemblyScope;
64using vixl::CodeBufferCheckScope;
65
Scott Wakelingfe885462016-09-22 10:24:38 +010066using RegisterList = vixl32::RegisterList;
67
68static bool ExpectedPairLayout(Location location) {
69 // We expected this for both core and fpu register pairs.
70 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
71}
Artem Serovd4cc5b22016-11-04 11:19:09 +000072// Use a local definition to prevent copying mistakes.
73static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
74static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010075static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010077
78#ifdef __
79#error "ARM Codegen VIXL macro-assembler macro already defined."
80#endif
81
Scott Wakelingfe885462016-09-22 10:24:38 +010082// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
83#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
84#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
85
86// Marker that code is yet to be, and must, be implemented.
87#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
88
Scott Wakelinga7812ae2016-10-17 10:03:36 +010089// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
90// for each live D registers they treat two corresponding S registers as live ones.
91//
92// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
93// from a list of contiguous S registers a list of contiguous D registers (processing first/last
94// S registers corner cases) and save/restore this new list treating them as D registers.
95// - decreasing code size
96// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
97// restored and then used in regular non SlowPath code as D register.
98//
99// For the following example (v means the S register is live):
100// D names: | D0 | D1 | D2 | D4 | ...
101// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
102// Live? | | v | v | v | v | v | v | | ...
103//
104// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
105// as D registers.
106//
107// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
108// for lists of floating-point registers.
109static size_t SaveContiguousSRegisterList(size_t first,
110 size_t last,
111 CodeGenerator* codegen,
112 size_t stack_offset) {
113 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
114 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
115 DCHECK_LE(first, last);
116 if ((first == last) && (first == 0)) {
117 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
118 return stack_offset + kSRegSizeInBytes;
119 }
120 if (first % 2 == 1) {
121 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
122 stack_offset += kSRegSizeInBytes;
123 }
124
125 bool save_last = false;
126 if (last % 2 == 0) {
127 save_last = true;
128 --last;
129 }
130
131 if (first < last) {
132 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
133 DCHECK_EQ((last - first + 1) % 2, 0u);
134 size_t number_of_d_regs = (last - first + 1) / 2;
135
136 if (number_of_d_regs == 1) {
137 __ Vstr(d_reg, MemOperand(sp, stack_offset));
138 } else if (number_of_d_regs > 1) {
139 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
140 vixl32::Register base = sp;
141 if (stack_offset != 0) {
142 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000143 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100144 }
145 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
146 }
147 stack_offset += number_of_d_regs * kDRegSizeInBytes;
148 }
149
150 if (save_last) {
151 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
152 stack_offset += kSRegSizeInBytes;
153 }
154
155 return stack_offset;
156}
157
158static size_t RestoreContiguousSRegisterList(size_t first,
159 size_t last,
160 CodeGenerator* codegen,
161 size_t stack_offset) {
162 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
163 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
164 DCHECK_LE(first, last);
165 if ((first == last) && (first == 0)) {
166 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
167 return stack_offset + kSRegSizeInBytes;
168 }
169 if (first % 2 == 1) {
170 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
171 stack_offset += kSRegSizeInBytes;
172 }
173
174 bool restore_last = false;
175 if (last % 2 == 0) {
176 restore_last = true;
177 --last;
178 }
179
180 if (first < last) {
181 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
182 DCHECK_EQ((last - first + 1) % 2, 0u);
183 size_t number_of_d_regs = (last - first + 1) / 2;
184 if (number_of_d_regs == 1) {
185 __ Vldr(d_reg, MemOperand(sp, stack_offset));
186 } else if (number_of_d_regs > 1) {
187 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
188 vixl32::Register base = sp;
189 if (stack_offset != 0) {
190 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000191 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100192 }
193 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
194 }
195 stack_offset += number_of_d_regs * kDRegSizeInBytes;
196 }
197
198 if (restore_last) {
199 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
200 stack_offset += kSRegSizeInBytes;
201 }
202
203 return stack_offset;
204}
205
206void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
207 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
208 size_t orig_offset = stack_offset;
209
210 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
211 for (uint32_t i : LowToHighBits(core_spills)) {
212 // If the register holds an object, update the stack mask.
213 if (locations->RegisterContainsObject(i)) {
214 locations->SetStackBit(stack_offset / kVRegSize);
215 }
216 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
217 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
218 saved_core_stack_offsets_[i] = stack_offset;
219 stack_offset += kArmWordSize;
220 }
221
222 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
223 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
224
225 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
226 orig_offset = stack_offset;
227 for (uint32_t i : LowToHighBits(fp_spills)) {
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_fpu_stack_offsets_[i] = stack_offset;
230 stack_offset += kArmWordSize;
231 }
232
233 stack_offset = orig_offset;
234 while (fp_spills != 0u) {
235 uint32_t begin = CTZ(fp_spills);
236 uint32_t tmp = fp_spills + (1u << begin);
237 fp_spills &= tmp; // Clear the contiguous range of 1s.
238 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
239 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
240 }
241 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
242}
243
244void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
245 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
246 size_t orig_offset = stack_offset;
247
248 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
249 for (uint32_t i : LowToHighBits(core_spills)) {
250 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
251 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
252 stack_offset += kArmWordSize;
253 }
254
255 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
256 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
257 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
258
259 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
260 while (fp_spills != 0u) {
261 uint32_t begin = CTZ(fp_spills);
262 uint32_t tmp = fp_spills + (1u << begin);
263 fp_spills &= tmp; // Clear the contiguous range of 1s.
264 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
265 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
266 }
267 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
268}
269
270class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
271 public:
272 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
273
274 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
275 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
276 __ Bind(GetEntryLabel());
277 if (instruction_->CanThrowIntoCatchBlock()) {
278 // Live registers will be restored in the catch block if caught.
279 SaveLiveRegisters(codegen, instruction_->GetLocations());
280 }
281 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
282 instruction_,
283 instruction_->GetDexPc(),
284 this);
285 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
286 }
287
288 bool IsFatal() const OVERRIDE { return true; }
289
290 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
291
292 private:
293 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
294};
295
Scott Wakelingfe885462016-09-22 10:24:38 +0100296class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
297 public:
298 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
299 : SlowPathCodeARMVIXL(instruction) {}
300
301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100302 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100303 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100304 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100305 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
306 }
307
308 bool IsFatal() const OVERRIDE { return true; }
309
310 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
311
312 private:
313 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
314};
315
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100316class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
317 public:
318 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
319 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
323 __ Bind(GetEntryLabel());
324 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
325 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
326 if (successor_ == nullptr) {
327 __ B(GetReturnLabel());
328 } else {
329 __ B(arm_codegen->GetLabelOf(successor_));
330 }
331 }
332
333 vixl32::Label* GetReturnLabel() {
334 DCHECK(successor_ == nullptr);
335 return &return_label_;
336 }
337
338 HBasicBlock* GetSuccessor() const {
339 return successor_;
340 }
341
342 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
343
344 private:
345 // If not null, the block to branch to after the suspend check.
346 HBasicBlock* const successor_;
347
348 // If `successor_` is null, the label to branch to after the suspend check.
349 vixl32::Label return_label_;
350
351 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
352};
353
Scott Wakelingc34dba72016-10-03 10:14:44 +0100354class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
355 public:
356 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
357 : SlowPathCodeARMVIXL(instruction) {}
358
359 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
360 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
361 LocationSummary* locations = instruction_->GetLocations();
362
363 __ Bind(GetEntryLabel());
364 if (instruction_->CanThrowIntoCatchBlock()) {
365 // Live registers will be restored in the catch block if caught.
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
367 }
368 // We're moving two locations to locations that could overlap, so we need a parallel
369 // move resolver.
370 InvokeRuntimeCallingConventionARMVIXL calling_convention;
371 codegen->EmitParallelMoves(
372 locations->InAt(0),
373 LocationFrom(calling_convention.GetRegisterAt(0)),
374 Primitive::kPrimInt,
375 locations->InAt(1),
376 LocationFrom(calling_convention.GetRegisterAt(1)),
377 Primitive::kPrimInt);
378 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
379 ? kQuickThrowStringBounds
380 : kQuickThrowArrayBounds;
381 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
382 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
383 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
384 }
385
386 bool IsFatal() const OVERRIDE { return true; }
387
388 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
389
390 private:
391 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
392};
393
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100394class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
395 public:
396 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
397 : SlowPathCodeARMVIXL(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
398 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
399 }
400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 LocationSummary* locations = at_->GetLocations();
403
404 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
405 __ Bind(GetEntryLabel());
406 SaveLiveRegisters(codegen, locations);
407
408 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800409 __ Mov(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100410 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
411 : kQuickInitializeType;
412 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
413 if (do_clinit_) {
414 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
415 } else {
416 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
417 }
418
419 // Move the class to the desired location.
420 Location out = locations->Out();
421 if (out.IsValid()) {
422 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
423 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
424 }
425 RestoreLiveRegisters(codegen, locations);
426 __ B(GetExitLabel());
427 }
428
429 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
430
431 private:
432 // The class this slow path will load.
433 HLoadClass* const cls_;
434
435 // The instruction where this slow path is happening.
436 // (Might be the load class or an initialization check).
437 HInstruction* const at_;
438
439 // The dex PC of `at_`.
440 const uint32_t dex_pc_;
441
442 // Whether to initialize the class.
443 const bool do_clinit_;
444
445 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
446};
447
Artem Serovd4cc5b22016-11-04 11:19:09 +0000448class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
449 public:
450 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
451 : SlowPathCodeARMVIXL(instruction) {}
452
453 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
454 LocationSummary* locations = instruction_->GetLocations();
455 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
456 HLoadString* load = instruction_->AsLoadString();
457 const uint32_t string_index = load->GetStringIndex().index_;
458 vixl32::Register out = OutputRegister(load);
459 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
460 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
461
462 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
463 __ Bind(GetEntryLabel());
464 SaveLiveRegisters(codegen, locations);
465
466 InvokeRuntimeCallingConventionARMVIXL calling_convention;
467 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
468 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
469 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
470 vixl32::Register entry_address = temp_is_r0 ? out : temp;
471 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
472 if (call_saves_everything_except_r0 && temp_is_r0) {
473 __ Mov(entry_address, temp);
474 }
475
476 __ Mov(calling_convention.GetRegisterAt(0), string_index);
477 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
478 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
479
480 // Store the resolved String to the .bss entry.
481 if (call_saves_everything_except_r0) {
482 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
483 __ Str(r0, MemOperand(entry_address));
484 } else {
485 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
486 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
487 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
488 arm_codegen->EmitMovwMovtPlaceholder(labels, out);
489 __ Str(r0, MemOperand(entry_address));
490 }
491
492 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
493 RestoreLiveRegisters(codegen, locations);
494
495 __ B(GetExitLabel());
496 }
497
498 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
499
500 private:
501 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
502};
503
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100504class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
505 public:
506 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
507 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
508
509 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
510 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100511 DCHECK(instruction_->IsCheckCast()
512 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
513
514 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
515 __ Bind(GetEntryLabel());
516
517 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100518 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100519 }
520
521 // We're moving two locations to locations that could overlap, so we need a parallel
522 // move resolver.
523 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100524
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800525 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800526 LocationFrom(calling_convention.GetRegisterAt(0)),
527 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800528 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800529 LocationFrom(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100531 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100532 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
533 instruction_,
534 instruction_->GetDexPc(),
535 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800536 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100537 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100538 } else {
539 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800540 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
541 instruction_,
542 instruction_->GetDexPc(),
543 this);
544 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100545 }
546
547 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100548 RestoreLiveRegisters(codegen, locations);
549 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100550 }
551 }
552
553 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
554
555 bool IsFatal() const OVERRIDE { return is_fatal_; }
556
557 private:
558 const bool is_fatal_;
559
560 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
561};
562
Scott Wakelingc34dba72016-10-03 10:14:44 +0100563class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
564 public:
565 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
566 : SlowPathCodeARMVIXL(instruction) {}
567
568 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
569 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
570 __ Bind(GetEntryLabel());
571 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
572 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
573 }
574
575 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
576
577 private:
578 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
579};
580
581class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
582 public:
583 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
584
585 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
586 LocationSummary* locations = instruction_->GetLocations();
587 __ Bind(GetEntryLabel());
588 SaveLiveRegisters(codegen, locations);
589
590 InvokeRuntimeCallingConventionARMVIXL calling_convention;
591 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
592 parallel_move.AddMove(
593 locations->InAt(0),
594 LocationFrom(calling_convention.GetRegisterAt(0)),
595 Primitive::kPrimNot,
596 nullptr);
597 parallel_move.AddMove(
598 locations->InAt(1),
599 LocationFrom(calling_convention.GetRegisterAt(1)),
600 Primitive::kPrimInt,
601 nullptr);
602 parallel_move.AddMove(
603 locations->InAt(2),
604 LocationFrom(calling_convention.GetRegisterAt(2)),
605 Primitive::kPrimNot,
606 nullptr);
607 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
608
609 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
610 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
611 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
612 RestoreLiveRegisters(codegen, locations);
613 __ B(GetExitLabel());
614 }
615
616 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
617
618 private:
619 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
620};
621
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000622// Slow path marking an object reference `ref` during a read
623// barrier. The field `obj.field` in the object `obj` holding this
624// reference does not get updated by this slow path after marking (see
625// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
626//
627// This means that after the execution of this slow path, `ref` will
628// always be up-to-date, but `obj.field` may not; i.e., after the
629// flip, `ref` will be a to-space reference, but `obj.field` will
630// probably still be a from-space reference (unless it gets updated by
631// another thread, or if another thread installed another object
632// reference (different from `ref`) in `obj.field`).
633class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
634 public:
635 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
636 Location ref,
637 Location entrypoint = Location::NoLocation())
638 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
639 DCHECK(kEmitCompilerReadBarrier);
640 }
641
642 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
643
644 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
645 LocationSummary* locations = instruction_->GetLocations();
646 vixl32::Register ref_reg = RegisterFrom(ref_);
647 DCHECK(locations->CanCall());
648 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
649 DCHECK(instruction_->IsInstanceFieldGet() ||
650 instruction_->IsStaticFieldGet() ||
651 instruction_->IsArrayGet() ||
652 instruction_->IsArraySet() ||
653 instruction_->IsLoadClass() ||
654 instruction_->IsLoadString() ||
655 instruction_->IsInstanceOf() ||
656 instruction_->IsCheckCast() ||
657 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
658 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
659 << "Unexpected instruction in read barrier marking slow path: "
660 << instruction_->DebugName();
661 // The read barrier instrumentation of object ArrayGet
662 // instructions does not support the HIntermediateAddress
663 // instruction.
664 DCHECK(!(instruction_->IsArrayGet() &&
665 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
666
667 __ Bind(GetEntryLabel());
668 // No need to save live registers; it's taken care of by the
669 // entrypoint. Also, there is no need to update the stack mask,
670 // as this runtime call will not trigger a garbage collection.
671 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
672 DCHECK(!ref_reg.Is(sp));
673 DCHECK(!ref_reg.Is(lr));
674 DCHECK(!ref_reg.Is(pc));
675 // IP is used internally by the ReadBarrierMarkRegX entry point
676 // as a temporary, it cannot be the entry point's input/output.
677 DCHECK(!ref_reg.Is(ip));
678 DCHECK(ref_reg.IsRegister()) << ref_reg;
679 // "Compact" slow path, saving two moves.
680 //
681 // Instead of using the standard runtime calling convention (input
682 // and output in R0):
683 //
684 // R0 <- ref
685 // R0 <- ReadBarrierMark(R0)
686 // ref <- R0
687 //
688 // we just use rX (the register containing `ref`) as input and output
689 // of a dedicated entrypoint:
690 //
691 // rX <- ReadBarrierMarkRegX(rX)
692 //
693 if (entrypoint_.IsValid()) {
694 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
695 __ Blx(RegisterFrom(entrypoint_));
696 } else {
697 int32_t entry_point_offset =
698 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
699 // This runtime call does not require a stack map.
700 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
701 }
702 __ B(GetExitLabel());
703 }
704
705 private:
706 // The location (register) of the marked object reference.
707 const Location ref_;
708
709 // The location of the entrypoint if already loaded.
710 const Location entrypoint_;
711
712 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
713};
714
715// Slow path marking an object reference `ref` during a read barrier,
716// and if needed, atomically updating the field `obj.field` in the
717// object `obj` holding this reference after marking (contrary to
718// ReadBarrierMarkSlowPathARM above, which never tries to update
719// `obj.field`).
720//
721// This means that after the execution of this slow path, both `ref`
722// and `obj.field` will be up-to-date; i.e., after the flip, both will
723// hold the same to-space reference (unless another thread installed
724// another object reference (different from `ref`) in `obj.field`).
725class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
726 public:
727 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
728 Location ref,
729 vixl32::Register obj,
730 Location field_offset,
731 vixl32::Register temp1,
732 vixl32::Register temp2)
733 : SlowPathCodeARMVIXL(instruction),
734 ref_(ref),
735 obj_(obj),
736 field_offset_(field_offset),
737 temp1_(temp1),
738 temp2_(temp2) {
739 DCHECK(kEmitCompilerReadBarrier);
740 }
741
742 const char* GetDescription() const OVERRIDE {
743 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
744 }
745
746 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
747 LocationSummary* locations = instruction_->GetLocations();
748 vixl32::Register ref_reg = RegisterFrom(ref_);
749 DCHECK(locations->CanCall());
750 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
751 // This slow path is only used by the UnsafeCASObject intrinsic.
752 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
753 << "Unexpected instruction in read barrier marking and field updating slow path: "
754 << instruction_->DebugName();
755 DCHECK(instruction_->GetLocations()->Intrinsified());
756 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
757 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
758
759 __ Bind(GetEntryLabel());
760
761 // Save the old reference.
762 // Note that we cannot use IP to save the old reference, as IP is
763 // used internally by the ReadBarrierMarkRegX entry point, and we
764 // need the old reference after the call to that entry point.
765 DCHECK(!temp1_.Is(ip));
766 __ Mov(temp1_, ref_reg);
767
768 // No need to save live registers; it's taken care of by the
769 // entrypoint. Also, there is no need to update the stack mask,
770 // as this runtime call will not trigger a garbage collection.
771 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
772 DCHECK(!ref_reg.Is(sp));
773 DCHECK(!ref_reg.Is(lr));
774 DCHECK(!ref_reg.Is(pc));
775 // IP is used internally by the ReadBarrierMarkRegX entry point
776 // as a temporary, it cannot be the entry point's input/output.
777 DCHECK(!ref_reg.Is(ip));
778 DCHECK(ref_reg.IsRegister()) << ref_reg;
779 // "Compact" slow path, saving two moves.
780 //
781 // Instead of using the standard runtime calling convention (input
782 // and output in R0):
783 //
784 // R0 <- ref
785 // R0 <- ReadBarrierMark(R0)
786 // ref <- R0
787 //
788 // we just use rX (the register containing `ref`) as input and output
789 // of a dedicated entrypoint:
790 //
791 // rX <- ReadBarrierMarkRegX(rX)
792 //
793 int32_t entry_point_offset =
794 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
795 // This runtime call does not require a stack map.
796 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
797
798 // If the new reference is different from the old reference,
799 // update the field in the holder (`*(obj_ + field_offset_)`).
800 //
801 // Note that this field could also hold a different object, if
802 // another thread had concurrently changed it. In that case, the
803 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
804 // (CAS) operation below would abort the CAS, leaving the field
805 // as-is.
806 vixl32::Label done;
807 __ Cmp(temp1_, ref_reg);
808 __ B(eq, &done);
809
810 // Update the the holder's field atomically. This may fail if
811 // mutator updates before us, but it's OK. This is achieved
812 // using a strong compare-and-set (CAS) operation with relaxed
813 // memory synchronization ordering, where the expected value is
814 // the old reference and the desired value is the new reference.
815
816 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
817 // Convenience aliases.
818 vixl32::Register base = obj_;
819 // The UnsafeCASObject intrinsic uses a register pair as field
820 // offset ("long offset"), of which only the low part contains
821 // data.
822 vixl32::Register offset = LowRegisterFrom(field_offset_);
823 vixl32::Register expected = temp1_;
824 vixl32::Register value = ref_reg;
825 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
826 vixl32::Register tmp = temp2_; // Value in memory.
827
828 __ Add(tmp_ptr, base, offset);
829
830 if (kPoisonHeapReferences) {
831 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
832 if (value.Is(expected)) {
833 // Do not poison `value`, as it is the same register as
834 // `expected`, which has just been poisoned.
835 } else {
836 arm_codegen->GetAssembler()->PoisonHeapReference(value);
837 }
838 }
839
840 // do {
841 // tmp = [r_ptr] - expected;
842 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
843
844 vixl32::Label loop_head, exit_loop;
845 __ Bind(&loop_head);
846
847 __ Ldrex(tmp, MemOperand(tmp_ptr));
848
849 __ Subs(tmp, tmp, expected);
850
851 {
Artem Serov0fb37192016-12-06 18:13:40 +0000852 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
853 2 * kMaxInstructionSizeInBytes,
854 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000855
856 __ it(ne);
857 __ clrex(ne);
858 }
859
860 __ B(ne, &exit_loop);
861
862 __ Strex(tmp, value, MemOperand(tmp_ptr));
863 __ Cmp(tmp, 1);
864 __ B(eq, &loop_head);
865
866 __ Bind(&exit_loop);
867
868 if (kPoisonHeapReferences) {
869 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
870 if (value.Is(expected)) {
871 // Do not unpoison `value`, as it is the same register as
872 // `expected`, which has just been unpoisoned.
873 } else {
874 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
875 }
876 }
877
878 __ Bind(&done);
879 __ B(GetExitLabel());
880 }
881
882 private:
883 // The location (register) of the marked object reference.
884 const Location ref_;
885 // The register containing the object holding the marked object reference field.
886 const vixl32::Register obj_;
887 // The location of the offset of the marked reference field within `obj_`.
888 Location field_offset_;
889
890 const vixl32::Register temp1_;
891 const vixl32::Register temp2_;
892
893 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
894};
895
896// Slow path generating a read barrier for a heap reference.
897class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
898 public:
899 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
900 Location out,
901 Location ref,
902 Location obj,
903 uint32_t offset,
904 Location index)
905 : SlowPathCodeARMVIXL(instruction),
906 out_(out),
907 ref_(ref),
908 obj_(obj),
909 offset_(offset),
910 index_(index) {
911 DCHECK(kEmitCompilerReadBarrier);
912 // If `obj` is equal to `out` or `ref`, it means the initial object
913 // has been overwritten by (or after) the heap object reference load
914 // to be instrumented, e.g.:
915 //
916 // __ LoadFromOffset(kLoadWord, out, out, offset);
917 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
918 //
919 // In that case, we have lost the information about the original
920 // object, and the emitted read barrier cannot work properly.
921 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
922 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
923 }
924
925 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
926 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
927 LocationSummary* locations = instruction_->GetLocations();
928 vixl32::Register reg_out = RegisterFrom(out_);
929 DCHECK(locations->CanCall());
930 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
931 DCHECK(instruction_->IsInstanceFieldGet() ||
932 instruction_->IsStaticFieldGet() ||
933 instruction_->IsArrayGet() ||
934 instruction_->IsInstanceOf() ||
935 instruction_->IsCheckCast() ||
936 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
937 << "Unexpected instruction in read barrier for heap reference slow path: "
938 << instruction_->DebugName();
939 // The read barrier instrumentation of object ArrayGet
940 // instructions does not support the HIntermediateAddress
941 // instruction.
942 DCHECK(!(instruction_->IsArrayGet() &&
943 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
944
945 __ Bind(GetEntryLabel());
946 SaveLiveRegisters(codegen, locations);
947
948 // We may have to change the index's value, but as `index_` is a
949 // constant member (like other "inputs" of this slow path),
950 // introduce a copy of it, `index`.
951 Location index = index_;
952 if (index_.IsValid()) {
953 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
954 if (instruction_->IsArrayGet()) {
955 // Compute the actual memory offset and store it in `index`.
956 vixl32::Register index_reg = RegisterFrom(index_);
957 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
958 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
959 // We are about to change the value of `index_reg` (see the
960 // calls to art::arm::Thumb2Assembler::Lsl and
961 // art::arm::Thumb2Assembler::AddConstant below), but it has
962 // not been saved by the previous call to
963 // art::SlowPathCode::SaveLiveRegisters, as it is a
964 // callee-save register --
965 // art::SlowPathCode::SaveLiveRegisters does not consider
966 // callee-save registers, as it has been designed with the
967 // assumption that callee-save registers are supposed to be
968 // handled by the called function. So, as a callee-save
969 // register, `index_reg` _would_ eventually be saved onto
970 // the stack, but it would be too late: we would have
971 // changed its value earlier. Therefore, we manually save
972 // it here into another freely available register,
973 // `free_reg`, chosen of course among the caller-save
974 // registers (as a callee-save `free_reg` register would
975 // exhibit the same problem).
976 //
977 // Note we could have requested a temporary register from
978 // the register allocator instead; but we prefer not to, as
979 // this is a slow path, and we know we can find a
980 // caller-save register that is available.
981 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
982 __ Mov(free_reg, index_reg);
983 index_reg = free_reg;
984 index = LocationFrom(index_reg);
985 } else {
986 // The initial register stored in `index_` has already been
987 // saved in the call to art::SlowPathCode::SaveLiveRegisters
988 // (as it is not a callee-save register), so we can freely
989 // use it.
990 }
991 // Shifting the index value contained in `index_reg` by the scale
992 // factor (2) cannot overflow in practice, as the runtime is
993 // unable to allocate object arrays with a size larger than
994 // 2^26 - 1 (that is, 2^28 - 4 bytes).
995 __ Lsl(index_reg, index_reg, TIMES_4);
996 static_assert(
997 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
998 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
999 __ Add(index_reg, index_reg, offset_);
1000 } else {
1001 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1002 // intrinsics, `index_` is not shifted by a scale factor of 2
1003 // (as in the case of ArrayGet), as it is actually an offset
1004 // to an object field within an object.
1005 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1006 DCHECK(instruction_->GetLocations()->Intrinsified());
1007 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1008 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1009 << instruction_->AsInvoke()->GetIntrinsic();
1010 DCHECK_EQ(offset_, 0U);
1011 DCHECK(index_.IsRegisterPair());
1012 // UnsafeGet's offset location is a register pair, the low
1013 // part contains the correct offset.
1014 index = index_.ToLow();
1015 }
1016 }
1017
1018 // We're moving two or three locations to locations that could
1019 // overlap, so we need a parallel move resolver.
1020 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1021 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1022 parallel_move.AddMove(ref_,
1023 LocationFrom(calling_convention.GetRegisterAt(0)),
1024 Primitive::kPrimNot,
1025 nullptr);
1026 parallel_move.AddMove(obj_,
1027 LocationFrom(calling_convention.GetRegisterAt(1)),
1028 Primitive::kPrimNot,
1029 nullptr);
1030 if (index.IsValid()) {
1031 parallel_move.AddMove(index,
1032 LocationFrom(calling_convention.GetRegisterAt(2)),
1033 Primitive::kPrimInt,
1034 nullptr);
1035 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1036 } else {
1037 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1038 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1039 }
1040 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1041 CheckEntrypointTypes<
1042 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1043 arm_codegen->Move32(out_, LocationFrom(r0));
1044
1045 RestoreLiveRegisters(codegen, locations);
1046 __ B(GetExitLabel());
1047 }
1048
1049 const char* GetDescription() const OVERRIDE {
1050 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1051 }
1052
1053 private:
1054 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1055 uint32_t ref = RegisterFrom(ref_).GetCode();
1056 uint32_t obj = RegisterFrom(obj_).GetCode();
1057 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1058 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1059 return vixl32::Register(i);
1060 }
1061 }
1062 // We shall never fail to find a free caller-save register, as
1063 // there are more than two core caller-save registers on ARM
1064 // (meaning it is possible to find one which is different from
1065 // `ref` and `obj`).
1066 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1067 LOG(FATAL) << "Could not find a free caller-save register";
1068 UNREACHABLE();
1069 }
1070
1071 const Location out_;
1072 const Location ref_;
1073 const Location obj_;
1074 const uint32_t offset_;
1075 // An additional location containing an index to an array.
1076 // Only used for HArrayGet and the UnsafeGetObject &
1077 // UnsafeGetObjectVolatile intrinsics.
1078 const Location index_;
1079
1080 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1081};
1082
1083// Slow path generating a read barrier for a GC root.
1084class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1085 public:
1086 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1087 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1088 DCHECK(kEmitCompilerReadBarrier);
1089 }
1090
1091 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1092 LocationSummary* locations = instruction_->GetLocations();
1093 vixl32::Register reg_out = RegisterFrom(out_);
1094 DCHECK(locations->CanCall());
1095 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1096 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1097 << "Unexpected instruction in read barrier for GC root slow path: "
1098 << instruction_->DebugName();
1099
1100 __ Bind(GetEntryLabel());
1101 SaveLiveRegisters(codegen, locations);
1102
1103 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1104 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1105 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1106 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1107 instruction_,
1108 instruction_->GetDexPc(),
1109 this);
1110 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1111 arm_codegen->Move32(out_, LocationFrom(r0));
1112
1113 RestoreLiveRegisters(codegen, locations);
1114 __ B(GetExitLabel());
1115 }
1116
1117 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1118
1119 private:
1120 const Location out_;
1121 const Location root_;
1122
1123 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1124};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001125
Scott Wakelingfe885462016-09-22 10:24:38 +01001126inline vixl32::Condition ARMCondition(IfCondition cond) {
1127 switch (cond) {
1128 case kCondEQ: return eq;
1129 case kCondNE: return ne;
1130 case kCondLT: return lt;
1131 case kCondLE: return le;
1132 case kCondGT: return gt;
1133 case kCondGE: return ge;
1134 case kCondB: return lo;
1135 case kCondBE: return ls;
1136 case kCondA: return hi;
1137 case kCondAE: return hs;
1138 }
1139 LOG(FATAL) << "Unreachable";
1140 UNREACHABLE();
1141}
1142
1143// Maps signed condition to unsigned condition.
1144inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1145 switch (cond) {
1146 case kCondEQ: return eq;
1147 case kCondNE: return ne;
1148 // Signed to unsigned.
1149 case kCondLT: return lo;
1150 case kCondLE: return ls;
1151 case kCondGT: return hi;
1152 case kCondGE: return hs;
1153 // Unsigned remain unchanged.
1154 case kCondB: return lo;
1155 case kCondBE: return ls;
1156 case kCondA: return hi;
1157 case kCondAE: return hs;
1158 }
1159 LOG(FATAL) << "Unreachable";
1160 UNREACHABLE();
1161}
1162
1163inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1164 // The ARM condition codes can express all the necessary branches, see the
1165 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1166 // There is no dex instruction or HIR that would need the missing conditions
1167 // "equal or unordered" or "not equal".
1168 switch (cond) {
1169 case kCondEQ: return eq;
1170 case kCondNE: return ne /* unordered */;
1171 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1172 case kCondLE: return gt_bias ? ls : le /* unordered */;
1173 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1174 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1175 default:
1176 LOG(FATAL) << "UNREACHABLE";
1177 UNREACHABLE();
1178 }
1179}
1180
Scott Wakelingfe885462016-09-22 10:24:38 +01001181void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1182 stream << vixl32::Register(reg);
1183}
1184
1185void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1186 stream << vixl32::SRegister(reg);
1187}
1188
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001189static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001190 uint32_t mask = 0;
1191 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1192 i <= regs.GetLastSRegister().GetCode();
1193 ++i) {
1194 mask |= (1 << i);
1195 }
1196 return mask;
1197}
1198
Artem Serovd4cc5b22016-11-04 11:19:09 +00001199// Saves the register in the stack. Returns the size taken on stack.
1200size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1201 uint32_t reg_id ATTRIBUTE_UNUSED) {
1202 TODO_VIXL32(FATAL);
1203 return 0;
1204}
1205
1206// Restores the register from the stack. Returns the size taken on stack.
1207size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1208 uint32_t reg_id ATTRIBUTE_UNUSED) {
1209 TODO_VIXL32(FATAL);
1210 return 0;
1211}
1212
1213size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1214 uint32_t reg_id ATTRIBUTE_UNUSED) {
1215 TODO_VIXL32(FATAL);
1216 return 0;
1217}
1218
1219size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1220 uint32_t reg_id ATTRIBUTE_UNUSED) {
1221 TODO_VIXL32(FATAL);
1222 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001223}
1224
Scott Wakelingfe885462016-09-22 10:24:38 +01001225#undef __
1226
1227CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1228 const ArmInstructionSetFeatures& isa_features,
1229 const CompilerOptions& compiler_options,
1230 OptimizingCompilerStats* stats)
1231 : CodeGenerator(graph,
1232 kNumberOfCoreRegisters,
1233 kNumberOfSRegisters,
1234 kNumberOfRegisterPairs,
1235 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001236 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001237 compiler_options,
1238 stats),
1239 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001240 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001241 location_builder_(graph, this),
1242 instruction_visitor_(graph, this),
1243 move_resolver_(graph->GetArena(), this),
1244 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001245 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001246 uint32_literals_(std::less<uint32_t>(),
1247 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1248 method_patches_(MethodReferenceComparator(),
1249 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1250 call_patches_(MethodReferenceComparator(),
1251 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001252 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1253 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001254 boot_image_string_patches_(StringReferenceValueComparator(),
1255 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001256 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001257 boot_image_type_patches_(TypeReferenceValueComparator(),
1258 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1259 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1260 boot_image_address_patches_(std::less<uint32_t>(),
1261 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1262 jit_string_patches_(StringReferenceValueComparator(),
1263 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1264 jit_class_patches_(TypeReferenceValueComparator(),
1265 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001266 // Always save the LR register to mimic Quick.
1267 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001268 // Give d14 and d15 as scratch registers to VIXL.
1269 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1270 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1271 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1272 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1273 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1274 // also want to investigate giving those 14 other D registers to the allocator.
1275 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1276 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001277}
1278
Artem Serov551b28f2016-10-18 19:11:30 +01001279void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1280 uint32_t num_entries = switch_instr_->GetNumEntries();
1281 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1282
1283 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001284 // underlying code buffer and we have generated a jump table of the right size, using
1285 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001286 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1287 num_entries * sizeof(int32_t),
1288 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001289 // TODO(VIXL): Check that using lower case bind is fine here.
1290 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001291 for (uint32_t i = 0; i < num_entries; i++) {
1292 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1293 }
1294}
1295
1296void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1297 uint32_t num_entries = switch_instr_->GetNumEntries();
1298 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1299
Artem Serov551b28f2016-10-18 19:11:30 +01001300 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1301 for (uint32_t i = 0; i < num_entries; i++) {
1302 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1303 DCHECK(target_label->IsBound());
1304 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1305 // When doing BX to address we need to have lower bit set to 1 in T32.
1306 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1307 jump_offset++;
1308 }
1309 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1310 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001311
Scott Wakelingb77051e2016-11-21 19:46:00 +00001312 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001313 }
1314}
1315
Artem Serov09a940d2016-11-11 16:15:11 +00001316void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001317 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001318 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001319 }
1320}
1321
Andreas Gampeca620d72016-11-08 08:09:33 -08001322#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001323
1324void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001325 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001326 GetAssembler()->FinalizeCode();
1327 CodeGenerator::Finalize(allocator);
1328}
1329
1330void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001331 // Stack register, LR and PC are always reserved.
1332 blocked_core_registers_[SP] = true;
1333 blocked_core_registers_[LR] = true;
1334 blocked_core_registers_[PC] = true;
1335
1336 // Reserve thread register.
1337 blocked_core_registers_[TR] = true;
1338
1339 // Reserve temp register.
1340 blocked_core_registers_[IP] = true;
1341
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001342 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1343 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1344 blocked_fpu_registers_[28] = true;
1345 blocked_fpu_registers_[29] = true;
1346 blocked_fpu_registers_[30] = true;
1347 blocked_fpu_registers_[31] = true;
1348
Scott Wakelingfe885462016-09-22 10:24:38 +01001349 if (GetGraph()->IsDebuggable()) {
1350 // Stubs do not save callee-save floating point registers. If the graph
1351 // is debuggable, we need to deal with these registers differently. For
1352 // now, just block them.
1353 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1354 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1355 ++i) {
1356 blocked_fpu_registers_[i] = true;
1357 }
1358 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001359}
1360
Scott Wakelingfe885462016-09-22 10:24:38 +01001361InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1362 CodeGeneratorARMVIXL* codegen)
1363 : InstructionCodeGenerator(graph, codegen),
1364 assembler_(codegen->GetAssembler()),
1365 codegen_(codegen) {}
1366
1367void CodeGeneratorARMVIXL::ComputeSpillMask() {
1368 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1369 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1370 // There is no easy instruction to restore just the PC on thumb2. We spill and
1371 // restore another arbitrary register.
1372 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1373 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1374 // We use vpush and vpop for saving and restoring floating point registers, which take
1375 // a SRegister and the number of registers to save/restore after that SRegister. We
1376 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1377 // but in the range.
1378 if (fpu_spill_mask_ != 0) {
1379 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1380 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1381 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1382 fpu_spill_mask_ |= (1 << i);
1383 }
1384 }
1385}
1386
1387void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1388 bool skip_overflow_check =
1389 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1390 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1391 __ Bind(&frame_entry_label_);
1392
1393 if (HasEmptyFrame()) {
1394 return;
1395 }
1396
Scott Wakelingfe885462016-09-22 10:24:38 +01001397 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001398 UseScratchRegisterScope temps(GetVIXLAssembler());
1399 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001400 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001401 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001402 ExactAssemblyScope aas(GetVIXLAssembler(),
1403 vixl32::kMaxInstructionSizeInBytes,
1404 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001405 __ ldr(temp, MemOperand(temp));
1406 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001407 }
1408
1409 __ Push(RegisterList(core_spill_mask_));
1410 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1411 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1412 0,
1413 core_spill_mask_,
1414 kArmWordSize);
1415 if (fpu_spill_mask_ != 0) {
1416 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1417
1418 // Check that list is contiguous.
1419 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1420
1421 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1422 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001423 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001424 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001425
1426 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1427 UseScratchRegisterScope temps(GetVIXLAssembler());
1428 vixl32::Register temp = temps.Acquire();
1429 // Initialize should_deoptimize flag to 0.
1430 __ Mov(temp, 0);
1431 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1432 }
1433
Scott Wakelingfe885462016-09-22 10:24:38 +01001434 int adjust = GetFrameSize() - FrameEntrySpillSize();
1435 __ Sub(sp, sp, adjust);
1436 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001437
1438 // Save the current method if we need it. Note that we do not
1439 // do this in HCurrentMethod, as the instruction might have been removed
1440 // in the SSA graph.
1441 if (RequiresCurrentMethod()) {
1442 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1443 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001444}
1445
1446void CodeGeneratorARMVIXL::GenerateFrameExit() {
1447 if (HasEmptyFrame()) {
1448 __ Bx(lr);
1449 return;
1450 }
1451 GetAssembler()->cfi().RememberState();
1452 int adjust = GetFrameSize() - FrameEntrySpillSize();
1453 __ Add(sp, sp, adjust);
1454 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1455 if (fpu_spill_mask_ != 0) {
1456 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1457
1458 // Check that list is contiguous.
1459 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1460
1461 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1462 GetAssembler()->cfi().AdjustCFAOffset(
1463 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001464 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001465 }
1466 // Pop LR into PC to return.
1467 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1468 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1469 __ Pop(RegisterList(pop_mask));
1470 GetAssembler()->cfi().RestoreState();
1471 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1472}
1473
1474void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1475 __ Bind(GetLabelOf(block));
1476}
1477
Artem Serovd4cc5b22016-11-04 11:19:09 +00001478Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1479 switch (type) {
1480 case Primitive::kPrimBoolean:
1481 case Primitive::kPrimByte:
1482 case Primitive::kPrimChar:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
1485 case Primitive::kPrimNot: {
1486 uint32_t index = gp_index_++;
1487 uint32_t stack_index = stack_index_++;
1488 if (index < calling_convention.GetNumberOfRegisters()) {
1489 return LocationFrom(calling_convention.GetRegisterAt(index));
1490 } else {
1491 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1492 }
1493 }
1494
1495 case Primitive::kPrimLong: {
1496 uint32_t index = gp_index_;
1497 uint32_t stack_index = stack_index_;
1498 gp_index_ += 2;
1499 stack_index_ += 2;
1500 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1501 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1502 // Skip R1, and use R2_R3 instead.
1503 gp_index_++;
1504 index++;
1505 }
1506 }
1507 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1508 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1509 calling_convention.GetRegisterAt(index + 1).GetCode());
1510
1511 return LocationFrom(calling_convention.GetRegisterAt(index),
1512 calling_convention.GetRegisterAt(index + 1));
1513 } else {
1514 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1515 }
1516 }
1517
1518 case Primitive::kPrimFloat: {
1519 uint32_t stack_index = stack_index_++;
1520 if (float_index_ % 2 == 0) {
1521 float_index_ = std::max(double_index_, float_index_);
1522 }
1523 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1524 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1525 } else {
1526 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1527 }
1528 }
1529
1530 case Primitive::kPrimDouble: {
1531 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1532 uint32_t stack_index = stack_index_;
1533 stack_index_ += 2;
1534 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1535 uint32_t index = double_index_;
1536 double_index_ += 2;
1537 Location result = LocationFrom(
1538 calling_convention.GetFpuRegisterAt(index),
1539 calling_convention.GetFpuRegisterAt(index + 1));
1540 DCHECK(ExpectedPairLayout(result));
1541 return result;
1542 } else {
1543 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1544 }
1545 }
1546
1547 case Primitive::kPrimVoid:
1548 LOG(FATAL) << "Unexpected parameter type " << type;
1549 break;
1550 }
1551 return Location::NoLocation();
1552}
1553
1554Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1555 switch (type) {
1556 case Primitive::kPrimBoolean:
1557 case Primitive::kPrimByte:
1558 case Primitive::kPrimChar:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimNot: {
1562 return LocationFrom(r0);
1563 }
1564
1565 case Primitive::kPrimFloat: {
1566 return LocationFrom(s0);
1567 }
1568
1569 case Primitive::kPrimLong: {
1570 return LocationFrom(r0, r1);
1571 }
1572
1573 case Primitive::kPrimDouble: {
1574 return LocationFrom(s0, s1);
1575 }
1576
1577 case Primitive::kPrimVoid:
1578 return Location::NoLocation();
1579 }
1580
1581 UNREACHABLE();
1582}
1583
1584Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1585 return LocationFrom(kMethodRegister);
1586}
1587
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001588void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1589 if (source.Equals(destination)) {
1590 return;
1591 }
1592 if (destination.IsRegister()) {
1593 if (source.IsRegister()) {
1594 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1595 } else if (source.IsFpuRegister()) {
1596 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1597 } else {
1598 GetAssembler()->LoadFromOffset(kLoadWord,
1599 RegisterFrom(destination),
1600 sp,
1601 source.GetStackIndex());
1602 }
1603 } else if (destination.IsFpuRegister()) {
1604 if (source.IsRegister()) {
1605 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1606 } else if (source.IsFpuRegister()) {
1607 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1608 } else {
1609 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1610 }
1611 } else {
1612 DCHECK(destination.IsStackSlot()) << destination;
1613 if (source.IsRegister()) {
1614 GetAssembler()->StoreToOffset(kStoreWord,
1615 RegisterFrom(source),
1616 sp,
1617 destination.GetStackIndex());
1618 } else if (source.IsFpuRegister()) {
1619 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1620 } else {
1621 DCHECK(source.IsStackSlot()) << source;
1622 UseScratchRegisterScope temps(GetVIXLAssembler());
1623 vixl32::Register temp = temps.Acquire();
1624 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1625 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1626 }
1627 }
1628}
1629
Artem Serovcfbe9132016-10-14 15:58:56 +01001630void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1631 DCHECK(location.IsRegister());
1632 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001633}
1634
1635void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001636 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1637 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1638 HParallelMove move(GetGraph()->GetArena());
1639 move.AddMove(src, dst, dst_type, nullptr);
1640 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001641}
1642
Artem Serovcfbe9132016-10-14 15:58:56 +01001643void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1644 if (location.IsRegister()) {
1645 locations->AddTemp(location);
1646 } else if (location.IsRegisterPair()) {
1647 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1648 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1649 } else {
1650 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1651 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001652}
1653
1654void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1655 HInstruction* instruction,
1656 uint32_t dex_pc,
1657 SlowPathCode* slow_path) {
1658 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001659 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1660 // Ensure the pc position is recorded immediately after the `blx` instruction.
1661 // 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 +00001662 ExactAssemblyScope aas(GetVIXLAssembler(),
1663 vixl32::k16BitT32InstructionSizeInBytes,
1664 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001665 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001666 if (EntrypointRequiresStackMap(entrypoint)) {
1667 RecordPcInfo(instruction, dex_pc, slow_path);
1668 }
1669}
1670
1671void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1672 HInstruction* instruction,
1673 SlowPathCode* slow_path) {
1674 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001675 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001676 __ Blx(lr);
1677}
1678
Scott Wakelingfe885462016-09-22 10:24:38 +01001679void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1680 DCHECK(!successor->IsExitBlock());
1681 HBasicBlock* block = got->GetBlock();
1682 HInstruction* previous = got->GetPrevious();
1683 HLoopInformation* info = block->GetLoopInformation();
1684
1685 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1686 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1687 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1688 return;
1689 }
1690 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1691 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1692 }
1693 if (!codegen_->GoesToNextBlock(block, successor)) {
1694 __ B(codegen_->GetLabelOf(successor));
1695 }
1696}
1697
1698void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1699 got->SetLocations(nullptr);
1700}
1701
1702void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1703 HandleGoto(got, got->GetSuccessor());
1704}
1705
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001706void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1707 try_boundary->SetLocations(nullptr);
1708}
1709
1710void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1711 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1712 if (!successor->IsExitBlock()) {
1713 HandleGoto(try_boundary, successor);
1714 }
1715}
1716
Scott Wakelingfe885462016-09-22 10:24:38 +01001717void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1718 exit->SetLocations(nullptr);
1719}
1720
1721void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1722}
1723
1724void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1725 Primitive::Type type = instruction->InputAt(0)->GetType();
1726 Location lhs_loc = instruction->GetLocations()->InAt(0);
1727 Location rhs_loc = instruction->GetLocations()->InAt(1);
1728 if (rhs_loc.IsConstant()) {
1729 // 0.0 is the only immediate that can be encoded directly in
1730 // a VCMP instruction.
1731 //
1732 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1733 // specify that in a floating-point comparison, positive zero
1734 // and negative zero are considered equal, so we can use the
1735 // literal 0.0 for both cases here.
1736 //
1737 // Note however that some methods (Float.equal, Float.compare,
1738 // Float.compareTo, Double.equal, Double.compare,
1739 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1740 // StrictMath.min) consider 0.0 to be (strictly) greater than
1741 // -0.0. So if we ever translate calls to these methods into a
1742 // HCompare instruction, we must handle the -0.0 case with
1743 // care here.
1744 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1745 if (type == Primitive::kPrimFloat) {
1746 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1747 } else {
1748 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001749 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001750 }
1751 } else {
1752 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001753 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001754 } else {
1755 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001756 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001757 }
1758 }
1759}
1760
1761void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1762 vixl32::Label* true_label,
1763 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1764 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1765 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1766 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1767}
1768
1769void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1770 vixl32::Label* true_label,
1771 vixl32::Label* false_label) {
1772 LocationSummary* locations = cond->GetLocations();
1773 Location left = locations->InAt(0);
1774 Location right = locations->InAt(1);
1775 IfCondition if_cond = cond->GetCondition();
1776
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001777 vixl32::Register left_high = HighRegisterFrom(left);
1778 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01001779 IfCondition true_high_cond = if_cond;
1780 IfCondition false_high_cond = cond->GetOppositeCondition();
1781 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
1782
1783 // Set the conditions for the test, remembering that == needs to be
1784 // decided using the low words.
1785 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1786 switch (if_cond) {
1787 case kCondEQ:
1788 case kCondNE:
1789 // Nothing to do.
1790 break;
1791 case kCondLT:
1792 false_high_cond = kCondGT;
1793 break;
1794 case kCondLE:
1795 true_high_cond = kCondLT;
1796 break;
1797 case kCondGT:
1798 false_high_cond = kCondLT;
1799 break;
1800 case kCondGE:
1801 true_high_cond = kCondGT;
1802 break;
1803 case kCondB:
1804 false_high_cond = kCondA;
1805 break;
1806 case kCondBE:
1807 true_high_cond = kCondB;
1808 break;
1809 case kCondA:
1810 false_high_cond = kCondB;
1811 break;
1812 case kCondAE:
1813 true_high_cond = kCondA;
1814 break;
1815 }
1816 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001817 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001818 int32_t val_low = Low32Bits(value);
1819 int32_t val_high = High32Bits(value);
1820
1821 __ Cmp(left_high, val_high);
1822 if (if_cond == kCondNE) {
1823 __ B(ARMCondition(true_high_cond), true_label);
1824 } else if (if_cond == kCondEQ) {
1825 __ B(ARMCondition(false_high_cond), false_label);
1826 } else {
1827 __ B(ARMCondition(true_high_cond), true_label);
1828 __ B(ARMCondition(false_high_cond), false_label);
1829 }
1830 // Must be equal high, so compare the lows.
1831 __ Cmp(left_low, val_low);
1832 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001833 vixl32::Register right_high = HighRegisterFrom(right);
1834 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001835
1836 __ Cmp(left_high, right_high);
1837 if (if_cond == kCondNE) {
1838 __ B(ARMCondition(true_high_cond), true_label);
1839 } else if (if_cond == kCondEQ) {
1840 __ B(ARMCondition(false_high_cond), false_label);
1841 } else {
1842 __ B(ARMCondition(true_high_cond), true_label);
1843 __ B(ARMCondition(false_high_cond), false_label);
1844 }
1845 // Must be equal high, so compare the lows.
1846 __ Cmp(left_low, right_low);
1847 }
1848 // The last comparison might be unsigned.
1849 // TODO: optimize cases where this is always true/false
1850 __ B(final_condition, true_label);
1851}
1852
1853void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1854 vixl32::Label* true_target_in,
1855 vixl32::Label* false_target_in) {
1856 // Generated branching requires both targets to be explicit. If either of the
1857 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1858 vixl32::Label fallthrough;
1859 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1860 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1861
1862 Primitive::Type type = condition->InputAt(0)->GetType();
1863 switch (type) {
1864 case Primitive::kPrimLong:
1865 GenerateLongComparesAndJumps(condition, true_target, false_target);
1866 break;
1867 case Primitive::kPrimFloat:
1868 case Primitive::kPrimDouble:
1869 GenerateVcmp(condition);
1870 GenerateFPJumps(condition, true_target, false_target);
1871 break;
1872 default:
1873 LOG(FATAL) << "Unexpected compare type " << type;
1874 }
1875
1876 if (false_target != &fallthrough) {
1877 __ B(false_target);
1878 }
1879
1880 if (true_target_in == nullptr || false_target_in == nullptr) {
1881 __ Bind(&fallthrough);
1882 }
1883}
1884
1885void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1886 size_t condition_input_index,
1887 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00001888 vixl32::Label* false_target,
1889 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001890 HInstruction* cond = instruction->InputAt(condition_input_index);
1891
1892 if (true_target == nullptr && false_target == nullptr) {
1893 // Nothing to do. The code always falls through.
1894 return;
1895 } else if (cond->IsIntConstant()) {
1896 // Constant condition, statically compared against "true" (integer value 1).
1897 if (cond->AsIntConstant()->IsTrue()) {
1898 if (true_target != nullptr) {
1899 __ B(true_target);
1900 }
1901 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00001902 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01001903 if (false_target != nullptr) {
1904 __ B(false_target);
1905 }
1906 }
1907 return;
1908 }
1909
1910 // The following code generates these patterns:
1911 // (1) true_target == nullptr && false_target != nullptr
1912 // - opposite condition true => branch to false_target
1913 // (2) true_target != nullptr && false_target == nullptr
1914 // - condition true => branch to true_target
1915 // (3) true_target != nullptr && false_target != nullptr
1916 // - condition true => branch to true_target
1917 // - branch to false_target
1918 if (IsBooleanValueOrMaterializedCondition(cond)) {
1919 // Condition has been materialized, compare the output to 0.
1920 if (kIsDebugBuild) {
1921 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1922 DCHECK(cond_val.IsRegister());
1923 }
1924 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001925 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
1926 false_target,
1927 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001928 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001929 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
1930 true_target,
1931 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001932 }
1933 } else {
1934 // Condition has not been materialized. Use its inputs as the comparison and
1935 // its condition as the branch condition.
1936 HCondition* condition = cond->AsCondition();
1937
1938 // If this is a long or FP comparison that has been folded into
1939 // the HCondition, generate the comparison directly.
1940 Primitive::Type type = condition->InputAt(0)->GetType();
1941 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1942 GenerateCompareTestAndBranch(condition, true_target, false_target);
1943 return;
1944 }
1945
1946 LocationSummary* locations = cond->GetLocations();
1947 DCHECK(locations->InAt(0).IsRegister());
1948 vixl32::Register left = InputRegisterAt(cond, 0);
1949 Location right = locations->InAt(1);
1950 if (right.IsRegister()) {
1951 __ Cmp(left, InputRegisterAt(cond, 1));
1952 } else {
1953 DCHECK(right.IsConstant());
1954 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1955 }
1956 if (true_target == nullptr) {
1957 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1958 } else {
1959 __ B(ARMCondition(condition->GetCondition()), true_target);
1960 }
1961 }
1962
1963 // If neither branch falls through (case 3), the conditional branch to `true_target`
1964 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1965 if (true_target != nullptr && false_target != nullptr) {
1966 __ B(false_target);
1967 }
1968}
1969
1970void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1972 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1973 locations->SetInAt(0, Location::RequiresRegister());
1974 }
1975}
1976
1977void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1978 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1979 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001980 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1981 nullptr : codegen_->GetLabelOf(true_successor);
1982 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1983 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001984 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1985}
1986
Scott Wakelingc34dba72016-10-03 10:14:44 +01001987void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1988 LocationSummary* locations = new (GetGraph()->GetArena())
1989 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1990 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1991 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1992 locations->SetInAt(0, Location::RequiresRegister());
1993 }
1994}
1995
1996void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1997 SlowPathCodeARMVIXL* slow_path =
1998 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
1999 GenerateTestAndBranch(deoptimize,
2000 /* condition_input_index */ 0,
2001 slow_path->GetEntryLabel(),
2002 /* false_target */ nullptr);
2003}
2004
Artem Serovd4cc5b22016-11-04 11:19:09 +00002005void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2006 LocationSummary* locations = new (GetGraph()->GetArena())
2007 LocationSummary(flag, LocationSummary::kNoCall);
2008 locations->SetOut(Location::RequiresRegister());
2009}
2010
2011void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2012 GetAssembler()->LoadFromOffset(kLoadWord,
2013 OutputRegister(flag),
2014 sp,
2015 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2016}
2017
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002018void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2020 if (Primitive::IsFloatingPointType(select->GetType())) {
2021 locations->SetInAt(0, Location::RequiresFpuRegister());
2022 locations->SetInAt(1, Location::RequiresFpuRegister());
2023 } else {
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RequiresRegister());
2026 }
2027 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2028 locations->SetInAt(2, Location::RequiresRegister());
2029 }
2030 locations->SetOut(Location::SameAsFirstInput());
2031}
2032
2033void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2034 LocationSummary* locations = select->GetLocations();
2035 vixl32::Label false_target;
2036 GenerateTestAndBranch(select,
2037 /* condition_input_index */ 2,
2038 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002039 &false_target,
2040 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002041 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2042 __ Bind(&false_target);
2043}
2044
Artem Serov551b28f2016-10-18 19:11:30 +01002045void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2046 new (GetGraph()->GetArena()) LocationSummary(info);
2047}
2048
2049void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2050 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2051}
2052
Scott Wakelingfe885462016-09-22 10:24:38 +01002053void CodeGeneratorARMVIXL::GenerateNop() {
2054 __ Nop();
2055}
2056
2057void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2058 LocationSummary* locations =
2059 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2060 // Handle the long/FP comparisons made in instruction simplification.
2061 switch (cond->InputAt(0)->GetType()) {
2062 case Primitive::kPrimLong:
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2065 if (!cond->IsEmittedAtUseSite()) {
2066 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2067 }
2068 break;
2069
Scott Wakelingfe885462016-09-22 10:24:38 +01002070 case Primitive::kPrimFloat:
2071 case Primitive::kPrimDouble:
2072 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002073 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002074 if (!cond->IsEmittedAtUseSite()) {
2075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2076 }
2077 break;
2078
2079 default:
2080 locations->SetInAt(0, Location::RequiresRegister());
2081 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2082 if (!cond->IsEmittedAtUseSite()) {
2083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2084 }
2085 }
2086}
2087
2088void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2089 if (cond->IsEmittedAtUseSite()) {
2090 return;
2091 }
2092
Artem Serov657022c2016-11-23 14:19:38 +00002093 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002094 vixl32::Register out = OutputRegister(cond);
2095 vixl32::Label true_label, false_label;
2096
2097 switch (cond->InputAt(0)->GetType()) {
2098 default: {
2099 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002100 if (right.IsRegister()) {
2101 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2102 } else {
2103 DCHECK(right.IsConstant());
2104 __ Cmp(InputRegisterAt(cond, 0),
2105 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2106 }
Artem Serov0fb37192016-12-06 18:13:40 +00002107 ExactAssemblyScope aas(GetVIXLAssembler(),
2108 3 * vixl32::kMaxInstructionSizeInBytes,
2109 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002110 __ ite(ARMCondition(cond->GetCondition()));
2111 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2112 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002113 return;
2114 }
2115 case Primitive::kPrimLong:
2116 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2117 break;
2118 case Primitive::kPrimFloat:
2119 case Primitive::kPrimDouble:
2120 GenerateVcmp(cond);
2121 GenerateFPJumps(cond, &true_label, &false_label);
2122 break;
2123 }
2124
2125 // Convert the jumps into the result.
2126 vixl32::Label done_label;
2127
2128 // False case: result = 0.
2129 __ Bind(&false_label);
2130 __ Mov(out, 0);
2131 __ B(&done_label);
2132
2133 // True case: result = 1.
2134 __ Bind(&true_label);
2135 __ Mov(out, 1);
2136 __ Bind(&done_label);
2137}
2138
2139void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2140 HandleCondition(comp);
2141}
2142
2143void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2144 HandleCondition(comp);
2145}
2146
2147void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2148 HandleCondition(comp);
2149}
2150
2151void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2152 HandleCondition(comp);
2153}
2154
2155void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2156 HandleCondition(comp);
2157}
2158
2159void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2160 HandleCondition(comp);
2161}
2162
2163void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2164 HandleCondition(comp);
2165}
2166
2167void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2168 HandleCondition(comp);
2169}
2170
2171void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2172 HandleCondition(comp);
2173}
2174
2175void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2176 HandleCondition(comp);
2177}
2178
2179void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2180 HandleCondition(comp);
2181}
2182
2183void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2184 HandleCondition(comp);
2185}
2186
2187void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2188 HandleCondition(comp);
2189}
2190
2191void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2192 HandleCondition(comp);
2193}
2194
2195void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2196 HandleCondition(comp);
2197}
2198
2199void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2200 HandleCondition(comp);
2201}
2202
2203void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2204 HandleCondition(comp);
2205}
2206
2207void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2208 HandleCondition(comp);
2209}
2210
2211void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2212 HandleCondition(comp);
2213}
2214
2215void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2216 HandleCondition(comp);
2217}
2218
2219void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2222 locations->SetOut(Location::ConstantLocation(constant));
2223}
2224
2225void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2226 // Will be generated at use site.
2227}
2228
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002229void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2232 locations->SetOut(Location::ConstantLocation(constant));
2233}
2234
2235void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2236 // Will be generated at use site.
2237}
2238
Scott Wakelingfe885462016-09-22 10:24:38 +01002239void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2242 locations->SetOut(Location::ConstantLocation(constant));
2243}
2244
2245void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2246 // Will be generated at use site.
2247}
2248
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002249void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2250 LocationSummary* locations =
2251 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2252 locations->SetOut(Location::ConstantLocation(constant));
2253}
2254
Scott Wakelingc34dba72016-10-03 10:14:44 +01002255void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2256 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002257 // Will be generated at use site.
2258}
2259
2260void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2261 LocationSummary* locations =
2262 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2263 locations->SetOut(Location::ConstantLocation(constant));
2264}
2265
Scott Wakelingc34dba72016-10-03 10:14:44 +01002266void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2267 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002268 // Will be generated at use site.
2269}
2270
Scott Wakelingfe885462016-09-22 10:24:38 +01002271void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2272 memory_barrier->SetLocations(nullptr);
2273}
2274
2275void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2276 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2277}
2278
2279void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2280 ret->SetLocations(nullptr);
2281}
2282
2283void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2284 codegen_->GenerateFrameExit();
2285}
2286
2287void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2288 LocationSummary* locations =
2289 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2290 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2291}
2292
2293void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2294 codegen_->GenerateFrameExit();
2295}
2296
Artem Serovcfbe9132016-10-14 15:58:56 +01002297void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2298 // The trampoline uses the same calling convention as dex calling conventions,
2299 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2300 // the method_idx.
2301 HandleInvoke(invoke);
2302}
2303
2304void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2305 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2306}
2307
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002308void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2309 // Explicit clinit checks triggered by static invokes must have been pruned by
2310 // art::PrepareForRegisterAllocation.
2311 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2312
Anton Kirilov5ec62182016-10-13 20:16:02 +01002313 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2314 if (intrinsic.TryDispatch(invoke)) {
2315 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2316 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2317 }
2318 return;
2319 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002320
2321 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002322
Artem Serovd4cc5b22016-11-04 11:19:09 +00002323 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2324 if (invoke->HasPcRelativeDexCache()) {
2325 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2326 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002327}
2328
Anton Kirilov5ec62182016-10-13 20:16:02 +01002329static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2330 if (invoke->GetLocations()->Intrinsified()) {
2331 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2332 intrinsic.Dispatch(invoke);
2333 return true;
2334 }
2335 return false;
2336}
2337
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002338void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2339 // Explicit clinit checks triggered by static invokes must have been pruned by
2340 // art::PrepareForRegisterAllocation.
2341 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2342
Anton Kirilov5ec62182016-10-13 20:16:02 +01002343 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2344 return;
2345 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002346
2347 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002348 codegen_->GenerateStaticOrDirectCall(
2349 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002350 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2351}
2352
2353void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002354 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002355 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2356}
2357
2358void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002359 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2360 if (intrinsic.TryDispatch(invoke)) {
2361 return;
2362 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002363
2364 HandleInvoke(invoke);
2365}
2366
2367void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002368 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2369 return;
2370 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002371
2372 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002373 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002374 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002375}
2376
Artem Serovcfbe9132016-10-14 15:58:56 +01002377void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2378 HandleInvoke(invoke);
2379 // Add the hidden argument.
2380 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2381}
2382
2383void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2384 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2385 LocationSummary* locations = invoke->GetLocations();
2386 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2387 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2388 Location receiver = locations->InAt(0);
2389 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2390
2391 DCHECK(!receiver.IsStackSlot());
2392
Alexandre Rames374ddf32016-11-04 10:40:49 +00002393 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2394 {
Artem Serov0fb37192016-12-06 18:13:40 +00002395 ExactAssemblyScope aas(GetVIXLAssembler(),
2396 vixl32::kMaxInstructionSizeInBytes,
2397 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002398 // /* HeapReference<Class> */ temp = receiver->klass_
2399 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2400 codegen_->MaybeRecordImplicitNullCheck(invoke);
2401 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002402 // Instead of simply (possibly) unpoisoning `temp` here, we should
2403 // emit a read barrier for the previous class reference load.
2404 // However this is not required in practice, as this is an
2405 // intermediate/temporary reference and because the current
2406 // concurrent copying collector keeps the from-space memory
2407 // intact/accessible until the end of the marking phase (the
2408 // concurrent copying collector may not in the future).
2409 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2410 GetAssembler()->LoadFromOffset(kLoadWord,
2411 temp,
2412 temp,
2413 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2414 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2415 invoke->GetImtIndex(), kArmPointerSize));
2416 // temp = temp->GetImtEntryAt(method_offset);
2417 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2418 uint32_t entry_point =
2419 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2420 // LR = temp->GetEntryPoint();
2421 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2422
2423 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2424 // instruction from clobbering it as they might use r12 as a scratch register.
2425 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002426
2427 {
2428 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2429 // so it checks if the application is using them (by passing them to the macro assembler
2430 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2431 // what is available, and is the opposite of the standard usage: Instead of requesting a
2432 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2433 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2434 // (to materialize the constant), since the destination register becomes available for such use
2435 // internally for the duration of the macro instruction.
2436 UseScratchRegisterScope temps(GetVIXLAssembler());
2437 temps.Exclude(hidden_reg);
2438 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2439 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002440 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002441 // Ensure the pc position is recorded immediately after the `blx` instruction.
2442 // 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 +00002443 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002444 vixl32::k16BitT32InstructionSizeInBytes,
2445 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002446 // LR();
2447 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002448 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002449 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002450 }
2451}
2452
Artem Serov02109dd2016-09-23 17:17:54 +01002453void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2454 LocationSummary* locations =
2455 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2456 switch (neg->GetResultType()) {
2457 case Primitive::kPrimInt: {
2458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2460 break;
2461 }
2462 case Primitive::kPrimLong: {
2463 locations->SetInAt(0, Location::RequiresRegister());
2464 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2465 break;
2466 }
2467
2468 case Primitive::kPrimFloat:
2469 case Primitive::kPrimDouble:
2470 locations->SetInAt(0, Location::RequiresFpuRegister());
2471 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2472 break;
2473
2474 default:
2475 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2476 }
2477}
2478
2479void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2480 LocationSummary* locations = neg->GetLocations();
2481 Location out = locations->Out();
2482 Location in = locations->InAt(0);
2483 switch (neg->GetResultType()) {
2484 case Primitive::kPrimInt:
2485 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2486 break;
2487
2488 case Primitive::kPrimLong:
2489 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2490 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2491 // We cannot emit an RSC (Reverse Subtract with Carry)
2492 // instruction here, as it does not exist in the Thumb-2
2493 // instruction set. We use the following approach
2494 // using SBC and SUB instead.
2495 //
2496 // out.hi = -C
2497 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2498 // out.hi = out.hi - in.hi
2499 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2500 break;
2501
2502 case Primitive::kPrimFloat:
2503 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00002504 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2509 }
2510}
2511
Scott Wakelingfe885462016-09-22 10:24:38 +01002512void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2513 Primitive::Type result_type = conversion->GetResultType();
2514 Primitive::Type input_type = conversion->GetInputType();
2515 DCHECK_NE(result_type, input_type);
2516
2517 // The float-to-long, double-to-long and long-to-float type conversions
2518 // rely on a call to the runtime.
2519 LocationSummary::CallKind call_kind =
2520 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2521 && result_type == Primitive::kPrimLong)
2522 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2523 ? LocationSummary::kCallOnMainOnly
2524 : LocationSummary::kNoCall;
2525 LocationSummary* locations =
2526 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2527
2528 // The Java language does not allow treating boolean as an integral type but
2529 // our bit representation makes it safe.
2530
2531 switch (result_type) {
2532 case Primitive::kPrimByte:
2533 switch (input_type) {
2534 case Primitive::kPrimLong:
2535 // Type conversion from long to byte is a result of code transformations.
2536 case Primitive::kPrimBoolean:
2537 // Boolean input is a result of code transformations.
2538 case Primitive::kPrimShort:
2539 case Primitive::kPrimInt:
2540 case Primitive::kPrimChar:
2541 // Processing a Dex `int-to-byte' instruction.
2542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2544 break;
2545
2546 default:
2547 LOG(FATAL) << "Unexpected type conversion from " << input_type
2548 << " to " << result_type;
2549 }
2550 break;
2551
2552 case Primitive::kPrimShort:
2553 switch (input_type) {
2554 case Primitive::kPrimLong:
2555 // Type conversion from long to short is a result of code transformations.
2556 case Primitive::kPrimBoolean:
2557 // Boolean input is a result of code transformations.
2558 case Primitive::kPrimByte:
2559 case Primitive::kPrimInt:
2560 case Primitive::kPrimChar:
2561 // Processing a Dex `int-to-short' instruction.
2562 locations->SetInAt(0, Location::RequiresRegister());
2563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2564 break;
2565
2566 default:
2567 LOG(FATAL) << "Unexpected type conversion from " << input_type
2568 << " to " << result_type;
2569 }
2570 break;
2571
2572 case Primitive::kPrimInt:
2573 switch (input_type) {
2574 case Primitive::kPrimLong:
2575 // Processing a Dex `long-to-int' instruction.
2576 locations->SetInAt(0, Location::Any());
2577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2578 break;
2579
2580 case Primitive::kPrimFloat:
2581 // Processing a Dex `float-to-int' instruction.
2582 locations->SetInAt(0, Location::RequiresFpuRegister());
2583 locations->SetOut(Location::RequiresRegister());
2584 locations->AddTemp(Location::RequiresFpuRegister());
2585 break;
2586
2587 case Primitive::kPrimDouble:
2588 // Processing a Dex `double-to-int' instruction.
2589 locations->SetInAt(0, Location::RequiresFpuRegister());
2590 locations->SetOut(Location::RequiresRegister());
2591 locations->AddTemp(Location::RequiresFpuRegister());
2592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
2600 case Primitive::kPrimLong:
2601 switch (input_type) {
2602 case Primitive::kPrimBoolean:
2603 // Boolean input is a result of code transformations.
2604 case Primitive::kPrimByte:
2605 case Primitive::kPrimShort:
2606 case Primitive::kPrimInt:
2607 case Primitive::kPrimChar:
2608 // Processing a Dex `int-to-long' instruction.
2609 locations->SetInAt(0, Location::RequiresRegister());
2610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2611 break;
2612
2613 case Primitive::kPrimFloat: {
2614 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002615 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2616 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2617 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002618 break;
2619 }
2620
2621 case Primitive::kPrimDouble: {
2622 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002623 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2624 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2625 calling_convention.GetFpuRegisterAt(1)));
2626 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002627 break;
2628 }
2629
2630 default:
2631 LOG(FATAL) << "Unexpected type conversion from " << input_type
2632 << " to " << result_type;
2633 }
2634 break;
2635
2636 case Primitive::kPrimChar:
2637 switch (input_type) {
2638 case Primitive::kPrimLong:
2639 // Type conversion from long to char is a result of code transformations.
2640 case Primitive::kPrimBoolean:
2641 // Boolean input is a result of code transformations.
2642 case Primitive::kPrimByte:
2643 case Primitive::kPrimShort:
2644 case Primitive::kPrimInt:
2645 // Processing a Dex `int-to-char' instruction.
2646 locations->SetInAt(0, Location::RequiresRegister());
2647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2648 break;
2649
2650 default:
2651 LOG(FATAL) << "Unexpected type conversion from " << input_type
2652 << " to " << result_type;
2653 }
2654 break;
2655
2656 case Primitive::kPrimFloat:
2657 switch (input_type) {
2658 case Primitive::kPrimBoolean:
2659 // Boolean input is a result of code transformations.
2660 case Primitive::kPrimByte:
2661 case Primitive::kPrimShort:
2662 case Primitive::kPrimInt:
2663 case Primitive::kPrimChar:
2664 // Processing a Dex `int-to-float' instruction.
2665 locations->SetInAt(0, Location::RequiresRegister());
2666 locations->SetOut(Location::RequiresFpuRegister());
2667 break;
2668
2669 case Primitive::kPrimLong: {
2670 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002671 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2672 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2673 calling_convention.GetRegisterAt(1)));
2674 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002675 break;
2676 }
2677
2678 case Primitive::kPrimDouble:
2679 // Processing a Dex `double-to-float' instruction.
2680 locations->SetInAt(0, Location::RequiresFpuRegister());
2681 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 };
2688 break;
2689
2690 case Primitive::kPrimDouble:
2691 switch (input_type) {
2692 case Primitive::kPrimBoolean:
2693 // Boolean input is a result of code transformations.
2694 case Primitive::kPrimByte:
2695 case Primitive::kPrimShort:
2696 case Primitive::kPrimInt:
2697 case Primitive::kPrimChar:
2698 // Processing a Dex `int-to-double' instruction.
2699 locations->SetInAt(0, Location::RequiresRegister());
2700 locations->SetOut(Location::RequiresFpuRegister());
2701 break;
2702
2703 case Primitive::kPrimLong:
2704 // Processing a Dex `long-to-double' instruction.
2705 locations->SetInAt(0, Location::RequiresRegister());
2706 locations->SetOut(Location::RequiresFpuRegister());
2707 locations->AddTemp(Location::RequiresFpuRegister());
2708 locations->AddTemp(Location::RequiresFpuRegister());
2709 break;
2710
2711 case Primitive::kPrimFloat:
2712 // Processing a Dex `float-to-double' instruction.
2713 locations->SetInAt(0, Location::RequiresFpuRegister());
2714 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 };
2721 break;
2722
2723 default:
2724 LOG(FATAL) << "Unexpected type conversion from " << input_type
2725 << " to " << result_type;
2726 }
2727}
2728
2729void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2730 LocationSummary* locations = conversion->GetLocations();
2731 Location out = locations->Out();
2732 Location in = locations->InAt(0);
2733 Primitive::Type result_type = conversion->GetResultType();
2734 Primitive::Type input_type = conversion->GetInputType();
2735 DCHECK_NE(result_type, input_type);
2736 switch (result_type) {
2737 case Primitive::kPrimByte:
2738 switch (input_type) {
2739 case Primitive::kPrimLong:
2740 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002741 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002742 break;
2743 case Primitive::kPrimBoolean:
2744 // Boolean input is a result of code transformations.
2745 case Primitive::kPrimShort:
2746 case Primitive::kPrimInt:
2747 case Primitive::kPrimChar:
2748 // Processing a Dex `int-to-byte' instruction.
2749 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2750 break;
2751
2752 default:
2753 LOG(FATAL) << "Unexpected type conversion from " << input_type
2754 << " to " << result_type;
2755 }
2756 break;
2757
2758 case Primitive::kPrimShort:
2759 switch (input_type) {
2760 case Primitive::kPrimLong:
2761 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002762 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002763 break;
2764 case Primitive::kPrimBoolean:
2765 // Boolean input is a result of code transformations.
2766 case Primitive::kPrimByte:
2767 case Primitive::kPrimInt:
2768 case Primitive::kPrimChar:
2769 // Processing a Dex `int-to-short' instruction.
2770 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2771 break;
2772
2773 default:
2774 LOG(FATAL) << "Unexpected type conversion from " << input_type
2775 << " to " << result_type;
2776 }
2777 break;
2778
2779 case Primitive::kPrimInt:
2780 switch (input_type) {
2781 case Primitive::kPrimLong:
2782 // Processing a Dex `long-to-int' instruction.
2783 DCHECK(out.IsRegister());
2784 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002785 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002786 } else if (in.IsDoubleStackSlot()) {
2787 GetAssembler()->LoadFromOffset(kLoadWord,
2788 OutputRegister(conversion),
2789 sp,
2790 in.GetStackIndex());
2791 } else {
2792 DCHECK(in.IsConstant());
2793 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00002794 int32_t value = Int32ConstantFrom(in);
2795 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002796 }
2797 break;
2798
2799 case Primitive::kPrimFloat: {
2800 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002801 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002802 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002803 __ Vmov(OutputRegister(conversion), temp);
2804 break;
2805 }
2806
2807 case Primitive::kPrimDouble: {
2808 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002809 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002810 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002811 __ Vmov(OutputRegister(conversion), temp_s);
2812 break;
2813 }
2814
2815 default:
2816 LOG(FATAL) << "Unexpected type conversion from " << input_type
2817 << " to " << result_type;
2818 }
2819 break;
2820
2821 case Primitive::kPrimLong:
2822 switch (input_type) {
2823 case Primitive::kPrimBoolean:
2824 // Boolean input is a result of code transformations.
2825 case Primitive::kPrimByte:
2826 case Primitive::kPrimShort:
2827 case Primitive::kPrimInt:
2828 case Primitive::kPrimChar:
2829 // Processing a Dex `int-to-long' instruction.
2830 DCHECK(out.IsRegisterPair());
2831 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002832 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002833 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002834 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01002835 break;
2836
2837 case Primitive::kPrimFloat:
2838 // Processing a Dex `float-to-long' instruction.
2839 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
2840 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2841 break;
2842
2843 case Primitive::kPrimDouble:
2844 // Processing a Dex `double-to-long' instruction.
2845 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
2846 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2847 break;
2848
2849 default:
2850 LOG(FATAL) << "Unexpected type conversion from " << input_type
2851 << " to " << result_type;
2852 }
2853 break;
2854
2855 case Primitive::kPrimChar:
2856 switch (input_type) {
2857 case Primitive::kPrimLong:
2858 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002859 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002860 break;
2861 case Primitive::kPrimBoolean:
2862 // Boolean input is a result of code transformations.
2863 case Primitive::kPrimByte:
2864 case Primitive::kPrimShort:
2865 case Primitive::kPrimInt:
2866 // Processing a Dex `int-to-char' instruction.
2867 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2868 break;
2869
2870 default:
2871 LOG(FATAL) << "Unexpected type conversion from " << input_type
2872 << " to " << result_type;
2873 }
2874 break;
2875
2876 case Primitive::kPrimFloat:
2877 switch (input_type) {
2878 case Primitive::kPrimBoolean:
2879 // Boolean input is a result of code transformations.
2880 case Primitive::kPrimByte:
2881 case Primitive::kPrimShort:
2882 case Primitive::kPrimInt:
2883 case Primitive::kPrimChar: {
2884 // Processing a Dex `int-to-float' instruction.
2885 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002886 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01002887 break;
2888 }
2889
2890 case Primitive::kPrimLong:
2891 // Processing a Dex `long-to-float' instruction.
2892 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
2893 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2894 break;
2895
2896 case Primitive::kPrimDouble:
2897 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002898 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002899 break;
2900
2901 default:
2902 LOG(FATAL) << "Unexpected type conversion from " << input_type
2903 << " to " << result_type;
2904 };
2905 break;
2906
2907 case Primitive::kPrimDouble:
2908 switch (input_type) {
2909 case Primitive::kPrimBoolean:
2910 // Boolean input is a result of code transformations.
2911 case Primitive::kPrimByte:
2912 case Primitive::kPrimShort:
2913 case Primitive::kPrimInt:
2914 case Primitive::kPrimChar: {
2915 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002916 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002917 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01002918 break;
2919 }
2920
2921 case Primitive::kPrimLong: {
2922 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002923 vixl32::Register low = LowRegisterFrom(in);
2924 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002925 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002926 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002927 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002928 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002929 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002930
2931 // temp_d = int-to-double(high)
2932 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002933 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01002934 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002935 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002936 // out_d = unsigned-to-double(low)
2937 __ Vmov(out_s, low);
2938 __ Vcvt(F64, U32, out_d, out_s);
2939 // out_d += temp_d * constant_d
2940 __ Vmla(F64, out_d, temp_d, constant_d);
2941 break;
2942 }
2943
2944 case Primitive::kPrimFloat:
2945 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002946 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002947 break;
2948
2949 default:
2950 LOG(FATAL) << "Unexpected type conversion from " << input_type
2951 << " to " << result_type;
2952 };
2953 break;
2954
2955 default:
2956 LOG(FATAL) << "Unexpected type conversion from " << input_type
2957 << " to " << result_type;
2958 }
2959}
2960
2961void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2964 switch (add->GetResultType()) {
2965 case Primitive::kPrimInt: {
2966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2969 break;
2970 }
2971
Scott Wakelingfe885462016-09-22 10:24:38 +01002972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00002974 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01002975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2976 break;
2977 }
2978
2979 case Primitive::kPrimFloat:
2980 case Primitive::kPrimDouble: {
2981 locations->SetInAt(0, Location::RequiresFpuRegister());
2982 locations->SetInAt(1, Location::RequiresFpuRegister());
2983 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2984 break;
2985 }
2986
2987 default:
2988 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2989 }
2990}
2991
2992void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
2993 LocationSummary* locations = add->GetLocations();
2994 Location out = locations->Out();
2995 Location first = locations->InAt(0);
2996 Location second = locations->InAt(1);
2997
2998 switch (add->GetResultType()) {
2999 case Primitive::kPrimInt: {
3000 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3001 }
3002 break;
3003
Scott Wakelingfe885462016-09-22 10:24:38 +01003004 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003005 if (second.IsConstant()) {
3006 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3007 GenerateAddLongConst(out, first, value);
3008 } else {
3009 DCHECK(second.IsRegisterPair());
3010 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3011 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3012 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003013 break;
3014 }
3015
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003016 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003017 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003018 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003019 break;
3020
3021 default:
3022 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3023 }
3024}
3025
3026void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3027 LocationSummary* locations =
3028 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3029 switch (sub->GetResultType()) {
3030 case Primitive::kPrimInt: {
3031 locations->SetInAt(0, Location::RequiresRegister());
3032 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3034 break;
3035 }
3036
Scott Wakelingfe885462016-09-22 10:24:38 +01003037 case Primitive::kPrimLong: {
3038 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003039 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3041 break;
3042 }
3043 case Primitive::kPrimFloat:
3044 case Primitive::kPrimDouble: {
3045 locations->SetInAt(0, Location::RequiresFpuRegister());
3046 locations->SetInAt(1, Location::RequiresFpuRegister());
3047 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3048 break;
3049 }
3050 default:
3051 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3052 }
3053}
3054
3055void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3056 LocationSummary* locations = sub->GetLocations();
3057 Location out = locations->Out();
3058 Location first = locations->InAt(0);
3059 Location second = locations->InAt(1);
3060 switch (sub->GetResultType()) {
3061 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003062 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003063 break;
3064 }
3065
Scott Wakelingfe885462016-09-22 10:24:38 +01003066 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003067 if (second.IsConstant()) {
3068 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3069 GenerateAddLongConst(out, first, -value);
3070 } else {
3071 DCHECK(second.IsRegisterPair());
3072 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3073 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3074 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003075 break;
3076 }
3077
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003078 case Primitive::kPrimFloat:
3079 case Primitive::kPrimDouble:
3080 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003081 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003082
3083 default:
3084 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3085 }
3086}
3087
3088void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3089 LocationSummary* locations =
3090 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3091 switch (mul->GetResultType()) {
3092 case Primitive::kPrimInt:
3093 case Primitive::kPrimLong: {
3094 locations->SetInAt(0, Location::RequiresRegister());
3095 locations->SetInAt(1, Location::RequiresRegister());
3096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3097 break;
3098 }
3099
3100 case Primitive::kPrimFloat:
3101 case Primitive::kPrimDouble: {
3102 locations->SetInAt(0, Location::RequiresFpuRegister());
3103 locations->SetInAt(1, Location::RequiresFpuRegister());
3104 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3105 break;
3106 }
3107
3108 default:
3109 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3110 }
3111}
3112
3113void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3114 LocationSummary* locations = mul->GetLocations();
3115 Location out = locations->Out();
3116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
3118 switch (mul->GetResultType()) {
3119 case Primitive::kPrimInt: {
3120 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3121 break;
3122 }
3123 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003124 vixl32::Register out_hi = HighRegisterFrom(out);
3125 vixl32::Register out_lo = LowRegisterFrom(out);
3126 vixl32::Register in1_hi = HighRegisterFrom(first);
3127 vixl32::Register in1_lo = LowRegisterFrom(first);
3128 vixl32::Register in2_hi = HighRegisterFrom(second);
3129 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003130
3131 // Extra checks to protect caused by the existence of R1_R2.
3132 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3133 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003134 DCHECK(!out_hi.Is(in1_lo));
3135 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003136
3137 // input: in1 - 64 bits, in2 - 64 bits
3138 // output: out
3139 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3140 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3141 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3142
3143 UseScratchRegisterScope temps(GetVIXLAssembler());
3144 vixl32::Register temp = temps.Acquire();
3145 // temp <- in1.lo * in2.hi
3146 __ Mul(temp, in1_lo, in2_hi);
3147 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3148 __ Mla(out_hi, in1_hi, in2_lo, temp);
3149 // out.lo <- (in1.lo * in2.lo)[31:0];
3150 __ Umull(out_lo, temp, in1_lo, in2_lo);
3151 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003152 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003153 break;
3154 }
3155
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003156 case Primitive::kPrimFloat:
3157 case Primitive::kPrimDouble:
3158 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003159 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003160
3161 default:
3162 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3163 }
3164}
3165
Scott Wakelingfe885462016-09-22 10:24:38 +01003166void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3167 DCHECK(instruction->IsDiv() || instruction->IsRem());
3168 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3169
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003170 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003171 DCHECK(second.IsConstant());
3172
3173 vixl32::Register out = OutputRegister(instruction);
3174 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003175 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003176 DCHECK(imm == 1 || imm == -1);
3177
3178 if (instruction->IsRem()) {
3179 __ Mov(out, 0);
3180 } else {
3181 if (imm == 1) {
3182 __ Mov(out, dividend);
3183 } else {
3184 __ Rsb(out, dividend, 0);
3185 }
3186 }
3187}
3188
3189void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3190 DCHECK(instruction->IsDiv() || instruction->IsRem());
3191 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3192
3193 LocationSummary* locations = instruction->GetLocations();
3194 Location second = locations->InAt(1);
3195 DCHECK(second.IsConstant());
3196
3197 vixl32::Register out = OutputRegister(instruction);
3198 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003199 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003200 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003201 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3202 int ctz_imm = CTZ(abs_imm);
3203
3204 if (ctz_imm == 1) {
3205 __ Lsr(temp, dividend, 32 - ctz_imm);
3206 } else {
3207 __ Asr(temp, dividend, 31);
3208 __ Lsr(temp, temp, 32 - ctz_imm);
3209 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003210 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003211
3212 if (instruction->IsDiv()) {
3213 __ Asr(out, out, ctz_imm);
3214 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003215 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003216 }
3217 } else {
3218 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003219 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003220 }
3221}
3222
3223void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3224 DCHECK(instruction->IsDiv() || instruction->IsRem());
3225 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3226
3227 LocationSummary* locations = instruction->GetLocations();
3228 Location second = locations->InAt(1);
3229 DCHECK(second.IsConstant());
3230
3231 vixl32::Register out = OutputRegister(instruction);
3232 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003233 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3234 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003235 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003236
3237 int64_t magic;
3238 int shift;
3239 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3240
Anton Kirilovdda43962016-11-21 19:55:20 +00003241 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3242 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003243 __ Smull(temp2, temp1, dividend, temp1);
3244
3245 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003246 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003247 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003248 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003249 }
3250
3251 if (shift != 0) {
3252 __ Asr(temp1, temp1, shift);
3253 }
3254
3255 if (instruction->IsDiv()) {
3256 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3257 } else {
3258 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3259 // TODO: Strength reduction for mls.
3260 __ Mov(temp2, imm);
3261 __ Mls(out, temp1, temp2, dividend);
3262 }
3263}
3264
3265void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3266 HBinaryOperation* instruction) {
3267 DCHECK(instruction->IsDiv() || instruction->IsRem());
3268 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3269
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003270 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003271 DCHECK(second.IsConstant());
3272
Anton Kirilov644032c2016-12-06 17:51:43 +00003273 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003274 if (imm == 0) {
3275 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3276 } else if (imm == 1 || imm == -1) {
3277 DivRemOneOrMinusOne(instruction);
3278 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3279 DivRemByPowerOfTwo(instruction);
3280 } else {
3281 DCHECK(imm <= -2 || imm >= 2);
3282 GenerateDivRemWithAnyConstant(instruction);
3283 }
3284}
3285
3286void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3287 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3288 if (div->GetResultType() == Primitive::kPrimLong) {
3289 // pLdiv runtime call.
3290 call_kind = LocationSummary::kCallOnMainOnly;
3291 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3292 // sdiv will be replaced by other instruction sequence.
3293 } else if (div->GetResultType() == Primitive::kPrimInt &&
3294 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3295 // pIdivmod runtime call.
3296 call_kind = LocationSummary::kCallOnMainOnly;
3297 }
3298
3299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3300
3301 switch (div->GetResultType()) {
3302 case Primitive::kPrimInt: {
3303 if (div->InputAt(1)->IsConstant()) {
3304 locations->SetInAt(0, Location::RequiresRegister());
3305 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003307 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003308 if (value == 1 || value == 0 || value == -1) {
3309 // No temp register required.
3310 } else {
3311 locations->AddTemp(Location::RequiresRegister());
3312 if (!IsPowerOfTwo(AbsOrMin(value))) {
3313 locations->AddTemp(Location::RequiresRegister());
3314 }
3315 }
3316 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3317 locations->SetInAt(0, Location::RequiresRegister());
3318 locations->SetInAt(1, Location::RequiresRegister());
3319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3320 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003321 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3322 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3323 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3324 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3325 // we only need the former.
3326 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003327 }
3328 break;
3329 }
3330 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003331 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3332 locations->SetInAt(0, LocationFrom(
3333 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3334 locations->SetInAt(1, LocationFrom(
3335 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3336 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003337 break;
3338 }
3339 case Primitive::kPrimFloat:
3340 case Primitive::kPrimDouble: {
3341 locations->SetInAt(0, Location::RequiresFpuRegister());
3342 locations->SetInAt(1, Location::RequiresFpuRegister());
3343 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3344 break;
3345 }
3346
3347 default:
3348 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3349 }
3350}
3351
3352void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003353 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003354 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003355
3356 switch (div->GetResultType()) {
3357 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003358 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003359 GenerateDivRemConstantIntegral(div);
3360 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3361 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3362 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003363 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3364 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3365 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3366 DCHECK(r0.Is(OutputRegister(div)));
3367
3368 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3369 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003370 }
3371 break;
3372 }
3373
3374 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003375 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3376 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3377 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3378 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3379 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3380 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3381 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3382
3383 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3384 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003385 break;
3386 }
3387
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003388 case Primitive::kPrimFloat:
3389 case Primitive::kPrimDouble:
3390 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003391 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003392
3393 default:
3394 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3395 }
3396}
3397
Artem Serov551b28f2016-10-18 19:11:30 +01003398void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3399 Primitive::Type type = rem->GetResultType();
3400
3401 // Most remainders are implemented in the runtime.
3402 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3403 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3404 // sdiv will be replaced by other instruction sequence.
3405 call_kind = LocationSummary::kNoCall;
3406 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3407 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3408 // Have hardware divide instruction for int, do it with three instructions.
3409 call_kind = LocationSummary::kNoCall;
3410 }
3411
3412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3413
3414 switch (type) {
3415 case Primitive::kPrimInt: {
3416 if (rem->InputAt(1)->IsConstant()) {
3417 locations->SetInAt(0, Location::RequiresRegister());
3418 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003420 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01003421 if (value == 1 || value == 0 || value == -1) {
3422 // No temp register required.
3423 } else {
3424 locations->AddTemp(Location::RequiresRegister());
3425 if (!IsPowerOfTwo(AbsOrMin(value))) {
3426 locations->AddTemp(Location::RequiresRegister());
3427 }
3428 }
3429 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3430 locations->SetInAt(0, Location::RequiresRegister());
3431 locations->SetInAt(1, Location::RequiresRegister());
3432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3433 locations->AddTemp(Location::RequiresRegister());
3434 } else {
3435 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3436 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3437 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3438 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3439 // we only need the latter.
3440 locations->SetOut(LocationFrom(r1));
3441 }
3442 break;
3443 }
3444 case Primitive::kPrimLong: {
3445 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3446 locations->SetInAt(0, LocationFrom(
3447 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3448 locations->SetInAt(1, LocationFrom(
3449 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3450 // The runtime helper puts the output in R2,R3.
3451 locations->SetOut(LocationFrom(r2, r3));
3452 break;
3453 }
3454 case Primitive::kPrimFloat: {
3455 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3456 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3457 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3458 locations->SetOut(LocationFrom(s0));
3459 break;
3460 }
3461
3462 case Primitive::kPrimDouble: {
3463 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3464 locations->SetInAt(0, LocationFrom(
3465 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3466 locations->SetInAt(1, LocationFrom(
3467 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3468 locations->SetOut(LocationFrom(s0, s1));
3469 break;
3470 }
3471
3472 default:
3473 LOG(FATAL) << "Unexpected rem type " << type;
3474 }
3475}
3476
3477void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3478 LocationSummary* locations = rem->GetLocations();
3479 Location second = locations->InAt(1);
3480
3481 Primitive::Type type = rem->GetResultType();
3482 switch (type) {
3483 case Primitive::kPrimInt: {
3484 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3485 vixl32::Register out_reg = OutputRegister(rem);
3486 if (second.IsConstant()) {
3487 GenerateDivRemConstantIntegral(rem);
3488 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3489 vixl32::Register reg2 = RegisterFrom(second);
3490 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3491
3492 // temp = reg1 / reg2 (integer division)
3493 // dest = reg1 - temp * reg2
3494 __ Sdiv(temp, reg1, reg2);
3495 __ Mls(out_reg, temp, reg2, reg1);
3496 } else {
3497 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3498 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3499 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3500 DCHECK(out_reg.Is(r1));
3501
3502 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3503 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3504 }
3505 break;
3506 }
3507
3508 case Primitive::kPrimLong: {
3509 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3510 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3511 break;
3512 }
3513
3514 case Primitive::kPrimFloat: {
3515 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3516 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3517 break;
3518 }
3519
3520 case Primitive::kPrimDouble: {
3521 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3522 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3523 break;
3524 }
3525
3526 default:
3527 LOG(FATAL) << "Unexpected rem type " << type;
3528 }
3529}
3530
3531
Scott Wakelingfe885462016-09-22 10:24:38 +01003532void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003533 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003534 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003535}
3536
3537void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3538 DivZeroCheckSlowPathARMVIXL* slow_path =
3539 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3540 codegen_->AddSlowPath(slow_path);
3541
3542 LocationSummary* locations = instruction->GetLocations();
3543 Location value = locations->InAt(0);
3544
3545 switch (instruction->GetType()) {
3546 case Primitive::kPrimBoolean:
3547 case Primitive::kPrimByte:
3548 case Primitive::kPrimChar:
3549 case Primitive::kPrimShort:
3550 case Primitive::kPrimInt: {
3551 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003552 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003553 } else {
3554 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003555 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003556 __ B(slow_path->GetEntryLabel());
3557 }
3558 }
3559 break;
3560 }
3561 case Primitive::kPrimLong: {
3562 if (value.IsRegisterPair()) {
3563 UseScratchRegisterScope temps(GetVIXLAssembler());
3564 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003565 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003566 __ B(eq, slow_path->GetEntryLabel());
3567 } else {
3568 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003569 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003570 __ B(slow_path->GetEntryLabel());
3571 }
3572 }
3573 break;
3574 }
3575 default:
3576 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3577 }
3578}
3579
Artem Serov02109dd2016-09-23 17:17:54 +01003580void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3581 LocationSummary* locations = ror->GetLocations();
3582 vixl32::Register in = InputRegisterAt(ror, 0);
3583 Location rhs = locations->InAt(1);
3584 vixl32::Register out = OutputRegister(ror);
3585
3586 if (rhs.IsConstant()) {
3587 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3588 // so map all rotations to a +ve. equivalent in that range.
3589 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3590 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3591 if (rot) {
3592 // Rotate, mapping left rotations to right equivalents if necessary.
3593 // (e.g. left by 2 bits == right by 30.)
3594 __ Ror(out, in, rot);
3595 } else if (!out.Is(in)) {
3596 __ Mov(out, in);
3597 }
3598 } else {
3599 __ Ror(out, in, RegisterFrom(rhs));
3600 }
3601}
3602
3603// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3604// rotates by swapping input regs (effectively rotating by the first 32-bits of
3605// a larger rotation) or flipping direction (thus treating larger right/left
3606// rotations as sub-word sized rotations in the other direction) as appropriate.
3607void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3608 LocationSummary* locations = ror->GetLocations();
3609 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3610 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3611 Location rhs = locations->InAt(1);
3612 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3613 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3614
3615 if (rhs.IsConstant()) {
3616 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3617 // Map all rotations to +ve. equivalents on the interval [0,63].
3618 rot &= kMaxLongShiftDistance;
3619 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3620 // logic below to a simple pair of binary orr.
3621 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3622 if (rot >= kArmBitsPerWord) {
3623 rot -= kArmBitsPerWord;
3624 std::swap(in_reg_hi, in_reg_lo);
3625 }
3626 // Rotate, or mov to out for zero or word size rotations.
3627 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003628 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003629 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003630 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003631 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3632 } else {
3633 __ Mov(out_reg_lo, in_reg_lo);
3634 __ Mov(out_reg_hi, in_reg_hi);
3635 }
3636 } else {
3637 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3638 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3639 vixl32::Label end;
3640 vixl32::Label shift_by_32_plus_shift_right;
3641
3642 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3643 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003644 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov02109dd2016-09-23 17:17:54 +01003645 __ B(cc, &shift_by_32_plus_shift_right);
3646
3647 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3648 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3649 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3650 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3651 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3652 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3653 __ Lsr(shift_left, in_reg_hi, shift_right);
3654 __ Add(out_reg_lo, out_reg_lo, shift_left);
3655 __ B(&end);
3656
3657 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3658 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3659 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3660 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3661 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3662 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3663 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3664 __ Lsl(shift_right, in_reg_hi, shift_left);
3665 __ Add(out_reg_lo, out_reg_lo, shift_right);
3666
3667 __ Bind(&end);
3668 }
3669}
3670
3671void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3674 switch (ror->GetResultType()) {
3675 case Primitive::kPrimInt: {
3676 locations->SetInAt(0, Location::RequiresRegister());
3677 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3679 break;
3680 }
3681 case Primitive::kPrimLong: {
3682 locations->SetInAt(0, Location::RequiresRegister());
3683 if (ror->InputAt(1)->IsConstant()) {
3684 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3685 } else {
3686 locations->SetInAt(1, Location::RequiresRegister());
3687 locations->AddTemp(Location::RequiresRegister());
3688 locations->AddTemp(Location::RequiresRegister());
3689 }
3690 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3691 break;
3692 }
3693 default:
3694 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3695 }
3696}
3697
3698void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3699 Primitive::Type type = ror->GetResultType();
3700 switch (type) {
3701 case Primitive::kPrimInt: {
3702 HandleIntegerRotate(ror);
3703 break;
3704 }
3705 case Primitive::kPrimLong: {
3706 HandleLongRotate(ror);
3707 break;
3708 }
3709 default:
3710 LOG(FATAL) << "Unexpected operation type " << type;
3711 UNREACHABLE();
3712 }
3713}
3714
Artem Serov02d37832016-10-25 15:25:33 +01003715void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3716 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3717
3718 LocationSummary* locations =
3719 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3720
3721 switch (op->GetResultType()) {
3722 case Primitive::kPrimInt: {
3723 locations->SetInAt(0, Location::RequiresRegister());
3724 if (op->InputAt(1)->IsConstant()) {
3725 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3726 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3727 } else {
3728 locations->SetInAt(1, Location::RequiresRegister());
3729 // Make the output overlap, as it will be used to hold the masked
3730 // second input.
3731 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3732 }
3733 break;
3734 }
3735 case Primitive::kPrimLong: {
3736 locations->SetInAt(0, Location::RequiresRegister());
3737 if (op->InputAt(1)->IsConstant()) {
3738 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3739 // For simplicity, use kOutputOverlap even though we only require that low registers
3740 // don't clash with high registers which the register allocator currently guarantees.
3741 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3742 } else {
3743 locations->SetInAt(1, Location::RequiresRegister());
3744 locations->AddTemp(Location::RequiresRegister());
3745 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3746 }
3747 break;
3748 }
3749 default:
3750 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3751 }
3752}
3753
3754void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3755 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3756
3757 LocationSummary* locations = op->GetLocations();
3758 Location out = locations->Out();
3759 Location first = locations->InAt(0);
3760 Location second = locations->InAt(1);
3761
3762 Primitive::Type type = op->GetResultType();
3763 switch (type) {
3764 case Primitive::kPrimInt: {
3765 vixl32::Register out_reg = OutputRegister(op);
3766 vixl32::Register first_reg = InputRegisterAt(op, 0);
3767 if (second.IsRegister()) {
3768 vixl32::Register second_reg = RegisterFrom(second);
3769 // ARM doesn't mask the shift count so we need to do it ourselves.
3770 __ And(out_reg, second_reg, kMaxIntShiftDistance);
3771 if (op->IsShl()) {
3772 __ Lsl(out_reg, first_reg, out_reg);
3773 } else if (op->IsShr()) {
3774 __ Asr(out_reg, first_reg, out_reg);
3775 } else {
3776 __ Lsr(out_reg, first_reg, out_reg);
3777 }
3778 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00003779 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003780 uint32_t shift_value = cst & kMaxIntShiftDistance;
3781 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
3782 __ Mov(out_reg, first_reg);
3783 } else if (op->IsShl()) {
3784 __ Lsl(out_reg, first_reg, shift_value);
3785 } else if (op->IsShr()) {
3786 __ Asr(out_reg, first_reg, shift_value);
3787 } else {
3788 __ Lsr(out_reg, first_reg, shift_value);
3789 }
3790 }
3791 break;
3792 }
3793 case Primitive::kPrimLong: {
3794 vixl32::Register o_h = HighRegisterFrom(out);
3795 vixl32::Register o_l = LowRegisterFrom(out);
3796
3797 vixl32::Register high = HighRegisterFrom(first);
3798 vixl32::Register low = LowRegisterFrom(first);
3799
3800 if (second.IsRegister()) {
3801 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3802
3803 vixl32::Register second_reg = RegisterFrom(second);
3804
3805 if (op->IsShl()) {
3806 __ And(o_l, second_reg, kMaxLongShiftDistance);
3807 // Shift the high part
3808 __ Lsl(o_h, high, o_l);
3809 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003810 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003811 __ Lsr(temp, low, temp);
3812 __ Orr(o_h, o_h, temp);
3813 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003814 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003815 {
Artem Serov0fb37192016-12-06 18:13:40 +00003816 ExactAssemblyScope guard(GetVIXLAssembler(),
3817 2 * vixl32::kMaxInstructionSizeInBytes,
3818 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003819 __ it(pl);
3820 __ lsl(pl, o_h, low, temp);
3821 }
3822 // Shift the low part
3823 __ Lsl(o_l, low, o_l);
3824 } else if (op->IsShr()) {
3825 __ And(o_h, second_reg, kMaxLongShiftDistance);
3826 // Shift the low part
3827 __ Lsr(o_l, low, o_h);
3828 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003829 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003830 __ Lsl(temp, high, temp);
3831 __ Orr(o_l, o_l, temp);
3832 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003833 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003834 {
Artem Serov0fb37192016-12-06 18:13:40 +00003835 ExactAssemblyScope guard(GetVIXLAssembler(),
3836 2 * vixl32::kMaxInstructionSizeInBytes,
3837 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003838 __ it(pl);
3839 __ asr(pl, o_l, high, temp);
3840 }
3841 // Shift the high part
3842 __ Asr(o_h, high, o_h);
3843 } else {
3844 __ And(o_h, second_reg, kMaxLongShiftDistance);
3845 // same as Shr except we use `Lsr`s and not `Asr`s
3846 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003847 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003848 __ Lsl(temp, high, temp);
3849 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003850 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003851 {
Artem Serov0fb37192016-12-06 18:13:40 +00003852 ExactAssemblyScope guard(GetVIXLAssembler(),
3853 2 * vixl32::kMaxInstructionSizeInBytes,
3854 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003855 __ it(pl);
3856 __ lsr(pl, o_l, high, temp);
3857 }
3858 __ Lsr(o_h, high, o_h);
3859 }
3860 } else {
3861 // Register allocator doesn't create partial overlap.
3862 DCHECK(!o_l.Is(high));
3863 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00003864 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003865 uint32_t shift_value = cst & kMaxLongShiftDistance;
3866 if (shift_value > 32) {
3867 if (op->IsShl()) {
3868 __ Lsl(o_h, low, shift_value - 32);
3869 __ Mov(o_l, 0);
3870 } else if (op->IsShr()) {
3871 __ Asr(o_l, high, shift_value - 32);
3872 __ Asr(o_h, high, 31);
3873 } else {
3874 __ Lsr(o_l, high, shift_value - 32);
3875 __ Mov(o_h, 0);
3876 }
3877 } else if (shift_value == 32) {
3878 if (op->IsShl()) {
3879 __ Mov(o_h, low);
3880 __ Mov(o_l, 0);
3881 } else if (op->IsShr()) {
3882 __ Mov(o_l, high);
3883 __ Asr(o_h, high, 31);
3884 } else {
3885 __ Mov(o_l, high);
3886 __ Mov(o_h, 0);
3887 }
3888 } else if (shift_value == 1) {
3889 if (op->IsShl()) {
3890 __ Lsls(o_l, low, 1);
3891 __ Adc(o_h, high, high);
3892 } else if (op->IsShr()) {
3893 __ Asrs(o_h, high, 1);
3894 __ Rrx(o_l, low);
3895 } else {
3896 __ Lsrs(o_h, high, 1);
3897 __ Rrx(o_l, low);
3898 }
3899 } else {
3900 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3901 if (op->IsShl()) {
3902 __ Lsl(o_h, high, shift_value);
3903 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
3904 __ Lsl(o_l, low, shift_value);
3905 } else if (op->IsShr()) {
3906 __ Lsr(o_l, low, shift_value);
3907 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3908 __ Asr(o_h, high, shift_value);
3909 } else {
3910 __ Lsr(o_l, low, shift_value);
3911 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3912 __ Lsr(o_h, high, shift_value);
3913 }
3914 }
3915 }
3916 break;
3917 }
3918 default:
3919 LOG(FATAL) << "Unexpected operation type " << type;
3920 UNREACHABLE();
3921 }
3922}
3923
3924void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
3925 HandleShift(shl);
3926}
3927
3928void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
3929 HandleShift(shl);
3930}
3931
3932void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
3933 HandleShift(shr);
3934}
3935
3936void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
3937 HandleShift(shr);
3938}
3939
3940void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
3941 HandleShift(ushr);
3942}
3943
3944void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
3945 HandleShift(ushr);
3946}
3947
3948void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3949 LocationSummary* locations =
3950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3951 if (instruction->IsStringAlloc()) {
3952 locations->AddTemp(LocationFrom(kMethodRegister));
3953 } else {
3954 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3955 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3956 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
3957 }
3958 locations->SetOut(LocationFrom(r0));
3959}
3960
3961void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3962 // Note: if heap poisoning is enabled, the entry point takes cares
3963 // of poisoning the reference.
3964 if (instruction->IsStringAlloc()) {
3965 // String is allocated through StringFactory. Call NewEmptyString entry point.
3966 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
3967 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3968 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
3969 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003970 // 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 +00003971 ExactAssemblyScope aas(GetVIXLAssembler(),
3972 vixl32::k16BitT32InstructionSizeInBytes,
3973 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01003974 __ blx(lr);
3975 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3976 } else {
3977 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3978 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3979 }
3980}
3981
3982void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
3983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3985 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3986 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
3987 locations->SetOut(LocationFrom(r0));
3988 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
3989 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
3990}
3991
3992void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
3993 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08003994 __ Mov(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01003995 // Note: if heap poisoning is enabled, the entry point takes cares
3996 // of poisoning the reference.
3997 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
3998 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3999}
4000
4001void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4004 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4005 if (location.IsStackSlot()) {
4006 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4007 } else if (location.IsDoubleStackSlot()) {
4008 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4009 }
4010 locations->SetOut(location);
4011}
4012
4013void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4014 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4015 // Nothing to do, the parameter is already at its location.
4016}
4017
4018void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4019 LocationSummary* locations =
4020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4021 locations->SetOut(LocationFrom(kMethodRegister));
4022}
4023
4024void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4025 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4026 // Nothing to do, the method is already at its location.
4027}
4028
4029void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4032 locations->SetInAt(0, Location::RequiresRegister());
4033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4034}
4035
4036void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4037 LocationSummary* locations = not_->GetLocations();
4038 Location out = locations->Out();
4039 Location in = locations->InAt(0);
4040 switch (not_->GetResultType()) {
4041 case Primitive::kPrimInt:
4042 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4043 break;
4044
4045 case Primitive::kPrimLong:
4046 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4047 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4048 break;
4049
4050 default:
4051 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4052 }
4053}
4054
Scott Wakelingc34dba72016-10-03 10:14:44 +01004055void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4056 LocationSummary* locations =
4057 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4060}
4061
4062void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4063 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4064}
4065
Artem Serov02d37832016-10-25 15:25:33 +01004066void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4069 switch (compare->InputAt(0)->GetType()) {
4070 case Primitive::kPrimBoolean:
4071 case Primitive::kPrimByte:
4072 case Primitive::kPrimShort:
4073 case Primitive::kPrimChar:
4074 case Primitive::kPrimInt:
4075 case Primitive::kPrimLong: {
4076 locations->SetInAt(0, Location::RequiresRegister());
4077 locations->SetInAt(1, Location::RequiresRegister());
4078 // Output overlaps because it is written before doing the low comparison.
4079 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4080 break;
4081 }
4082 case Primitive::kPrimFloat:
4083 case Primitive::kPrimDouble: {
4084 locations->SetInAt(0, Location::RequiresFpuRegister());
4085 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4086 locations->SetOut(Location::RequiresRegister());
4087 break;
4088 }
4089 default:
4090 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4091 }
4092}
4093
4094void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4095 LocationSummary* locations = compare->GetLocations();
4096 vixl32::Register out = OutputRegister(compare);
4097 Location left = locations->InAt(0);
4098 Location right = locations->InAt(1);
4099
4100 vixl32::Label less, greater, done;
4101 Primitive::Type type = compare->InputAt(0)->GetType();
4102 vixl32::Condition less_cond = vixl32::Condition(kNone);
4103 switch (type) {
4104 case Primitive::kPrimBoolean:
4105 case Primitive::kPrimByte:
4106 case Primitive::kPrimShort:
4107 case Primitive::kPrimChar:
4108 case Primitive::kPrimInt: {
4109 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4110 __ Mov(out, 0);
4111 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4112 less_cond = lt;
4113 break;
4114 }
4115 case Primitive::kPrimLong: {
4116 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
4117 __ B(lt, &less);
4118 __ B(gt, &greater);
4119 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4120 __ Mov(out, 0);
4121 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4122 less_cond = lo;
4123 break;
4124 }
4125 case Primitive::kPrimFloat:
4126 case Primitive::kPrimDouble: {
4127 __ Mov(out, 0);
4128 GenerateVcmp(compare);
4129 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4130 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4131 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4132 break;
4133 }
4134 default:
4135 LOG(FATAL) << "Unexpected compare type " << type;
4136 UNREACHABLE();
4137 }
4138
4139 __ B(eq, &done);
4140 __ B(less_cond, &less);
4141
4142 __ Bind(&greater);
4143 __ Mov(out, 1);
4144 __ B(&done);
4145
4146 __ Bind(&less);
4147 __ Mov(out, -1);
4148
4149 __ Bind(&done);
4150}
4151
4152void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4153 LocationSummary* locations =
4154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4155 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4156 locations->SetInAt(i, Location::Any());
4157 }
4158 locations->SetOut(Location::Any());
4159}
4160
4161void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4162 LOG(FATAL) << "Unreachable";
4163}
4164
4165void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4166 // TODO (ported from quick): revisit ARM barrier kinds.
4167 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4168 switch (kind) {
4169 case MemBarrierKind::kAnyStore:
4170 case MemBarrierKind::kLoadAny:
4171 case MemBarrierKind::kAnyAny: {
4172 flavor = DmbOptions::ISH;
4173 break;
4174 }
4175 case MemBarrierKind::kStoreStore: {
4176 flavor = DmbOptions::ISHST;
4177 break;
4178 }
4179 default:
4180 LOG(FATAL) << "Unexpected memory barrier " << kind;
4181 }
4182 __ Dmb(flavor);
4183}
4184
4185void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4186 uint32_t offset,
4187 vixl32::Register out_lo,
4188 vixl32::Register out_hi) {
4189 UseScratchRegisterScope temps(GetVIXLAssembler());
4190 if (offset != 0) {
4191 vixl32::Register temp = temps.Acquire();
4192 __ Add(temp, addr, offset);
4193 addr = temp;
4194 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004195 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004196}
4197
4198void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4199 uint32_t offset,
4200 vixl32::Register value_lo,
4201 vixl32::Register value_hi,
4202 vixl32::Register temp1,
4203 vixl32::Register temp2,
4204 HInstruction* instruction) {
4205 UseScratchRegisterScope temps(GetVIXLAssembler());
4206 vixl32::Label fail;
4207 if (offset != 0) {
4208 vixl32::Register temp = temps.Acquire();
4209 __ Add(temp, addr, offset);
4210 addr = temp;
4211 }
4212 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004213 {
4214 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004215 ExactAssemblyScope aas(GetVIXLAssembler(),
4216 vixl32::kMaxInstructionSizeInBytes,
4217 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004218 // We need a load followed by store. (The address used in a STREX instruction must
4219 // be the same as the address in the most recently executed LDREX instruction.)
4220 __ ldrexd(temp1, temp2, MemOperand(addr));
4221 codegen_->MaybeRecordImplicitNullCheck(instruction);
4222 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004223 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004224 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004225}
Artem Serov02109dd2016-09-23 17:17:54 +01004226
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004227void LocationsBuilderARMVIXL::HandleFieldSet(
4228 HInstruction* instruction, const FieldInfo& field_info) {
4229 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4230
4231 LocationSummary* locations =
4232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4233 locations->SetInAt(0, Location::RequiresRegister());
4234
4235 Primitive::Type field_type = field_info.GetFieldType();
4236 if (Primitive::IsFloatingPointType(field_type)) {
4237 locations->SetInAt(1, Location::RequiresFpuRegister());
4238 } else {
4239 locations->SetInAt(1, Location::RequiresRegister());
4240 }
4241
4242 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4243 bool generate_volatile = field_info.IsVolatile()
4244 && is_wide
4245 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4246 bool needs_write_barrier =
4247 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4248 // Temporary registers for the write barrier.
4249 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4250 if (needs_write_barrier) {
4251 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4252 locations->AddTemp(Location::RequiresRegister());
4253 } else if (generate_volatile) {
4254 // ARM encoding have some additional constraints for ldrexd/strexd:
4255 // - registers need to be consecutive
4256 // - the first register should be even but not R14.
4257 // We don't test for ARM yet, and the assertion makes sure that we
4258 // revisit this if we ever enable ARM encoding.
4259 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4260
4261 locations->AddTemp(Location::RequiresRegister());
4262 locations->AddTemp(Location::RequiresRegister());
4263 if (field_type == Primitive::kPrimDouble) {
4264 // For doubles we need two more registers to copy the value.
4265 locations->AddTemp(LocationFrom(r2));
4266 locations->AddTemp(LocationFrom(r3));
4267 }
4268 }
4269}
4270
4271void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4272 const FieldInfo& field_info,
4273 bool value_can_be_null) {
4274 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4275
4276 LocationSummary* locations = instruction->GetLocations();
4277 vixl32::Register base = InputRegisterAt(instruction, 0);
4278 Location value = locations->InAt(1);
4279
4280 bool is_volatile = field_info.IsVolatile();
4281 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4282 Primitive::Type field_type = field_info.GetFieldType();
4283 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4284 bool needs_write_barrier =
4285 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4286
4287 if (is_volatile) {
4288 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4289 }
4290
4291 switch (field_type) {
4292 case Primitive::kPrimBoolean:
4293 case Primitive::kPrimByte: {
4294 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4295 break;
4296 }
4297
4298 case Primitive::kPrimShort:
4299 case Primitive::kPrimChar: {
4300 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4301 break;
4302 }
4303
4304 case Primitive::kPrimInt:
4305 case Primitive::kPrimNot: {
4306 if (kPoisonHeapReferences && needs_write_barrier) {
4307 // Note that in the case where `value` is a null reference,
4308 // we do not enter this block, as a null reference does not
4309 // need poisoning.
4310 DCHECK_EQ(field_type, Primitive::kPrimNot);
4311 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4312 __ Mov(temp, RegisterFrom(value));
4313 GetAssembler()->PoisonHeapReference(temp);
4314 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4315 } else {
4316 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4317 }
4318 break;
4319 }
4320
4321 case Primitive::kPrimLong: {
4322 if (is_volatile && !atomic_ldrd_strd) {
4323 GenerateWideAtomicStore(base,
4324 offset,
4325 LowRegisterFrom(value),
4326 HighRegisterFrom(value),
4327 RegisterFrom(locations->GetTemp(0)),
4328 RegisterFrom(locations->GetTemp(1)),
4329 instruction);
4330 } else {
4331 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4332 codegen_->MaybeRecordImplicitNullCheck(instruction);
4333 }
4334 break;
4335 }
4336
4337 case Primitive::kPrimFloat: {
4338 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4339 break;
4340 }
4341
4342 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004343 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004344 if (is_volatile && !atomic_ldrd_strd) {
4345 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4346 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4347
4348 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4349
4350 GenerateWideAtomicStore(base,
4351 offset,
4352 value_reg_lo,
4353 value_reg_hi,
4354 RegisterFrom(locations->GetTemp(2)),
4355 RegisterFrom(locations->GetTemp(3)),
4356 instruction);
4357 } else {
4358 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4359 codegen_->MaybeRecordImplicitNullCheck(instruction);
4360 }
4361 break;
4362 }
4363
4364 case Primitive::kPrimVoid:
4365 LOG(FATAL) << "Unreachable type " << field_type;
4366 UNREACHABLE();
4367 }
4368
4369 // Longs and doubles are handled in the switch.
4370 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004371 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4372 // should use a scope and the assembler to emit the store instruction to guarantee that we
4373 // record the pc at the correct position. But the `Assembler` does not automatically handle
4374 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4375 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004376 codegen_->MaybeRecordImplicitNullCheck(instruction);
4377 }
4378
4379 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4380 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4381 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4382 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4383 }
4384
4385 if (is_volatile) {
4386 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4387 }
4388}
4389
Artem Serov02d37832016-10-25 15:25:33 +01004390void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4391 const FieldInfo& field_info) {
4392 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4393
4394 bool object_field_get_with_read_barrier =
4395 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4396 LocationSummary* locations =
4397 new (GetGraph()->GetArena()) LocationSummary(instruction,
4398 object_field_get_with_read_barrier ?
4399 LocationSummary::kCallOnSlowPath :
4400 LocationSummary::kNoCall);
4401 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4402 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4403 }
4404 locations->SetInAt(0, Location::RequiresRegister());
4405
4406 bool volatile_for_double = field_info.IsVolatile()
4407 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4408 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4409 // The output overlaps in case of volatile long: we don't want the
4410 // code generated by GenerateWideAtomicLoad to overwrite the
4411 // object's location. Likewise, in the case of an object field get
4412 // with read barriers enabled, we do not want the load to overwrite
4413 // the object's location, as we need it to emit the read barrier.
4414 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4415 object_field_get_with_read_barrier;
4416
4417 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4418 locations->SetOut(Location::RequiresFpuRegister());
4419 } else {
4420 locations->SetOut(Location::RequiresRegister(),
4421 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4422 }
4423 if (volatile_for_double) {
4424 // ARM encoding have some additional constraints for ldrexd/strexd:
4425 // - registers need to be consecutive
4426 // - the first register should be even but not R14.
4427 // We don't test for ARM yet, and the assertion makes sure that we
4428 // revisit this if we ever enable ARM encoding.
4429 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4430 locations->AddTemp(Location::RequiresRegister());
4431 locations->AddTemp(Location::RequiresRegister());
4432 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4433 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004434 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004435 locations->AddTemp(Location::RequiresRegister());
4436 }
4437}
4438
4439Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4440 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4441 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4442 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4443 return Location::ConstantLocation(input->AsConstant());
4444 } else {
4445 return Location::RequiresFpuRegister();
4446 }
4447}
4448
Artem Serov02109dd2016-09-23 17:17:54 +01004449Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4450 Opcode opcode) {
4451 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4452 if (constant->IsConstant() &&
4453 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4454 return Location::ConstantLocation(constant->AsConstant());
4455 }
4456 return Location::RequiresRegister();
4457}
4458
4459bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4460 Opcode opcode) {
4461 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4462 if (Primitive::Is64BitType(input_cst->GetType())) {
4463 Opcode high_opcode = opcode;
4464 SetCc low_set_cc = kCcDontCare;
4465 switch (opcode) {
4466 case SUB:
4467 // Flip the operation to an ADD.
4468 value = -value;
4469 opcode = ADD;
4470 FALLTHROUGH_INTENDED;
4471 case ADD:
4472 if (Low32Bits(value) == 0u) {
4473 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4474 }
4475 high_opcode = ADC;
4476 low_set_cc = kCcSet;
4477 break;
4478 default:
4479 break;
4480 }
4481 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4482 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4483 } else {
4484 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4485 }
4486}
4487
4488// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4489// enabled.
4490bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4491 Opcode opcode,
4492 SetCc set_cc) {
4493 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4494 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4495 return true;
4496 }
4497 Opcode neg_opcode = kNoOperand;
4498 switch (opcode) {
4499 case AND: neg_opcode = BIC; value = ~value; break;
4500 case ORR: neg_opcode = ORN; value = ~value; break;
4501 case ADD: neg_opcode = SUB; value = -value; break;
4502 case ADC: neg_opcode = SBC; value = ~value; break;
4503 case SUB: neg_opcode = ADD; value = -value; break;
4504 case SBC: neg_opcode = ADC; value = ~value; break;
4505 default:
4506 return false;
4507 }
4508 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4509}
4510
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004511void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4512 const FieldInfo& field_info) {
4513 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4514
4515 LocationSummary* locations = instruction->GetLocations();
4516 vixl32::Register base = InputRegisterAt(instruction, 0);
4517 Location out = locations->Out();
4518 bool is_volatile = field_info.IsVolatile();
4519 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4520 Primitive::Type field_type = field_info.GetFieldType();
4521 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4522
4523 switch (field_type) {
4524 case Primitive::kPrimBoolean:
4525 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4526 break;
4527
4528 case Primitive::kPrimByte:
4529 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4530 break;
4531
4532 case Primitive::kPrimShort:
4533 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4534 break;
4535
4536 case Primitive::kPrimChar:
4537 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4538 break;
4539
4540 case Primitive::kPrimInt:
4541 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4542 break;
4543
4544 case Primitive::kPrimNot: {
4545 // /* HeapReference<Object> */ out = *(base + offset)
4546 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004547 Location temp_loc = locations->GetTemp(0);
4548 // Note that a potential implicit null check is handled in this
4549 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4550 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4551 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4552 if (is_volatile) {
4553 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4554 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004555 } else {
4556 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004557 codegen_->MaybeRecordImplicitNullCheck(instruction);
4558 if (is_volatile) {
4559 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4560 }
4561 // If read barriers are enabled, emit read barriers other than
4562 // Baker's using a slow path (and also unpoison the loaded
4563 // reference, if heap poisoning is enabled).
4564 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4565 }
4566 break;
4567 }
4568
4569 case Primitive::kPrimLong:
4570 if (is_volatile && !atomic_ldrd_strd) {
4571 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4572 } else {
4573 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4574 }
4575 break;
4576
4577 case Primitive::kPrimFloat:
4578 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4579 break;
4580
4581 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004582 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004583 if (is_volatile && !atomic_ldrd_strd) {
4584 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4585 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4586 GenerateWideAtomicLoad(base, offset, lo, hi);
4587 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4588 // scope.
4589 codegen_->MaybeRecordImplicitNullCheck(instruction);
4590 __ Vmov(out_dreg, lo, hi);
4591 } else {
4592 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004593 codegen_->MaybeRecordImplicitNullCheck(instruction);
4594 }
4595 break;
4596 }
4597
4598 case Primitive::kPrimVoid:
4599 LOG(FATAL) << "Unreachable type " << field_type;
4600 UNREACHABLE();
4601 }
4602
4603 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4604 // Potential implicit null checks, in the case of reference or
4605 // double fields, are handled in the previous switch statement.
4606 } else {
4607 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004608 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4609 // should use a scope and the assembler to emit the load instruction to guarantee that we
4610 // record the pc at the correct position. But the `Assembler` does not automatically handle
4611 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4612 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004613 codegen_->MaybeRecordImplicitNullCheck(instruction);
4614 }
4615
4616 if (is_volatile) {
4617 if (field_type == Primitive::kPrimNot) {
4618 // Memory barriers, in the case of references, are also handled
4619 // in the previous switch statement.
4620 } else {
4621 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4622 }
4623 }
4624}
4625
4626void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4627 HandleFieldSet(instruction, instruction->GetFieldInfo());
4628}
4629
4630void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4631 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4632}
4633
4634void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4635 HandleFieldGet(instruction, instruction->GetFieldInfo());
4636}
4637
4638void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4639 HandleFieldGet(instruction, instruction->GetFieldInfo());
4640}
4641
4642void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4643 HandleFieldGet(instruction, instruction->GetFieldInfo());
4644}
4645
4646void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4647 HandleFieldGet(instruction, instruction->GetFieldInfo());
4648}
4649
Scott Wakelingc34dba72016-10-03 10:14:44 +01004650void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4651 HandleFieldSet(instruction, instruction->GetFieldInfo());
4652}
4653
4654void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4655 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4656}
4657
Artem Serovcfbe9132016-10-14 15:58:56 +01004658void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4659 HUnresolvedInstanceFieldGet* instruction) {
4660 FieldAccessCallingConventionARMVIXL calling_convention;
4661 codegen_->CreateUnresolvedFieldLocationSummary(
4662 instruction, instruction->GetFieldType(), calling_convention);
4663}
4664
4665void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4666 HUnresolvedInstanceFieldGet* instruction) {
4667 FieldAccessCallingConventionARMVIXL calling_convention;
4668 codegen_->GenerateUnresolvedFieldAccess(instruction,
4669 instruction->GetFieldType(),
4670 instruction->GetFieldIndex(),
4671 instruction->GetDexPc(),
4672 calling_convention);
4673}
4674
4675void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4676 HUnresolvedInstanceFieldSet* instruction) {
4677 FieldAccessCallingConventionARMVIXL calling_convention;
4678 codegen_->CreateUnresolvedFieldLocationSummary(
4679 instruction, instruction->GetFieldType(), calling_convention);
4680}
4681
4682void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4683 HUnresolvedInstanceFieldSet* instruction) {
4684 FieldAccessCallingConventionARMVIXL calling_convention;
4685 codegen_->GenerateUnresolvedFieldAccess(instruction,
4686 instruction->GetFieldType(),
4687 instruction->GetFieldIndex(),
4688 instruction->GetDexPc(),
4689 calling_convention);
4690}
4691
4692void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4693 HUnresolvedStaticFieldGet* instruction) {
4694 FieldAccessCallingConventionARMVIXL calling_convention;
4695 codegen_->CreateUnresolvedFieldLocationSummary(
4696 instruction, instruction->GetFieldType(), calling_convention);
4697}
4698
4699void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4700 HUnresolvedStaticFieldGet* instruction) {
4701 FieldAccessCallingConventionARMVIXL calling_convention;
4702 codegen_->GenerateUnresolvedFieldAccess(instruction,
4703 instruction->GetFieldType(),
4704 instruction->GetFieldIndex(),
4705 instruction->GetDexPc(),
4706 calling_convention);
4707}
4708
4709void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4710 HUnresolvedStaticFieldSet* instruction) {
4711 FieldAccessCallingConventionARMVIXL calling_convention;
4712 codegen_->CreateUnresolvedFieldLocationSummary(
4713 instruction, instruction->GetFieldType(), calling_convention);
4714}
4715
4716void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4717 HUnresolvedStaticFieldSet* instruction) {
4718 FieldAccessCallingConventionARMVIXL calling_convention;
4719 codegen_->GenerateUnresolvedFieldAccess(instruction,
4720 instruction->GetFieldType(),
4721 instruction->GetFieldIndex(),
4722 instruction->GetDexPc(),
4723 calling_convention);
4724}
4725
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004726void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004727 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004728 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004729}
4730
4731void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4732 if (CanMoveNullCheckToUser(instruction)) {
4733 return;
4734 }
4735
4736 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004737 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004738 ExactAssemblyScope aas(GetVIXLAssembler(),
4739 vixl32::kMaxInstructionSizeInBytes,
4740 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004741 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4742 RecordPcInfo(instruction, instruction->GetDexPc());
4743}
4744
4745void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4746 NullCheckSlowPathARMVIXL* slow_path =
4747 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4748 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004749 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004750}
4751
4752void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4753 codegen_->GenerateNullCheck(instruction);
4754}
4755
Scott Wakelingc34dba72016-10-03 10:14:44 +01004756static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4757 switch (type) {
4758 case Primitive::kPrimNot:
4759 return kLoadWord;
4760 case Primitive::kPrimBoolean:
4761 return kLoadUnsignedByte;
4762 case Primitive::kPrimByte:
4763 return kLoadSignedByte;
4764 case Primitive::kPrimChar:
4765 return kLoadUnsignedHalfword;
4766 case Primitive::kPrimShort:
4767 return kLoadSignedHalfword;
4768 case Primitive::kPrimInt:
4769 return kLoadWord;
4770 case Primitive::kPrimLong:
4771 return kLoadWordPair;
4772 case Primitive::kPrimFloat:
4773 return kLoadSWord;
4774 case Primitive::kPrimDouble:
4775 return kLoadDWord;
4776 default:
4777 LOG(FATAL) << "Unreachable type " << type;
4778 UNREACHABLE();
4779 }
4780}
4781
4782static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4783 switch (type) {
4784 case Primitive::kPrimNot:
4785 return kStoreWord;
4786 case Primitive::kPrimBoolean:
4787 case Primitive::kPrimByte:
4788 return kStoreByte;
4789 case Primitive::kPrimChar:
4790 case Primitive::kPrimShort:
4791 return kStoreHalfword;
4792 case Primitive::kPrimInt:
4793 return kStoreWord;
4794 case Primitive::kPrimLong:
4795 return kStoreWordPair;
4796 case Primitive::kPrimFloat:
4797 return kStoreSWord;
4798 case Primitive::kPrimDouble:
4799 return kStoreDWord;
4800 default:
4801 LOG(FATAL) << "Unreachable type " << type;
4802 UNREACHABLE();
4803 }
4804}
4805
4806void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
4807 Location out_loc,
4808 vixl32::Register base,
4809 vixl32::Register reg_index,
4810 vixl32::Condition cond) {
4811 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4812 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4813
4814 switch (type) {
4815 case Primitive::kPrimByte:
4816 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
4817 break;
4818 case Primitive::kPrimBoolean:
4819 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
4820 break;
4821 case Primitive::kPrimShort:
4822 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
4823 break;
4824 case Primitive::kPrimChar:
4825 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
4826 break;
4827 case Primitive::kPrimNot:
4828 case Primitive::kPrimInt:
4829 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
4830 break;
4831 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4832 case Primitive::kPrimLong:
4833 case Primitive::kPrimFloat:
4834 case Primitive::kPrimDouble:
4835 default:
4836 LOG(FATAL) << "Unreachable type " << type;
4837 UNREACHABLE();
4838 }
4839}
4840
4841void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
4842 Location loc,
4843 vixl32::Register base,
4844 vixl32::Register reg_index,
4845 vixl32::Condition cond) {
4846 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4847 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4848
4849 switch (type) {
4850 case Primitive::kPrimByte:
4851 case Primitive::kPrimBoolean:
4852 __ Strb(cond, RegisterFrom(loc), mem_address);
4853 break;
4854 case Primitive::kPrimShort:
4855 case Primitive::kPrimChar:
4856 __ Strh(cond, RegisterFrom(loc), mem_address);
4857 break;
4858 case Primitive::kPrimNot:
4859 case Primitive::kPrimInt:
4860 __ Str(cond, RegisterFrom(loc), mem_address);
4861 break;
4862 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4863 case Primitive::kPrimLong:
4864 case Primitive::kPrimFloat:
4865 case Primitive::kPrimDouble:
4866 default:
4867 LOG(FATAL) << "Unreachable type " << type;
4868 UNREACHABLE();
4869 }
4870}
4871
4872void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4873 bool object_array_get_with_read_barrier =
4874 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4875 LocationSummary* locations =
4876 new (GetGraph()->GetArena()) LocationSummary(instruction,
4877 object_array_get_with_read_barrier ?
4878 LocationSummary::kCallOnSlowPath :
4879 LocationSummary::kNoCall);
4880 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004881 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004882 }
4883 locations->SetInAt(0, Location::RequiresRegister());
4884 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4885 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4886 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4887 } else {
4888 // The output overlaps in the case of an object array get with
4889 // read barriers enabled: we do not want the move to overwrite the
4890 // array's location, as we need it to emit the read barrier.
4891 locations->SetOut(
4892 Location::RequiresRegister(),
4893 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4894 }
4895 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004896 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004897 // Also need for String compression feature.
4898 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4899 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004900 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004901 }
4902}
4903
4904void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004905 LocationSummary* locations = instruction->GetLocations();
4906 Location obj_loc = locations->InAt(0);
4907 vixl32::Register obj = InputRegisterAt(instruction, 0);
4908 Location index = locations->InAt(1);
4909 Location out_loc = locations->Out();
4910 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4911 Primitive::Type type = instruction->GetType();
4912 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4913 instruction->IsStringCharAt();
4914 HInstruction* array_instr = instruction->GetArray();
4915 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01004916
4917 switch (type) {
4918 case Primitive::kPrimBoolean:
4919 case Primitive::kPrimByte:
4920 case Primitive::kPrimShort:
4921 case Primitive::kPrimChar:
4922 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004923 vixl32::Register length;
4924 if (maybe_compressed_char_at) {
4925 length = RegisterFrom(locations->GetTemp(0));
4926 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4927 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
4928 codegen_->MaybeRecordImplicitNullCheck(instruction);
4929 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01004930 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00004931 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004932 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004933 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004934 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4935 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4936 "Expecting 0=compressed, 1=uncompressed");
4937 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004938 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
4939 RegisterFrom(out_loc),
4940 obj,
4941 data_offset + const_index);
4942 __ B(&done);
4943 __ Bind(&uncompressed_load);
4944 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4945 RegisterFrom(out_loc),
4946 obj,
4947 data_offset + (const_index << 1));
4948 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004949 } else {
4950 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4951
4952 LoadOperandType load_type = GetLoadOperandType(type);
4953 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
4954 }
4955 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004956 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004957 vixl32::Register temp = temps.Acquire();
4958
4959 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01004960 // We do not need to compute the intermediate address from the array: the
4961 // input instruction has done it already. See the comment in
4962 // `TryExtractArrayAccessAddress()`.
4963 if (kIsDebugBuild) {
4964 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00004965 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01004966 }
4967 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01004968 } else {
4969 __ Add(temp, obj, data_offset);
4970 }
4971 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004972 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004973 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4974 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4975 "Expecting 0=compressed, 1=uncompressed");
4976 __ B(cs, &uncompressed_load);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004977 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
4978 __ B(&done);
4979 __ Bind(&uncompressed_load);
4980 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
4981 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004982 } else {
4983 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4984 }
4985 }
4986 break;
4987 }
4988
4989 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004990 // The read barrier instrumentation of object ArrayGet
4991 // instructions does not support the HIntermediateAddress
4992 // instruction.
4993 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4994
Scott Wakelingc34dba72016-10-03 10:14:44 +01004995 static_assert(
4996 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4997 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4998 // /* HeapReference<Object> */ out =
4999 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5000 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005001 Location temp = locations->GetTemp(0);
5002 // Note that a potential implicit null check is handled in this
5003 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5004 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5005 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005006 } else {
5007 vixl32::Register out = OutputRegister(instruction);
5008 if (index.IsConstant()) {
5009 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005010 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005011 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005012 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5013 // we should use a scope and the assembler to emit the load instruction to guarantee that
5014 // we record the pc at the correct position. But the `Assembler` does not automatically
5015 // handle unencodable offsets. Practically, everything is fine because the helper and
5016 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005017 codegen_->MaybeRecordImplicitNullCheck(instruction);
5018 // If read barriers are enabled, emit read barriers other than
5019 // Baker's using a slow path (and also unpoison the loaded
5020 // reference, if heap poisoning is enabled).
5021 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5022 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005023 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005024 vixl32::Register temp = temps.Acquire();
5025
5026 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005027 // We do not need to compute the intermediate address from the array: the
5028 // input instruction has done it already. See the comment in
5029 // `TryExtractArrayAccessAddress()`.
5030 if (kIsDebugBuild) {
5031 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005032 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005033 }
5034 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005035 } else {
5036 __ Add(temp, obj, data_offset);
5037 }
5038 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005039 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005040 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5041 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5042 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005043 codegen_->MaybeRecordImplicitNullCheck(instruction);
5044 // If read barriers are enabled, emit read barriers other than
5045 // Baker's using a slow path (and also unpoison the loaded
5046 // reference, if heap poisoning is enabled).
5047 codegen_->MaybeGenerateReadBarrierSlow(
5048 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5049 }
5050 }
5051 break;
5052 }
5053
5054 case Primitive::kPrimLong: {
5055 if (index.IsConstant()) {
5056 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005057 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005058 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5059 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005060 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005061 vixl32::Register temp = temps.Acquire();
5062 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5063 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5064 }
5065 break;
5066 }
5067
5068 case Primitive::kPrimFloat: {
5069 vixl32::SRegister out = SRegisterFrom(out_loc);
5070 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005071 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005072 GetAssembler()->LoadSFromOffset(out, obj, offset);
5073 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005074 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005075 vixl32::Register temp = temps.Acquire();
5076 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5077 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5078 }
5079 break;
5080 }
5081
5082 case Primitive::kPrimDouble: {
5083 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005084 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005085 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5086 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005087 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005088 vixl32::Register temp = temps.Acquire();
5089 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5090 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5091 }
5092 break;
5093 }
5094
5095 case Primitive::kPrimVoid:
5096 LOG(FATAL) << "Unreachable type " << type;
5097 UNREACHABLE();
5098 }
5099
5100 if (type == Primitive::kPrimNot) {
5101 // Potential implicit null checks, in the case of reference
5102 // arrays, are handled in the previous switch statement.
5103 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005104 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5105 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005106 codegen_->MaybeRecordImplicitNullCheck(instruction);
5107 }
5108}
5109
5110void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5111 Primitive::Type value_type = instruction->GetComponentType();
5112
5113 bool needs_write_barrier =
5114 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5115 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5116
5117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5118 instruction,
5119 may_need_runtime_call_for_type_check ?
5120 LocationSummary::kCallOnSlowPath :
5121 LocationSummary::kNoCall);
5122
5123 locations->SetInAt(0, Location::RequiresRegister());
5124 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5125 if (Primitive::IsFloatingPointType(value_type)) {
5126 locations->SetInAt(2, Location::RequiresFpuRegister());
5127 } else {
5128 locations->SetInAt(2, Location::RequiresRegister());
5129 }
5130 if (needs_write_barrier) {
5131 // Temporary registers for the write barrier.
5132 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5133 locations->AddTemp(Location::RequiresRegister());
5134 }
5135}
5136
5137void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005138 LocationSummary* locations = instruction->GetLocations();
5139 vixl32::Register array = InputRegisterAt(instruction, 0);
5140 Location index = locations->InAt(1);
5141 Primitive::Type value_type = instruction->GetComponentType();
5142 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5143 bool needs_write_barrier =
5144 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5145 uint32_t data_offset =
5146 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5147 Location value_loc = locations->InAt(2);
5148 HInstruction* array_instr = instruction->GetArray();
5149 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005150
5151 switch (value_type) {
5152 case Primitive::kPrimBoolean:
5153 case Primitive::kPrimByte:
5154 case Primitive::kPrimShort:
5155 case Primitive::kPrimChar:
5156 case Primitive::kPrimInt: {
5157 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005158 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005159 uint32_t full_offset =
5160 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5161 StoreOperandType store_type = GetStoreOperandType(value_type);
5162 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5163 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005164 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005165 vixl32::Register temp = temps.Acquire();
5166
5167 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005168 // We do not need to compute the intermediate address from the array: the
5169 // input instruction has done it already. See the comment in
5170 // `TryExtractArrayAccessAddress()`.
5171 if (kIsDebugBuild) {
5172 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005173 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005174 }
5175 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005176 } else {
5177 __ Add(temp, array, data_offset);
5178 }
5179 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5180 }
5181 break;
5182 }
5183
5184 case Primitive::kPrimNot: {
5185 vixl32::Register value = RegisterFrom(value_loc);
5186 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5187 // See the comment in instruction_simplifier_shared.cc.
5188 DCHECK(!has_intermediate_address);
5189
5190 if (instruction->InputAt(2)->IsNullConstant()) {
5191 // Just setting null.
5192 if (index.IsConstant()) {
5193 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005194 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005195 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5196 } else {
5197 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005198 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005199 vixl32::Register temp = temps.Acquire();
5200 __ Add(temp, array, data_offset);
5201 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5202 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005203 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5204 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005205 codegen_->MaybeRecordImplicitNullCheck(instruction);
5206 DCHECK(!needs_write_barrier);
5207 DCHECK(!may_need_runtime_call_for_type_check);
5208 break;
5209 }
5210
5211 DCHECK(needs_write_barrier);
5212 Location temp1_loc = locations->GetTemp(0);
5213 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5214 Location temp2_loc = locations->GetTemp(1);
5215 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5216 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5217 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5218 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5219 vixl32::Label done;
5220 SlowPathCodeARMVIXL* slow_path = nullptr;
5221
5222 if (may_need_runtime_call_for_type_check) {
5223 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5224 codegen_->AddSlowPath(slow_path);
5225 if (instruction->GetValueCanBeNull()) {
5226 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005227 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005228 if (index.IsConstant()) {
5229 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005230 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005231 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5232 } else {
5233 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005234 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005235 vixl32::Register temp = temps.Acquire();
5236 __ Add(temp, array, data_offset);
5237 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5238 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005239 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5240 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005241 codegen_->MaybeRecordImplicitNullCheck(instruction);
5242 __ B(&done);
5243 __ Bind(&non_zero);
5244 }
5245
5246 // Note that when read barriers are enabled, the type checks
5247 // are performed without read barriers. This is fine, even in
5248 // the case where a class object is in the from-space after
5249 // the flip, as a comparison involving such a type would not
5250 // produce a false positive; it may of course produce a false
5251 // negative, in which case we would take the ArraySet slow
5252 // path.
5253
Alexandre Rames374ddf32016-11-04 10:40:49 +00005254 {
5255 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005256 ExactAssemblyScope aas(GetVIXLAssembler(),
5257 vixl32::kMaxInstructionSizeInBytes,
5258 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005259 // /* HeapReference<Class> */ temp1 = array->klass_
5260 __ ldr(temp1, MemOperand(array, class_offset));
5261 codegen_->MaybeRecordImplicitNullCheck(instruction);
5262 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005263 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5264
5265 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5266 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5267 // /* HeapReference<Class> */ temp2 = value->klass_
5268 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5269 // If heap poisoning is enabled, no need to unpoison `temp1`
5270 // nor `temp2`, as we are comparing two poisoned references.
5271 __ Cmp(temp1, temp2);
5272
5273 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5274 vixl32::Label do_put;
5275 __ B(eq, &do_put);
5276 // If heap poisoning is enabled, the `temp1` reference has
5277 // not been unpoisoned yet; unpoison it now.
5278 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5279
5280 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5281 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5282 // If heap poisoning is enabled, no need to unpoison
5283 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005284 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005285 __ Bind(&do_put);
5286 } else {
5287 __ B(ne, slow_path->GetEntryLabel());
5288 }
5289 }
5290
5291 vixl32::Register source = value;
5292 if (kPoisonHeapReferences) {
5293 // Note that in the case where `value` is a null reference,
5294 // we do not enter this block, as a null reference does not
5295 // need poisoning.
5296 DCHECK_EQ(value_type, Primitive::kPrimNot);
5297 __ Mov(temp1, value);
5298 GetAssembler()->PoisonHeapReference(temp1);
5299 source = temp1;
5300 }
5301
5302 if (index.IsConstant()) {
5303 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005304 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005305 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5306 } else {
5307 DCHECK(index.IsRegister()) << index;
5308
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005309 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005310 vixl32::Register temp = temps.Acquire();
5311 __ Add(temp, array, data_offset);
5312 codegen_->StoreToShiftedRegOffset(value_type,
5313 LocationFrom(source),
5314 temp,
5315 RegisterFrom(index));
5316 }
5317
5318 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005319 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5320 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005321 codegen_->MaybeRecordImplicitNullCheck(instruction);
5322 }
5323
5324 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5325
5326 if (done.IsReferenced()) {
5327 __ Bind(&done);
5328 }
5329
5330 if (slow_path != nullptr) {
5331 __ Bind(slow_path->GetExitLabel());
5332 }
5333
5334 break;
5335 }
5336
5337 case Primitive::kPrimLong: {
5338 Location value = locations->InAt(2);
5339 if (index.IsConstant()) {
5340 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005341 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005342 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5343 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005344 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005345 vixl32::Register temp = temps.Acquire();
5346 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5347 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5348 }
5349 break;
5350 }
5351
5352 case Primitive::kPrimFloat: {
5353 Location value = locations->InAt(2);
5354 DCHECK(value.IsFpuRegister());
5355 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005356 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005357 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5358 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005359 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005360 vixl32::Register temp = temps.Acquire();
5361 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5362 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5363 }
5364 break;
5365 }
5366
5367 case Primitive::kPrimDouble: {
5368 Location value = locations->InAt(2);
5369 DCHECK(value.IsFpuRegisterPair());
5370 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005371 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005372 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5373 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005374 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005375 vixl32::Register temp = temps.Acquire();
5376 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5377 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5378 }
5379 break;
5380 }
5381
5382 case Primitive::kPrimVoid:
5383 LOG(FATAL) << "Unreachable type " << value_type;
5384 UNREACHABLE();
5385 }
5386
5387 // Objects are handled in the switch.
5388 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005389 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5390 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005391 codegen_->MaybeRecordImplicitNullCheck(instruction);
5392 }
5393}
5394
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005395void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5396 LocationSummary* locations =
5397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5398 locations->SetInAt(0, Location::RequiresRegister());
5399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5400}
5401
5402void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5403 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5404 vixl32::Register obj = InputRegisterAt(instruction, 0);
5405 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005406 {
Artem Serov0fb37192016-12-06 18:13:40 +00005407 ExactAssemblyScope aas(GetVIXLAssembler(),
5408 vixl32::kMaxInstructionSizeInBytes,
5409 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005410 __ ldr(out, MemOperand(obj, offset));
5411 codegen_->MaybeRecordImplicitNullCheck(instruction);
5412 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005413 // Mask out compression flag from String's array length.
5414 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005415 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005416 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005417}
5418
Artem Serov2bbc9532016-10-21 11:51:50 +01005419void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005420 LocationSummary* locations =
5421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5422
5423 locations->SetInAt(0, Location::RequiresRegister());
5424 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5426}
5427
5428void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5429 vixl32::Register out = OutputRegister(instruction);
5430 vixl32::Register first = InputRegisterAt(instruction, 0);
5431 Location second = instruction->GetLocations()->InAt(1);
5432
Artem Serov2bbc9532016-10-21 11:51:50 +01005433 if (second.IsRegister()) {
5434 __ Add(out, first, RegisterFrom(second));
5435 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005436 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01005437 }
5438}
5439
Scott Wakelingc34dba72016-10-03 10:14:44 +01005440void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5441 RegisterSet caller_saves = RegisterSet::Empty();
5442 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5443 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5444 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5445 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5446 locations->SetInAt(0, Location::RequiresRegister());
5447 locations->SetInAt(1, Location::RequiresRegister());
5448}
5449
5450void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5451 SlowPathCodeARMVIXL* slow_path =
5452 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5453 codegen_->AddSlowPath(slow_path);
5454
5455 vixl32::Register index = InputRegisterAt(instruction, 0);
5456 vixl32::Register length = InputRegisterAt(instruction, 1);
5457
5458 __ Cmp(index, length);
5459 __ B(hs, slow_path->GetEntryLabel());
5460}
5461
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005462void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5463 vixl32::Register card,
5464 vixl32::Register object,
5465 vixl32::Register value,
5466 bool can_be_null) {
5467 vixl32::Label is_null;
5468 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005469 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005470 }
5471 GetAssembler()->LoadFromOffset(
5472 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005473 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005474 __ Strb(card, MemOperand(card, temp));
5475 if (can_be_null) {
5476 __ Bind(&is_null);
5477 }
5478}
5479
Scott Wakelingfe885462016-09-22 10:24:38 +01005480void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5481 LOG(FATAL) << "Unreachable";
5482}
5483
5484void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5485 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5486}
5487
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005488void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005489 LocationSummary* locations =
5490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5491 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005492}
5493
5494void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5495 HBasicBlock* block = instruction->GetBlock();
5496 if (block->GetLoopInformation() != nullptr) {
5497 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5498 // The back edge will generate the suspend check.
5499 return;
5500 }
5501 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5502 // The goto will generate the suspend check.
5503 return;
5504 }
5505 GenerateSuspendCheck(instruction, nullptr);
5506}
5507
5508void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5509 HBasicBlock* successor) {
5510 SuspendCheckSlowPathARMVIXL* slow_path =
5511 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5512 if (slow_path == nullptr) {
5513 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5514 instruction->SetSlowPath(slow_path);
5515 codegen_->AddSlowPath(slow_path);
5516 if (successor != nullptr) {
5517 DCHECK(successor->IsLoopHeader());
5518 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5519 }
5520 } else {
5521 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5522 }
5523
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005524 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005525 vixl32::Register temp = temps.Acquire();
5526 GetAssembler()->LoadFromOffset(
5527 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5528 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005529 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005530 __ Bind(slow_path->GetReturnLabel());
5531 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005532 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005533 __ B(slow_path->GetEntryLabel());
5534 }
5535}
5536
Scott Wakelingfe885462016-09-22 10:24:38 +01005537ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5538 return codegen_->GetAssembler();
5539}
5540
5541void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005542 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005543 MoveOperands* move = moves_[index];
5544 Location source = move->GetSource();
5545 Location destination = move->GetDestination();
5546
5547 if (source.IsRegister()) {
5548 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005549 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005550 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005551 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005552 } else {
5553 DCHECK(destination.IsStackSlot());
5554 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005555 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005556 sp,
5557 destination.GetStackIndex());
5558 }
5559 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005560 if (destination.IsRegister()) {
5561 GetAssembler()->LoadFromOffset(kLoadWord,
5562 RegisterFrom(destination),
5563 sp,
5564 source.GetStackIndex());
5565 } else if (destination.IsFpuRegister()) {
5566 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5567 } else {
5568 DCHECK(destination.IsStackSlot());
5569 vixl32::Register temp = temps.Acquire();
5570 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5571 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5572 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005573 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005574 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005575 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005576 } else if (destination.IsFpuRegister()) {
5577 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5578 } else {
5579 DCHECK(destination.IsStackSlot());
5580 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5581 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005582 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005583 if (destination.IsDoubleStackSlot()) {
5584 vixl32::DRegister temp = temps.AcquireD();
5585 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5586 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5587 } else if (destination.IsRegisterPair()) {
5588 DCHECK(ExpectedPairLayout(destination));
5589 GetAssembler()->LoadFromOffset(
5590 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5591 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005592 DCHECK(destination.IsFpuRegisterPair()) << destination;
5593 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005594 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005595 } else if (source.IsRegisterPair()) {
5596 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005597 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5598 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005599 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005600 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005601 } else {
5602 DCHECK(destination.IsDoubleStackSlot()) << destination;
5603 DCHECK(ExpectedPairLayout(source));
5604 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005605 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005606 sp,
5607 destination.GetStackIndex());
5608 }
5609 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005610 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005611 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005612 } else if (destination.IsFpuRegisterPair()) {
5613 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5614 } else {
5615 DCHECK(destination.IsDoubleStackSlot()) << destination;
5616 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5617 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005618 } else {
5619 DCHECK(source.IsConstant()) << source;
5620 HConstant* constant = source.GetConstant();
5621 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5622 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5623 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005624 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005625 } else {
5626 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005627 vixl32::Register temp = temps.Acquire();
5628 __ Mov(temp, value);
5629 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5630 }
5631 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005632 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01005633 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005634 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5635 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005636 } else {
5637 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005638 vixl32::Register temp = temps.Acquire();
5639 __ Mov(temp, Low32Bits(value));
5640 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5641 __ Mov(temp, High32Bits(value));
5642 GetAssembler()->StoreToOffset(kStoreWord,
5643 temp,
5644 sp,
5645 destination.GetHighStackIndex(kArmWordSize));
5646 }
5647 } else if (constant->IsDoubleConstant()) {
5648 double value = constant->AsDoubleConstant()->GetValue();
5649 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005650 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005651 } else {
5652 DCHECK(destination.IsDoubleStackSlot()) << destination;
5653 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005654 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005655 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005656 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005657 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005658 GetAssembler()->StoreToOffset(kStoreWord,
5659 temp,
5660 sp,
5661 destination.GetHighStackIndex(kArmWordSize));
5662 }
5663 } else {
5664 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5665 float value = constant->AsFloatConstant()->GetValue();
5666 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005667 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005668 } else {
5669 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005670 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005671 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005672 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5673 }
5674 }
5675 }
5676}
5677
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005678void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5679 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5680 vixl32::Register temp = temps.Acquire();
5681 __ Mov(temp, reg);
5682 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5683 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005684}
5685
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005686void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5687 // TODO(VIXL32): Double check the performance of this implementation.
5688 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5689 vixl32::Register temp = temps.Acquire();
5690 vixl32::SRegister temp_s = temps.AcquireS();
5691
5692 __ Ldr(temp, MemOperand(sp, mem1));
5693 __ Vldr(temp_s, MemOperand(sp, mem2));
5694 __ Str(temp, MemOperand(sp, mem2));
5695 __ Vstr(temp_s, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005696}
5697
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005698void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5699 MoveOperands* move = moves_[index];
5700 Location source = move->GetSource();
5701 Location destination = move->GetDestination();
5702 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5703
5704 if (source.IsRegister() && destination.IsRegister()) {
5705 vixl32::Register temp = temps.Acquire();
5706 DCHECK(!RegisterFrom(source).Is(temp));
5707 DCHECK(!RegisterFrom(destination).Is(temp));
5708 __ Mov(temp, RegisterFrom(destination));
5709 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5710 __ Mov(RegisterFrom(source), temp);
5711 } else if (source.IsRegister() && destination.IsStackSlot()) {
5712 Exchange(RegisterFrom(source), destination.GetStackIndex());
5713 } else if (source.IsStackSlot() && destination.IsRegister()) {
5714 Exchange(RegisterFrom(destination), source.GetStackIndex());
5715 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005716 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005717 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005718 vixl32::SRegister temp = temps.AcquireS();
5719 __ Vmov(temp, SRegisterFrom(source));
5720 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5721 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005722 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5723 vixl32::DRegister temp = temps.AcquireD();
5724 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5725 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5726 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5727 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5728 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5729 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5730 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5731 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5732 vixl32::DRegister temp = temps.AcquireD();
5733 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5734 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5735 GetAssembler()->StoreDToOffset(temp, sp, mem);
5736 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005737 vixl32::DRegister first = DRegisterFrom(source);
5738 vixl32::DRegister second = DRegisterFrom(destination);
5739 vixl32::DRegister temp = temps.AcquireD();
5740 __ Vmov(temp, first);
5741 __ Vmov(first, second);
5742 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005743 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005744 vixl32::DRegister reg = source.IsFpuRegisterPair()
5745 ? DRegisterFrom(source)
5746 : DRegisterFrom(destination);
5747 int mem = source.IsFpuRegisterPair()
5748 ? destination.GetStackIndex()
5749 : source.GetStackIndex();
5750 vixl32::DRegister temp = temps.AcquireD();
5751 __ Vmov(temp, reg);
5752 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5753 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005754 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005755 vixl32::SRegister reg = source.IsFpuRegister()
5756 ? SRegisterFrom(source)
5757 : SRegisterFrom(destination);
5758 int mem = source.IsFpuRegister()
5759 ? destination.GetStackIndex()
5760 : source.GetStackIndex();
5761 vixl32::Register temp = temps.Acquire();
5762 __ Vmov(temp, reg);
5763 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5764 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005765 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5766 vixl32::DRegister temp1 = temps.AcquireD();
5767 vixl32::DRegister temp2 = temps.AcquireD();
5768 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
5769 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
5770 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
5771 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
5772 } else {
5773 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
5774 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005775}
5776
5777void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
5778 TODO_VIXL32(FATAL);
5779}
5780
5781void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
5782 TODO_VIXL32(FATAL);
5783}
5784
Artem Serov02d37832016-10-25 15:25:33 +01005785HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005786 HLoadClass::LoadKind desired_class_load_kind) {
5787 switch (desired_class_load_kind) {
5788 case HLoadClass::LoadKind::kReferrersClass:
5789 break;
5790 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005791 DCHECK(!GetCompilerOptions().GetCompilePic());
5792 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005793 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5794 DCHECK(GetCompilerOptions().GetCompilePic());
5795 break;
5796 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005797 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005798 case HLoadClass::LoadKind::kJitTableAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005799 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005800 case HLoadClass::LoadKind::kDexCachePcRelative:
5801 DCHECK(!Runtime::Current()->UseJitCompilation());
5802 // We disable pc-relative load when there is an irreducible loop, as the optimization
5803 // is incompatible with it.
5804 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5805 // with irreducible loops.
5806 if (GetGraph()->HasIrreducibleLoops()) {
5807 return HLoadClass::LoadKind::kDexCacheViaMethod;
5808 }
5809 break;
5810 case HLoadClass::LoadKind::kDexCacheViaMethod:
5811 break;
5812 }
5813 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005814}
5815
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005816void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
5817 if (cls->NeedsAccessCheck()) {
5818 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5819 CodeGenerator::CreateLoadClassLocationSummary(
5820 cls,
5821 LocationFrom(calling_convention.GetRegisterAt(0)),
5822 LocationFrom(r0),
5823 /* code_generator_supports_read_barrier */ true);
5824 return;
5825 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005826
Artem Serovd4cc5b22016-11-04 11:19:09 +00005827 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5828 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 ? LocationSummary::kCallOnSlowPath
5830 : LocationSummary::kNoCall;
5831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005832 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005833 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00005834 }
5835
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005836 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5837 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5838 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5839 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5840 locations->SetInAt(0, Location::RequiresRegister());
5841 }
5842 locations->SetOut(Location::RequiresRegister());
5843}
5844
5845void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) {
5846 LocationSummary* locations = cls->GetLocations();
5847 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005848 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005849 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
5850 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
5851 return;
5852 }
5853
5854 Location out_loc = locations->Out();
5855 vixl32::Register out = OutputRegister(cls);
5856
Artem Serovd4cc5b22016-11-04 11:19:09 +00005857 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5858 ? kWithoutReadBarrier
5859 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005860 bool generate_null_check = false;
5861 switch (cls->GetLoadKind()) {
5862 case HLoadClass::LoadKind::kReferrersClass: {
5863 DCHECK(!cls->CanCallRuntime());
5864 DCHECK(!cls->MustGenerateClinitCheck());
5865 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5866 vixl32::Register current_method = InputRegisterAt(cls, 0);
5867 GenerateGcRootFieldLoad(cls,
5868 out_loc,
5869 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01005870 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00005871 read_barrier_option);
5872 break;
5873 }
5874 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005875 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5876 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5877 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005878 break;
5879 }
5880 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5881 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5882 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
5883 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5884 codegen_->EmitMovwMovtPlaceholder(labels, out);
5885 break;
5886 }
5887 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005888 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5889 DCHECK_NE(cls->GetAddress(), 0u);
5890 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5891 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005892 break;
5893 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005894 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005895 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5896 cls->GetTypeIndex(),
5897 cls->GetAddress()));
5898 // /* GcRoot<mirror::Class> */ out = *out
5899 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005900 break;
5901 }
5902 case HLoadClass::LoadKind::kDexCachePcRelative: {
5903 vixl32::Register base_reg = InputRegisterAt(cls, 0);
5904 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5905 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5906 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5907 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, read_barrier_option);
5908 generate_null_check = !cls->IsInDexCache();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005909 break;
5910 }
5911 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5912 // /* GcRoot<mirror::Class>[] */ out =
5913 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5914 vixl32::Register current_method = InputRegisterAt(cls, 0);
5915 const int32_t resolved_types_offset =
5916 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value();
5917 GetAssembler()->LoadFromOffset(kLoadWord, out, current_method, resolved_types_offset);
5918 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005919 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005920 GenerateGcRootFieldLoad(cls, out_loc, out, offset, read_barrier_option);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005921 generate_null_check = !cls->IsInDexCache();
5922 break;
5923 }
5924 default:
5925 TODO_VIXL32(FATAL);
5926 }
5927
5928 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5929 DCHECK(cls->CanCallRuntime());
5930 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
5931 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5932 codegen_->AddSlowPath(slow_path);
5933 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005934 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005935 }
5936 if (cls->MustGenerateClinitCheck()) {
5937 GenerateClassInitializationCheck(slow_path, out);
5938 } else {
5939 __ Bind(slow_path->GetExitLabel());
5940 }
5941 }
5942}
5943
Artem Serov02d37832016-10-25 15:25:33 +01005944void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5945 LocationSummary* locations =
5946 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5947 locations->SetInAt(0, Location::RequiresRegister());
5948 if (check->HasUses()) {
5949 locations->SetOut(Location::SameAsFirstInput());
5950 }
5951}
5952
5953void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5954 // We assume the class is not null.
5955 LoadClassSlowPathARMVIXL* slow_path =
5956 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
5957 check,
5958 check->GetDexPc(),
5959 /* do_clinit */ true);
5960 codegen_->AddSlowPath(slow_path);
5961 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
5962}
5963
5964void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
5965 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
5966 UseScratchRegisterScope temps(GetVIXLAssembler());
5967 vixl32::Register temp = temps.Acquire();
5968 GetAssembler()->LoadFromOffset(kLoadWord,
5969 temp,
5970 class_reg,
5971 mirror::Class::StatusOffset().Int32Value());
5972 __ Cmp(temp, mirror::Class::kStatusInitialized);
5973 __ B(lt, slow_path->GetEntryLabel());
5974 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5975 // properly. Therefore, we do a memory fence.
5976 __ Dmb(ISH);
5977 __ Bind(slow_path->GetExitLabel());
5978}
5979
Artem Serov02d37832016-10-25 15:25:33 +01005980HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005981 HLoadString::LoadKind desired_string_load_kind) {
5982 switch (desired_string_load_kind) {
5983 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005984 DCHECK(!GetCompilerOptions().GetCompilePic());
5985 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005986 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5987 DCHECK(GetCompilerOptions().GetCompilePic());
5988 break;
5989 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005990 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005991 case HLoadString::LoadKind::kBssEntry:
5992 DCHECK(!Runtime::Current()->UseJitCompilation());
5993 break;
5994 case HLoadString::LoadKind::kJitTableAddress:
5995 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005996 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005997 case HLoadString::LoadKind::kDexCacheViaMethod:
5998 break;
5999 }
6000 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006001}
6002
6003void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006004 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006006 HLoadString::LoadKind load_kind = load->GetLoadKind();
6007 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006008 locations->SetOut(LocationFrom(r0));
6009 } else {
6010 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006011 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6012 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6013 // Rely on the pResolveString and/or marking to save everything, including temps.
6014 // Note that IP may theoretically be clobbered by saving/restoring the live register
6015 // (only one thanks to the custom calling convention), so we request a different temp.
6016 locations->AddTemp(Location::RequiresRegister());
6017 RegisterSet caller_saves = RegisterSet::Empty();
6018 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6019 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6020 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6021 // that the the kPrimNot result register is the same as the first argument register.
6022 locations->SetCustomSlowPathCallerSaves(caller_saves);
6023 } else {
6024 // For non-Baker read barrier we have a temp-clobbering call.
6025 }
6026 }
Artem Serov02d37832016-10-25 15:25:33 +01006027 }
6028}
6029
6030void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006031 LocationSummary* locations = load->GetLocations();
6032 Location out_loc = locations->Out();
6033 vixl32::Register out = OutputRegister(load);
6034 HLoadString::LoadKind load_kind = load->GetLoadKind();
6035
6036 switch (load_kind) {
6037 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006038 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6039 load->GetStringIndex()));
6040 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006041 }
6042 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6043 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6044 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6045 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
6046 codegen_->EmitMovwMovtPlaceholder(labels, out);
6047 return; // No dex cache slow path.
6048 }
6049 case HLoadString::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006050 DCHECK_NE(load->GetAddress(), 0u);
6051 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6052 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6053 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006054 }
6055 case HLoadString::LoadKind::kBssEntry: {
6056 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6057 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
6058 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6059 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
6060 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6061 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6062 LoadStringSlowPathARMVIXL* slow_path =
6063 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6064 codegen_->AddSlowPath(slow_path);
6065 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6066 __ Bind(slow_path->GetExitLabel());
6067 return;
6068 }
6069 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006070 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6071 load->GetStringIndex()));
6072 // /* GcRoot<mirror::String> */ out = *out
6073 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6074 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006075 }
6076 default:
6077 break;
6078 }
Artem Serov02d37832016-10-25 15:25:33 +01006079
6080 // TODO: Re-add the compiler code to do string dex cache lookup again.
6081 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6082 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006083 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006084 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6085 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6086}
6087
6088static int32_t GetExceptionTlsOffset() {
6089 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6090}
6091
6092void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6093 LocationSummary* locations =
6094 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6095 locations->SetOut(Location::RequiresRegister());
6096}
6097
6098void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6099 vixl32::Register out = OutputRegister(load);
6100 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6101}
6102
6103
6104void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6105 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6106}
6107
6108void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6109 UseScratchRegisterScope temps(GetVIXLAssembler());
6110 vixl32::Register temp = temps.Acquire();
6111 __ Mov(temp, 0);
6112 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6113}
6114
6115void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6116 LocationSummary* locations =
6117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6118 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6119 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6120}
6121
6122void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6123 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6124 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6125}
6126
Artem Serov657022c2016-11-23 14:19:38 +00006127// Temp is used for read barrier.
6128static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6129 if (kEmitCompilerReadBarrier &&
6130 (kUseBakerReadBarrier ||
6131 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6132 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6133 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6134 return 1;
6135 }
6136 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006137}
6138
Artem Serov657022c2016-11-23 14:19:38 +00006139// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6140// interface pointer, one for loading the current interface.
6141// The other checks have one temp for loading the object's class.
6142static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6143 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6144 return 3;
6145 }
6146 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6147}
Artem Serovcfbe9132016-10-14 15:58:56 +01006148
6149void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6150 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6151 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6152 bool baker_read_barrier_slow_path = false;
6153 switch (type_check_kind) {
6154 case TypeCheckKind::kExactCheck:
6155 case TypeCheckKind::kAbstractClassCheck:
6156 case TypeCheckKind::kClassHierarchyCheck:
6157 case TypeCheckKind::kArrayObjectCheck:
6158 call_kind =
6159 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6160 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6161 break;
6162 case TypeCheckKind::kArrayCheck:
6163 case TypeCheckKind::kUnresolvedCheck:
6164 case TypeCheckKind::kInterfaceCheck:
6165 call_kind = LocationSummary::kCallOnSlowPath;
6166 break;
6167 }
6168
6169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6170 if (baker_read_barrier_slow_path) {
6171 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6172 }
6173 locations->SetInAt(0, Location::RequiresRegister());
6174 locations->SetInAt(1, Location::RequiresRegister());
6175 // The "out" register is used as a temporary, so it overlaps with the inputs.
6176 // Note that TypeCheckSlowPathARM uses this register too.
6177 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006178 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006179}
6180
6181void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6182 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6183 LocationSummary* locations = instruction->GetLocations();
6184 Location obj_loc = locations->InAt(0);
6185 vixl32::Register obj = InputRegisterAt(instruction, 0);
6186 vixl32::Register cls = InputRegisterAt(instruction, 1);
6187 Location out_loc = locations->Out();
6188 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006189 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6190 DCHECK_LE(num_temps, 1u);
6191 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006192 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6193 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6194 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6195 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6196 vixl32::Label done, zero;
6197 SlowPathCodeARMVIXL* slow_path = nullptr;
6198
6199 // Return 0 if `obj` is null.
6200 // avoid null check if we know obj is not null.
6201 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006202 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006203 }
6204
Artem Serovcfbe9132016-10-14 15:58:56 +01006205 switch (type_check_kind) {
6206 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006207 // /* HeapReference<Class> */ out = obj->klass_
6208 GenerateReferenceLoadTwoRegisters(instruction,
6209 out_loc,
6210 obj_loc,
6211 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006212 maybe_temp_loc,
6213 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006214 __ Cmp(out, cls);
6215 // Classes must be equal for the instanceof to succeed.
6216 __ B(ne, &zero);
6217 __ Mov(out, 1);
6218 __ B(&done);
6219 break;
6220 }
6221
6222 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006223 // /* HeapReference<Class> */ out = obj->klass_
6224 GenerateReferenceLoadTwoRegisters(instruction,
6225 out_loc,
6226 obj_loc,
6227 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006228 maybe_temp_loc,
6229 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006230 // If the class is abstract, we eagerly fetch the super class of the
6231 // object to avoid doing a comparison we know will fail.
6232 vixl32::Label loop;
6233 __ Bind(&loop);
6234 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006235 GenerateReferenceLoadOneRegister(instruction,
6236 out_loc,
6237 super_offset,
6238 maybe_temp_loc,
6239 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006240 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006241 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006242 __ Cmp(out, cls);
6243 __ B(ne, &loop);
6244 __ Mov(out, 1);
6245 if (zero.IsReferenced()) {
6246 __ B(&done);
6247 }
6248 break;
6249 }
6250
6251 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006252 // /* HeapReference<Class> */ out = obj->klass_
6253 GenerateReferenceLoadTwoRegisters(instruction,
6254 out_loc,
6255 obj_loc,
6256 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006257 maybe_temp_loc,
6258 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006259 // Walk over the class hierarchy to find a match.
6260 vixl32::Label loop, success;
6261 __ Bind(&loop);
6262 __ Cmp(out, cls);
6263 __ B(eq, &success);
6264 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006265 GenerateReferenceLoadOneRegister(instruction,
6266 out_loc,
6267 super_offset,
6268 maybe_temp_loc,
6269 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006270 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006271 // If `out` is null, we use it for the result, and jump to `done`.
6272 __ B(&done);
6273 __ Bind(&success);
6274 __ Mov(out, 1);
6275 if (zero.IsReferenced()) {
6276 __ B(&done);
6277 }
6278 break;
6279 }
6280
6281 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006282 // /* HeapReference<Class> */ out = obj->klass_
6283 GenerateReferenceLoadTwoRegisters(instruction,
6284 out_loc,
6285 obj_loc,
6286 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006287 maybe_temp_loc,
6288 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006289 // Do an exact check.
6290 vixl32::Label exact_check;
6291 __ Cmp(out, cls);
6292 __ B(eq, &exact_check);
6293 // Otherwise, we need to check that the object's class is a non-primitive array.
6294 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006295 GenerateReferenceLoadOneRegister(instruction,
6296 out_loc,
6297 component_offset,
6298 maybe_temp_loc,
6299 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006300 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006301 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006302 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6303 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006304 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006305 __ Bind(&exact_check);
6306 __ Mov(out, 1);
6307 __ B(&done);
6308 break;
6309 }
6310
6311 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006312 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006313 // /* HeapReference<Class> */ out = obj->klass_
6314 GenerateReferenceLoadTwoRegisters(instruction,
6315 out_loc,
6316 obj_loc,
6317 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006318 maybe_temp_loc,
6319 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006320 __ Cmp(out, cls);
6321 DCHECK(locations->OnlyCallsOnSlowPath());
6322 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6323 /* is_fatal */ false);
6324 codegen_->AddSlowPath(slow_path);
6325 __ B(ne, slow_path->GetEntryLabel());
6326 __ Mov(out, 1);
6327 if (zero.IsReferenced()) {
6328 __ B(&done);
6329 }
6330 break;
6331 }
6332
6333 case TypeCheckKind::kUnresolvedCheck:
6334 case TypeCheckKind::kInterfaceCheck: {
6335 // Note that we indeed only call on slow path, but we always go
6336 // into the slow path for the unresolved and interface check
6337 // cases.
6338 //
6339 // We cannot directly call the InstanceofNonTrivial runtime
6340 // entry point without resorting to a type checking slow path
6341 // here (i.e. by calling InvokeRuntime directly), as it would
6342 // require to assign fixed registers for the inputs of this
6343 // HInstanceOf instruction (following the runtime calling
6344 // convention), which might be cluttered by the potential first
6345 // read barrier emission at the beginning of this method.
6346 //
6347 // TODO: Introduce a new runtime entry point taking the object
6348 // to test (instead of its class) as argument, and let it deal
6349 // with the read barrier issues. This will let us refactor this
6350 // case of the `switch` code as it was previously (with a direct
6351 // call to the runtime not using a type checking slow path).
6352 // This should also be beneficial for the other cases above.
6353 DCHECK(locations->OnlyCallsOnSlowPath());
6354 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6355 /* is_fatal */ false);
6356 codegen_->AddSlowPath(slow_path);
6357 __ B(slow_path->GetEntryLabel());
6358 if (zero.IsReferenced()) {
6359 __ B(&done);
6360 }
6361 break;
6362 }
6363 }
6364
6365 if (zero.IsReferenced()) {
6366 __ Bind(&zero);
6367 __ Mov(out, 0);
6368 }
6369
6370 if (done.IsReferenced()) {
6371 __ Bind(&done);
6372 }
6373
6374 if (slow_path != nullptr) {
6375 __ Bind(slow_path->GetExitLabel());
6376 }
6377}
6378
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006379void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6380 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6381 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6382
6383 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6384 switch (type_check_kind) {
6385 case TypeCheckKind::kExactCheck:
6386 case TypeCheckKind::kAbstractClassCheck:
6387 case TypeCheckKind::kClassHierarchyCheck:
6388 case TypeCheckKind::kArrayObjectCheck:
6389 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6390 LocationSummary::kCallOnSlowPath :
6391 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6392 break;
6393 case TypeCheckKind::kArrayCheck:
6394 case TypeCheckKind::kUnresolvedCheck:
6395 case TypeCheckKind::kInterfaceCheck:
6396 call_kind = LocationSummary::kCallOnSlowPath;
6397 break;
6398 }
6399
6400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6401 locations->SetInAt(0, Location::RequiresRegister());
6402 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006403 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006404}
6405
6406void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6407 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6408 LocationSummary* locations = instruction->GetLocations();
6409 Location obj_loc = locations->InAt(0);
6410 vixl32::Register obj = InputRegisterAt(instruction, 0);
6411 vixl32::Register cls = InputRegisterAt(instruction, 1);
6412 Location temp_loc = locations->GetTemp(0);
6413 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006414 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6415 DCHECK_LE(num_temps, 3u);
6416 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6417 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6418 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6419 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6420 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6421 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6422 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6423 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6424 const uint32_t object_array_data_offset =
6425 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006426
Artem Serov657022c2016-11-23 14:19:38 +00006427 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6428 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6429 // read barriers is done for performance and code size reasons.
6430 bool is_type_check_slow_path_fatal = false;
6431 if (!kEmitCompilerReadBarrier) {
6432 is_type_check_slow_path_fatal =
6433 (type_check_kind == TypeCheckKind::kExactCheck ||
6434 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6435 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6436 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6437 !instruction->CanThrowIntoCatchBlock();
6438 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006439 SlowPathCodeARMVIXL* type_check_slow_path =
6440 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6441 is_type_check_slow_path_fatal);
6442 codegen_->AddSlowPath(type_check_slow_path);
6443
6444 vixl32::Label done;
6445 // Avoid null check if we know obj is not null.
6446 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006447 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006448 }
6449
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006450 switch (type_check_kind) {
6451 case TypeCheckKind::kExactCheck:
6452 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006453 // /* HeapReference<Class> */ temp = obj->klass_
6454 GenerateReferenceLoadTwoRegisters(instruction,
6455 temp_loc,
6456 obj_loc,
6457 class_offset,
6458 maybe_temp2_loc,
6459 kWithoutReadBarrier);
6460
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006461 __ Cmp(temp, cls);
6462 // Jump to slow path for throwing the exception or doing a
6463 // more involved array check.
6464 __ B(ne, type_check_slow_path->GetEntryLabel());
6465 break;
6466 }
6467
6468 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006469 // /* HeapReference<Class> */ temp = obj->klass_
6470 GenerateReferenceLoadTwoRegisters(instruction,
6471 temp_loc,
6472 obj_loc,
6473 class_offset,
6474 maybe_temp2_loc,
6475 kWithoutReadBarrier);
6476
Artem Serovcfbe9132016-10-14 15:58:56 +01006477 // If the class is abstract, we eagerly fetch the super class of the
6478 // object to avoid doing a comparison we know will fail.
6479 vixl32::Label loop;
6480 __ Bind(&loop);
6481 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006482 GenerateReferenceLoadOneRegister(instruction,
6483 temp_loc,
6484 super_offset,
6485 maybe_temp2_loc,
6486 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006487
6488 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6489 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006490 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006491
6492 // Otherwise, compare the classes.
6493 __ Cmp(temp, cls);
6494 __ B(ne, &loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006495 break;
6496 }
6497
6498 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006499 // /* HeapReference<Class> */ temp = obj->klass_
6500 GenerateReferenceLoadTwoRegisters(instruction,
6501 temp_loc,
6502 obj_loc,
6503 class_offset,
6504 maybe_temp2_loc,
6505 kWithoutReadBarrier);
6506
Artem Serovcfbe9132016-10-14 15:58:56 +01006507 // Walk over the class hierarchy to find a match.
6508 vixl32::Label loop;
6509 __ Bind(&loop);
6510 __ Cmp(temp, cls);
6511 __ B(eq, &done);
6512
6513 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006514 GenerateReferenceLoadOneRegister(instruction,
6515 temp_loc,
6516 super_offset,
6517 maybe_temp2_loc,
6518 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006519
6520 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6521 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006522 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006523 // Otherwise, jump to the beginning of the loop.
6524 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006525 break;
6526 }
6527
Artem Serovcfbe9132016-10-14 15:58:56 +01006528 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006529 // /* HeapReference<Class> */ temp = obj->klass_
6530 GenerateReferenceLoadTwoRegisters(instruction,
6531 temp_loc,
6532 obj_loc,
6533 class_offset,
6534 maybe_temp2_loc,
6535 kWithoutReadBarrier);
6536
Artem Serovcfbe9132016-10-14 15:58:56 +01006537 // Do an exact check.
6538 __ Cmp(temp, cls);
6539 __ B(eq, &done);
6540
6541 // Otherwise, we need to check that the object's class is a non-primitive array.
6542 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006543 GenerateReferenceLoadOneRegister(instruction,
6544 temp_loc,
6545 component_offset,
6546 maybe_temp2_loc,
6547 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006548 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006549 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006550 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6551 // to further check that this component type is not a primitive type.
6552 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6553 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006554 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006555 break;
6556 }
6557
6558 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006559 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006560 // We cannot directly call the CheckCast runtime entry point
6561 // without resorting to a type checking slow path here (i.e. by
6562 // calling InvokeRuntime directly), as it would require to
6563 // assign fixed registers for the inputs of this HInstanceOf
6564 // instruction (following the runtime calling convention), which
6565 // might be cluttered by the potential first read barrier
6566 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006567
Artem Serovcfbe9132016-10-14 15:58:56 +01006568 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006569 break;
Artem Serov657022c2016-11-23 14:19:38 +00006570
6571 case TypeCheckKind::kInterfaceCheck: {
6572 // Avoid read barriers to improve performance of the fast path. We can not get false
6573 // positives by doing this.
6574 // /* HeapReference<Class> */ temp = obj->klass_
6575 GenerateReferenceLoadTwoRegisters(instruction,
6576 temp_loc,
6577 obj_loc,
6578 class_offset,
6579 maybe_temp2_loc,
6580 kWithoutReadBarrier);
6581
6582 // /* HeapReference<Class> */ temp = temp->iftable_
6583 GenerateReferenceLoadTwoRegisters(instruction,
6584 temp_loc,
6585 temp_loc,
6586 iftable_offset,
6587 maybe_temp2_loc,
6588 kWithoutReadBarrier);
6589 // Iftable is never null.
6590 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6591 // Loop through the iftable and check if any class matches.
6592 vixl32::Label start_loop;
6593 __ Bind(&start_loop);
6594 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6595 type_check_slow_path->GetEntryLabel());
6596 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6597 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6598 // Go to next interface.
6599 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6600 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6601 // Compare the classes and continue the loop if they do not match.
6602 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
6603 __ B(ne, &start_loop);
6604 break;
6605 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006606 }
6607 __ Bind(&done);
6608
6609 __ Bind(type_check_slow_path->GetExitLabel());
6610}
6611
Artem Serov551b28f2016-10-18 19:11:30 +01006612void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6613 LocationSummary* locations =
6614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6615 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6616 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6617}
6618
6619void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6620 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6621 instruction,
6622 instruction->GetDexPc());
6623 if (instruction->IsEnter()) {
6624 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6625 } else {
6626 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6627 }
6628}
6629
Artem Serov02109dd2016-09-23 17:17:54 +01006630void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6631 HandleBitwiseOperation(instruction, AND);
6632}
6633
6634void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6635 HandleBitwiseOperation(instruction, ORR);
6636}
6637
6638void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6639 HandleBitwiseOperation(instruction, EOR);
6640}
6641
6642void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6643 LocationSummary* locations =
6644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6645 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6646 || instruction->GetResultType() == Primitive::kPrimLong);
6647 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6648 locations->SetInAt(0, Location::RequiresRegister());
6649 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6651}
6652
6653void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6654 HandleBitwiseOperation(instruction);
6655}
6656
6657void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6658 HandleBitwiseOperation(instruction);
6659}
6660
6661void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6662 HandleBitwiseOperation(instruction);
6663}
6664
Artem Serov2bbc9532016-10-21 11:51:50 +01006665void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6666 LocationSummary* locations =
6667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6668 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6669 || instruction->GetResultType() == Primitive::kPrimLong);
6670
6671 locations->SetInAt(0, Location::RequiresRegister());
6672 locations->SetInAt(1, Location::RequiresRegister());
6673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6674}
6675
6676void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6677 LocationSummary* locations = instruction->GetLocations();
6678 Location first = locations->InAt(0);
6679 Location second = locations->InAt(1);
6680 Location out = locations->Out();
6681
6682 if (instruction->GetResultType() == Primitive::kPrimInt) {
6683 vixl32::Register first_reg = RegisterFrom(first);
6684 vixl32::Register second_reg = RegisterFrom(second);
6685 vixl32::Register out_reg = RegisterFrom(out);
6686
6687 switch (instruction->GetOpKind()) {
6688 case HInstruction::kAnd:
6689 __ Bic(out_reg, first_reg, second_reg);
6690 break;
6691 case HInstruction::kOr:
6692 __ Orn(out_reg, first_reg, second_reg);
6693 break;
6694 // There is no EON on arm.
6695 case HInstruction::kXor:
6696 default:
6697 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6698 UNREACHABLE();
6699 }
6700 return;
6701
6702 } else {
6703 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6704 vixl32::Register first_low = LowRegisterFrom(first);
6705 vixl32::Register first_high = HighRegisterFrom(first);
6706 vixl32::Register second_low = LowRegisterFrom(second);
6707 vixl32::Register second_high = HighRegisterFrom(second);
6708 vixl32::Register out_low = LowRegisterFrom(out);
6709 vixl32::Register out_high = HighRegisterFrom(out);
6710
6711 switch (instruction->GetOpKind()) {
6712 case HInstruction::kAnd:
6713 __ Bic(out_low, first_low, second_low);
6714 __ Bic(out_high, first_high, second_high);
6715 break;
6716 case HInstruction::kOr:
6717 __ Orn(out_low, first_low, second_low);
6718 __ Orn(out_high, first_high, second_high);
6719 break;
6720 // There is no EON on arm.
6721 case HInstruction::kXor:
6722 default:
6723 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6724 UNREACHABLE();
6725 }
6726 }
6727}
6728
Artem Serov02109dd2016-09-23 17:17:54 +01006729// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6730void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
6731 vixl32::Register first,
6732 uint32_t value) {
6733 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6734 if (value == 0xffffffffu) {
6735 if (!out.Is(first)) {
6736 __ Mov(out, first);
6737 }
6738 return;
6739 }
6740 if (value == 0u) {
6741 __ Mov(out, 0);
6742 return;
6743 }
6744 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
6745 __ And(out, first, value);
6746 } else {
6747 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
6748 __ Bic(out, first, ~value);
6749 }
6750}
6751
6752// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6753void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
6754 vixl32::Register first,
6755 uint32_t value) {
6756 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6757 if (value == 0u) {
6758 if (!out.Is(first)) {
6759 __ Mov(out, first);
6760 }
6761 return;
6762 }
6763 if (value == 0xffffffffu) {
6764 __ Mvn(out, 0);
6765 return;
6766 }
6767 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
6768 __ Orr(out, first, value);
6769 } else {
6770 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
6771 __ Orn(out, first, ~value);
6772 }
6773}
6774
6775// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6776void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
6777 vixl32::Register first,
6778 uint32_t value) {
6779 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6780 if (value == 0u) {
6781 if (!out.Is(first)) {
6782 __ Mov(out, first);
6783 }
6784 return;
6785 }
6786 __ Eor(out, first, value);
6787}
6788
Anton Kirilovdda43962016-11-21 19:55:20 +00006789void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
6790 Location first,
6791 uint64_t value) {
6792 vixl32::Register out_low = LowRegisterFrom(out);
6793 vixl32::Register out_high = HighRegisterFrom(out);
6794 vixl32::Register first_low = LowRegisterFrom(first);
6795 vixl32::Register first_high = HighRegisterFrom(first);
6796 uint32_t value_low = Low32Bits(value);
6797 uint32_t value_high = High32Bits(value);
6798 if (value_low == 0u) {
6799 if (!out_low.Is(first_low)) {
6800 __ Mov(out_low, first_low);
6801 }
6802 __ Add(out_high, first_high, value_high);
6803 return;
6804 }
6805 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006806 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006807 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006808 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006809 __ Sbc(out_high, first_high, ~value_high);
6810 } else {
6811 LOG(FATAL) << "Unexpected constant " << value_high;
6812 UNREACHABLE();
6813 }
6814}
6815
Artem Serov02109dd2016-09-23 17:17:54 +01006816void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
6817 LocationSummary* locations = instruction->GetLocations();
6818 Location first = locations->InAt(0);
6819 Location second = locations->InAt(1);
6820 Location out = locations->Out();
6821
6822 if (second.IsConstant()) {
6823 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6824 uint32_t value_low = Low32Bits(value);
6825 if (instruction->GetResultType() == Primitive::kPrimInt) {
6826 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6827 vixl32::Register out_reg = OutputRegister(instruction);
6828 if (instruction->IsAnd()) {
6829 GenerateAndConst(out_reg, first_reg, value_low);
6830 } else if (instruction->IsOr()) {
6831 GenerateOrrConst(out_reg, first_reg, value_low);
6832 } else {
6833 DCHECK(instruction->IsXor());
6834 GenerateEorConst(out_reg, first_reg, value_low);
6835 }
6836 } else {
6837 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6838 uint32_t value_high = High32Bits(value);
6839 vixl32::Register first_low = LowRegisterFrom(first);
6840 vixl32::Register first_high = HighRegisterFrom(first);
6841 vixl32::Register out_low = LowRegisterFrom(out);
6842 vixl32::Register out_high = HighRegisterFrom(out);
6843 if (instruction->IsAnd()) {
6844 GenerateAndConst(out_low, first_low, value_low);
6845 GenerateAndConst(out_high, first_high, value_high);
6846 } else if (instruction->IsOr()) {
6847 GenerateOrrConst(out_low, first_low, value_low);
6848 GenerateOrrConst(out_high, first_high, value_high);
6849 } else {
6850 DCHECK(instruction->IsXor());
6851 GenerateEorConst(out_low, first_low, value_low);
6852 GenerateEorConst(out_high, first_high, value_high);
6853 }
6854 }
6855 return;
6856 }
6857
6858 if (instruction->GetResultType() == Primitive::kPrimInt) {
6859 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6860 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
6861 vixl32::Register out_reg = OutputRegister(instruction);
6862 if (instruction->IsAnd()) {
6863 __ And(out_reg, first_reg, second_reg);
6864 } else if (instruction->IsOr()) {
6865 __ Orr(out_reg, first_reg, second_reg);
6866 } else {
6867 DCHECK(instruction->IsXor());
6868 __ Eor(out_reg, first_reg, second_reg);
6869 }
6870 } else {
6871 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6872 vixl32::Register first_low = LowRegisterFrom(first);
6873 vixl32::Register first_high = HighRegisterFrom(first);
6874 vixl32::Register second_low = LowRegisterFrom(second);
6875 vixl32::Register second_high = HighRegisterFrom(second);
6876 vixl32::Register out_low = LowRegisterFrom(out);
6877 vixl32::Register out_high = HighRegisterFrom(out);
6878 if (instruction->IsAnd()) {
6879 __ And(out_low, first_low, second_low);
6880 __ And(out_high, first_high, second_high);
6881 } else if (instruction->IsOr()) {
6882 __ Orr(out_low, first_low, second_low);
6883 __ Orr(out_high, first_high, second_high);
6884 } else {
6885 DCHECK(instruction->IsXor());
6886 __ Eor(out_low, first_low, second_low);
6887 __ Eor(out_high, first_high, second_high);
6888 }
6889 }
6890}
6891
Artem Serovcfbe9132016-10-14 15:58:56 +01006892void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006893 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01006894 Location out,
6895 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006896 Location maybe_temp,
6897 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01006898 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006899 if (read_barrier_option == kWithReadBarrier) {
6900 CHECK(kEmitCompilerReadBarrier);
6901 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6902 if (kUseBakerReadBarrier) {
6903 // Load with fast path based Baker's read barrier.
6904 // /* HeapReference<Object> */ out = *(out + offset)
6905 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6906 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
6907 } else {
6908 // Load with slow path based read barrier.
6909 // Save the value of `out` into `maybe_temp` before overwriting it
6910 // in the following move operation, as we will need it for the
6911 // read barrier below.
6912 __ Mov(RegisterFrom(maybe_temp), out_reg);
6913 // /* HeapReference<Object> */ out = *(out + offset)
6914 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6915 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6916 }
Artem Serovcfbe9132016-10-14 15:58:56 +01006917 } else {
6918 // Plain load with no read barrier.
6919 // /* HeapReference<Object> */ out = *(out + offset)
6920 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6921 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6922 }
6923}
6924
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006925void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006926 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006927 Location out,
6928 Location obj,
6929 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006930 Location maybe_temp,
6931 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006932 vixl32::Register out_reg = RegisterFrom(out);
6933 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006934 if (read_barrier_option == kWithReadBarrier) {
6935 CHECK(kEmitCompilerReadBarrier);
6936 if (kUseBakerReadBarrier) {
6937 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6938 // Load with fast path based Baker's read barrier.
6939 // /* HeapReference<Object> */ out = *(obj + offset)
6940 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6941 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
6942 } else {
6943 // Load with slow path based read barrier.
6944 // /* HeapReference<Object> */ out = *(obj + offset)
6945 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6946 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6947 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006948 } else {
6949 // Plain load with no read barrier.
6950 // /* HeapReference<Object> */ out = *(obj + offset)
6951 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6952 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6953 }
6954}
6955
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006956void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006957 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006958 Location root,
6959 vixl32::Register obj,
6960 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00006961 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006962 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006963 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006964 DCHECK(kEmitCompilerReadBarrier);
6965 if (kUseBakerReadBarrier) {
6966 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6967 // Baker's read barrier are used:
6968 //
6969 // root = obj.field;
6970 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6971 // if (temp != null) {
6972 // root = temp(root)
6973 // }
6974
6975 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6976 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
6977 static_assert(
6978 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6979 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6980 "have different sizes.");
6981 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6982 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6983 "have different sizes.");
6984
6985 // Slow path marking the GC root `root`.
6986 Location temp = LocationFrom(lr);
6987 SlowPathCodeARMVIXL* slow_path =
6988 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
6989 instruction,
6990 root,
6991 /*entrypoint*/ temp);
6992 codegen_->AddSlowPath(slow_path);
6993
6994 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6995 const int32_t entry_point_offset =
6996 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6997 // Loading the entrypoint does not require a load acquire since it is only changed when
6998 // threads are suspended or running a checkpoint.
6999 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
7000 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7001 // checking GetIsGcMarking.
7002 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7003 __ Bind(slow_path->GetExitLabel());
7004 } else {
7005 // GC root loaded through a slow path for read barriers other
7006 // than Baker's.
7007 // /* GcRoot<mirror::Object>* */ root = obj + offset
7008 __ Add(root_reg, obj, offset);
7009 // /* mirror::Object* */ root = root->Read()
7010 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7011 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007012 } else {
7013 // Plain GC root load with no read barrier.
7014 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7015 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7016 // Note that GC roots are not affected by heap poisoning, thus we
7017 // do not have to unpoison `root_reg` here.
7018 }
7019}
7020
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007021void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7022 Location ref,
7023 vixl32::Register obj,
7024 uint32_t offset,
7025 Location temp,
7026 bool needs_null_check) {
7027 DCHECK(kEmitCompilerReadBarrier);
7028 DCHECK(kUseBakerReadBarrier);
7029
7030 // /* HeapReference<Object> */ ref = *(obj + offset)
7031 Location no_index = Location::NoLocation();
7032 ScaleFactor no_scale_factor = TIMES_1;
7033 GenerateReferenceLoadWithBakerReadBarrier(
7034 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007035}
7036
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007037void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7038 Location ref,
7039 vixl32::Register obj,
7040 uint32_t data_offset,
7041 Location index,
7042 Location temp,
7043 bool needs_null_check) {
7044 DCHECK(kEmitCompilerReadBarrier);
7045 DCHECK(kUseBakerReadBarrier);
7046
7047 static_assert(
7048 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7049 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7050 // /* HeapReference<Object> */ ref =
7051 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7052 ScaleFactor scale_factor = TIMES_4;
7053 GenerateReferenceLoadWithBakerReadBarrier(
7054 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007055}
7056
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007057void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7058 Location ref,
7059 vixl32::Register obj,
7060 uint32_t offset,
7061 Location index,
7062 ScaleFactor scale_factor,
7063 Location temp,
7064 bool needs_null_check,
7065 bool always_update_field,
7066 vixl32::Register* temp2) {
7067 DCHECK(kEmitCompilerReadBarrier);
7068 DCHECK(kUseBakerReadBarrier);
7069
7070 // In slow path based read barriers, the read barrier call is
7071 // inserted after the original load. However, in fast path based
7072 // Baker's read barriers, we need to perform the load of
7073 // mirror::Object::monitor_ *before* the original reference load.
7074 // This load-load ordering is required by the read barrier.
7075 // The fast path/slow path (for Baker's algorithm) should look like:
7076 //
7077 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7078 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7079 // HeapReference<Object> ref = *src; // Original reference load.
7080 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7081 // if (is_gray) {
7082 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7083 // }
7084 //
7085 // Note: the original implementation in ReadBarrier::Barrier is
7086 // slightly more complex as it performs additional checks that we do
7087 // not do here for performance reasons.
7088
7089 vixl32::Register ref_reg = RegisterFrom(ref);
7090 vixl32::Register temp_reg = RegisterFrom(temp);
7091 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7092
7093 // /* int32_t */ monitor = obj->monitor_
7094 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7095 if (needs_null_check) {
7096 MaybeRecordImplicitNullCheck(instruction);
7097 }
7098 // /* LockWord */ lock_word = LockWord(monitor)
7099 static_assert(sizeof(LockWord) == sizeof(int32_t),
7100 "art::LockWord and int32_t have different sizes.");
7101
7102 // Introduce a dependency on the lock_word including the rb_state,
7103 // which shall prevent load-load reordering without using
7104 // a memory barrier (which would be more expensive).
7105 // `obj` is unchanged by this operation, but its value now depends
7106 // on `temp_reg`.
7107 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7108
7109 // The actual reference load.
7110 if (index.IsValid()) {
7111 // Load types involving an "index": ArrayGet,
7112 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7113 // intrinsics.
7114 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7115 if (index.IsConstant()) {
7116 size_t computed_offset =
7117 (Int32ConstantFrom(index) << scale_factor) + offset;
7118 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7119 } else {
7120 // Handle the special case of the
7121 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7122 // intrinsics, which use a register pair as index ("long
7123 // offset"), of which only the low part contains data.
7124 vixl32::Register index_reg = index.IsRegisterPair()
7125 ? LowRegisterFrom(index)
7126 : RegisterFrom(index);
7127 UseScratchRegisterScope temps(GetVIXLAssembler());
7128 const vixl32::Register temp3 = temps.Acquire();
7129 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7130 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7131 }
7132 } else {
7133 // /* HeapReference<Object> */ ref = *(obj + offset)
7134 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7135 }
7136
7137 // Object* ref = ref_addr->AsMirrorPtr()
7138 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7139
7140 // Slow path marking the object `ref` when it is gray.
7141 SlowPathCodeARMVIXL* slow_path;
7142 if (always_update_field) {
7143 DCHECK(temp2 != nullptr);
7144 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7145 // of the form `obj + field_offset`, where `obj` is a register and
7146 // `field_offset` is a register pair (of which only the lower half
7147 // is used). Thus `offset` and `scale_factor` above are expected
7148 // to be null in this code path.
7149 DCHECK_EQ(offset, 0u);
7150 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7151 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
7152 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7153 } else {
7154 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
7155 }
7156 AddSlowPath(slow_path);
7157
7158 // if (rb_state == ReadBarrier::GrayState())
7159 // ref = ReadBarrier::Mark(ref);
7160 // Given the numeric representation, it's enough to check the low bit of the
7161 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7162 // which can be a 16-bit instruction unlike the TST immediate.
7163 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7164 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7165 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7166 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
7167 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007168}
7169
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007170void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7171 Location out,
7172 Location ref,
7173 Location obj,
7174 uint32_t offset,
7175 Location index) {
7176 DCHECK(kEmitCompilerReadBarrier);
7177
7178 // Insert a slow path based read barrier *after* the reference load.
7179 //
7180 // If heap poisoning is enabled, the unpoisoning of the loaded
7181 // reference will be carried out by the runtime within the slow
7182 // path.
7183 //
7184 // Note that `ref` currently does not get unpoisoned (when heap
7185 // poisoning is enabled), which is alright as the `ref` argument is
7186 // not used by the artReadBarrierSlow entry point.
7187 //
7188 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7189 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7190 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7191 AddSlowPath(slow_path);
7192
7193 __ B(slow_path->GetEntryLabel());
7194 __ Bind(slow_path->GetExitLabel());
7195}
7196
7197void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007198 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007199 Location ref,
7200 Location obj,
7201 uint32_t offset,
7202 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007203 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007204 // Baker's read barriers shall be handled by the fast path
7205 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007206 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007207 // If heap poisoning is enabled, unpoisoning will be taken care of
7208 // by the runtime within the slow path.
7209 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007210 } else if (kPoisonHeapReferences) {
7211 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7212 }
7213}
7214
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007215void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7216 Location out,
7217 Location root) {
7218 DCHECK(kEmitCompilerReadBarrier);
7219
7220 // Insert a slow path based read barrier *after* the GC root load.
7221 //
7222 // Note that GC roots are not affected by heap poisoning, so we do
7223 // not need to do anything special for this here.
7224 SlowPathCodeARMVIXL* slow_path =
7225 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7226 AddSlowPath(slow_path);
7227
7228 __ B(slow_path->GetEntryLabel());
7229 __ Bind(slow_path->GetExitLabel());
7230}
7231
Artem Serov02d37832016-10-25 15:25:33 +01007232// Check if the desired_dispatch_info is supported. If it is, return it,
7233// otherwise return a fall-back info that should be used instead.
7234HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007235 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
7236 HInvokeStaticOrDirect* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007237 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7238 // We disable pc-relative load when there is an irreducible loop, as the optimization
7239 // is incompatible with it.
7240 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7241 // with irreducible loops.
7242 if (GetGraph()->HasIrreducibleLoops() &&
7243 (dispatch_info.method_load_kind ==
7244 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7245 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7246 }
7247
7248 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
7249 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
7250 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
7251 // Calls across dex files are more likely to exceed the available BL range,
7252 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7253 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7254 (desired_dispatch_info.method_load_kind ==
7255 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7256 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7257 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7258 return HInvokeStaticOrDirect::DispatchInfo {
7259 dispatch_info.method_load_kind,
7260 code_ptr_location,
7261 dispatch_info.method_load_data,
7262 0u
7263 };
7264 }
7265 }
7266 return dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007267}
7268
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007269vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7270 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7271 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7272 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7273 if (!invoke->GetLocations()->Intrinsified()) {
7274 return RegisterFrom(location);
7275 }
7276 // For intrinsics we allow any location, so it may be on the stack.
7277 if (!location.IsRegister()) {
7278 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7279 return temp;
7280 }
7281 // For register locations, check if the register was saved. If so, get it from the stack.
7282 // Note: There is a chance that the register was saved but not overwritten, so we could
7283 // save one load. However, since this is just an intrinsic slow path we prefer this
7284 // simple and more robust approach rather that trying to determine if that's the case.
7285 SlowPathCode* slow_path = GetCurrentSlowPath();
7286 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7287 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
7288 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7289 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7290 return temp;
7291 }
7292 return RegisterFrom(location);
7293}
7294
7295void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
7296 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007297 // For better instruction scheduling we load the direct code pointer before the method pointer.
7298 switch (invoke->GetCodePtrLocation()) {
7299 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7300 // LR = code address from literal pool with link-time patch.
Artem Serovc5fcb442016-12-02 19:19:58 +00007301 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007302 break;
7303 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7304 // LR = invoke->GetDirectCodePtr();
7305 __ Mov(lr, Operand::From(invoke->GetDirectCodePtr()));
7306 break;
7307 default:
7308 break;
7309 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007310
Artem Serovd4cc5b22016-11-04 11:19:09 +00007311 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007312 switch (invoke->GetMethodLoadKind()) {
7313 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7314 uint32_t offset =
7315 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7316 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007317 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7318 break;
7319 }
7320 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7321 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7322 break;
7323 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7324 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7325 break;
7326 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Artem Serovc5fcb442016-12-02 19:19:58 +00007327 __ Ldr(RegisterFrom(temp), DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007328 break;
7329 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7330 HArmDexCacheArraysBase* base =
7331 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7332 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7333 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7334 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007335 break;
7336 }
7337 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7338 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7339 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007340 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007341 if (current_method.IsRegister()) {
7342 method_reg = RegisterFrom(current_method);
7343 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007344 DCHECK(invoke->GetLocations()->Intrinsified());
7345 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007346 method_reg = reg;
7347 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007348 }
7349 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7350 GetAssembler()->LoadFromOffset(
7351 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007352 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007353 method_reg,
7354 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7355 // temp = temp[index_in_cache];
7356 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7357 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7358 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007359 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007360 break;
7361 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007362 }
7363
Artem Serovd4cc5b22016-11-04 11:19:09 +00007364 switch (invoke->GetCodePtrLocation()) {
7365 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7366 __ Bl(GetFrameEntryLabel());
7367 break;
7368 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
7369 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7370 invoke->GetTargetMethod().dex_method_index);
7371 {
Artem Serov0fb37192016-12-06 18:13:40 +00007372 ExactAssemblyScope aas(GetVIXLAssembler(),
7373 vixl32::kMaxInstructionSizeInBytes,
7374 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007375 __ bind(&relative_call_patches_.back().label);
7376 // Arbitrarily branch to the BL itself, override at link time.
7377 __ bl(&relative_call_patches_.back().label);
7378 }
7379 break;
7380 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7381 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7382 // LR prepared above for better instruction scheduling.
7383 // LR()
Alexandre Rames374ddf32016-11-04 10:40:49 +00007384 {
7385 // 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 +00007386 ExactAssemblyScope aas(GetVIXLAssembler(),
7387 vixl32::k16BitT32InstructionSizeInBytes,
7388 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007389 __ blx(lr);
7390 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007391 break;
7392 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7393 // LR = callee_method->entry_point_from_quick_compiled_code_
7394 GetAssembler()->LoadFromOffset(
7395 kLoadWord,
7396 lr,
7397 RegisterFrom(callee_method),
7398 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007399 {
7400 // 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 +00007401 ExactAssemblyScope aas(GetVIXLAssembler(),
7402 vixl32::k16BitT32InstructionSizeInBytes,
7403 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007404 // LR()
7405 __ blx(lr);
7406 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007407 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007408 }
7409
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007410 DCHECK(!IsLeafMethod());
7411}
7412
7413void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7414 vixl32::Register temp = RegisterFrom(temp_location);
7415 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7416 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7417
7418 // Use the calling convention instead of the location of the receiver, as
7419 // intrinsics may have put the receiver in a different register. In the intrinsics
7420 // slow path, the arguments have been moved to the right place, so here we are
7421 // guaranteed that the receiver is the first register of the calling convention.
7422 InvokeDexCallingConventionARMVIXL calling_convention;
7423 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7424 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007425 {
7426 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007427 ExactAssemblyScope aas(GetVIXLAssembler(),
7428 vixl32::kMaxInstructionSizeInBytes,
7429 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007430 // /* HeapReference<Class> */ temp = receiver->klass_
7431 __ ldr(temp, MemOperand(receiver, class_offset));
7432 MaybeRecordImplicitNullCheck(invoke);
7433 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007434 // Instead of simply (possibly) unpoisoning `temp` here, we should
7435 // emit a read barrier for the previous class reference load.
7436 // However this is not required in practice, as this is an
7437 // intermediate/temporary reference and because the current
7438 // concurrent copying collector keeps the from-space memory
7439 // intact/accessible until the end of the marking phase (the
7440 // concurrent copying collector may not in the future).
7441 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7442
7443 // temp = temp->GetMethodAt(method_offset);
7444 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7445 kArmPointerSize).Int32Value();
7446 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7447 // LR = temp->GetEntryPoint();
7448 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7449 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007450 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7451 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7452 // that.
7453 // 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 +00007454 ExactAssemblyScope aas(GetVIXLAssembler(),
7455 vixl32::k16BitT32InstructionSizeInBytes,
7456 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007457 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007458}
7459
Artem Serovd4cc5b22016-11-04 11:19:09 +00007460CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
7461 const DexFile& dex_file, uint32_t string_index) {
7462 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7463}
7464
7465CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7466 const DexFile& dex_file, dex::TypeIndex type_index) {
7467 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7468}
7469
7470CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7471 const DexFile& dex_file, uint32_t element_offset) {
7472 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7473}
7474
7475CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7476 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7477 patches->emplace_back(dex_file, offset_or_index);
7478 return &patches->back();
7479}
7480
Artem Serovc5fcb442016-12-02 19:19:58 +00007481VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7482 const DexFile& dex_file,
7483 dex::StringIndex string_index) {
7484 return boot_image_string_patches_.GetOrCreate(
7485 StringReference(&dex_file, string_index),
7486 [this]() {
7487 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7488 });
7489}
7490
7491VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7492 const DexFile& dex_file,
7493 dex::TypeIndex type_index) {
7494 return boot_image_type_patches_.GetOrCreate(
7495 TypeReference(&dex_file, type_index),
7496 [this]() {
7497 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7498 });
7499}
7500
7501VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
7502 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7503 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7504 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7505}
7506
7507VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7508 return DeduplicateUint32Literal(address, &uint32_literals_);
7509}
7510
7511VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(const DexFile& dex_file,
7512 dex::StringIndex string_index) {
7513 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
7514 return jit_string_patches_.GetOrCreate(
7515 StringReference(&dex_file, string_index),
7516 [this]() {
7517 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7518 });
7519}
7520
7521VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7522 dex::TypeIndex type_index,
7523 uint64_t address) {
7524 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
7525 return jit_class_patches_.GetOrCreate(
7526 TypeReference(&dex_file, type_index),
7527 [this]() {
7528 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7529 });
7530}
7531
Artem Serovd4cc5b22016-11-04 11:19:09 +00007532template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7533inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7534 const ArenaDeque<PcRelativePatchInfo>& infos,
7535 ArenaVector<LinkerPatch>* linker_patches) {
7536 for (const PcRelativePatchInfo& info : infos) {
7537 const DexFile& dex_file = info.target_dex_file;
7538 size_t offset_or_index = info.offset_or_index;
7539 DCHECK(info.add_pc_label.IsBound());
7540 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7541 // Add MOVW patch.
7542 DCHECK(info.movw_label.IsBound());
7543 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7544 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7545 // Add MOVT patch.
7546 DCHECK(info.movt_label.IsBound());
7547 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7548 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7549 }
7550}
7551
7552void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7553 DCHECK(linker_patches->empty());
7554 size_t size =
Artem Serovc5fcb442016-12-02 19:19:58 +00007555 method_patches_.size() +
7556 call_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007557 relative_call_patches_.size() +
7558 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007559 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007560 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007561 boot_image_type_patches_.size() +
7562 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
7563 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007564 linker_patches->reserve(size);
Artem Serovc5fcb442016-12-02 19:19:58 +00007565 for (const auto& entry : method_patches_) {
7566 const MethodReference& target_method = entry.first;
7567 VIXLUInt32Literal* literal = entry.second;
7568 DCHECK(literal->IsBound());
7569 uint32_t literal_offset = literal->GetLocation();
7570 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7571 target_method.dex_file,
7572 target_method.dex_method_index));
7573 }
7574 for (const auto& entry : call_patches_) {
7575 const MethodReference& target_method = entry.first;
7576 VIXLUInt32Literal* literal = entry.second;
7577 DCHECK(literal->IsBound());
7578 uint32_t literal_offset = literal->GetLocation();
7579 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7580 target_method.dex_file,
7581 target_method.dex_method_index));
7582 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007583 for (const PatchInfo<vixl32::Label>& info : relative_call_patches_) {
7584 uint32_t literal_offset = info.label.GetLocation();
7585 linker_patches->push_back(
7586 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
7587 }
7588 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7589 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007590 for (const auto& entry : boot_image_string_patches_) {
7591 const StringReference& target_string = entry.first;
7592 VIXLUInt32Literal* literal = entry.second;
7593 DCHECK(literal->IsBound());
7594 uint32_t literal_offset = literal->GetLocation();
7595 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7596 target_string.dex_file,
7597 target_string.string_index.index_));
7598 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007599 if (!GetCompilerOptions().IsBootImage()) {
7600 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7601 linker_patches);
7602 } else {
7603 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7604 linker_patches);
7605 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007606 for (const auto& entry : boot_image_type_patches_) {
7607 const TypeReference& target_type = entry.first;
7608 VIXLUInt32Literal* literal = entry.second;
7609 DCHECK(literal->IsBound());
7610 uint32_t literal_offset = literal->GetLocation();
7611 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7612 target_type.dex_file,
7613 target_type.type_index.index_));
7614 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007615 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7616 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007617 for (const auto& entry : boot_image_address_patches_) {
7618 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7619 VIXLUInt32Literal* literal = entry.second;
7620 DCHECK(literal->IsBound());
7621 uint32_t literal_offset = literal->GetLocation();
7622 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7623 }
7624}
7625
7626VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7627 uint32_t value,
7628 Uint32ToLiteralMap* map) {
7629 return map->GetOrCreate(
7630 value,
7631 [this, value]() {
7632 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7633 });
7634}
7635
7636VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7637 MethodReference target_method,
7638 MethodToLiteralMap* map) {
7639 return map->GetOrCreate(
7640 target_method,
7641 [this]() {
7642 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7643 });
7644}
7645
7646VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodAddressLiteral(
7647 MethodReference target_method) {
7648 return DeduplicateMethodLiteral(target_method, &method_patches_);
7649}
7650
7651VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodCodeLiteral(
7652 MethodReference target_method) {
7653 return DeduplicateMethodLiteral(target_method, &call_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007654}
7655
Artem Serov2bbc9532016-10-21 11:51:50 +01007656void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7657 LocationSummary* locations =
7658 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7659 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7660 Location::RequiresRegister());
7661 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7662 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7663 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7664}
7665
7666void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7667 vixl32::Register res = OutputRegister(instr);
7668 vixl32::Register accumulator =
7669 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7670 vixl32::Register mul_left =
7671 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7672 vixl32::Register mul_right =
7673 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7674
7675 if (instr->GetOpKind() == HInstruction::kAdd) {
7676 __ Mla(res, mul_left, mul_right, accumulator);
7677 } else {
7678 __ Mls(res, mul_left, mul_right, accumulator);
7679 }
7680}
7681
Artem Serov551b28f2016-10-18 19:11:30 +01007682void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7683 // Nothing to do, this should be removed during prepare for register allocator.
7684 LOG(FATAL) << "Unreachable";
7685}
7686
7687void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7688 // Nothing to do, this should be removed during prepare for register allocator.
7689 LOG(FATAL) << "Unreachable";
7690}
7691
7692// Simple implementation of packed switch - generate cascaded compare/jumps.
7693void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7694 LocationSummary* locations =
7695 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7696 locations->SetInAt(0, Location::RequiresRegister());
7697 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7698 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7699 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7700 if (switch_instr->GetStartValue() != 0) {
7701 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7702 }
7703 }
7704}
7705
7706// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7707void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7708 int32_t lower_bound = switch_instr->GetStartValue();
7709 uint32_t num_entries = switch_instr->GetNumEntries();
7710 LocationSummary* locations = switch_instr->GetLocations();
7711 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7712 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7713
7714 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7715 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7716 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007717 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007718 vixl32::Register temp_reg = temps.Acquire();
7719 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7720 // the immediate, because IP is used as the destination register. For the other
7721 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7722 // and they can be encoded in the instruction without making use of IP register.
7723 __ Adds(temp_reg, value_reg, -lower_bound);
7724
7725 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7726 // Jump to successors[0] if value == lower_bound.
7727 __ B(eq, codegen_->GetLabelOf(successors[0]));
7728 int32_t last_index = 0;
7729 for (; num_entries - last_index > 2; last_index += 2) {
7730 __ Adds(temp_reg, temp_reg, -2);
7731 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7732 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7733 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7734 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7735 }
7736 if (num_entries - last_index == 2) {
7737 // The last missing case_value.
7738 __ Cmp(temp_reg, 1);
7739 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7740 }
7741
7742 // And the default for any other value.
7743 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7744 __ B(codegen_->GetLabelOf(default_block));
7745 }
7746 } else {
7747 // Create a table lookup.
7748 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7749
7750 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7751
7752 // Remove the bias.
7753 vixl32::Register key_reg;
7754 if (lower_bound != 0) {
7755 key_reg = RegisterFrom(locations->GetTemp(1));
7756 __ Sub(key_reg, value_reg, lower_bound);
7757 } else {
7758 key_reg = value_reg;
7759 }
7760
7761 // Check whether the value is in the table, jump to default block if not.
7762 __ Cmp(key_reg, num_entries - 1);
7763 __ B(hi, codegen_->GetLabelOf(default_block));
7764
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007765 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007766 vixl32::Register jump_offset = temps.Acquire();
7767
7768 // Load jump offset from the table.
7769 __ Adr(table_base, jump_table->GetTableStartLabel());
7770 __ Ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
7771
7772 // Jump to target block by branching to table_base(pc related) + offset.
7773 vixl32::Register target_address = table_base;
7774 __ Add(target_address, table_base, jump_offset);
7775 __ Bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007776
7777 jump_table->EmitTable(codegen_);
Artem Serov551b28f2016-10-18 19:11:30 +01007778 }
7779}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007780void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7782 locations->SetOut(Location::RequiresRegister());
7783}
7784
7785void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7786 vixl32::Register base_reg = OutputRegister(base);
7787 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7788 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7789 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
7790}
Artem Serov551b28f2016-10-18 19:11:30 +01007791
Artem Serov02d37832016-10-25 15:25:33 +01007792// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007793void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7794 if (!trg.IsValid()) {
7795 DCHECK_EQ(type, Primitive::kPrimVoid);
7796 return;
7797 }
7798
7799 DCHECK_NE(type, Primitive::kPrimVoid);
7800
Artem Serovd4cc5b22016-11-04 11:19:09 +00007801 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007802 if (return_loc.Equals(trg)) {
7803 return;
7804 }
7805
7806 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7807 // with the last branch.
7808 if (type == Primitive::kPrimLong) {
7809 TODO_VIXL32(FATAL);
7810 } else if (type == Primitive::kPrimDouble) {
7811 TODO_VIXL32(FATAL);
7812 } else {
7813 // Let the parallel move resolver take care of all of this.
7814 HParallelMove parallel_move(GetGraph()->GetArena());
7815 parallel_move.AddMove(return_loc, trg, type, nullptr);
7816 GetMoveResolver()->EmitNativeCode(&parallel_move);
7817 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007818}
Scott Wakelingfe885462016-09-22 10:24:38 +01007819
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007820void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7821 LocationSummary* locations =
7822 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7823 locations->SetInAt(0, Location::RequiresRegister());
7824 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01007825}
7826
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007827void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7828 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7829 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7830 instruction->GetIndex(), kArmPointerSize).SizeValue();
7831 GetAssembler()->LoadFromOffset(kLoadWord,
7832 OutputRegister(instruction),
7833 InputRegisterAt(instruction, 0),
7834 method_offset);
7835 } else {
7836 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7837 instruction->GetIndex(), kArmPointerSize));
7838 GetAssembler()->LoadFromOffset(kLoadWord,
7839 OutputRegister(instruction),
7840 InputRegisterAt(instruction, 0),
7841 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7842 GetAssembler()->LoadFromOffset(kLoadWord,
7843 OutputRegister(instruction),
7844 OutputRegister(instruction),
7845 method_offset);
7846 }
Artem Serov551b28f2016-10-18 19:11:30 +01007847}
7848
Artem Serovc5fcb442016-12-02 19:19:58 +00007849static void PatchJitRootUse(uint8_t* code,
7850 const uint8_t* roots_data,
7851 VIXLUInt32Literal* literal,
7852 uint64_t index_in_table) {
7853 DCHECK(literal->IsBound());
7854 uint32_t literal_offset = literal->GetLocation();
7855 uintptr_t address =
7856 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7857 uint8_t* data = code + literal_offset;
7858 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7859}
7860
7861void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7862 for (const auto& entry : jit_string_patches_) {
7863 const auto& it = jit_string_roots_.find(entry.first);
7864 DCHECK(it != jit_string_roots_.end());
7865 PatchJitRootUse(code, roots_data, entry.second, it->second);
7866 }
7867 for (const auto& entry : jit_class_patches_) {
7868 const auto& it = jit_class_roots_.find(entry.first);
7869 DCHECK(it != jit_class_roots_.end());
7870 PatchJitRootUse(code, roots_data, entry.second, it->second);
7871 }
7872}
7873
Artem Serovd4cc5b22016-11-04 11:19:09 +00007874void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
7875 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
7876 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00007877 ExactAssemblyScope aas(GetVIXLAssembler(),
7878 3 * vixl32::kMaxInstructionSizeInBytes,
7879 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007880 // TODO(VIXL): Think about using mov instead of movw.
7881 __ bind(&labels->movw_label);
7882 __ movw(out, /* placeholder */ 0u);
7883 __ bind(&labels->movt_label);
7884 __ movt(out, /* placeholder */ 0u);
7885 __ bind(&labels->add_pc_label);
7886 __ add(out, out, pc);
7887}
7888
Scott Wakelingfe885462016-09-22 10:24:38 +01007889#undef __
7890#undef QUICK_ENTRY_POINT
7891#undef TODO_VIXL32
7892
7893} // namespace arm
7894} // namespace art