blob: 58bf2de70b8572447659bdd511568da729c274b3 [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)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000397 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100398 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
399 }
400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000403 Location out = locations->Out();
404 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100405
406 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000411 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
412 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
413 bool is_load_class_bss_entry =
414 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
415 vixl32::Register entry_address;
416 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
417 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
418 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
419 // the kSaveEverything call.
420 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
421 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
422 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
423 if (temp_is_r0) {
424 __ Mov(entry_address, temp);
425 }
426 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000427 dex::TypeIndex type_index = cls_->GetTypeIndex();
428 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100429 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
430 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000431 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100432 if (do_clinit_) {
433 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
434 } else {
435 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
436 }
437
Vladimir Markoea4c1262017-02-06 19:59:33 +0000438 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
439 if (is_load_class_bss_entry) {
440 if (call_saves_everything_except_r0) {
441 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
442 __ Str(r0, MemOperand(entry_address));
443 } else {
444 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
445 UseScratchRegisterScope temps(
446 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
447 vixl32::Register temp = temps.Acquire();
448 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
449 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
450 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
451 __ Str(r0, MemOperand(temp));
452 }
453 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100454 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100455 if (out.IsValid()) {
456 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
457 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
458 }
459 RestoreLiveRegisters(codegen, locations);
460 __ B(GetExitLabel());
461 }
462
463 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
464
465 private:
466 // The class this slow path will load.
467 HLoadClass* const cls_;
468
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100469 // The dex PC of `at_`.
470 const uint32_t dex_pc_;
471
472 // Whether to initialize the class.
473 const bool do_clinit_;
474
475 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
476};
477
Artem Serovd4cc5b22016-11-04 11:19:09 +0000478class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
479 public:
480 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
481 : SlowPathCodeARMVIXL(instruction) {}
482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000484 DCHECK(instruction_->IsLoadString());
485 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000486 LocationSummary* locations = instruction_->GetLocations();
487 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
488 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000489 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000490 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000491 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
492
493 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
494 __ Bind(GetEntryLabel());
495 SaveLiveRegisters(codegen, locations);
496
497 InvokeRuntimeCallingConventionARMVIXL calling_convention;
498 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000499 // the kSaveEverything call.
500 vixl32::Register entry_address;
501 if (call_saves_everything_except_r0) {
502 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
503 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
504 entry_address = temp_is_r0 ? out : temp;
505 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
506 if (temp_is_r0) {
507 __ Mov(entry_address, temp);
508 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000509 }
510
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000511 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000512 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
513 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
514
515 // Store the resolved String to the .bss entry.
516 if (call_saves_everything_except_r0) {
517 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
518 __ Str(r0, MemOperand(entry_address));
519 } else {
520 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000521 UseScratchRegisterScope temps(
522 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
523 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000524 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
525 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000526 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
527 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000528 }
529
530 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
531 RestoreLiveRegisters(codegen, locations);
532
533 __ B(GetExitLabel());
534 }
535
536 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
537
538 private:
539 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
540};
541
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100542class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
543 public:
544 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
545 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100549 DCHECK(instruction_->IsCheckCast()
550 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
551
552 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
553 __ Bind(GetEntryLabel());
554
555 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100556 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100557 }
558
559 // We're moving two locations to locations that could overlap, so we need a parallel
560 // move resolver.
561 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100562
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800563 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800564 LocationFrom(calling_convention.GetRegisterAt(0)),
565 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800566 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800567 LocationFrom(calling_convention.GetRegisterAt(1)),
568 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100569 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100570 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
571 instruction_,
572 instruction_->GetDexPc(),
573 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800574 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100575 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100576 } else {
577 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800578 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
579 instruction_,
580 instruction_->GetDexPc(),
581 this);
582 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100583 }
584
585 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100586 RestoreLiveRegisters(codegen, locations);
587 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100588 }
589 }
590
591 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
592
593 bool IsFatal() const OVERRIDE { return is_fatal_; }
594
595 private:
596 const bool is_fatal_;
597
598 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
599};
600
Scott Wakelingc34dba72016-10-03 10:14:44 +0100601class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
602 public:
603 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
604 : SlowPathCodeARMVIXL(instruction) {}
605
606 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
607 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
608 __ Bind(GetEntryLabel());
609 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
610 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
611 }
612
613 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
614
615 private:
616 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
617};
618
619class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
620 public:
621 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
622
623 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
624 LocationSummary* locations = instruction_->GetLocations();
625 __ Bind(GetEntryLabel());
626 SaveLiveRegisters(codegen, locations);
627
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
629 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
630 parallel_move.AddMove(
631 locations->InAt(0),
632 LocationFrom(calling_convention.GetRegisterAt(0)),
633 Primitive::kPrimNot,
634 nullptr);
635 parallel_move.AddMove(
636 locations->InAt(1),
637 LocationFrom(calling_convention.GetRegisterAt(1)),
638 Primitive::kPrimInt,
639 nullptr);
640 parallel_move.AddMove(
641 locations->InAt(2),
642 LocationFrom(calling_convention.GetRegisterAt(2)),
643 Primitive::kPrimNot,
644 nullptr);
645 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
646
647 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
648 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
649 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
650 RestoreLiveRegisters(codegen, locations);
651 __ B(GetExitLabel());
652 }
653
654 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
655
656 private:
657 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
658};
659
Roland Levillain47b3ab22017-02-27 14:31:35 +0000660// Slow path marking an object reference `ref` during a read
661// barrier. The field `obj.field` in the object `obj` holding this
662// reference does not get updated by this slow path after marking (see
663// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000664//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000665// This means that after the execution of this slow path, `ref` will
666// always be up-to-date, but `obj.field` may not; i.e., after the
667// flip, `ref` will be a to-space reference, but `obj.field` will
668// probably still be a from-space reference (unless it gets updated by
669// another thread, or if another thread installed another object
670// reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000671class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
672 public:
673 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
674 Location ref,
675 Location entrypoint = Location::NoLocation())
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000676 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
677 DCHECK(kEmitCompilerReadBarrier);
678 }
679
Roland Levillain47b3ab22017-02-27 14:31:35 +0000680 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000681
Roland Levillain47b3ab22017-02-27 14:31:35 +0000682 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
683 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000684 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000685 DCHECK(locations->CanCall());
686 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
687 DCHECK(instruction_->IsInstanceFieldGet() ||
688 instruction_->IsStaticFieldGet() ||
689 instruction_->IsArrayGet() ||
690 instruction_->IsArraySet() ||
691 instruction_->IsLoadClass() ||
692 instruction_->IsLoadString() ||
693 instruction_->IsInstanceOf() ||
694 instruction_->IsCheckCast() ||
695 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
696 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
697 << "Unexpected instruction in read barrier marking slow path: "
698 << instruction_->DebugName();
699 // The read barrier instrumentation of object ArrayGet
700 // instructions does not support the HIntermediateAddress
701 // instruction.
702 DCHECK(!(instruction_->IsArrayGet() &&
703 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
704
705 __ Bind(GetEntryLabel());
706 // No need to save live registers; it's taken care of by the
707 // entrypoint. Also, there is no need to update the stack mask,
708 // as this runtime call will not trigger a garbage collection.
709 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
710 DCHECK(!ref_reg.Is(sp));
711 DCHECK(!ref_reg.Is(lr));
712 DCHECK(!ref_reg.Is(pc));
713 // IP is used internally by the ReadBarrierMarkRegX entry point
714 // as a temporary, it cannot be the entry point's input/output.
715 DCHECK(!ref_reg.Is(ip));
716 DCHECK(ref_reg.IsRegister()) << ref_reg;
717 // "Compact" slow path, saving two moves.
718 //
719 // Instead of using the standard runtime calling convention (input
720 // and output in R0):
721 //
722 // R0 <- ref
723 // R0 <- ReadBarrierMark(R0)
724 // ref <- R0
725 //
726 // we just use rX (the register containing `ref`) as input and output
727 // of a dedicated entrypoint:
728 //
729 // rX <- ReadBarrierMarkRegX(rX)
730 //
731 if (entrypoint_.IsValid()) {
732 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
733 __ Blx(RegisterFrom(entrypoint_));
734 } else {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000735 int32_t entry_point_offset =
736 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
737 // This runtime call does not require a stack map.
738 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
739 }
740 __ B(GetExitLabel());
741 }
742
743 private:
744 // The location (register) of the marked object reference.
745 const Location ref_;
746
747 // The location of the entrypoint if already loaded.
748 const Location entrypoint_;
749
750 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
751};
752
753// Slow path marking an object reference `ref` during a read barrier,
754// and if needed, atomically updating the field `obj.field` in the
755// object `obj` holding this reference after marking (contrary to
756// ReadBarrierMarkSlowPathARM above, which never tries to update
757// `obj.field`).
758//
759// This means that after the execution of this slow path, both `ref`
760// and `obj.field` will be up-to-date; i.e., after the flip, both will
761// hold the same to-space reference (unless another thread installed
762// another object reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000763class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
764 public:
765 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
766 Location ref,
767 vixl32::Register obj,
768 Location field_offset,
769 vixl32::Register temp1,
Roland Levillain35345a52017-02-27 14:32:08 +0000770 vixl32::Register temp2)
Roland Levillain47b3ab22017-02-27 14:31:35 +0000771 : SlowPathCodeARMVIXL(instruction),
772 ref_(ref),
773 obj_(obj),
774 field_offset_(field_offset),
775 temp1_(temp1),
Roland Levillain35345a52017-02-27 14:32:08 +0000776 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000777 DCHECK(kEmitCompilerReadBarrier);
778 }
779
780 const char* GetDescription() const OVERRIDE {
781 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
782 }
783
784 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
785 LocationSummary* locations = instruction_->GetLocations();
786 vixl32::Register ref_reg = RegisterFrom(ref_);
787 DCHECK(locations->CanCall());
788 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
789 // This slow path is only used by the UnsafeCASObject intrinsic.
790 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
791 << "Unexpected instruction in read barrier marking and field updating slow path: "
792 << instruction_->DebugName();
793 DCHECK(instruction_->GetLocations()->Intrinsified());
794 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
795 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
796
797 __ Bind(GetEntryLabel());
798
799 // Save the old reference.
800 // Note that we cannot use IP to save the old reference, as IP is
801 // used internally by the ReadBarrierMarkRegX entry point, and we
802 // need the old reference after the call to that entry point.
803 DCHECK(!temp1_.Is(ip));
804 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000805
806 // No need to save live registers; it's taken care of by the
807 // entrypoint. Also, there is no need to update the stack mask,
808 // as this runtime call will not trigger a garbage collection.
809 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
810 DCHECK(!ref_reg.Is(sp));
811 DCHECK(!ref_reg.Is(lr));
812 DCHECK(!ref_reg.Is(pc));
813 // IP is used internally by the ReadBarrierMarkRegX entry point
814 // as a temporary, it cannot be the entry point's input/output.
815 DCHECK(!ref_reg.Is(ip));
816 DCHECK(ref_reg.IsRegister()) << ref_reg;
817 // "Compact" slow path, saving two moves.
818 //
819 // Instead of using the standard runtime calling convention (input
820 // and output in R0):
821 //
822 // R0 <- ref
823 // R0 <- ReadBarrierMark(R0)
824 // ref <- R0
825 //
826 // we just use rX (the register containing `ref`) as input and output
827 // of a dedicated entrypoint:
828 //
829 // rX <- ReadBarrierMarkRegX(rX)
830 //
Roland Levillain35345a52017-02-27 14:32:08 +0000831 int32_t entry_point_offset =
832 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
833 // This runtime call does not require a stack map.
834 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000835
836 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000837 // update the field in the holder (`*(obj_ + field_offset_)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000838 //
839 // Note that this field could also hold a different object, if
840 // another thread had concurrently changed it. In that case, the
841 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
842 // (CAS) operation below would abort the CAS, leaving the field
843 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000844 vixl32::Label done;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000845 __ Cmp(temp1_, ref_reg);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000846 __ B(eq, &done, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000847
848 // Update the the holder's field atomically. This may fail if
849 // mutator updates before us, but it's OK. This is achieved
850 // using a strong compare-and-set (CAS) operation with relaxed
851 // memory synchronization ordering, where the expected value is
852 // the old reference and the desired value is the new reference.
853
854 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
855 // Convenience aliases.
856 vixl32::Register base = obj_;
857 // The UnsafeCASObject intrinsic uses a register pair as field
858 // offset ("long offset"), of which only the low part contains
859 // data.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000860 vixl32::Register offset = LowRegisterFrom(field_offset_);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000861 vixl32::Register expected = temp1_;
862 vixl32::Register value = ref_reg;
863 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
864 vixl32::Register tmp = temp2_; // Value in memory.
865
866 __ Add(tmp_ptr, base, offset);
867
868 if (kPoisonHeapReferences) {
869 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
870 if (value.Is(expected)) {
871 // Do not poison `value`, as it is the same register as
872 // `expected`, which has just been poisoned.
873 } else {
874 arm_codegen->GetAssembler()->PoisonHeapReference(value);
875 }
876 }
877
878 // do {
879 // tmp = [r_ptr] - expected;
880 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
881
882 vixl32::Label loop_head, exit_loop;
883 __ Bind(&loop_head);
884
885 __ Ldrex(tmp, MemOperand(tmp_ptr));
886
887 __ Subs(tmp, tmp, expected);
888
889 {
Artem Serov0fb37192016-12-06 18:13:40 +0000890 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
891 2 * kMaxInstructionSizeInBytes,
892 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000893
894 __ it(ne);
895 __ clrex(ne);
896 }
897
Artem Serov517d9f62016-12-12 15:51:15 +0000898 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000899
900 __ Strex(tmp, value, MemOperand(tmp_ptr));
901 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +0000902 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000903
904 __ Bind(&exit_loop);
905
906 if (kPoisonHeapReferences) {
907 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
908 if (value.Is(expected)) {
909 // Do not unpoison `value`, as it is the same register as
910 // `expected`, which has just been unpoisoned.
911 } else {
912 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
913 }
914 }
915
Roland Levillain47b3ab22017-02-27 14:31:35 +0000916 __ Bind(&done);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000917 __ B(GetExitLabel());
918 }
919
920 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000921 // The location (register) of the marked object reference.
922 const Location ref_;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000923 // The register containing the object holding the marked object reference field.
924 const vixl32::Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000925 // The location of the offset of the marked reference field within `obj_`.
926 Location field_offset_;
927
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000928 const vixl32::Register temp1_;
929 const vixl32::Register temp2_;
930
Roland Levillain47b3ab22017-02-27 14:31:35 +0000931 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000932};
933
934// Slow path generating a read barrier for a heap reference.
935class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
936 public:
937 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
938 Location out,
939 Location ref,
940 Location obj,
941 uint32_t offset,
942 Location index)
943 : SlowPathCodeARMVIXL(instruction),
944 out_(out),
945 ref_(ref),
946 obj_(obj),
947 offset_(offset),
948 index_(index) {
949 DCHECK(kEmitCompilerReadBarrier);
950 // If `obj` is equal to `out` or `ref`, it means the initial object
951 // has been overwritten by (or after) the heap object reference load
952 // to be instrumented, e.g.:
953 //
954 // __ LoadFromOffset(kLoadWord, out, out, offset);
955 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
956 //
957 // In that case, we have lost the information about the original
958 // object, and the emitted read barrier cannot work properly.
959 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
960 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
961 }
962
963 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
964 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
965 LocationSummary* locations = instruction_->GetLocations();
966 vixl32::Register reg_out = RegisterFrom(out_);
967 DCHECK(locations->CanCall());
968 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
969 DCHECK(instruction_->IsInstanceFieldGet() ||
970 instruction_->IsStaticFieldGet() ||
971 instruction_->IsArrayGet() ||
972 instruction_->IsInstanceOf() ||
973 instruction_->IsCheckCast() ||
974 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
975 << "Unexpected instruction in read barrier for heap reference slow path: "
976 << instruction_->DebugName();
977 // The read barrier instrumentation of object ArrayGet
978 // instructions does not support the HIntermediateAddress
979 // instruction.
980 DCHECK(!(instruction_->IsArrayGet() &&
981 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
982
983 __ Bind(GetEntryLabel());
984 SaveLiveRegisters(codegen, locations);
985
986 // We may have to change the index's value, but as `index_` is a
987 // constant member (like other "inputs" of this slow path),
988 // introduce a copy of it, `index`.
989 Location index = index_;
990 if (index_.IsValid()) {
991 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
992 if (instruction_->IsArrayGet()) {
993 // Compute the actual memory offset and store it in `index`.
994 vixl32::Register index_reg = RegisterFrom(index_);
995 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
996 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
997 // We are about to change the value of `index_reg` (see the
998 // calls to art::arm::Thumb2Assembler::Lsl and
999 // art::arm::Thumb2Assembler::AddConstant below), but it has
1000 // not been saved by the previous call to
1001 // art::SlowPathCode::SaveLiveRegisters, as it is a
1002 // callee-save register --
1003 // art::SlowPathCode::SaveLiveRegisters does not consider
1004 // callee-save registers, as it has been designed with the
1005 // assumption that callee-save registers are supposed to be
1006 // handled by the called function. So, as a callee-save
1007 // register, `index_reg` _would_ eventually be saved onto
1008 // the stack, but it would be too late: we would have
1009 // changed its value earlier. Therefore, we manually save
1010 // it here into another freely available register,
1011 // `free_reg`, chosen of course among the caller-save
1012 // registers (as a callee-save `free_reg` register would
1013 // exhibit the same problem).
1014 //
1015 // Note we could have requested a temporary register from
1016 // the register allocator instead; but we prefer not to, as
1017 // this is a slow path, and we know we can find a
1018 // caller-save register that is available.
1019 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1020 __ Mov(free_reg, index_reg);
1021 index_reg = free_reg;
1022 index = LocationFrom(index_reg);
1023 } else {
1024 // The initial register stored in `index_` has already been
1025 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1026 // (as it is not a callee-save register), so we can freely
1027 // use it.
1028 }
1029 // Shifting the index value contained in `index_reg` by the scale
1030 // factor (2) cannot overflow in practice, as the runtime is
1031 // unable to allocate object arrays with a size larger than
1032 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1033 __ Lsl(index_reg, index_reg, TIMES_4);
1034 static_assert(
1035 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1036 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1037 __ Add(index_reg, index_reg, offset_);
1038 } else {
1039 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1040 // intrinsics, `index_` is not shifted by a scale factor of 2
1041 // (as in the case of ArrayGet), as it is actually an offset
1042 // to an object field within an object.
1043 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1044 DCHECK(instruction_->GetLocations()->Intrinsified());
1045 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1046 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1047 << instruction_->AsInvoke()->GetIntrinsic();
1048 DCHECK_EQ(offset_, 0U);
1049 DCHECK(index_.IsRegisterPair());
1050 // UnsafeGet's offset location is a register pair, the low
1051 // part contains the correct offset.
1052 index = index_.ToLow();
1053 }
1054 }
1055
1056 // We're moving two or three locations to locations that could
1057 // overlap, so we need a parallel move resolver.
1058 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1059 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1060 parallel_move.AddMove(ref_,
1061 LocationFrom(calling_convention.GetRegisterAt(0)),
1062 Primitive::kPrimNot,
1063 nullptr);
1064 parallel_move.AddMove(obj_,
1065 LocationFrom(calling_convention.GetRegisterAt(1)),
1066 Primitive::kPrimNot,
1067 nullptr);
1068 if (index.IsValid()) {
1069 parallel_move.AddMove(index,
1070 LocationFrom(calling_convention.GetRegisterAt(2)),
1071 Primitive::kPrimInt,
1072 nullptr);
1073 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1074 } else {
1075 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1076 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1077 }
1078 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1079 CheckEntrypointTypes<
1080 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1081 arm_codegen->Move32(out_, LocationFrom(r0));
1082
1083 RestoreLiveRegisters(codegen, locations);
1084 __ B(GetExitLabel());
1085 }
1086
1087 const char* GetDescription() const OVERRIDE {
1088 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1089 }
1090
1091 private:
1092 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1093 uint32_t ref = RegisterFrom(ref_).GetCode();
1094 uint32_t obj = RegisterFrom(obj_).GetCode();
1095 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1096 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1097 return vixl32::Register(i);
1098 }
1099 }
1100 // We shall never fail to find a free caller-save register, as
1101 // there are more than two core caller-save registers on ARM
1102 // (meaning it is possible to find one which is different from
1103 // `ref` and `obj`).
1104 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1105 LOG(FATAL) << "Could not find a free caller-save register";
1106 UNREACHABLE();
1107 }
1108
1109 const Location out_;
1110 const Location ref_;
1111 const Location obj_;
1112 const uint32_t offset_;
1113 // An additional location containing an index to an array.
1114 // Only used for HArrayGet and the UnsafeGetObject &
1115 // UnsafeGetObjectVolatile intrinsics.
1116 const Location index_;
1117
1118 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1119};
1120
1121// Slow path generating a read barrier for a GC root.
1122class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1123 public:
1124 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1125 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1126 DCHECK(kEmitCompilerReadBarrier);
1127 }
1128
1129 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1130 LocationSummary* locations = instruction_->GetLocations();
1131 vixl32::Register reg_out = RegisterFrom(out_);
1132 DCHECK(locations->CanCall());
1133 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1134 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1135 << "Unexpected instruction in read barrier for GC root slow path: "
1136 << instruction_->DebugName();
1137
1138 __ Bind(GetEntryLabel());
1139 SaveLiveRegisters(codegen, locations);
1140
1141 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1142 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1143 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1144 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1145 instruction_,
1146 instruction_->GetDexPc(),
1147 this);
1148 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1149 arm_codegen->Move32(out_, LocationFrom(r0));
1150
1151 RestoreLiveRegisters(codegen, locations);
1152 __ B(GetExitLabel());
1153 }
1154
1155 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1156
1157 private:
1158 const Location out_;
1159 const Location root_;
1160
1161 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1162};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001163
Scott Wakelingfe885462016-09-22 10:24:38 +01001164inline vixl32::Condition ARMCondition(IfCondition cond) {
1165 switch (cond) {
1166 case kCondEQ: return eq;
1167 case kCondNE: return ne;
1168 case kCondLT: return lt;
1169 case kCondLE: return le;
1170 case kCondGT: return gt;
1171 case kCondGE: return ge;
1172 case kCondB: return lo;
1173 case kCondBE: return ls;
1174 case kCondA: return hi;
1175 case kCondAE: return hs;
1176 }
1177 LOG(FATAL) << "Unreachable";
1178 UNREACHABLE();
1179}
1180
1181// Maps signed condition to unsigned condition.
1182inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1183 switch (cond) {
1184 case kCondEQ: return eq;
1185 case kCondNE: return ne;
1186 // Signed to unsigned.
1187 case kCondLT: return lo;
1188 case kCondLE: return ls;
1189 case kCondGT: return hi;
1190 case kCondGE: return hs;
1191 // Unsigned remain unchanged.
1192 case kCondB: return lo;
1193 case kCondBE: return ls;
1194 case kCondA: return hi;
1195 case kCondAE: return hs;
1196 }
1197 LOG(FATAL) << "Unreachable";
1198 UNREACHABLE();
1199}
1200
1201inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1202 // The ARM condition codes can express all the necessary branches, see the
1203 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1204 // There is no dex instruction or HIR that would need the missing conditions
1205 // "equal or unordered" or "not equal".
1206 switch (cond) {
1207 case kCondEQ: return eq;
1208 case kCondNE: return ne /* unordered */;
1209 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1210 case kCondLE: return gt_bias ? ls : le /* unordered */;
1211 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1212 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1213 default:
1214 LOG(FATAL) << "UNREACHABLE";
1215 UNREACHABLE();
1216 }
1217}
1218
Anton Kirilov74234da2017-01-13 14:42:47 +00001219inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1220 switch (op_kind) {
1221 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1222 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1223 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1224 default:
1225 LOG(FATAL) << "Unexpected op kind " << op_kind;
1226 UNREACHABLE();
1227 }
1228}
1229
Scott Wakelingfe885462016-09-22 10:24:38 +01001230void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1231 stream << vixl32::Register(reg);
1232}
1233
1234void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1235 stream << vixl32::SRegister(reg);
1236}
1237
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001238static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001239 uint32_t mask = 0;
1240 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1241 i <= regs.GetLastSRegister().GetCode();
1242 ++i) {
1243 mask |= (1 << i);
1244 }
1245 return mask;
1246}
1247
Artem Serovd4cc5b22016-11-04 11:19:09 +00001248// Saves the register in the stack. Returns the size taken on stack.
1249size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1250 uint32_t reg_id ATTRIBUTE_UNUSED) {
1251 TODO_VIXL32(FATAL);
1252 return 0;
1253}
1254
1255// Restores the register from the stack. Returns the size taken on stack.
1256size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1257 uint32_t reg_id ATTRIBUTE_UNUSED) {
1258 TODO_VIXL32(FATAL);
1259 return 0;
1260}
1261
1262size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1263 uint32_t reg_id ATTRIBUTE_UNUSED) {
1264 TODO_VIXL32(FATAL);
1265 return 0;
1266}
1267
1268size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1269 uint32_t reg_id ATTRIBUTE_UNUSED) {
1270 TODO_VIXL32(FATAL);
1271 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001272}
1273
Anton Kirilov74234da2017-01-13 14:42:47 +00001274static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1275 vixl32::Register out,
1276 vixl32::Register first,
1277 const Operand& second,
1278 CodeGeneratorARMVIXL* codegen) {
1279 if (second.IsImmediate() && second.GetImmediate() == 0) {
1280 const Operand in = kind == HInstruction::kAnd
1281 ? Operand(0)
1282 : Operand(first);
1283
1284 __ Mov(out, in);
1285 } else {
1286 switch (kind) {
1287 case HInstruction::kAdd:
1288 __ Add(out, first, second);
1289 break;
1290 case HInstruction::kAnd:
1291 __ And(out, first, second);
1292 break;
1293 case HInstruction::kOr:
1294 __ Orr(out, first, second);
1295 break;
1296 case HInstruction::kSub:
1297 __ Sub(out, first, second);
1298 break;
1299 case HInstruction::kXor:
1300 __ Eor(out, first, second);
1301 break;
1302 default:
1303 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1304 UNREACHABLE();
1305 }
1306 }
1307}
1308
1309static void GenerateDataProc(HInstruction::InstructionKind kind,
1310 const Location& out,
1311 const Location& first,
1312 const Operand& second_lo,
1313 const Operand& second_hi,
1314 CodeGeneratorARMVIXL* codegen) {
1315 const vixl32::Register first_hi = HighRegisterFrom(first);
1316 const vixl32::Register first_lo = LowRegisterFrom(first);
1317 const vixl32::Register out_hi = HighRegisterFrom(out);
1318 const vixl32::Register out_lo = LowRegisterFrom(out);
1319
1320 if (kind == HInstruction::kAdd) {
1321 __ Adds(out_lo, first_lo, second_lo);
1322 __ Adc(out_hi, first_hi, second_hi);
1323 } else if (kind == HInstruction::kSub) {
1324 __ Subs(out_lo, first_lo, second_lo);
1325 __ Sbc(out_hi, first_hi, second_hi);
1326 } else {
1327 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1328 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1329 }
1330}
1331
1332static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1333 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1334}
1335
1336static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1337 CodeGeneratorARMVIXL* codegen) {
1338 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1339 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1340
1341 const LocationSummary* const locations = instruction->GetLocations();
1342 const uint32_t shift_value = instruction->GetShiftAmount();
1343 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1344 const Location first = locations->InAt(0);
1345 const Location second = locations->InAt(1);
1346 const Location out = locations->Out();
1347 const vixl32::Register first_hi = HighRegisterFrom(first);
1348 const vixl32::Register first_lo = LowRegisterFrom(first);
1349 const vixl32::Register out_hi = HighRegisterFrom(out);
1350 const vixl32::Register out_lo = LowRegisterFrom(out);
1351 const vixl32::Register second_hi = HighRegisterFrom(second);
1352 const vixl32::Register second_lo = LowRegisterFrom(second);
1353 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1354
1355 if (shift_value >= 32) {
1356 if (shift == ShiftType::LSL) {
1357 GenerateDataProcInstruction(kind,
1358 out_hi,
1359 first_hi,
1360 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1361 codegen);
1362 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1363 } else if (shift == ShiftType::ASR) {
1364 GenerateDataProc(kind,
1365 out,
1366 first,
1367 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1368 Operand(second_hi, ShiftType::ASR, 31),
1369 codegen);
1370 } else {
1371 DCHECK_EQ(shift, ShiftType::LSR);
1372 GenerateDataProc(kind,
1373 out,
1374 first,
1375 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1376 0,
1377 codegen);
1378 }
1379 } else {
1380 DCHECK_GT(shift_value, 1U);
1381 DCHECK_LT(shift_value, 32U);
1382
1383 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1384
1385 if (shift == ShiftType::LSL) {
1386 // We are not doing this for HInstruction::kAdd because the output will require
1387 // Location::kOutputOverlap; not applicable to other cases.
1388 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1389 GenerateDataProcInstruction(kind,
1390 out_hi,
1391 first_hi,
1392 Operand(second_hi, ShiftType::LSL, shift_value),
1393 codegen);
1394 GenerateDataProcInstruction(kind,
1395 out_hi,
1396 out_hi,
1397 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1398 codegen);
1399 GenerateDataProcInstruction(kind,
1400 out_lo,
1401 first_lo,
1402 Operand(second_lo, ShiftType::LSL, shift_value),
1403 codegen);
1404 } else {
1405 const vixl32::Register temp = temps.Acquire();
1406
1407 __ Lsl(temp, second_hi, shift_value);
1408 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1409 GenerateDataProc(kind,
1410 out,
1411 first,
1412 Operand(second_lo, ShiftType::LSL, shift_value),
1413 temp,
1414 codegen);
1415 }
1416 } else {
1417 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1418
1419 // We are not doing this for HInstruction::kAdd because the output will require
1420 // Location::kOutputOverlap; not applicable to other cases.
1421 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1422 GenerateDataProcInstruction(kind,
1423 out_lo,
1424 first_lo,
1425 Operand(second_lo, ShiftType::LSR, shift_value),
1426 codegen);
1427 GenerateDataProcInstruction(kind,
1428 out_lo,
1429 out_lo,
1430 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1431 codegen);
1432 GenerateDataProcInstruction(kind,
1433 out_hi,
1434 first_hi,
1435 Operand(second_hi, shift, shift_value),
1436 codegen);
1437 } else {
1438 const vixl32::Register temp = temps.Acquire();
1439
1440 __ Lsr(temp, second_lo, shift_value);
1441 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1442 GenerateDataProc(kind,
1443 out,
1444 first,
1445 temp,
1446 Operand(second_hi, shift, shift_value),
1447 codegen);
1448 }
1449 }
1450 }
1451}
1452
Scott Wakelingfe885462016-09-22 10:24:38 +01001453#undef __
1454
1455CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1456 const ArmInstructionSetFeatures& isa_features,
1457 const CompilerOptions& compiler_options,
1458 OptimizingCompilerStats* stats)
1459 : CodeGenerator(graph,
1460 kNumberOfCoreRegisters,
1461 kNumberOfSRegisters,
1462 kNumberOfRegisterPairs,
1463 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001464 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001465 compiler_options,
1466 stats),
1467 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001468 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001469 location_builder_(graph, this),
1470 instruction_visitor_(graph, this),
1471 move_resolver_(graph->GetArena(), this),
1472 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001473 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001474 uint32_literals_(std::less<uint32_t>(),
1475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001476 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001477 boot_image_string_patches_(StringReferenceValueComparator(),
1478 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001479 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001480 boot_image_type_patches_(TypeReferenceValueComparator(),
1481 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1482 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001483 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001484 jit_string_patches_(StringReferenceValueComparator(),
1485 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1486 jit_class_patches_(TypeReferenceValueComparator(),
1487 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001488 // Always save the LR register to mimic Quick.
1489 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001490 // Give d14 and d15 as scratch registers to VIXL.
1491 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1492 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1493 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1494 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1495 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1496 // also want to investigate giving those 14 other D registers to the allocator.
1497 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1498 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001499}
1500
Artem Serov551b28f2016-10-18 19:11:30 +01001501void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1502 uint32_t num_entries = switch_instr_->GetNumEntries();
1503 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1504
1505 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001506 // underlying code buffer and we have generated a jump table of the right size, using
1507 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001508 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1509 num_entries * sizeof(int32_t),
1510 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001511 // TODO(VIXL): Check that using lower case bind is fine here.
1512 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001513 for (uint32_t i = 0; i < num_entries; i++) {
1514 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1515 }
1516}
1517
1518void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1519 uint32_t num_entries = switch_instr_->GetNumEntries();
1520 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1521
Artem Serov551b28f2016-10-18 19:11:30 +01001522 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1523 for (uint32_t i = 0; i < num_entries; i++) {
1524 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1525 DCHECK(target_label->IsBound());
1526 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1527 // When doing BX to address we need to have lower bit set to 1 in T32.
1528 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1529 jump_offset++;
1530 }
1531 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1532 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001533
Scott Wakelingb77051e2016-11-21 19:46:00 +00001534 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001535 }
1536}
1537
Artem Serov09a940d2016-11-11 16:15:11 +00001538void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001539 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001540 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001541 }
1542}
1543
Andreas Gampeca620d72016-11-08 08:09:33 -08001544#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001545
1546void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001547 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001548 GetAssembler()->FinalizeCode();
1549 CodeGenerator::Finalize(allocator);
1550}
1551
1552void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001553 // Stack register, LR and PC are always reserved.
1554 blocked_core_registers_[SP] = true;
1555 blocked_core_registers_[LR] = true;
1556 blocked_core_registers_[PC] = true;
1557
1558 // Reserve thread register.
1559 blocked_core_registers_[TR] = true;
1560
1561 // Reserve temp register.
1562 blocked_core_registers_[IP] = true;
1563
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001564 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1565 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1566 blocked_fpu_registers_[28] = true;
1567 blocked_fpu_registers_[29] = true;
1568 blocked_fpu_registers_[30] = true;
1569 blocked_fpu_registers_[31] = true;
1570
Scott Wakelingfe885462016-09-22 10:24:38 +01001571 if (GetGraph()->IsDebuggable()) {
1572 // Stubs do not save callee-save floating point registers. If the graph
1573 // is debuggable, we need to deal with these registers differently. For
1574 // now, just block them.
1575 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1576 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1577 ++i) {
1578 blocked_fpu_registers_[i] = true;
1579 }
1580 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001581}
1582
Scott Wakelingfe885462016-09-22 10:24:38 +01001583InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1584 CodeGeneratorARMVIXL* codegen)
1585 : InstructionCodeGenerator(graph, codegen),
1586 assembler_(codegen->GetAssembler()),
1587 codegen_(codegen) {}
1588
1589void CodeGeneratorARMVIXL::ComputeSpillMask() {
1590 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1591 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1592 // There is no easy instruction to restore just the PC on thumb2. We spill and
1593 // restore another arbitrary register.
1594 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1595 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1596 // We use vpush and vpop for saving and restoring floating point registers, which take
1597 // a SRegister and the number of registers to save/restore after that SRegister. We
1598 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1599 // but in the range.
1600 if (fpu_spill_mask_ != 0) {
1601 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1602 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1603 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1604 fpu_spill_mask_ |= (1 << i);
1605 }
1606 }
1607}
1608
1609void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1610 bool skip_overflow_check =
1611 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1612 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1613 __ Bind(&frame_entry_label_);
1614
1615 if (HasEmptyFrame()) {
1616 return;
1617 }
1618
Scott Wakelingfe885462016-09-22 10:24:38 +01001619 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001620 UseScratchRegisterScope temps(GetVIXLAssembler());
1621 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001622 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001623 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001624 ExactAssemblyScope aas(GetVIXLAssembler(),
1625 vixl32::kMaxInstructionSizeInBytes,
1626 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001627 __ ldr(temp, MemOperand(temp));
1628 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001629 }
1630
1631 __ Push(RegisterList(core_spill_mask_));
1632 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1633 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1634 0,
1635 core_spill_mask_,
1636 kArmWordSize);
1637 if (fpu_spill_mask_ != 0) {
1638 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1639
1640 // Check that list is contiguous.
1641 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1642
1643 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1644 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001645 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001646 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001647
1648 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1649 UseScratchRegisterScope temps(GetVIXLAssembler());
1650 vixl32::Register temp = temps.Acquire();
1651 // Initialize should_deoptimize flag to 0.
1652 __ Mov(temp, 0);
1653 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1654 }
1655
Scott Wakelingfe885462016-09-22 10:24:38 +01001656 int adjust = GetFrameSize() - FrameEntrySpillSize();
1657 __ Sub(sp, sp, adjust);
1658 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001659
1660 // Save the current method if we need it. Note that we do not
1661 // do this in HCurrentMethod, as the instruction might have been removed
1662 // in the SSA graph.
1663 if (RequiresCurrentMethod()) {
1664 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1665 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001666}
1667
1668void CodeGeneratorARMVIXL::GenerateFrameExit() {
1669 if (HasEmptyFrame()) {
1670 __ Bx(lr);
1671 return;
1672 }
1673 GetAssembler()->cfi().RememberState();
1674 int adjust = GetFrameSize() - FrameEntrySpillSize();
1675 __ Add(sp, sp, adjust);
1676 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1677 if (fpu_spill_mask_ != 0) {
1678 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1679
1680 // Check that list is contiguous.
1681 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1682
1683 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1684 GetAssembler()->cfi().AdjustCFAOffset(
1685 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001686 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001687 }
1688 // Pop LR into PC to return.
1689 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1690 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1691 __ Pop(RegisterList(pop_mask));
1692 GetAssembler()->cfi().RestoreState();
1693 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1694}
1695
1696void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1697 __ Bind(GetLabelOf(block));
1698}
1699
Artem Serovd4cc5b22016-11-04 11:19:09 +00001700Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1701 switch (type) {
1702 case Primitive::kPrimBoolean:
1703 case Primitive::kPrimByte:
1704 case Primitive::kPrimChar:
1705 case Primitive::kPrimShort:
1706 case Primitive::kPrimInt:
1707 case Primitive::kPrimNot: {
1708 uint32_t index = gp_index_++;
1709 uint32_t stack_index = stack_index_++;
1710 if (index < calling_convention.GetNumberOfRegisters()) {
1711 return LocationFrom(calling_convention.GetRegisterAt(index));
1712 } else {
1713 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1714 }
1715 }
1716
1717 case Primitive::kPrimLong: {
1718 uint32_t index = gp_index_;
1719 uint32_t stack_index = stack_index_;
1720 gp_index_ += 2;
1721 stack_index_ += 2;
1722 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1723 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1724 // Skip R1, and use R2_R3 instead.
1725 gp_index_++;
1726 index++;
1727 }
1728 }
1729 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1730 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1731 calling_convention.GetRegisterAt(index + 1).GetCode());
1732
1733 return LocationFrom(calling_convention.GetRegisterAt(index),
1734 calling_convention.GetRegisterAt(index + 1));
1735 } else {
1736 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1737 }
1738 }
1739
1740 case Primitive::kPrimFloat: {
1741 uint32_t stack_index = stack_index_++;
1742 if (float_index_ % 2 == 0) {
1743 float_index_ = std::max(double_index_, float_index_);
1744 }
1745 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1746 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1747 } else {
1748 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1749 }
1750 }
1751
1752 case Primitive::kPrimDouble: {
1753 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1754 uint32_t stack_index = stack_index_;
1755 stack_index_ += 2;
1756 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1757 uint32_t index = double_index_;
1758 double_index_ += 2;
1759 Location result = LocationFrom(
1760 calling_convention.GetFpuRegisterAt(index),
1761 calling_convention.GetFpuRegisterAt(index + 1));
1762 DCHECK(ExpectedPairLayout(result));
1763 return result;
1764 } else {
1765 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1766 }
1767 }
1768
1769 case Primitive::kPrimVoid:
1770 LOG(FATAL) << "Unexpected parameter type " << type;
1771 break;
1772 }
1773 return Location::NoLocation();
1774}
1775
1776Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1777 switch (type) {
1778 case Primitive::kPrimBoolean:
1779 case Primitive::kPrimByte:
1780 case Primitive::kPrimChar:
1781 case Primitive::kPrimShort:
1782 case Primitive::kPrimInt:
1783 case Primitive::kPrimNot: {
1784 return LocationFrom(r0);
1785 }
1786
1787 case Primitive::kPrimFloat: {
1788 return LocationFrom(s0);
1789 }
1790
1791 case Primitive::kPrimLong: {
1792 return LocationFrom(r0, r1);
1793 }
1794
1795 case Primitive::kPrimDouble: {
1796 return LocationFrom(s0, s1);
1797 }
1798
1799 case Primitive::kPrimVoid:
1800 return Location::NoLocation();
1801 }
1802
1803 UNREACHABLE();
1804}
1805
1806Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1807 return LocationFrom(kMethodRegister);
1808}
1809
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001810void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1811 if (source.Equals(destination)) {
1812 return;
1813 }
1814 if (destination.IsRegister()) {
1815 if (source.IsRegister()) {
1816 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1817 } else if (source.IsFpuRegister()) {
1818 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1819 } else {
1820 GetAssembler()->LoadFromOffset(kLoadWord,
1821 RegisterFrom(destination),
1822 sp,
1823 source.GetStackIndex());
1824 }
1825 } else if (destination.IsFpuRegister()) {
1826 if (source.IsRegister()) {
1827 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1828 } else if (source.IsFpuRegister()) {
1829 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1830 } else {
1831 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1832 }
1833 } else {
1834 DCHECK(destination.IsStackSlot()) << destination;
1835 if (source.IsRegister()) {
1836 GetAssembler()->StoreToOffset(kStoreWord,
1837 RegisterFrom(source),
1838 sp,
1839 destination.GetStackIndex());
1840 } else if (source.IsFpuRegister()) {
1841 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1842 } else {
1843 DCHECK(source.IsStackSlot()) << source;
1844 UseScratchRegisterScope temps(GetVIXLAssembler());
1845 vixl32::Register temp = temps.Acquire();
1846 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1847 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1848 }
1849 }
1850}
1851
Artem Serovcfbe9132016-10-14 15:58:56 +01001852void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1853 DCHECK(location.IsRegister());
1854 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001855}
1856
1857void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001858 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1859 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1860 HParallelMove move(GetGraph()->GetArena());
1861 move.AddMove(src, dst, dst_type, nullptr);
1862 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001863}
1864
Artem Serovcfbe9132016-10-14 15:58:56 +01001865void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1866 if (location.IsRegister()) {
1867 locations->AddTemp(location);
1868 } else if (location.IsRegisterPair()) {
1869 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1870 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1871 } else {
1872 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1873 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001874}
1875
1876void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1877 HInstruction* instruction,
1878 uint32_t dex_pc,
1879 SlowPathCode* slow_path) {
1880 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001881 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1882 // Ensure the pc position is recorded immediately after the `blx` instruction.
1883 // 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 +00001884 ExactAssemblyScope aas(GetVIXLAssembler(),
1885 vixl32::k16BitT32InstructionSizeInBytes,
1886 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001887 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001888 if (EntrypointRequiresStackMap(entrypoint)) {
1889 RecordPcInfo(instruction, dex_pc, slow_path);
1890 }
1891}
1892
1893void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1894 HInstruction* instruction,
1895 SlowPathCode* slow_path) {
1896 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001897 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001898 __ Blx(lr);
1899}
1900
Scott Wakelingfe885462016-09-22 10:24:38 +01001901void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1902 DCHECK(!successor->IsExitBlock());
1903 HBasicBlock* block = got->GetBlock();
1904 HInstruction* previous = got->GetPrevious();
1905 HLoopInformation* info = block->GetLoopInformation();
1906
1907 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1908 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1909 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1910 return;
1911 }
1912 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1913 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1914 }
1915 if (!codegen_->GoesToNextBlock(block, successor)) {
1916 __ B(codegen_->GetLabelOf(successor));
1917 }
1918}
1919
1920void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1921 got->SetLocations(nullptr);
1922}
1923
1924void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1925 HandleGoto(got, got->GetSuccessor());
1926}
1927
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001928void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1929 try_boundary->SetLocations(nullptr);
1930}
1931
1932void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1933 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1934 if (!successor->IsExitBlock()) {
1935 HandleGoto(try_boundary, successor);
1936 }
1937}
1938
Scott Wakelingfe885462016-09-22 10:24:38 +01001939void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1940 exit->SetLocations(nullptr);
1941}
1942
1943void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1944}
1945
1946void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1947 Primitive::Type type = instruction->InputAt(0)->GetType();
1948 Location lhs_loc = instruction->GetLocations()->InAt(0);
1949 Location rhs_loc = instruction->GetLocations()->InAt(1);
1950 if (rhs_loc.IsConstant()) {
1951 // 0.0 is the only immediate that can be encoded directly in
1952 // a VCMP instruction.
1953 //
1954 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1955 // specify that in a floating-point comparison, positive zero
1956 // and negative zero are considered equal, so we can use the
1957 // literal 0.0 for both cases here.
1958 //
1959 // Note however that some methods (Float.equal, Float.compare,
1960 // Float.compareTo, Double.equal, Double.compare,
1961 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1962 // StrictMath.min) consider 0.0 to be (strictly) greater than
1963 // -0.0. So if we ever translate calls to these methods into a
1964 // HCompare instruction, we must handle the -0.0 case with
1965 // care here.
1966 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1967 if (type == Primitive::kPrimFloat) {
1968 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1969 } else {
1970 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001971 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001972 }
1973 } else {
1974 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001975 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001976 } else {
1977 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001978 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001979 }
1980 }
1981}
1982
1983void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1984 vixl32::Label* true_label,
1985 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1986 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1987 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1988 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1989}
1990
1991void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1992 vixl32::Label* true_label,
1993 vixl32::Label* false_label) {
1994 LocationSummary* locations = cond->GetLocations();
1995 Location left = locations->InAt(0);
1996 Location right = locations->InAt(1);
1997 IfCondition if_cond = cond->GetCondition();
1998
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001999 vixl32::Register left_high = HighRegisterFrom(left);
2000 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002001 IfCondition true_high_cond = if_cond;
2002 IfCondition false_high_cond = cond->GetOppositeCondition();
2003 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2004
2005 // Set the conditions for the test, remembering that == needs to be
2006 // decided using the low words.
2007 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
2008 switch (if_cond) {
2009 case kCondEQ:
2010 case kCondNE:
2011 // Nothing to do.
2012 break;
2013 case kCondLT:
2014 false_high_cond = kCondGT;
2015 break;
2016 case kCondLE:
2017 true_high_cond = kCondLT;
2018 break;
2019 case kCondGT:
2020 false_high_cond = kCondLT;
2021 break;
2022 case kCondGE:
2023 true_high_cond = kCondGT;
2024 break;
2025 case kCondB:
2026 false_high_cond = kCondA;
2027 break;
2028 case kCondBE:
2029 true_high_cond = kCondB;
2030 break;
2031 case kCondA:
2032 false_high_cond = kCondB;
2033 break;
2034 case kCondAE:
2035 true_high_cond = kCondA;
2036 break;
2037 }
2038 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002039 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002040 int32_t val_low = Low32Bits(value);
2041 int32_t val_high = High32Bits(value);
2042
2043 __ Cmp(left_high, val_high);
2044 if (if_cond == kCondNE) {
2045 __ B(ARMCondition(true_high_cond), true_label);
2046 } else if (if_cond == kCondEQ) {
2047 __ B(ARMCondition(false_high_cond), false_label);
2048 } else {
2049 __ B(ARMCondition(true_high_cond), true_label);
2050 __ B(ARMCondition(false_high_cond), false_label);
2051 }
2052 // Must be equal high, so compare the lows.
2053 __ Cmp(left_low, val_low);
2054 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002055 vixl32::Register right_high = HighRegisterFrom(right);
2056 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002057
2058 __ Cmp(left_high, right_high);
2059 if (if_cond == kCondNE) {
2060 __ B(ARMCondition(true_high_cond), true_label);
2061 } else if (if_cond == kCondEQ) {
2062 __ B(ARMCondition(false_high_cond), false_label);
2063 } else {
2064 __ B(ARMCondition(true_high_cond), true_label);
2065 __ B(ARMCondition(false_high_cond), false_label);
2066 }
2067 // Must be equal high, so compare the lows.
2068 __ Cmp(left_low, right_low);
2069 }
2070 // The last comparison might be unsigned.
2071 // TODO: optimize cases where this is always true/false
2072 __ B(final_condition, true_label);
2073}
2074
2075void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2076 vixl32::Label* true_target_in,
2077 vixl32::Label* false_target_in) {
2078 // Generated branching requires both targets to be explicit. If either of the
2079 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2080 vixl32::Label fallthrough;
2081 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2082 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2083
2084 Primitive::Type type = condition->InputAt(0)->GetType();
2085 switch (type) {
2086 case Primitive::kPrimLong:
2087 GenerateLongComparesAndJumps(condition, true_target, false_target);
2088 break;
2089 case Primitive::kPrimFloat:
2090 case Primitive::kPrimDouble:
2091 GenerateVcmp(condition);
2092 GenerateFPJumps(condition, true_target, false_target);
2093 break;
2094 default:
2095 LOG(FATAL) << "Unexpected compare type " << type;
2096 }
2097
2098 if (false_target != &fallthrough) {
2099 __ B(false_target);
2100 }
2101
2102 if (true_target_in == nullptr || false_target_in == nullptr) {
2103 __ Bind(&fallthrough);
2104 }
2105}
2106
2107void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2108 size_t condition_input_index,
2109 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002110 vixl32::Label* false_target,
2111 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002112 HInstruction* cond = instruction->InputAt(condition_input_index);
2113
2114 if (true_target == nullptr && false_target == nullptr) {
2115 // Nothing to do. The code always falls through.
2116 return;
2117 } else if (cond->IsIntConstant()) {
2118 // Constant condition, statically compared against "true" (integer value 1).
2119 if (cond->AsIntConstant()->IsTrue()) {
2120 if (true_target != nullptr) {
2121 __ B(true_target);
2122 }
2123 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002124 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002125 if (false_target != nullptr) {
2126 __ B(false_target);
2127 }
2128 }
2129 return;
2130 }
2131
2132 // The following code generates these patterns:
2133 // (1) true_target == nullptr && false_target != nullptr
2134 // - opposite condition true => branch to false_target
2135 // (2) true_target != nullptr && false_target == nullptr
2136 // - condition true => branch to true_target
2137 // (3) true_target != nullptr && false_target != nullptr
2138 // - condition true => branch to true_target
2139 // - branch to false_target
2140 if (IsBooleanValueOrMaterializedCondition(cond)) {
2141 // Condition has been materialized, compare the output to 0.
2142 if (kIsDebugBuild) {
2143 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2144 DCHECK(cond_val.IsRegister());
2145 }
2146 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002147 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2148 false_target,
2149 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002150 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002151 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2152 true_target,
2153 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002154 }
2155 } else {
2156 // Condition has not been materialized. Use its inputs as the comparison and
2157 // its condition as the branch condition.
2158 HCondition* condition = cond->AsCondition();
2159
2160 // If this is a long or FP comparison that has been folded into
2161 // the HCondition, generate the comparison directly.
2162 Primitive::Type type = condition->InputAt(0)->GetType();
2163 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2164 GenerateCompareTestAndBranch(condition, true_target, false_target);
2165 return;
2166 }
2167
2168 LocationSummary* locations = cond->GetLocations();
2169 DCHECK(locations->InAt(0).IsRegister());
2170 vixl32::Register left = InputRegisterAt(cond, 0);
2171 Location right = locations->InAt(1);
2172 if (right.IsRegister()) {
2173 __ Cmp(left, InputRegisterAt(cond, 1));
2174 } else {
2175 DCHECK(right.IsConstant());
2176 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2177 }
2178 if (true_target == nullptr) {
2179 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
2180 } else {
2181 __ B(ARMCondition(condition->GetCondition()), true_target);
2182 }
2183 }
2184
2185 // If neither branch falls through (case 3), the conditional branch to `true_target`
2186 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2187 if (true_target != nullptr && false_target != nullptr) {
2188 __ B(false_target);
2189 }
2190}
2191
2192void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2193 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2194 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2195 locations->SetInAt(0, Location::RequiresRegister());
2196 }
2197}
2198
2199void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2200 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2201 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002202 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2203 nullptr : codegen_->GetLabelOf(true_successor);
2204 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2205 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002206 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2207}
2208
Scott Wakelingc34dba72016-10-03 10:14:44 +01002209void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2210 LocationSummary* locations = new (GetGraph()->GetArena())
2211 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2212 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2213 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2214 locations->SetInAt(0, Location::RequiresRegister());
2215 }
2216}
2217
2218void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2219 SlowPathCodeARMVIXL* slow_path =
2220 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2221 GenerateTestAndBranch(deoptimize,
2222 /* condition_input_index */ 0,
2223 slow_path->GetEntryLabel(),
2224 /* false_target */ nullptr);
2225}
2226
Artem Serovd4cc5b22016-11-04 11:19:09 +00002227void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2228 LocationSummary* locations = new (GetGraph()->GetArena())
2229 LocationSummary(flag, LocationSummary::kNoCall);
2230 locations->SetOut(Location::RequiresRegister());
2231}
2232
2233void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2234 GetAssembler()->LoadFromOffset(kLoadWord,
2235 OutputRegister(flag),
2236 sp,
2237 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2238}
2239
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002240void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2242 if (Primitive::IsFloatingPointType(select->GetType())) {
2243 locations->SetInAt(0, Location::RequiresFpuRegister());
2244 locations->SetInAt(1, Location::RequiresFpuRegister());
2245 } else {
2246 locations->SetInAt(0, Location::RequiresRegister());
2247 locations->SetInAt(1, Location::RequiresRegister());
2248 }
2249 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2250 locations->SetInAt(2, Location::RequiresRegister());
2251 }
2252 locations->SetOut(Location::SameAsFirstInput());
2253}
2254
2255void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2256 LocationSummary* locations = select->GetLocations();
2257 vixl32::Label false_target;
2258 GenerateTestAndBranch(select,
2259 /* condition_input_index */ 2,
2260 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002261 &false_target,
2262 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002263 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2264 __ Bind(&false_target);
2265}
2266
Artem Serov551b28f2016-10-18 19:11:30 +01002267void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2268 new (GetGraph()->GetArena()) LocationSummary(info);
2269}
2270
2271void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2272 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2273}
2274
Scott Wakelingfe885462016-09-22 10:24:38 +01002275void CodeGeneratorARMVIXL::GenerateNop() {
2276 __ Nop();
2277}
2278
2279void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2280 LocationSummary* locations =
2281 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2282 // Handle the long/FP comparisons made in instruction simplification.
2283 switch (cond->InputAt(0)->GetType()) {
2284 case Primitive::kPrimLong:
2285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2287 if (!cond->IsEmittedAtUseSite()) {
2288 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2289 }
2290 break;
2291
Scott Wakelingfe885462016-09-22 10:24:38 +01002292 case Primitive::kPrimFloat:
2293 case Primitive::kPrimDouble:
2294 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002295 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002296 if (!cond->IsEmittedAtUseSite()) {
2297 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2298 }
2299 break;
2300
2301 default:
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2304 if (!cond->IsEmittedAtUseSite()) {
2305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2306 }
2307 }
2308}
2309
2310void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2311 if (cond->IsEmittedAtUseSite()) {
2312 return;
2313 }
2314
Artem Serov657022c2016-11-23 14:19:38 +00002315 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002316 vixl32::Register out = OutputRegister(cond);
2317 vixl32::Label true_label, false_label;
2318
2319 switch (cond->InputAt(0)->GetType()) {
2320 default: {
2321 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002322 if (right.IsRegister()) {
2323 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2324 } else {
2325 DCHECK(right.IsConstant());
2326 __ Cmp(InputRegisterAt(cond, 0),
2327 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2328 }
Artem Serov0fb37192016-12-06 18:13:40 +00002329 ExactAssemblyScope aas(GetVIXLAssembler(),
2330 3 * vixl32::kMaxInstructionSizeInBytes,
2331 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002332 __ ite(ARMCondition(cond->GetCondition()));
2333 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2334 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002335 return;
2336 }
2337 case Primitive::kPrimLong:
2338 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2339 break;
2340 case Primitive::kPrimFloat:
2341 case Primitive::kPrimDouble:
2342 GenerateVcmp(cond);
2343 GenerateFPJumps(cond, &true_label, &false_label);
2344 break;
2345 }
2346
2347 // Convert the jumps into the result.
2348 vixl32::Label done_label;
2349
2350 // False case: result = 0.
2351 __ Bind(&false_label);
2352 __ Mov(out, 0);
2353 __ B(&done_label);
2354
2355 // True case: result = 1.
2356 __ Bind(&true_label);
2357 __ Mov(out, 1);
2358 __ Bind(&done_label);
2359}
2360
2361void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2362 HandleCondition(comp);
2363}
2364
2365void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2366 HandleCondition(comp);
2367}
2368
2369void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2370 HandleCondition(comp);
2371}
2372
2373void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2374 HandleCondition(comp);
2375}
2376
2377void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2378 HandleCondition(comp);
2379}
2380
2381void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2382 HandleCondition(comp);
2383}
2384
2385void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2386 HandleCondition(comp);
2387}
2388
2389void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2390 HandleCondition(comp);
2391}
2392
2393void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2394 HandleCondition(comp);
2395}
2396
2397void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2398 HandleCondition(comp);
2399}
2400
2401void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2402 HandleCondition(comp);
2403}
2404
2405void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2406 HandleCondition(comp);
2407}
2408
2409void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2410 HandleCondition(comp);
2411}
2412
2413void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2414 HandleCondition(comp);
2415}
2416
2417void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2418 HandleCondition(comp);
2419}
2420
2421void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2422 HandleCondition(comp);
2423}
2424
2425void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2426 HandleCondition(comp);
2427}
2428
2429void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2430 HandleCondition(comp);
2431}
2432
2433void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2434 HandleCondition(comp);
2435}
2436
2437void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2438 HandleCondition(comp);
2439}
2440
2441void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2442 LocationSummary* locations =
2443 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2444 locations->SetOut(Location::ConstantLocation(constant));
2445}
2446
2447void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2448 // Will be generated at use site.
2449}
2450
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002451void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2452 LocationSummary* locations =
2453 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2454 locations->SetOut(Location::ConstantLocation(constant));
2455}
2456
2457void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2458 // Will be generated at use site.
2459}
2460
Scott Wakelingfe885462016-09-22 10:24:38 +01002461void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2462 LocationSummary* locations =
2463 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2464 locations->SetOut(Location::ConstantLocation(constant));
2465}
2466
2467void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2468 // Will be generated at use site.
2469}
2470
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002471void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2472 LocationSummary* locations =
2473 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2474 locations->SetOut(Location::ConstantLocation(constant));
2475}
2476
Scott Wakelingc34dba72016-10-03 10:14:44 +01002477void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2478 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002479 // Will be generated at use site.
2480}
2481
2482void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2485 locations->SetOut(Location::ConstantLocation(constant));
2486}
2487
Scott Wakelingc34dba72016-10-03 10:14:44 +01002488void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2489 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002490 // Will be generated at use site.
2491}
2492
Scott Wakelingfe885462016-09-22 10:24:38 +01002493void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2494 memory_barrier->SetLocations(nullptr);
2495}
2496
2497void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2498 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2499}
2500
2501void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2502 ret->SetLocations(nullptr);
2503}
2504
2505void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2506 codegen_->GenerateFrameExit();
2507}
2508
2509void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2510 LocationSummary* locations =
2511 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2512 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2513}
2514
2515void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2516 codegen_->GenerateFrameExit();
2517}
2518
Artem Serovcfbe9132016-10-14 15:58:56 +01002519void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2520 // The trampoline uses the same calling convention as dex calling conventions,
2521 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2522 // the method_idx.
2523 HandleInvoke(invoke);
2524}
2525
2526void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2527 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2528}
2529
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002530void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2531 // Explicit clinit checks triggered by static invokes must have been pruned by
2532 // art::PrepareForRegisterAllocation.
2533 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2534
Anton Kirilov5ec62182016-10-13 20:16:02 +01002535 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2536 if (intrinsic.TryDispatch(invoke)) {
2537 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2538 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2539 }
2540 return;
2541 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002542
2543 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002544
Artem Serovd4cc5b22016-11-04 11:19:09 +00002545 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2546 if (invoke->HasPcRelativeDexCache()) {
2547 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2548 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002549}
2550
Anton Kirilov5ec62182016-10-13 20:16:02 +01002551static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2552 if (invoke->GetLocations()->Intrinsified()) {
2553 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2554 intrinsic.Dispatch(invoke);
2555 return true;
2556 }
2557 return false;
2558}
2559
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002560void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2561 // Explicit clinit checks triggered by static invokes must have been pruned by
2562 // art::PrepareForRegisterAllocation.
2563 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2564
Anton Kirilov5ec62182016-10-13 20:16:02 +01002565 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2566 return;
2567 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002568
2569 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002570 codegen_->GenerateStaticOrDirectCall(
2571 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002572 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2573}
2574
2575void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002576 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002577 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2578}
2579
2580void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002581 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2582 if (intrinsic.TryDispatch(invoke)) {
2583 return;
2584 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002585
2586 HandleInvoke(invoke);
2587}
2588
2589void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002590 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2591 return;
2592 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002593
2594 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002595 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002596 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002597}
2598
Artem Serovcfbe9132016-10-14 15:58:56 +01002599void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2600 HandleInvoke(invoke);
2601 // Add the hidden argument.
2602 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2603}
2604
2605void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2606 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2607 LocationSummary* locations = invoke->GetLocations();
2608 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2609 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2610 Location receiver = locations->InAt(0);
2611 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2612
2613 DCHECK(!receiver.IsStackSlot());
2614
Alexandre Rames374ddf32016-11-04 10:40:49 +00002615 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2616 {
Artem Serov0fb37192016-12-06 18:13:40 +00002617 ExactAssemblyScope aas(GetVIXLAssembler(),
2618 vixl32::kMaxInstructionSizeInBytes,
2619 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002620 // /* HeapReference<Class> */ temp = receiver->klass_
2621 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2622 codegen_->MaybeRecordImplicitNullCheck(invoke);
2623 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002624 // Instead of simply (possibly) unpoisoning `temp` here, we should
2625 // emit a read barrier for the previous class reference load.
2626 // However this is not required in practice, as this is an
2627 // intermediate/temporary reference and because the current
2628 // concurrent copying collector keeps the from-space memory
2629 // intact/accessible until the end of the marking phase (the
2630 // concurrent copying collector may not in the future).
2631 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2632 GetAssembler()->LoadFromOffset(kLoadWord,
2633 temp,
2634 temp,
2635 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2636 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2637 invoke->GetImtIndex(), kArmPointerSize));
2638 // temp = temp->GetImtEntryAt(method_offset);
2639 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2640 uint32_t entry_point =
2641 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2642 // LR = temp->GetEntryPoint();
2643 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2644
2645 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2646 // instruction from clobbering it as they might use r12 as a scratch register.
2647 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002648
2649 {
2650 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2651 // so it checks if the application is using them (by passing them to the macro assembler
2652 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2653 // what is available, and is the opposite of the standard usage: Instead of requesting a
2654 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2655 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2656 // (to materialize the constant), since the destination register becomes available for such use
2657 // internally for the duration of the macro instruction.
2658 UseScratchRegisterScope temps(GetVIXLAssembler());
2659 temps.Exclude(hidden_reg);
2660 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2661 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002662 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002663 // Ensure the pc position is recorded immediately after the `blx` instruction.
2664 // 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 +00002665 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002666 vixl32::k16BitT32InstructionSizeInBytes,
2667 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002668 // LR();
2669 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002670 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002671 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002672 }
2673}
2674
Orion Hodsonac141392017-01-13 11:53:47 +00002675void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2676 HandleInvoke(invoke);
2677}
2678
2679void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2680 codegen_->GenerateInvokePolymorphicCall(invoke);
2681}
2682
Artem Serov02109dd2016-09-23 17:17:54 +01002683void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2684 LocationSummary* locations =
2685 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2686 switch (neg->GetResultType()) {
2687 case Primitive::kPrimInt: {
2688 locations->SetInAt(0, Location::RequiresRegister());
2689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2690 break;
2691 }
2692 case Primitive::kPrimLong: {
2693 locations->SetInAt(0, Location::RequiresRegister());
2694 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2695 break;
2696 }
2697
2698 case Primitive::kPrimFloat:
2699 case Primitive::kPrimDouble:
2700 locations->SetInAt(0, Location::RequiresFpuRegister());
2701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2702 break;
2703
2704 default:
2705 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2706 }
2707}
2708
2709void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2710 LocationSummary* locations = neg->GetLocations();
2711 Location out = locations->Out();
2712 Location in = locations->InAt(0);
2713 switch (neg->GetResultType()) {
2714 case Primitive::kPrimInt:
2715 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2716 break;
2717
2718 case Primitive::kPrimLong:
2719 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2720 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2721 // We cannot emit an RSC (Reverse Subtract with Carry)
2722 // instruction here, as it does not exist in the Thumb-2
2723 // instruction set. We use the following approach
2724 // using SBC and SUB instead.
2725 //
2726 // out.hi = -C
2727 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2728 // out.hi = out.hi - in.hi
2729 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2730 break;
2731
2732 case Primitive::kPrimFloat:
2733 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00002734 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01002735 break;
2736
2737 default:
2738 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2739 }
2740}
2741
Scott Wakelingfe885462016-09-22 10:24:38 +01002742void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2743 Primitive::Type result_type = conversion->GetResultType();
2744 Primitive::Type input_type = conversion->GetInputType();
2745 DCHECK_NE(result_type, input_type);
2746
2747 // The float-to-long, double-to-long and long-to-float type conversions
2748 // rely on a call to the runtime.
2749 LocationSummary::CallKind call_kind =
2750 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2751 && result_type == Primitive::kPrimLong)
2752 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2753 ? LocationSummary::kCallOnMainOnly
2754 : LocationSummary::kNoCall;
2755 LocationSummary* locations =
2756 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2757
2758 // The Java language does not allow treating boolean as an integral type but
2759 // our bit representation makes it safe.
2760
2761 switch (result_type) {
2762 case Primitive::kPrimByte:
2763 switch (input_type) {
2764 case Primitive::kPrimLong:
2765 // Type conversion from long to byte is a result of code transformations.
2766 case Primitive::kPrimBoolean:
2767 // Boolean input is a result of code transformations.
2768 case Primitive::kPrimShort:
2769 case Primitive::kPrimInt:
2770 case Primitive::kPrimChar:
2771 // Processing a Dex `int-to-byte' instruction.
2772 locations->SetInAt(0, Location::RequiresRegister());
2773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2774 break;
2775
2776 default:
2777 LOG(FATAL) << "Unexpected type conversion from " << input_type
2778 << " to " << result_type;
2779 }
2780 break;
2781
2782 case Primitive::kPrimShort:
2783 switch (input_type) {
2784 case Primitive::kPrimLong:
2785 // Type conversion from long to short is a result of code transformations.
2786 case Primitive::kPrimBoolean:
2787 // Boolean input is a result of code transformations.
2788 case Primitive::kPrimByte:
2789 case Primitive::kPrimInt:
2790 case Primitive::kPrimChar:
2791 // Processing a Dex `int-to-short' instruction.
2792 locations->SetInAt(0, Location::RequiresRegister());
2793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2794 break;
2795
2796 default:
2797 LOG(FATAL) << "Unexpected type conversion from " << input_type
2798 << " to " << result_type;
2799 }
2800 break;
2801
2802 case Primitive::kPrimInt:
2803 switch (input_type) {
2804 case Primitive::kPrimLong:
2805 // Processing a Dex `long-to-int' instruction.
2806 locations->SetInAt(0, Location::Any());
2807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2808 break;
2809
2810 case Primitive::kPrimFloat:
2811 // Processing a Dex `float-to-int' instruction.
2812 locations->SetInAt(0, Location::RequiresFpuRegister());
2813 locations->SetOut(Location::RequiresRegister());
2814 locations->AddTemp(Location::RequiresFpuRegister());
2815 break;
2816
2817 case Primitive::kPrimDouble:
2818 // Processing a Dex `double-to-int' instruction.
2819 locations->SetInAt(0, Location::RequiresFpuRegister());
2820 locations->SetOut(Location::RequiresRegister());
2821 locations->AddTemp(Location::RequiresFpuRegister());
2822 break;
2823
2824 default:
2825 LOG(FATAL) << "Unexpected type conversion from " << input_type
2826 << " to " << result_type;
2827 }
2828 break;
2829
2830 case Primitive::kPrimLong:
2831 switch (input_type) {
2832 case Primitive::kPrimBoolean:
2833 // Boolean input is a result of code transformations.
2834 case Primitive::kPrimByte:
2835 case Primitive::kPrimShort:
2836 case Primitive::kPrimInt:
2837 case Primitive::kPrimChar:
2838 // Processing a Dex `int-to-long' instruction.
2839 locations->SetInAt(0, Location::RequiresRegister());
2840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2841 break;
2842
2843 case Primitive::kPrimFloat: {
2844 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002845 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2846 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2847 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002848 break;
2849 }
2850
2851 case Primitive::kPrimDouble: {
2852 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002853 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2854 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2855 calling_convention.GetFpuRegisterAt(1)));
2856 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002857 break;
2858 }
2859
2860 default:
2861 LOG(FATAL) << "Unexpected type conversion from " << input_type
2862 << " to " << result_type;
2863 }
2864 break;
2865
2866 case Primitive::kPrimChar:
2867 switch (input_type) {
2868 case Primitive::kPrimLong:
2869 // Type conversion from long to char is a result of code transformations.
2870 case Primitive::kPrimBoolean:
2871 // Boolean input is a result of code transformations.
2872 case Primitive::kPrimByte:
2873 case Primitive::kPrimShort:
2874 case Primitive::kPrimInt:
2875 // Processing a Dex `int-to-char' instruction.
2876 locations->SetInAt(0, Location::RequiresRegister());
2877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2878 break;
2879
2880 default:
2881 LOG(FATAL) << "Unexpected type conversion from " << input_type
2882 << " to " << result_type;
2883 }
2884 break;
2885
2886 case Primitive::kPrimFloat:
2887 switch (input_type) {
2888 case Primitive::kPrimBoolean:
2889 // Boolean input is a result of code transformations.
2890 case Primitive::kPrimByte:
2891 case Primitive::kPrimShort:
2892 case Primitive::kPrimInt:
2893 case Primitive::kPrimChar:
2894 // Processing a Dex `int-to-float' instruction.
2895 locations->SetInAt(0, Location::RequiresRegister());
2896 locations->SetOut(Location::RequiresFpuRegister());
2897 break;
2898
2899 case Primitive::kPrimLong: {
2900 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002901 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2902 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2903 calling_convention.GetRegisterAt(1)));
2904 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002905 break;
2906 }
2907
2908 case Primitive::kPrimDouble:
2909 // Processing a Dex `double-to-float' instruction.
2910 locations->SetInAt(0, Location::RequiresFpuRegister());
2911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2912 break;
2913
2914 default:
2915 LOG(FATAL) << "Unexpected type conversion from " << input_type
2916 << " to " << result_type;
2917 };
2918 break;
2919
2920 case Primitive::kPrimDouble:
2921 switch (input_type) {
2922 case Primitive::kPrimBoolean:
2923 // Boolean input is a result of code transformations.
2924 case Primitive::kPrimByte:
2925 case Primitive::kPrimShort:
2926 case Primitive::kPrimInt:
2927 case Primitive::kPrimChar:
2928 // Processing a Dex `int-to-double' instruction.
2929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetOut(Location::RequiresFpuRegister());
2931 break;
2932
2933 case Primitive::kPrimLong:
2934 // Processing a Dex `long-to-double' instruction.
2935 locations->SetInAt(0, Location::RequiresRegister());
2936 locations->SetOut(Location::RequiresFpuRegister());
2937 locations->AddTemp(Location::RequiresFpuRegister());
2938 locations->AddTemp(Location::RequiresFpuRegister());
2939 break;
2940
2941 case Primitive::kPrimFloat:
2942 // Processing a Dex `float-to-double' instruction.
2943 locations->SetInAt(0, Location::RequiresFpuRegister());
2944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2945 break;
2946
2947 default:
2948 LOG(FATAL) << "Unexpected type conversion from " << input_type
2949 << " to " << result_type;
2950 };
2951 break;
2952
2953 default:
2954 LOG(FATAL) << "Unexpected type conversion from " << input_type
2955 << " to " << result_type;
2956 }
2957}
2958
2959void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2960 LocationSummary* locations = conversion->GetLocations();
2961 Location out = locations->Out();
2962 Location in = locations->InAt(0);
2963 Primitive::Type result_type = conversion->GetResultType();
2964 Primitive::Type input_type = conversion->GetInputType();
2965 DCHECK_NE(result_type, input_type);
2966 switch (result_type) {
2967 case Primitive::kPrimByte:
2968 switch (input_type) {
2969 case Primitive::kPrimLong:
2970 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002971 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002972 break;
2973 case Primitive::kPrimBoolean:
2974 // Boolean input is a result of code transformations.
2975 case Primitive::kPrimShort:
2976 case Primitive::kPrimInt:
2977 case Primitive::kPrimChar:
2978 // Processing a Dex `int-to-byte' instruction.
2979 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2980 break;
2981
2982 default:
2983 LOG(FATAL) << "Unexpected type conversion from " << input_type
2984 << " to " << result_type;
2985 }
2986 break;
2987
2988 case Primitive::kPrimShort:
2989 switch (input_type) {
2990 case Primitive::kPrimLong:
2991 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002992 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002993 break;
2994 case Primitive::kPrimBoolean:
2995 // Boolean input is a result of code transformations.
2996 case Primitive::kPrimByte:
2997 case Primitive::kPrimInt:
2998 case Primitive::kPrimChar:
2999 // Processing a Dex `int-to-short' instruction.
3000 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3001 break;
3002
3003 default:
3004 LOG(FATAL) << "Unexpected type conversion from " << input_type
3005 << " to " << result_type;
3006 }
3007 break;
3008
3009 case Primitive::kPrimInt:
3010 switch (input_type) {
3011 case Primitive::kPrimLong:
3012 // Processing a Dex `long-to-int' instruction.
3013 DCHECK(out.IsRegister());
3014 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003015 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003016 } else if (in.IsDoubleStackSlot()) {
3017 GetAssembler()->LoadFromOffset(kLoadWord,
3018 OutputRegister(conversion),
3019 sp,
3020 in.GetStackIndex());
3021 } else {
3022 DCHECK(in.IsConstant());
3023 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00003024 int32_t value = Int32ConstantFrom(in);
3025 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003026 }
3027 break;
3028
3029 case Primitive::kPrimFloat: {
3030 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003031 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003032 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003033 __ Vmov(OutputRegister(conversion), temp);
3034 break;
3035 }
3036
3037 case Primitive::kPrimDouble: {
3038 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003039 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003040 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003041 __ Vmov(OutputRegister(conversion), temp_s);
3042 break;
3043 }
3044
3045 default:
3046 LOG(FATAL) << "Unexpected type conversion from " << input_type
3047 << " to " << result_type;
3048 }
3049 break;
3050
3051 case Primitive::kPrimLong:
3052 switch (input_type) {
3053 case Primitive::kPrimBoolean:
3054 // Boolean input is a result of code transformations.
3055 case Primitive::kPrimByte:
3056 case Primitive::kPrimShort:
3057 case Primitive::kPrimInt:
3058 case Primitive::kPrimChar:
3059 // Processing a Dex `int-to-long' instruction.
3060 DCHECK(out.IsRegisterPair());
3061 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003062 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003063 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003064 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003065 break;
3066
3067 case Primitive::kPrimFloat:
3068 // Processing a Dex `float-to-long' instruction.
3069 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3070 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3071 break;
3072
3073 case Primitive::kPrimDouble:
3074 // Processing a Dex `double-to-long' instruction.
3075 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3076 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3077 break;
3078
3079 default:
3080 LOG(FATAL) << "Unexpected type conversion from " << input_type
3081 << " to " << result_type;
3082 }
3083 break;
3084
3085 case Primitive::kPrimChar:
3086 switch (input_type) {
3087 case Primitive::kPrimLong:
3088 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003089 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003090 break;
3091 case Primitive::kPrimBoolean:
3092 // Boolean input is a result of code transformations.
3093 case Primitive::kPrimByte:
3094 case Primitive::kPrimShort:
3095 case Primitive::kPrimInt:
3096 // Processing a Dex `int-to-char' instruction.
3097 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3098 break;
3099
3100 default:
3101 LOG(FATAL) << "Unexpected type conversion from " << input_type
3102 << " to " << result_type;
3103 }
3104 break;
3105
3106 case Primitive::kPrimFloat:
3107 switch (input_type) {
3108 case Primitive::kPrimBoolean:
3109 // Boolean input is a result of code transformations.
3110 case Primitive::kPrimByte:
3111 case Primitive::kPrimShort:
3112 case Primitive::kPrimInt:
3113 case Primitive::kPrimChar: {
3114 // Processing a Dex `int-to-float' instruction.
3115 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003116 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003117 break;
3118 }
3119
3120 case Primitive::kPrimLong:
3121 // Processing a Dex `long-to-float' instruction.
3122 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3123 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3124 break;
3125
3126 case Primitive::kPrimDouble:
3127 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003128 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003129 break;
3130
3131 default:
3132 LOG(FATAL) << "Unexpected type conversion from " << input_type
3133 << " to " << result_type;
3134 };
3135 break;
3136
3137 case Primitive::kPrimDouble:
3138 switch (input_type) {
3139 case Primitive::kPrimBoolean:
3140 // Boolean input is a result of code transformations.
3141 case Primitive::kPrimByte:
3142 case Primitive::kPrimShort:
3143 case Primitive::kPrimInt:
3144 case Primitive::kPrimChar: {
3145 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003146 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003147 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003148 break;
3149 }
3150
3151 case Primitive::kPrimLong: {
3152 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003153 vixl32::Register low = LowRegisterFrom(in);
3154 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003156 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003157 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003158 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003159 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003160
3161 // temp_d = int-to-double(high)
3162 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003163 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003164 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003165 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003166 // out_d = unsigned-to-double(low)
3167 __ Vmov(out_s, low);
3168 __ Vcvt(F64, U32, out_d, out_s);
3169 // out_d += temp_d * constant_d
3170 __ Vmla(F64, out_d, temp_d, constant_d);
3171 break;
3172 }
3173
3174 case Primitive::kPrimFloat:
3175 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003176 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003177 break;
3178
3179 default:
3180 LOG(FATAL) << "Unexpected type conversion from " << input_type
3181 << " to " << result_type;
3182 };
3183 break;
3184
3185 default:
3186 LOG(FATAL) << "Unexpected type conversion from " << input_type
3187 << " to " << result_type;
3188 }
3189}
3190
3191void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3192 LocationSummary* locations =
3193 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3194 switch (add->GetResultType()) {
3195 case Primitive::kPrimInt: {
3196 locations->SetInAt(0, Location::RequiresRegister());
3197 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3199 break;
3200 }
3201
Scott Wakelingfe885462016-09-22 10:24:38 +01003202 case Primitive::kPrimLong: {
3203 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003204 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3206 break;
3207 }
3208
3209 case Primitive::kPrimFloat:
3210 case Primitive::kPrimDouble: {
3211 locations->SetInAt(0, Location::RequiresFpuRegister());
3212 locations->SetInAt(1, Location::RequiresFpuRegister());
3213 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3214 break;
3215 }
3216
3217 default:
3218 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3219 }
3220}
3221
3222void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3223 LocationSummary* locations = add->GetLocations();
3224 Location out = locations->Out();
3225 Location first = locations->InAt(0);
3226 Location second = locations->InAt(1);
3227
3228 switch (add->GetResultType()) {
3229 case Primitive::kPrimInt: {
3230 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3231 }
3232 break;
3233
Scott Wakelingfe885462016-09-22 10:24:38 +01003234 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003235 if (second.IsConstant()) {
3236 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3237 GenerateAddLongConst(out, first, value);
3238 } else {
3239 DCHECK(second.IsRegisterPair());
3240 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3241 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3242 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003243 break;
3244 }
3245
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003246 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003247 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003248 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003249 break;
3250
3251 default:
3252 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3253 }
3254}
3255
3256void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3257 LocationSummary* locations =
3258 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3259 switch (sub->GetResultType()) {
3260 case Primitive::kPrimInt: {
3261 locations->SetInAt(0, Location::RequiresRegister());
3262 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3264 break;
3265 }
3266
Scott Wakelingfe885462016-09-22 10:24:38 +01003267 case Primitive::kPrimLong: {
3268 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003269 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3271 break;
3272 }
3273 case Primitive::kPrimFloat:
3274 case Primitive::kPrimDouble: {
3275 locations->SetInAt(0, Location::RequiresFpuRegister());
3276 locations->SetInAt(1, Location::RequiresFpuRegister());
3277 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3278 break;
3279 }
3280 default:
3281 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3282 }
3283}
3284
3285void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3286 LocationSummary* locations = sub->GetLocations();
3287 Location out = locations->Out();
3288 Location first = locations->InAt(0);
3289 Location second = locations->InAt(1);
3290 switch (sub->GetResultType()) {
3291 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003292 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003293 break;
3294 }
3295
Scott Wakelingfe885462016-09-22 10:24:38 +01003296 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003297 if (second.IsConstant()) {
3298 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3299 GenerateAddLongConst(out, first, -value);
3300 } else {
3301 DCHECK(second.IsRegisterPair());
3302 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3303 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3304 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003305 break;
3306 }
3307
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003308 case Primitive::kPrimFloat:
3309 case Primitive::kPrimDouble:
3310 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003311 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003312
3313 default:
3314 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3315 }
3316}
3317
3318void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3319 LocationSummary* locations =
3320 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3321 switch (mul->GetResultType()) {
3322 case Primitive::kPrimInt:
3323 case Primitive::kPrimLong: {
3324 locations->SetInAt(0, Location::RequiresRegister());
3325 locations->SetInAt(1, Location::RequiresRegister());
3326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3327 break;
3328 }
3329
3330 case Primitive::kPrimFloat:
3331 case Primitive::kPrimDouble: {
3332 locations->SetInAt(0, Location::RequiresFpuRegister());
3333 locations->SetInAt(1, Location::RequiresFpuRegister());
3334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3335 break;
3336 }
3337
3338 default:
3339 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3340 }
3341}
3342
3343void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3344 LocationSummary* locations = mul->GetLocations();
3345 Location out = locations->Out();
3346 Location first = locations->InAt(0);
3347 Location second = locations->InAt(1);
3348 switch (mul->GetResultType()) {
3349 case Primitive::kPrimInt: {
3350 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3351 break;
3352 }
3353 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003354 vixl32::Register out_hi = HighRegisterFrom(out);
3355 vixl32::Register out_lo = LowRegisterFrom(out);
3356 vixl32::Register in1_hi = HighRegisterFrom(first);
3357 vixl32::Register in1_lo = LowRegisterFrom(first);
3358 vixl32::Register in2_hi = HighRegisterFrom(second);
3359 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003360
3361 // Extra checks to protect caused by the existence of R1_R2.
3362 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3363 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003364 DCHECK(!out_hi.Is(in1_lo));
3365 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003366
3367 // input: in1 - 64 bits, in2 - 64 bits
3368 // output: out
3369 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3370 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3371 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3372
3373 UseScratchRegisterScope temps(GetVIXLAssembler());
3374 vixl32::Register temp = temps.Acquire();
3375 // temp <- in1.lo * in2.hi
3376 __ Mul(temp, in1_lo, in2_hi);
3377 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3378 __ Mla(out_hi, in1_hi, in2_lo, temp);
3379 // out.lo <- (in1.lo * in2.lo)[31:0];
3380 __ Umull(out_lo, temp, in1_lo, in2_lo);
3381 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003382 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003383 break;
3384 }
3385
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003386 case Primitive::kPrimFloat:
3387 case Primitive::kPrimDouble:
3388 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003389 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003390
3391 default:
3392 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3393 }
3394}
3395
Scott Wakelingfe885462016-09-22 10:24:38 +01003396void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3397 DCHECK(instruction->IsDiv() || instruction->IsRem());
3398 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3399
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003400 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003401 DCHECK(second.IsConstant());
3402
3403 vixl32::Register out = OutputRegister(instruction);
3404 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003405 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003406 DCHECK(imm == 1 || imm == -1);
3407
3408 if (instruction->IsRem()) {
3409 __ Mov(out, 0);
3410 } else {
3411 if (imm == 1) {
3412 __ Mov(out, dividend);
3413 } else {
3414 __ Rsb(out, dividend, 0);
3415 }
3416 }
3417}
3418
3419void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3420 DCHECK(instruction->IsDiv() || instruction->IsRem());
3421 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3422
3423 LocationSummary* locations = instruction->GetLocations();
3424 Location second = locations->InAt(1);
3425 DCHECK(second.IsConstant());
3426
3427 vixl32::Register out = OutputRegister(instruction);
3428 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003429 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003430 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003431 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3432 int ctz_imm = CTZ(abs_imm);
3433
3434 if (ctz_imm == 1) {
3435 __ Lsr(temp, dividend, 32 - ctz_imm);
3436 } else {
3437 __ Asr(temp, dividend, 31);
3438 __ Lsr(temp, temp, 32 - ctz_imm);
3439 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003440 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003441
3442 if (instruction->IsDiv()) {
3443 __ Asr(out, out, ctz_imm);
3444 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003445 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003446 }
3447 } else {
3448 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003449 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003450 }
3451}
3452
3453void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3454 DCHECK(instruction->IsDiv() || instruction->IsRem());
3455 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3456
3457 LocationSummary* locations = instruction->GetLocations();
3458 Location second = locations->InAt(1);
3459 DCHECK(second.IsConstant());
3460
3461 vixl32::Register out = OutputRegister(instruction);
3462 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003463 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3464 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003465 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003466
3467 int64_t magic;
3468 int shift;
3469 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3470
Anton Kirilovdda43962016-11-21 19:55:20 +00003471 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3472 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003473 __ Smull(temp2, temp1, dividend, temp1);
3474
3475 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003476 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003477 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003478 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003479 }
3480
3481 if (shift != 0) {
3482 __ Asr(temp1, temp1, shift);
3483 }
3484
3485 if (instruction->IsDiv()) {
3486 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3487 } else {
3488 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3489 // TODO: Strength reduction for mls.
3490 __ Mov(temp2, imm);
3491 __ Mls(out, temp1, temp2, dividend);
3492 }
3493}
3494
3495void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3496 HBinaryOperation* instruction) {
3497 DCHECK(instruction->IsDiv() || instruction->IsRem());
3498 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3499
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003500 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003501 DCHECK(second.IsConstant());
3502
Anton Kirilov644032c2016-12-06 17:51:43 +00003503 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003504 if (imm == 0) {
3505 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3506 } else if (imm == 1 || imm == -1) {
3507 DivRemOneOrMinusOne(instruction);
3508 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3509 DivRemByPowerOfTwo(instruction);
3510 } else {
3511 DCHECK(imm <= -2 || imm >= 2);
3512 GenerateDivRemWithAnyConstant(instruction);
3513 }
3514}
3515
3516void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3517 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3518 if (div->GetResultType() == Primitive::kPrimLong) {
3519 // pLdiv runtime call.
3520 call_kind = LocationSummary::kCallOnMainOnly;
3521 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3522 // sdiv will be replaced by other instruction sequence.
3523 } else if (div->GetResultType() == Primitive::kPrimInt &&
3524 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3525 // pIdivmod runtime call.
3526 call_kind = LocationSummary::kCallOnMainOnly;
3527 }
3528
3529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3530
3531 switch (div->GetResultType()) {
3532 case Primitive::kPrimInt: {
3533 if (div->InputAt(1)->IsConstant()) {
3534 locations->SetInAt(0, Location::RequiresRegister());
3535 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003537 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003538 if (value == 1 || value == 0 || value == -1) {
3539 // No temp register required.
3540 } else {
3541 locations->AddTemp(Location::RequiresRegister());
3542 if (!IsPowerOfTwo(AbsOrMin(value))) {
3543 locations->AddTemp(Location::RequiresRegister());
3544 }
3545 }
3546 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3547 locations->SetInAt(0, Location::RequiresRegister());
3548 locations->SetInAt(1, Location::RequiresRegister());
3549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3550 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003551 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3552 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3553 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003554 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003555 // we only need the former.
3556 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003557 }
3558 break;
3559 }
3560 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003561 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3562 locations->SetInAt(0, LocationFrom(
3563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3564 locations->SetInAt(1, LocationFrom(
3565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3566 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003567 break;
3568 }
3569 case Primitive::kPrimFloat:
3570 case Primitive::kPrimDouble: {
3571 locations->SetInAt(0, Location::RequiresFpuRegister());
3572 locations->SetInAt(1, Location::RequiresFpuRegister());
3573 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3574 break;
3575 }
3576
3577 default:
3578 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3579 }
3580}
3581
3582void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003583 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003584 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003585
3586 switch (div->GetResultType()) {
3587 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003588 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003589 GenerateDivRemConstantIntegral(div);
3590 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3591 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3592 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003593 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3594 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3595 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3596 DCHECK(r0.Is(OutputRegister(div)));
3597
3598 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3599 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003600 }
3601 break;
3602 }
3603
3604 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3606 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3607 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3608 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3609 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3610 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3611 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3612
3613 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3614 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003615 break;
3616 }
3617
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003618 case Primitive::kPrimFloat:
3619 case Primitive::kPrimDouble:
3620 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003621 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003622
3623 default:
3624 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3625 }
3626}
3627
Artem Serov551b28f2016-10-18 19:11:30 +01003628void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3629 Primitive::Type type = rem->GetResultType();
3630
3631 // Most remainders are implemented in the runtime.
3632 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3633 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3634 // sdiv will be replaced by other instruction sequence.
3635 call_kind = LocationSummary::kNoCall;
3636 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3637 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3638 // Have hardware divide instruction for int, do it with three instructions.
3639 call_kind = LocationSummary::kNoCall;
3640 }
3641
3642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3643
3644 switch (type) {
3645 case Primitive::kPrimInt: {
3646 if (rem->InputAt(1)->IsConstant()) {
3647 locations->SetInAt(0, Location::RequiresRegister());
3648 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003650 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01003651 if (value == 1 || value == 0 || value == -1) {
3652 // No temp register required.
3653 } else {
3654 locations->AddTemp(Location::RequiresRegister());
3655 if (!IsPowerOfTwo(AbsOrMin(value))) {
3656 locations->AddTemp(Location::RequiresRegister());
3657 }
3658 }
3659 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3660 locations->SetInAt(0, Location::RequiresRegister());
3661 locations->SetInAt(1, Location::RequiresRegister());
3662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3663 locations->AddTemp(Location::RequiresRegister());
3664 } else {
3665 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3666 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3667 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003668 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003669 // we only need the latter.
3670 locations->SetOut(LocationFrom(r1));
3671 }
3672 break;
3673 }
3674 case Primitive::kPrimLong: {
3675 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3676 locations->SetInAt(0, LocationFrom(
3677 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3678 locations->SetInAt(1, LocationFrom(
3679 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3680 // The runtime helper puts the output in R2,R3.
3681 locations->SetOut(LocationFrom(r2, r3));
3682 break;
3683 }
3684 case Primitive::kPrimFloat: {
3685 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3686 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3687 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3688 locations->SetOut(LocationFrom(s0));
3689 break;
3690 }
3691
3692 case Primitive::kPrimDouble: {
3693 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3694 locations->SetInAt(0, LocationFrom(
3695 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3696 locations->SetInAt(1, LocationFrom(
3697 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3698 locations->SetOut(LocationFrom(s0, s1));
3699 break;
3700 }
3701
3702 default:
3703 LOG(FATAL) << "Unexpected rem type " << type;
3704 }
3705}
3706
3707void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3708 LocationSummary* locations = rem->GetLocations();
3709 Location second = locations->InAt(1);
3710
3711 Primitive::Type type = rem->GetResultType();
3712 switch (type) {
3713 case Primitive::kPrimInt: {
3714 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3715 vixl32::Register out_reg = OutputRegister(rem);
3716 if (second.IsConstant()) {
3717 GenerateDivRemConstantIntegral(rem);
3718 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3719 vixl32::Register reg2 = RegisterFrom(second);
3720 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3721
3722 // temp = reg1 / reg2 (integer division)
3723 // dest = reg1 - temp * reg2
3724 __ Sdiv(temp, reg1, reg2);
3725 __ Mls(out_reg, temp, reg2, reg1);
3726 } else {
3727 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3728 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3729 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3730 DCHECK(out_reg.Is(r1));
3731
3732 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3733 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3734 }
3735 break;
3736 }
3737
3738 case Primitive::kPrimLong: {
3739 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3740 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3741 break;
3742 }
3743
3744 case Primitive::kPrimFloat: {
3745 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3746 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3747 break;
3748 }
3749
3750 case Primitive::kPrimDouble: {
3751 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3752 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3753 break;
3754 }
3755
3756 default:
3757 LOG(FATAL) << "Unexpected rem type " << type;
3758 }
3759}
3760
3761
Scott Wakelingfe885462016-09-22 10:24:38 +01003762void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003763 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003764 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003765}
3766
3767void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3768 DivZeroCheckSlowPathARMVIXL* slow_path =
3769 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3770 codegen_->AddSlowPath(slow_path);
3771
3772 LocationSummary* locations = instruction->GetLocations();
3773 Location value = locations->InAt(0);
3774
3775 switch (instruction->GetType()) {
3776 case Primitive::kPrimBoolean:
3777 case Primitive::kPrimByte:
3778 case Primitive::kPrimChar:
3779 case Primitive::kPrimShort:
3780 case Primitive::kPrimInt: {
3781 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003782 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003783 } else {
3784 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003785 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003786 __ B(slow_path->GetEntryLabel());
3787 }
3788 }
3789 break;
3790 }
3791 case Primitive::kPrimLong: {
3792 if (value.IsRegisterPair()) {
3793 UseScratchRegisterScope temps(GetVIXLAssembler());
3794 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003795 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003796 __ B(eq, slow_path->GetEntryLabel());
3797 } else {
3798 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003799 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003800 __ B(slow_path->GetEntryLabel());
3801 }
3802 }
3803 break;
3804 }
3805 default:
3806 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3807 }
3808}
3809
Artem Serov02109dd2016-09-23 17:17:54 +01003810void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3811 LocationSummary* locations = ror->GetLocations();
3812 vixl32::Register in = InputRegisterAt(ror, 0);
3813 Location rhs = locations->InAt(1);
3814 vixl32::Register out = OutputRegister(ror);
3815
3816 if (rhs.IsConstant()) {
3817 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3818 // so map all rotations to a +ve. equivalent in that range.
3819 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3820 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3821 if (rot) {
3822 // Rotate, mapping left rotations to right equivalents if necessary.
3823 // (e.g. left by 2 bits == right by 30.)
3824 __ Ror(out, in, rot);
3825 } else if (!out.Is(in)) {
3826 __ Mov(out, in);
3827 }
3828 } else {
3829 __ Ror(out, in, RegisterFrom(rhs));
3830 }
3831}
3832
3833// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3834// rotates by swapping input regs (effectively rotating by the first 32-bits of
3835// a larger rotation) or flipping direction (thus treating larger right/left
3836// rotations as sub-word sized rotations in the other direction) as appropriate.
3837void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3838 LocationSummary* locations = ror->GetLocations();
3839 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3840 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3841 Location rhs = locations->InAt(1);
3842 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3843 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3844
3845 if (rhs.IsConstant()) {
3846 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3847 // Map all rotations to +ve. equivalents on the interval [0,63].
3848 rot &= kMaxLongShiftDistance;
3849 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3850 // logic below to a simple pair of binary orr.
3851 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3852 if (rot >= kArmBitsPerWord) {
3853 rot -= kArmBitsPerWord;
3854 std::swap(in_reg_hi, in_reg_lo);
3855 }
3856 // Rotate, or mov to out for zero or word size rotations.
3857 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003858 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003859 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003860 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003861 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3862 } else {
3863 __ Mov(out_reg_lo, in_reg_lo);
3864 __ Mov(out_reg_hi, in_reg_hi);
3865 }
3866 } else {
3867 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3868 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3869 vixl32::Label end;
3870 vixl32::Label shift_by_32_plus_shift_right;
3871
3872 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3873 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003874 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00003875 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01003876
3877 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3878 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3879 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3880 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3881 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3882 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3883 __ Lsr(shift_left, in_reg_hi, shift_right);
3884 __ Add(out_reg_lo, out_reg_lo, shift_left);
3885 __ B(&end);
3886
3887 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3888 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3889 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3890 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3891 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3892 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3893 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3894 __ Lsl(shift_right, in_reg_hi, shift_left);
3895 __ Add(out_reg_lo, out_reg_lo, shift_right);
3896
3897 __ Bind(&end);
3898 }
3899}
3900
3901void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3902 LocationSummary* locations =
3903 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3904 switch (ror->GetResultType()) {
3905 case Primitive::kPrimInt: {
3906 locations->SetInAt(0, Location::RequiresRegister());
3907 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3909 break;
3910 }
3911 case Primitive::kPrimLong: {
3912 locations->SetInAt(0, Location::RequiresRegister());
3913 if (ror->InputAt(1)->IsConstant()) {
3914 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3915 } else {
3916 locations->SetInAt(1, Location::RequiresRegister());
3917 locations->AddTemp(Location::RequiresRegister());
3918 locations->AddTemp(Location::RequiresRegister());
3919 }
3920 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3921 break;
3922 }
3923 default:
3924 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3925 }
3926}
3927
3928void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3929 Primitive::Type type = ror->GetResultType();
3930 switch (type) {
3931 case Primitive::kPrimInt: {
3932 HandleIntegerRotate(ror);
3933 break;
3934 }
3935 case Primitive::kPrimLong: {
3936 HandleLongRotate(ror);
3937 break;
3938 }
3939 default:
3940 LOG(FATAL) << "Unexpected operation type " << type;
3941 UNREACHABLE();
3942 }
3943}
3944
Artem Serov02d37832016-10-25 15:25:33 +01003945void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3946 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3947
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3950
3951 switch (op->GetResultType()) {
3952 case Primitive::kPrimInt: {
3953 locations->SetInAt(0, Location::RequiresRegister());
3954 if (op->InputAt(1)->IsConstant()) {
3955 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3957 } else {
3958 locations->SetInAt(1, Location::RequiresRegister());
3959 // Make the output overlap, as it will be used to hold the masked
3960 // second input.
3961 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3962 }
3963 break;
3964 }
3965 case Primitive::kPrimLong: {
3966 locations->SetInAt(0, Location::RequiresRegister());
3967 if (op->InputAt(1)->IsConstant()) {
3968 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3969 // For simplicity, use kOutputOverlap even though we only require that low registers
3970 // don't clash with high registers which the register allocator currently guarantees.
3971 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3972 } else {
3973 locations->SetInAt(1, Location::RequiresRegister());
3974 locations->AddTemp(Location::RequiresRegister());
3975 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3976 }
3977 break;
3978 }
3979 default:
3980 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3981 }
3982}
3983
3984void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3985 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3986
3987 LocationSummary* locations = op->GetLocations();
3988 Location out = locations->Out();
3989 Location first = locations->InAt(0);
3990 Location second = locations->InAt(1);
3991
3992 Primitive::Type type = op->GetResultType();
3993 switch (type) {
3994 case Primitive::kPrimInt: {
3995 vixl32::Register out_reg = OutputRegister(op);
3996 vixl32::Register first_reg = InputRegisterAt(op, 0);
3997 if (second.IsRegister()) {
3998 vixl32::Register second_reg = RegisterFrom(second);
3999 // ARM doesn't mask the shift count so we need to do it ourselves.
4000 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4001 if (op->IsShl()) {
4002 __ Lsl(out_reg, first_reg, out_reg);
4003 } else if (op->IsShr()) {
4004 __ Asr(out_reg, first_reg, out_reg);
4005 } else {
4006 __ Lsr(out_reg, first_reg, out_reg);
4007 }
4008 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004009 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004010 uint32_t shift_value = cst & kMaxIntShiftDistance;
4011 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4012 __ Mov(out_reg, first_reg);
4013 } else if (op->IsShl()) {
4014 __ Lsl(out_reg, first_reg, shift_value);
4015 } else if (op->IsShr()) {
4016 __ Asr(out_reg, first_reg, shift_value);
4017 } else {
4018 __ Lsr(out_reg, first_reg, shift_value);
4019 }
4020 }
4021 break;
4022 }
4023 case Primitive::kPrimLong: {
4024 vixl32::Register o_h = HighRegisterFrom(out);
4025 vixl32::Register o_l = LowRegisterFrom(out);
4026
4027 vixl32::Register high = HighRegisterFrom(first);
4028 vixl32::Register low = LowRegisterFrom(first);
4029
4030 if (second.IsRegister()) {
4031 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4032
4033 vixl32::Register second_reg = RegisterFrom(second);
4034
4035 if (op->IsShl()) {
4036 __ And(o_l, second_reg, kMaxLongShiftDistance);
4037 // Shift the high part
4038 __ Lsl(o_h, high, o_l);
4039 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004040 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004041 __ Lsr(temp, low, temp);
4042 __ Orr(o_h, o_h, temp);
4043 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004044 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004045 {
Artem Serov0fb37192016-12-06 18:13:40 +00004046 ExactAssemblyScope guard(GetVIXLAssembler(),
4047 2 * vixl32::kMaxInstructionSizeInBytes,
4048 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004049 __ it(pl);
4050 __ lsl(pl, o_h, low, temp);
4051 }
4052 // Shift the low part
4053 __ Lsl(o_l, low, o_l);
4054 } else if (op->IsShr()) {
4055 __ And(o_h, second_reg, kMaxLongShiftDistance);
4056 // Shift the low part
4057 __ Lsr(o_l, low, o_h);
4058 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004059 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004060 __ Lsl(temp, high, temp);
4061 __ Orr(o_l, o_l, temp);
4062 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004063 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004064 {
Artem Serov0fb37192016-12-06 18:13:40 +00004065 ExactAssemblyScope guard(GetVIXLAssembler(),
4066 2 * vixl32::kMaxInstructionSizeInBytes,
4067 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004068 __ it(pl);
4069 __ asr(pl, o_l, high, temp);
4070 }
4071 // Shift the high part
4072 __ Asr(o_h, high, o_h);
4073 } else {
4074 __ And(o_h, second_reg, kMaxLongShiftDistance);
4075 // same as Shr except we use `Lsr`s and not `Asr`s
4076 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004077 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004078 __ Lsl(temp, high, temp);
4079 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004080 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004081 {
Artem Serov0fb37192016-12-06 18:13:40 +00004082 ExactAssemblyScope guard(GetVIXLAssembler(),
4083 2 * vixl32::kMaxInstructionSizeInBytes,
4084 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004085 __ it(pl);
4086 __ lsr(pl, o_l, high, temp);
4087 }
4088 __ Lsr(o_h, high, o_h);
4089 }
4090 } else {
4091 // Register allocator doesn't create partial overlap.
4092 DCHECK(!o_l.Is(high));
4093 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004094 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004095 uint32_t shift_value = cst & kMaxLongShiftDistance;
4096 if (shift_value > 32) {
4097 if (op->IsShl()) {
4098 __ Lsl(o_h, low, shift_value - 32);
4099 __ Mov(o_l, 0);
4100 } else if (op->IsShr()) {
4101 __ Asr(o_l, high, shift_value - 32);
4102 __ Asr(o_h, high, 31);
4103 } else {
4104 __ Lsr(o_l, high, shift_value - 32);
4105 __ Mov(o_h, 0);
4106 }
4107 } else if (shift_value == 32) {
4108 if (op->IsShl()) {
4109 __ Mov(o_h, low);
4110 __ Mov(o_l, 0);
4111 } else if (op->IsShr()) {
4112 __ Mov(o_l, high);
4113 __ Asr(o_h, high, 31);
4114 } else {
4115 __ Mov(o_l, high);
4116 __ Mov(o_h, 0);
4117 }
4118 } else if (shift_value == 1) {
4119 if (op->IsShl()) {
4120 __ Lsls(o_l, low, 1);
4121 __ Adc(o_h, high, high);
4122 } else if (op->IsShr()) {
4123 __ Asrs(o_h, high, 1);
4124 __ Rrx(o_l, low);
4125 } else {
4126 __ Lsrs(o_h, high, 1);
4127 __ Rrx(o_l, low);
4128 }
4129 } else {
4130 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4131 if (op->IsShl()) {
4132 __ Lsl(o_h, high, shift_value);
4133 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4134 __ Lsl(o_l, low, shift_value);
4135 } else if (op->IsShr()) {
4136 __ Lsr(o_l, low, shift_value);
4137 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4138 __ Asr(o_h, high, shift_value);
4139 } else {
4140 __ Lsr(o_l, low, shift_value);
4141 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4142 __ Lsr(o_h, high, shift_value);
4143 }
4144 }
4145 }
4146 break;
4147 }
4148 default:
4149 LOG(FATAL) << "Unexpected operation type " << type;
4150 UNREACHABLE();
4151 }
4152}
4153
4154void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4155 HandleShift(shl);
4156}
4157
4158void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4159 HandleShift(shl);
4160}
4161
4162void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4163 HandleShift(shr);
4164}
4165
4166void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4167 HandleShift(shr);
4168}
4169
4170void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4171 HandleShift(ushr);
4172}
4173
4174void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4175 HandleShift(ushr);
4176}
4177
4178void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4179 LocationSummary* locations =
4180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4181 if (instruction->IsStringAlloc()) {
4182 locations->AddTemp(LocationFrom(kMethodRegister));
4183 } else {
4184 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4185 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004186 }
4187 locations->SetOut(LocationFrom(r0));
4188}
4189
4190void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4191 // Note: if heap poisoning is enabled, the entry point takes cares
4192 // of poisoning the reference.
4193 if (instruction->IsStringAlloc()) {
4194 // String is allocated through StringFactory. Call NewEmptyString entry point.
4195 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4196 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4197 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4198 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004199 // 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 +00004200 ExactAssemblyScope aas(GetVIXLAssembler(),
4201 vixl32::k16BitT32InstructionSizeInBytes,
4202 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004203 __ blx(lr);
4204 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4205 } else {
4206 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004207 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004208 }
4209}
4210
4211void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4212 LocationSummary* locations =
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4214 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004215 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004216 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4217 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004218}
4219
4220void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004221 // Note: if heap poisoning is enabled, the entry point takes cares
4222 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004223 QuickEntrypointEnum entrypoint =
4224 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4225 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004226 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004227 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004228}
4229
4230void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4231 LocationSummary* locations =
4232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4233 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4234 if (location.IsStackSlot()) {
4235 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4236 } else if (location.IsDoubleStackSlot()) {
4237 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4238 }
4239 locations->SetOut(location);
4240}
4241
4242void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4243 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4244 // Nothing to do, the parameter is already at its location.
4245}
4246
4247void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4248 LocationSummary* locations =
4249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4250 locations->SetOut(LocationFrom(kMethodRegister));
4251}
4252
4253void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4254 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4255 // Nothing to do, the method is already at its location.
4256}
4257
4258void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4259 LocationSummary* locations =
4260 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4261 locations->SetInAt(0, Location::RequiresRegister());
4262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4263}
4264
4265void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4266 LocationSummary* locations = not_->GetLocations();
4267 Location out = locations->Out();
4268 Location in = locations->InAt(0);
4269 switch (not_->GetResultType()) {
4270 case Primitive::kPrimInt:
4271 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4272 break;
4273
4274 case Primitive::kPrimLong:
4275 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4276 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4277 break;
4278
4279 default:
4280 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4281 }
4282}
4283
Scott Wakelingc34dba72016-10-03 10:14:44 +01004284void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4285 LocationSummary* locations =
4286 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4287 locations->SetInAt(0, Location::RequiresRegister());
4288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4289}
4290
4291void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4292 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4293}
4294
Artem Serov02d37832016-10-25 15:25:33 +01004295void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4296 LocationSummary* locations =
4297 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4298 switch (compare->InputAt(0)->GetType()) {
4299 case Primitive::kPrimBoolean:
4300 case Primitive::kPrimByte:
4301 case Primitive::kPrimShort:
4302 case Primitive::kPrimChar:
4303 case Primitive::kPrimInt:
4304 case Primitive::kPrimLong: {
4305 locations->SetInAt(0, Location::RequiresRegister());
4306 locations->SetInAt(1, Location::RequiresRegister());
4307 // Output overlaps because it is written before doing the low comparison.
4308 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4309 break;
4310 }
4311 case Primitive::kPrimFloat:
4312 case Primitive::kPrimDouble: {
4313 locations->SetInAt(0, Location::RequiresFpuRegister());
4314 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4315 locations->SetOut(Location::RequiresRegister());
4316 break;
4317 }
4318 default:
4319 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4320 }
4321}
4322
4323void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4324 LocationSummary* locations = compare->GetLocations();
4325 vixl32::Register out = OutputRegister(compare);
4326 Location left = locations->InAt(0);
4327 Location right = locations->InAt(1);
4328
4329 vixl32::Label less, greater, done;
4330 Primitive::Type type = compare->InputAt(0)->GetType();
4331 vixl32::Condition less_cond = vixl32::Condition(kNone);
4332 switch (type) {
4333 case Primitive::kPrimBoolean:
4334 case Primitive::kPrimByte:
4335 case Primitive::kPrimShort:
4336 case Primitive::kPrimChar:
4337 case Primitive::kPrimInt: {
4338 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4339 __ Mov(out, 0);
4340 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4341 less_cond = lt;
4342 break;
4343 }
4344 case Primitive::kPrimLong: {
4345 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004346 __ B(lt, &less, /* far_target */ false);
4347 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004348 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4349 __ Mov(out, 0);
4350 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4351 less_cond = lo;
4352 break;
4353 }
4354 case Primitive::kPrimFloat:
4355 case Primitive::kPrimDouble: {
4356 __ Mov(out, 0);
4357 GenerateVcmp(compare);
4358 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4359 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4360 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4361 break;
4362 }
4363 default:
4364 LOG(FATAL) << "Unexpected compare type " << type;
4365 UNREACHABLE();
4366 }
4367
Artem Serov517d9f62016-12-12 15:51:15 +00004368 __ B(eq, &done, /* far_target */ false);
4369 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004370
4371 __ Bind(&greater);
4372 __ Mov(out, 1);
4373 __ B(&done);
4374
4375 __ Bind(&less);
4376 __ Mov(out, -1);
4377
4378 __ Bind(&done);
4379}
4380
4381void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4382 LocationSummary* locations =
4383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4384 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4385 locations->SetInAt(i, Location::Any());
4386 }
4387 locations->SetOut(Location::Any());
4388}
4389
4390void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4391 LOG(FATAL) << "Unreachable";
4392}
4393
4394void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4395 // TODO (ported from quick): revisit ARM barrier kinds.
4396 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4397 switch (kind) {
4398 case MemBarrierKind::kAnyStore:
4399 case MemBarrierKind::kLoadAny:
4400 case MemBarrierKind::kAnyAny: {
4401 flavor = DmbOptions::ISH;
4402 break;
4403 }
4404 case MemBarrierKind::kStoreStore: {
4405 flavor = DmbOptions::ISHST;
4406 break;
4407 }
4408 default:
4409 LOG(FATAL) << "Unexpected memory barrier " << kind;
4410 }
4411 __ Dmb(flavor);
4412}
4413
4414void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4415 uint32_t offset,
4416 vixl32::Register out_lo,
4417 vixl32::Register out_hi) {
4418 UseScratchRegisterScope temps(GetVIXLAssembler());
4419 if (offset != 0) {
4420 vixl32::Register temp = temps.Acquire();
4421 __ Add(temp, addr, offset);
4422 addr = temp;
4423 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004424 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004425}
4426
4427void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4428 uint32_t offset,
4429 vixl32::Register value_lo,
4430 vixl32::Register value_hi,
4431 vixl32::Register temp1,
4432 vixl32::Register temp2,
4433 HInstruction* instruction) {
4434 UseScratchRegisterScope temps(GetVIXLAssembler());
4435 vixl32::Label fail;
4436 if (offset != 0) {
4437 vixl32::Register temp = temps.Acquire();
4438 __ Add(temp, addr, offset);
4439 addr = temp;
4440 }
4441 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004442 {
4443 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004444 ExactAssemblyScope aas(GetVIXLAssembler(),
4445 vixl32::kMaxInstructionSizeInBytes,
4446 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004447 // We need a load followed by store. (The address used in a STREX instruction must
4448 // be the same as the address in the most recently executed LDREX instruction.)
4449 __ ldrexd(temp1, temp2, MemOperand(addr));
4450 codegen_->MaybeRecordImplicitNullCheck(instruction);
4451 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004452 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004453 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004454}
Artem Serov02109dd2016-09-23 17:17:54 +01004455
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004456void LocationsBuilderARMVIXL::HandleFieldSet(
4457 HInstruction* instruction, const FieldInfo& field_info) {
4458 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4459
4460 LocationSummary* locations =
4461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4462 locations->SetInAt(0, Location::RequiresRegister());
4463
4464 Primitive::Type field_type = field_info.GetFieldType();
4465 if (Primitive::IsFloatingPointType(field_type)) {
4466 locations->SetInAt(1, Location::RequiresFpuRegister());
4467 } else {
4468 locations->SetInAt(1, Location::RequiresRegister());
4469 }
4470
4471 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4472 bool generate_volatile = field_info.IsVolatile()
4473 && is_wide
4474 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4475 bool needs_write_barrier =
4476 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4477 // Temporary registers for the write barrier.
4478 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4479 if (needs_write_barrier) {
4480 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4481 locations->AddTemp(Location::RequiresRegister());
4482 } else if (generate_volatile) {
4483 // ARM encoding have some additional constraints for ldrexd/strexd:
4484 // - registers need to be consecutive
4485 // - the first register should be even but not R14.
4486 // We don't test for ARM yet, and the assertion makes sure that we
4487 // revisit this if we ever enable ARM encoding.
4488 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4489
4490 locations->AddTemp(Location::RequiresRegister());
4491 locations->AddTemp(Location::RequiresRegister());
4492 if (field_type == Primitive::kPrimDouble) {
4493 // For doubles we need two more registers to copy the value.
4494 locations->AddTemp(LocationFrom(r2));
4495 locations->AddTemp(LocationFrom(r3));
4496 }
4497 }
4498}
4499
4500void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4501 const FieldInfo& field_info,
4502 bool value_can_be_null) {
4503 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4504
4505 LocationSummary* locations = instruction->GetLocations();
4506 vixl32::Register base = InputRegisterAt(instruction, 0);
4507 Location value = locations->InAt(1);
4508
4509 bool is_volatile = field_info.IsVolatile();
4510 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4511 Primitive::Type field_type = field_info.GetFieldType();
4512 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4513 bool needs_write_barrier =
4514 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4515
4516 if (is_volatile) {
4517 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4518 }
4519
4520 switch (field_type) {
4521 case Primitive::kPrimBoolean:
4522 case Primitive::kPrimByte: {
4523 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4524 break;
4525 }
4526
4527 case Primitive::kPrimShort:
4528 case Primitive::kPrimChar: {
4529 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4530 break;
4531 }
4532
4533 case Primitive::kPrimInt:
4534 case Primitive::kPrimNot: {
4535 if (kPoisonHeapReferences && needs_write_barrier) {
4536 // Note that in the case where `value` is a null reference,
4537 // we do not enter this block, as a null reference does not
4538 // need poisoning.
4539 DCHECK_EQ(field_type, Primitive::kPrimNot);
4540 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4541 __ Mov(temp, RegisterFrom(value));
4542 GetAssembler()->PoisonHeapReference(temp);
4543 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4544 } else {
4545 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4546 }
4547 break;
4548 }
4549
4550 case Primitive::kPrimLong: {
4551 if (is_volatile && !atomic_ldrd_strd) {
4552 GenerateWideAtomicStore(base,
4553 offset,
4554 LowRegisterFrom(value),
4555 HighRegisterFrom(value),
4556 RegisterFrom(locations->GetTemp(0)),
4557 RegisterFrom(locations->GetTemp(1)),
4558 instruction);
4559 } else {
4560 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4561 codegen_->MaybeRecordImplicitNullCheck(instruction);
4562 }
4563 break;
4564 }
4565
4566 case Primitive::kPrimFloat: {
4567 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4568 break;
4569 }
4570
4571 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004572 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004573 if (is_volatile && !atomic_ldrd_strd) {
4574 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4575 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4576
4577 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4578
4579 GenerateWideAtomicStore(base,
4580 offset,
4581 value_reg_lo,
4582 value_reg_hi,
4583 RegisterFrom(locations->GetTemp(2)),
4584 RegisterFrom(locations->GetTemp(3)),
4585 instruction);
4586 } else {
4587 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4588 codegen_->MaybeRecordImplicitNullCheck(instruction);
4589 }
4590 break;
4591 }
4592
4593 case Primitive::kPrimVoid:
4594 LOG(FATAL) << "Unreachable type " << field_type;
4595 UNREACHABLE();
4596 }
4597
4598 // Longs and doubles are handled in the switch.
4599 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004600 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4601 // should use a scope and the assembler to emit the store instruction to guarantee that we
4602 // record the pc at the correct position. But the `Assembler` does not automatically handle
4603 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4604 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004605 codegen_->MaybeRecordImplicitNullCheck(instruction);
4606 }
4607
4608 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4609 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4610 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4611 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4612 }
4613
4614 if (is_volatile) {
4615 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4616 }
4617}
4618
Artem Serov02d37832016-10-25 15:25:33 +01004619void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4620 const FieldInfo& field_info) {
4621 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4622
4623 bool object_field_get_with_read_barrier =
4624 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4625 LocationSummary* locations =
4626 new (GetGraph()->GetArena()) LocationSummary(instruction,
4627 object_field_get_with_read_barrier ?
4628 LocationSummary::kCallOnSlowPath :
4629 LocationSummary::kNoCall);
4630 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4631 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4632 }
4633 locations->SetInAt(0, Location::RequiresRegister());
4634
4635 bool volatile_for_double = field_info.IsVolatile()
4636 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4637 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4638 // The output overlaps in case of volatile long: we don't want the
4639 // code generated by GenerateWideAtomicLoad to overwrite the
4640 // object's location. Likewise, in the case of an object field get
4641 // with read barriers enabled, we do not want the load to overwrite
4642 // the object's location, as we need it to emit the read barrier.
4643 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4644 object_field_get_with_read_barrier;
4645
4646 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4647 locations->SetOut(Location::RequiresFpuRegister());
4648 } else {
4649 locations->SetOut(Location::RequiresRegister(),
4650 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4651 }
4652 if (volatile_for_double) {
4653 // ARM encoding have some additional constraints for ldrexd/strexd:
4654 // - registers need to be consecutive
4655 // - the first register should be even but not R14.
4656 // We don't test for ARM yet, and the assertion makes sure that we
4657 // revisit this if we ever enable ARM encoding.
4658 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4659 locations->AddTemp(Location::RequiresRegister());
4660 locations->AddTemp(Location::RequiresRegister());
4661 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4662 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004663 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004664 locations->AddTemp(Location::RequiresRegister());
4665 }
4666}
4667
4668Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4669 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4670 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4671 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4672 return Location::ConstantLocation(input->AsConstant());
4673 } else {
4674 return Location::RequiresFpuRegister();
4675 }
4676}
4677
Artem Serov02109dd2016-09-23 17:17:54 +01004678Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4679 Opcode opcode) {
4680 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4681 if (constant->IsConstant() &&
4682 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4683 return Location::ConstantLocation(constant->AsConstant());
4684 }
4685 return Location::RequiresRegister();
4686}
4687
4688bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4689 Opcode opcode) {
4690 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4691 if (Primitive::Is64BitType(input_cst->GetType())) {
4692 Opcode high_opcode = opcode;
4693 SetCc low_set_cc = kCcDontCare;
4694 switch (opcode) {
4695 case SUB:
4696 // Flip the operation to an ADD.
4697 value = -value;
4698 opcode = ADD;
4699 FALLTHROUGH_INTENDED;
4700 case ADD:
4701 if (Low32Bits(value) == 0u) {
4702 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4703 }
4704 high_opcode = ADC;
4705 low_set_cc = kCcSet;
4706 break;
4707 default:
4708 break;
4709 }
4710 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4711 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4712 } else {
4713 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4714 }
4715}
4716
4717// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4718// enabled.
4719bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4720 Opcode opcode,
4721 SetCc set_cc) {
4722 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4723 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4724 return true;
4725 }
4726 Opcode neg_opcode = kNoOperand;
4727 switch (opcode) {
4728 case AND: neg_opcode = BIC; value = ~value; break;
4729 case ORR: neg_opcode = ORN; value = ~value; break;
4730 case ADD: neg_opcode = SUB; value = -value; break;
4731 case ADC: neg_opcode = SBC; value = ~value; break;
4732 case SUB: neg_opcode = ADD; value = -value; break;
4733 case SBC: neg_opcode = ADC; value = ~value; break;
4734 default:
4735 return false;
4736 }
4737 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4738}
4739
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004740void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4741 const FieldInfo& field_info) {
4742 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4743
4744 LocationSummary* locations = instruction->GetLocations();
4745 vixl32::Register base = InputRegisterAt(instruction, 0);
4746 Location out = locations->Out();
4747 bool is_volatile = field_info.IsVolatile();
4748 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4749 Primitive::Type field_type = field_info.GetFieldType();
4750 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4751
4752 switch (field_type) {
4753 case Primitive::kPrimBoolean:
4754 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4755 break;
4756
4757 case Primitive::kPrimByte:
4758 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4759 break;
4760
4761 case Primitive::kPrimShort:
4762 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4763 break;
4764
4765 case Primitive::kPrimChar:
4766 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4767 break;
4768
4769 case Primitive::kPrimInt:
4770 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4771 break;
4772
4773 case Primitive::kPrimNot: {
4774 // /* HeapReference<Object> */ out = *(base + offset)
4775 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004776 Location temp_loc = locations->GetTemp(0);
4777 // Note that a potential implicit null check is handled in this
4778 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4779 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4780 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4781 if (is_volatile) {
4782 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4783 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004784 } else {
4785 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004786 codegen_->MaybeRecordImplicitNullCheck(instruction);
4787 if (is_volatile) {
4788 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4789 }
4790 // If read barriers are enabled, emit read barriers other than
4791 // Baker's using a slow path (and also unpoison the loaded
4792 // reference, if heap poisoning is enabled).
4793 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4794 }
4795 break;
4796 }
4797
4798 case Primitive::kPrimLong:
4799 if (is_volatile && !atomic_ldrd_strd) {
4800 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4801 } else {
4802 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4803 }
4804 break;
4805
4806 case Primitive::kPrimFloat:
4807 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4808 break;
4809
4810 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004811 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004812 if (is_volatile && !atomic_ldrd_strd) {
4813 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4814 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4815 GenerateWideAtomicLoad(base, offset, lo, hi);
4816 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4817 // scope.
4818 codegen_->MaybeRecordImplicitNullCheck(instruction);
4819 __ Vmov(out_dreg, lo, hi);
4820 } else {
4821 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004822 codegen_->MaybeRecordImplicitNullCheck(instruction);
4823 }
4824 break;
4825 }
4826
4827 case Primitive::kPrimVoid:
4828 LOG(FATAL) << "Unreachable type " << field_type;
4829 UNREACHABLE();
4830 }
4831
4832 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4833 // Potential implicit null checks, in the case of reference or
4834 // double fields, are handled in the previous switch statement.
4835 } else {
4836 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004837 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4838 // should use a scope and the assembler to emit the load instruction to guarantee that we
4839 // record the pc at the correct position. But the `Assembler` does not automatically handle
4840 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4841 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004842 codegen_->MaybeRecordImplicitNullCheck(instruction);
4843 }
4844
4845 if (is_volatile) {
4846 if (field_type == Primitive::kPrimNot) {
4847 // Memory barriers, in the case of references, are also handled
4848 // in the previous switch statement.
4849 } else {
4850 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4851 }
4852 }
4853}
4854
4855void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4856 HandleFieldSet(instruction, instruction->GetFieldInfo());
4857}
4858
4859void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4861}
4862
4863void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4864 HandleFieldGet(instruction, instruction->GetFieldInfo());
4865}
4866
4867void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4868 HandleFieldGet(instruction, instruction->GetFieldInfo());
4869}
4870
4871void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4872 HandleFieldGet(instruction, instruction->GetFieldInfo());
4873}
4874
4875void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4876 HandleFieldGet(instruction, instruction->GetFieldInfo());
4877}
4878
Scott Wakelingc34dba72016-10-03 10:14:44 +01004879void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4880 HandleFieldSet(instruction, instruction->GetFieldInfo());
4881}
4882
4883void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4884 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4885}
4886
Artem Serovcfbe9132016-10-14 15:58:56 +01004887void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4888 HUnresolvedInstanceFieldGet* instruction) {
4889 FieldAccessCallingConventionARMVIXL calling_convention;
4890 codegen_->CreateUnresolvedFieldLocationSummary(
4891 instruction, instruction->GetFieldType(), calling_convention);
4892}
4893
4894void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4895 HUnresolvedInstanceFieldGet* instruction) {
4896 FieldAccessCallingConventionARMVIXL calling_convention;
4897 codegen_->GenerateUnresolvedFieldAccess(instruction,
4898 instruction->GetFieldType(),
4899 instruction->GetFieldIndex(),
4900 instruction->GetDexPc(),
4901 calling_convention);
4902}
4903
4904void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4905 HUnresolvedInstanceFieldSet* instruction) {
4906 FieldAccessCallingConventionARMVIXL calling_convention;
4907 codegen_->CreateUnresolvedFieldLocationSummary(
4908 instruction, instruction->GetFieldType(), calling_convention);
4909}
4910
4911void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4912 HUnresolvedInstanceFieldSet* instruction) {
4913 FieldAccessCallingConventionARMVIXL calling_convention;
4914 codegen_->GenerateUnresolvedFieldAccess(instruction,
4915 instruction->GetFieldType(),
4916 instruction->GetFieldIndex(),
4917 instruction->GetDexPc(),
4918 calling_convention);
4919}
4920
4921void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4922 HUnresolvedStaticFieldGet* instruction) {
4923 FieldAccessCallingConventionARMVIXL calling_convention;
4924 codegen_->CreateUnresolvedFieldLocationSummary(
4925 instruction, instruction->GetFieldType(), calling_convention);
4926}
4927
4928void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4929 HUnresolvedStaticFieldGet* instruction) {
4930 FieldAccessCallingConventionARMVIXL calling_convention;
4931 codegen_->GenerateUnresolvedFieldAccess(instruction,
4932 instruction->GetFieldType(),
4933 instruction->GetFieldIndex(),
4934 instruction->GetDexPc(),
4935 calling_convention);
4936}
4937
4938void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4939 HUnresolvedStaticFieldSet* instruction) {
4940 FieldAccessCallingConventionARMVIXL calling_convention;
4941 codegen_->CreateUnresolvedFieldLocationSummary(
4942 instruction, instruction->GetFieldType(), calling_convention);
4943}
4944
4945void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4946 HUnresolvedStaticFieldSet* instruction) {
4947 FieldAccessCallingConventionARMVIXL calling_convention;
4948 codegen_->GenerateUnresolvedFieldAccess(instruction,
4949 instruction->GetFieldType(),
4950 instruction->GetFieldIndex(),
4951 instruction->GetDexPc(),
4952 calling_convention);
4953}
4954
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004955void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004956 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004957 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004958}
4959
4960void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4961 if (CanMoveNullCheckToUser(instruction)) {
4962 return;
4963 }
4964
4965 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004966 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004967 ExactAssemblyScope aas(GetVIXLAssembler(),
4968 vixl32::kMaxInstructionSizeInBytes,
4969 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004970 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4971 RecordPcInfo(instruction, instruction->GetDexPc());
4972}
4973
4974void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4975 NullCheckSlowPathARMVIXL* slow_path =
4976 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4977 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004978 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004979}
4980
4981void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4982 codegen_->GenerateNullCheck(instruction);
4983}
4984
Scott Wakelingc34dba72016-10-03 10:14:44 +01004985static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4986 switch (type) {
4987 case Primitive::kPrimNot:
4988 return kLoadWord;
4989 case Primitive::kPrimBoolean:
4990 return kLoadUnsignedByte;
4991 case Primitive::kPrimByte:
4992 return kLoadSignedByte;
4993 case Primitive::kPrimChar:
4994 return kLoadUnsignedHalfword;
4995 case Primitive::kPrimShort:
4996 return kLoadSignedHalfword;
4997 case Primitive::kPrimInt:
4998 return kLoadWord;
4999 case Primitive::kPrimLong:
5000 return kLoadWordPair;
5001 case Primitive::kPrimFloat:
5002 return kLoadSWord;
5003 case Primitive::kPrimDouble:
5004 return kLoadDWord;
5005 default:
5006 LOG(FATAL) << "Unreachable type " << type;
5007 UNREACHABLE();
5008 }
5009}
5010
5011static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5012 switch (type) {
5013 case Primitive::kPrimNot:
5014 return kStoreWord;
5015 case Primitive::kPrimBoolean:
5016 case Primitive::kPrimByte:
5017 return kStoreByte;
5018 case Primitive::kPrimChar:
5019 case Primitive::kPrimShort:
5020 return kStoreHalfword;
5021 case Primitive::kPrimInt:
5022 return kStoreWord;
5023 case Primitive::kPrimLong:
5024 return kStoreWordPair;
5025 case Primitive::kPrimFloat:
5026 return kStoreSWord;
5027 case Primitive::kPrimDouble:
5028 return kStoreDWord;
5029 default:
5030 LOG(FATAL) << "Unreachable type " << type;
5031 UNREACHABLE();
5032 }
5033}
5034
5035void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5036 Location out_loc,
5037 vixl32::Register base,
5038 vixl32::Register reg_index,
5039 vixl32::Condition cond) {
5040 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5041 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5042
5043 switch (type) {
5044 case Primitive::kPrimByte:
5045 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5046 break;
5047 case Primitive::kPrimBoolean:
5048 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5049 break;
5050 case Primitive::kPrimShort:
5051 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5052 break;
5053 case Primitive::kPrimChar:
5054 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5055 break;
5056 case Primitive::kPrimNot:
5057 case Primitive::kPrimInt:
5058 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5059 break;
5060 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5061 case Primitive::kPrimLong:
5062 case Primitive::kPrimFloat:
5063 case Primitive::kPrimDouble:
5064 default:
5065 LOG(FATAL) << "Unreachable type " << type;
5066 UNREACHABLE();
5067 }
5068}
5069
5070void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5071 Location loc,
5072 vixl32::Register base,
5073 vixl32::Register reg_index,
5074 vixl32::Condition cond) {
5075 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5076 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5077
5078 switch (type) {
5079 case Primitive::kPrimByte:
5080 case Primitive::kPrimBoolean:
5081 __ Strb(cond, RegisterFrom(loc), mem_address);
5082 break;
5083 case Primitive::kPrimShort:
5084 case Primitive::kPrimChar:
5085 __ Strh(cond, RegisterFrom(loc), mem_address);
5086 break;
5087 case Primitive::kPrimNot:
5088 case Primitive::kPrimInt:
5089 __ Str(cond, RegisterFrom(loc), mem_address);
5090 break;
5091 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5092 case Primitive::kPrimLong:
5093 case Primitive::kPrimFloat:
5094 case Primitive::kPrimDouble:
5095 default:
5096 LOG(FATAL) << "Unreachable type " << type;
5097 UNREACHABLE();
5098 }
5099}
5100
5101void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5102 bool object_array_get_with_read_barrier =
5103 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5104 LocationSummary* locations =
5105 new (GetGraph()->GetArena()) LocationSummary(instruction,
5106 object_array_get_with_read_barrier ?
5107 LocationSummary::kCallOnSlowPath :
5108 LocationSummary::kNoCall);
5109 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005110 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005111 }
5112 locations->SetInAt(0, Location::RequiresRegister());
5113 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5114 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5115 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5116 } else {
5117 // The output overlaps in the case of an object array get with
5118 // read barriers enabled: we do not want the move to overwrite the
5119 // array's location, as we need it to emit the read barrier.
5120 locations->SetOut(
5121 Location::RequiresRegister(),
5122 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5123 }
5124 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005125 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005126 // Also need for String compression feature.
5127 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5128 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005129 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005130 }
5131}
5132
5133void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005134 LocationSummary* locations = instruction->GetLocations();
5135 Location obj_loc = locations->InAt(0);
5136 vixl32::Register obj = InputRegisterAt(instruction, 0);
5137 Location index = locations->InAt(1);
5138 Location out_loc = locations->Out();
5139 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5140 Primitive::Type type = instruction->GetType();
5141 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5142 instruction->IsStringCharAt();
5143 HInstruction* array_instr = instruction->GetArray();
5144 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005145
5146 switch (type) {
5147 case Primitive::kPrimBoolean:
5148 case Primitive::kPrimByte:
5149 case Primitive::kPrimShort:
5150 case Primitive::kPrimChar:
5151 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005152 vixl32::Register length;
5153 if (maybe_compressed_char_at) {
5154 length = RegisterFrom(locations->GetTemp(0));
5155 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5156 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5157 codegen_->MaybeRecordImplicitNullCheck(instruction);
5158 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005159 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005160 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005161 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005162 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005163 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5164 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5165 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005166 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005167 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5168 RegisterFrom(out_loc),
5169 obj,
5170 data_offset + const_index);
5171 __ B(&done);
5172 __ Bind(&uncompressed_load);
5173 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5174 RegisterFrom(out_loc),
5175 obj,
5176 data_offset + (const_index << 1));
5177 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005178 } else {
5179 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5180
5181 LoadOperandType load_type = GetLoadOperandType(type);
5182 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5183 }
5184 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005185 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005186 vixl32::Register temp = temps.Acquire();
5187
5188 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005189 // We do not need to compute the intermediate address from the array: the
5190 // input instruction has done it already. See the comment in
5191 // `TryExtractArrayAccessAddress()`.
5192 if (kIsDebugBuild) {
5193 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005194 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005195 }
5196 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005197 } else {
5198 __ Add(temp, obj, data_offset);
5199 }
5200 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005201 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005202 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5203 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5204 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005205 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005206 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
5207 __ B(&done);
5208 __ Bind(&uncompressed_load);
5209 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
5210 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005211 } else {
5212 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5213 }
5214 }
5215 break;
5216 }
5217
5218 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005219 // The read barrier instrumentation of object ArrayGet
5220 // instructions does not support the HIntermediateAddress
5221 // instruction.
5222 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5223
Scott Wakelingc34dba72016-10-03 10:14:44 +01005224 static_assert(
5225 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5226 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5227 // /* HeapReference<Object> */ out =
5228 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5229 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005230 Location temp = locations->GetTemp(0);
5231 // Note that a potential implicit null check is handled in this
5232 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5233 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5234 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005235 } else {
5236 vixl32::Register out = OutputRegister(instruction);
5237 if (index.IsConstant()) {
5238 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005239 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005240 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005241 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5242 // we should use a scope and the assembler to emit the load instruction to guarantee that
5243 // we record the pc at the correct position. But the `Assembler` does not automatically
5244 // handle unencodable offsets. Practically, everything is fine because the helper and
5245 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005246 codegen_->MaybeRecordImplicitNullCheck(instruction);
5247 // If read barriers are enabled, emit read barriers other than
5248 // Baker's using a slow path (and also unpoison the loaded
5249 // reference, if heap poisoning is enabled).
5250 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5251 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005252 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005253 vixl32::Register temp = temps.Acquire();
5254
5255 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005256 // We do not need to compute the intermediate address from the array: the
5257 // input instruction has done it already. See the comment in
5258 // `TryExtractArrayAccessAddress()`.
5259 if (kIsDebugBuild) {
5260 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005261 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005262 }
5263 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005264 } else {
5265 __ Add(temp, obj, data_offset);
5266 }
5267 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005268 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005269 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5270 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5271 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005272 codegen_->MaybeRecordImplicitNullCheck(instruction);
5273 // If read barriers are enabled, emit read barriers other than
5274 // Baker's using a slow path (and also unpoison the loaded
5275 // reference, if heap poisoning is enabled).
5276 codegen_->MaybeGenerateReadBarrierSlow(
5277 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5278 }
5279 }
5280 break;
5281 }
5282
5283 case Primitive::kPrimLong: {
5284 if (index.IsConstant()) {
5285 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005286 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005287 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5288 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005289 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005290 vixl32::Register temp = temps.Acquire();
5291 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5292 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5293 }
5294 break;
5295 }
5296
5297 case Primitive::kPrimFloat: {
5298 vixl32::SRegister out = SRegisterFrom(out_loc);
5299 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005300 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005301 GetAssembler()->LoadSFromOffset(out, obj, offset);
5302 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005303 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005304 vixl32::Register temp = temps.Acquire();
5305 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5306 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5307 }
5308 break;
5309 }
5310
5311 case Primitive::kPrimDouble: {
5312 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005313 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005314 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5315 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005316 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005317 vixl32::Register temp = temps.Acquire();
5318 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5319 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5320 }
5321 break;
5322 }
5323
5324 case Primitive::kPrimVoid:
5325 LOG(FATAL) << "Unreachable type " << type;
5326 UNREACHABLE();
5327 }
5328
5329 if (type == Primitive::kPrimNot) {
5330 // Potential implicit null checks, in the case of reference
5331 // arrays, are handled in the previous switch statement.
5332 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005333 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5334 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005335 codegen_->MaybeRecordImplicitNullCheck(instruction);
5336 }
5337}
5338
5339void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5340 Primitive::Type value_type = instruction->GetComponentType();
5341
5342 bool needs_write_barrier =
5343 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5344 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5345
5346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5347 instruction,
5348 may_need_runtime_call_for_type_check ?
5349 LocationSummary::kCallOnSlowPath :
5350 LocationSummary::kNoCall);
5351
5352 locations->SetInAt(0, Location::RequiresRegister());
5353 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5354 if (Primitive::IsFloatingPointType(value_type)) {
5355 locations->SetInAt(2, Location::RequiresFpuRegister());
5356 } else {
5357 locations->SetInAt(2, Location::RequiresRegister());
5358 }
5359 if (needs_write_barrier) {
5360 // Temporary registers for the write barrier.
5361 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5362 locations->AddTemp(Location::RequiresRegister());
5363 }
5364}
5365
5366void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005367 LocationSummary* locations = instruction->GetLocations();
5368 vixl32::Register array = InputRegisterAt(instruction, 0);
5369 Location index = locations->InAt(1);
5370 Primitive::Type value_type = instruction->GetComponentType();
5371 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5372 bool needs_write_barrier =
5373 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5374 uint32_t data_offset =
5375 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5376 Location value_loc = locations->InAt(2);
5377 HInstruction* array_instr = instruction->GetArray();
5378 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005379
5380 switch (value_type) {
5381 case Primitive::kPrimBoolean:
5382 case Primitive::kPrimByte:
5383 case Primitive::kPrimShort:
5384 case Primitive::kPrimChar:
5385 case Primitive::kPrimInt: {
5386 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005387 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005388 uint32_t full_offset =
5389 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5390 StoreOperandType store_type = GetStoreOperandType(value_type);
5391 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5392 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005393 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005394 vixl32::Register temp = temps.Acquire();
5395
5396 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005397 // We do not need to compute the intermediate address from the array: the
5398 // input instruction has done it already. See the comment in
5399 // `TryExtractArrayAccessAddress()`.
5400 if (kIsDebugBuild) {
5401 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005402 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005403 }
5404 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005405 } else {
5406 __ Add(temp, array, data_offset);
5407 }
5408 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5409 }
5410 break;
5411 }
5412
5413 case Primitive::kPrimNot: {
5414 vixl32::Register value = RegisterFrom(value_loc);
5415 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5416 // See the comment in instruction_simplifier_shared.cc.
5417 DCHECK(!has_intermediate_address);
5418
5419 if (instruction->InputAt(2)->IsNullConstant()) {
5420 // Just setting null.
5421 if (index.IsConstant()) {
5422 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005423 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005424 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5425 } else {
5426 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005427 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005428 vixl32::Register temp = temps.Acquire();
5429 __ Add(temp, array, data_offset);
5430 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5431 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005432 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5433 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005434 codegen_->MaybeRecordImplicitNullCheck(instruction);
5435 DCHECK(!needs_write_barrier);
5436 DCHECK(!may_need_runtime_call_for_type_check);
5437 break;
5438 }
5439
5440 DCHECK(needs_write_barrier);
5441 Location temp1_loc = locations->GetTemp(0);
5442 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5443 Location temp2_loc = locations->GetTemp(1);
5444 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5445 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5446 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5447 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5448 vixl32::Label done;
5449 SlowPathCodeARMVIXL* slow_path = nullptr;
5450
5451 if (may_need_runtime_call_for_type_check) {
5452 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5453 codegen_->AddSlowPath(slow_path);
5454 if (instruction->GetValueCanBeNull()) {
5455 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005456 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005457 if (index.IsConstant()) {
5458 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005459 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005460 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5461 } else {
5462 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005463 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005464 vixl32::Register temp = temps.Acquire();
5465 __ Add(temp, array, data_offset);
5466 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5467 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005468 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5469 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005470 codegen_->MaybeRecordImplicitNullCheck(instruction);
5471 __ B(&done);
5472 __ Bind(&non_zero);
5473 }
5474
5475 // Note that when read barriers are enabled, the type checks
5476 // are performed without read barriers. This is fine, even in
5477 // the case where a class object is in the from-space after
5478 // the flip, as a comparison involving such a type would not
5479 // produce a false positive; it may of course produce a false
5480 // negative, in which case we would take the ArraySet slow
5481 // path.
5482
Alexandre Rames374ddf32016-11-04 10:40:49 +00005483 {
5484 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005485 ExactAssemblyScope aas(GetVIXLAssembler(),
5486 vixl32::kMaxInstructionSizeInBytes,
5487 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005488 // /* HeapReference<Class> */ temp1 = array->klass_
5489 __ ldr(temp1, MemOperand(array, class_offset));
5490 codegen_->MaybeRecordImplicitNullCheck(instruction);
5491 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005492 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5493
5494 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5495 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5496 // /* HeapReference<Class> */ temp2 = value->klass_
5497 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5498 // If heap poisoning is enabled, no need to unpoison `temp1`
5499 // nor `temp2`, as we are comparing two poisoned references.
5500 __ Cmp(temp1, temp2);
5501
5502 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5503 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00005504 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005505 // If heap poisoning is enabled, the `temp1` reference has
5506 // not been unpoisoned yet; unpoison it now.
5507 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5508
5509 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5510 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5511 // If heap poisoning is enabled, no need to unpoison
5512 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005513 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005514 __ Bind(&do_put);
5515 } else {
5516 __ B(ne, slow_path->GetEntryLabel());
5517 }
5518 }
5519
5520 vixl32::Register source = value;
5521 if (kPoisonHeapReferences) {
5522 // Note that in the case where `value` is a null reference,
5523 // we do not enter this block, as a null reference does not
5524 // need poisoning.
5525 DCHECK_EQ(value_type, Primitive::kPrimNot);
5526 __ Mov(temp1, value);
5527 GetAssembler()->PoisonHeapReference(temp1);
5528 source = temp1;
5529 }
5530
5531 if (index.IsConstant()) {
5532 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005533 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005534 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5535 } else {
5536 DCHECK(index.IsRegister()) << index;
5537
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005538 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005539 vixl32::Register temp = temps.Acquire();
5540 __ Add(temp, array, data_offset);
5541 codegen_->StoreToShiftedRegOffset(value_type,
5542 LocationFrom(source),
5543 temp,
5544 RegisterFrom(index));
5545 }
5546
5547 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005548 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5549 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005550 codegen_->MaybeRecordImplicitNullCheck(instruction);
5551 }
5552
5553 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5554
5555 if (done.IsReferenced()) {
5556 __ Bind(&done);
5557 }
5558
5559 if (slow_path != nullptr) {
5560 __ Bind(slow_path->GetExitLabel());
5561 }
5562
5563 break;
5564 }
5565
5566 case Primitive::kPrimLong: {
5567 Location value = locations->InAt(2);
5568 if (index.IsConstant()) {
5569 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005570 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005571 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5572 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005573 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005574 vixl32::Register temp = temps.Acquire();
5575 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5576 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5577 }
5578 break;
5579 }
5580
5581 case Primitive::kPrimFloat: {
5582 Location value = locations->InAt(2);
5583 DCHECK(value.IsFpuRegister());
5584 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005585 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005586 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5587 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005588 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005589 vixl32::Register temp = temps.Acquire();
5590 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5591 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5592 }
5593 break;
5594 }
5595
5596 case Primitive::kPrimDouble: {
5597 Location value = locations->InAt(2);
5598 DCHECK(value.IsFpuRegisterPair());
5599 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005600 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005601 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5602 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005603 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005604 vixl32::Register temp = temps.Acquire();
5605 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5606 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5607 }
5608 break;
5609 }
5610
5611 case Primitive::kPrimVoid:
5612 LOG(FATAL) << "Unreachable type " << value_type;
5613 UNREACHABLE();
5614 }
5615
5616 // Objects are handled in the switch.
5617 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005618 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5619 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005620 codegen_->MaybeRecordImplicitNullCheck(instruction);
5621 }
5622}
5623
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005624void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5625 LocationSummary* locations =
5626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5627 locations->SetInAt(0, Location::RequiresRegister());
5628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5629}
5630
5631void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5632 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5633 vixl32::Register obj = InputRegisterAt(instruction, 0);
5634 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005635 {
Artem Serov0fb37192016-12-06 18:13:40 +00005636 ExactAssemblyScope aas(GetVIXLAssembler(),
5637 vixl32::kMaxInstructionSizeInBytes,
5638 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005639 __ ldr(out, MemOperand(obj, offset));
5640 codegen_->MaybeRecordImplicitNullCheck(instruction);
5641 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005642 // Mask out compression flag from String's array length.
5643 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005644 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005645 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005646}
5647
Artem Serov2bbc9532016-10-21 11:51:50 +01005648void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005649 LocationSummary* locations =
5650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5651
5652 locations->SetInAt(0, Location::RequiresRegister());
5653 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5655}
5656
5657void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5658 vixl32::Register out = OutputRegister(instruction);
5659 vixl32::Register first = InputRegisterAt(instruction, 0);
5660 Location second = instruction->GetLocations()->InAt(1);
5661
Artem Serov2bbc9532016-10-21 11:51:50 +01005662 if (second.IsRegister()) {
5663 __ Add(out, first, RegisterFrom(second));
5664 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005665 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01005666 }
5667}
5668
Scott Wakelingc34dba72016-10-03 10:14:44 +01005669void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5670 RegisterSet caller_saves = RegisterSet::Empty();
5671 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5672 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5673 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5674 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5675 locations->SetInAt(0, Location::RequiresRegister());
5676 locations->SetInAt(1, Location::RequiresRegister());
5677}
5678
5679void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5680 SlowPathCodeARMVIXL* slow_path =
5681 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5682 codegen_->AddSlowPath(slow_path);
5683
5684 vixl32::Register index = InputRegisterAt(instruction, 0);
5685 vixl32::Register length = InputRegisterAt(instruction, 1);
5686
5687 __ Cmp(index, length);
5688 __ B(hs, slow_path->GetEntryLabel());
5689}
5690
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005691void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5692 vixl32::Register card,
5693 vixl32::Register object,
5694 vixl32::Register value,
5695 bool can_be_null) {
5696 vixl32::Label is_null;
5697 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005698 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005699 }
5700 GetAssembler()->LoadFromOffset(
5701 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005702 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005703 __ Strb(card, MemOperand(card, temp));
5704 if (can_be_null) {
5705 __ Bind(&is_null);
5706 }
5707}
5708
Scott Wakelingfe885462016-09-22 10:24:38 +01005709void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5710 LOG(FATAL) << "Unreachable";
5711}
5712
5713void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5714 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5715}
5716
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005717void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005718 LocationSummary* locations =
5719 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5720 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005721}
5722
5723void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5724 HBasicBlock* block = instruction->GetBlock();
5725 if (block->GetLoopInformation() != nullptr) {
5726 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5727 // The back edge will generate the suspend check.
5728 return;
5729 }
5730 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5731 // The goto will generate the suspend check.
5732 return;
5733 }
5734 GenerateSuspendCheck(instruction, nullptr);
5735}
5736
5737void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5738 HBasicBlock* successor) {
5739 SuspendCheckSlowPathARMVIXL* slow_path =
5740 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5741 if (slow_path == nullptr) {
5742 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5743 instruction->SetSlowPath(slow_path);
5744 codegen_->AddSlowPath(slow_path);
5745 if (successor != nullptr) {
5746 DCHECK(successor->IsLoopHeader());
5747 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5748 }
5749 } else {
5750 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5751 }
5752
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005753 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005754 vixl32::Register temp = temps.Acquire();
5755 GetAssembler()->LoadFromOffset(
5756 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5757 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005758 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005759 __ Bind(slow_path->GetReturnLabel());
5760 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005761 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005762 __ B(slow_path->GetEntryLabel());
5763 }
5764}
5765
Scott Wakelingfe885462016-09-22 10:24:38 +01005766ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5767 return codegen_->GetAssembler();
5768}
5769
5770void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005771 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005772 MoveOperands* move = moves_[index];
5773 Location source = move->GetSource();
5774 Location destination = move->GetDestination();
5775
5776 if (source.IsRegister()) {
5777 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005778 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005779 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005780 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005781 } else {
5782 DCHECK(destination.IsStackSlot());
5783 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005784 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005785 sp,
5786 destination.GetStackIndex());
5787 }
5788 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005789 if (destination.IsRegister()) {
5790 GetAssembler()->LoadFromOffset(kLoadWord,
5791 RegisterFrom(destination),
5792 sp,
5793 source.GetStackIndex());
5794 } else if (destination.IsFpuRegister()) {
5795 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5796 } else {
5797 DCHECK(destination.IsStackSlot());
5798 vixl32::Register temp = temps.Acquire();
5799 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5800 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5801 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005802 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005803 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005804 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005805 } else if (destination.IsFpuRegister()) {
5806 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5807 } else {
5808 DCHECK(destination.IsStackSlot());
5809 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5810 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005811 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005812 if (destination.IsDoubleStackSlot()) {
5813 vixl32::DRegister temp = temps.AcquireD();
5814 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5815 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5816 } else if (destination.IsRegisterPair()) {
5817 DCHECK(ExpectedPairLayout(destination));
5818 GetAssembler()->LoadFromOffset(
5819 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5820 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005821 DCHECK(destination.IsFpuRegisterPair()) << destination;
5822 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005823 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005824 } else if (source.IsRegisterPair()) {
5825 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005826 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5827 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005828 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005829 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005830 } else {
5831 DCHECK(destination.IsDoubleStackSlot()) << destination;
5832 DCHECK(ExpectedPairLayout(source));
5833 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005834 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005835 sp,
5836 destination.GetStackIndex());
5837 }
5838 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005839 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005840 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005841 } else if (destination.IsFpuRegisterPair()) {
5842 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5843 } else {
5844 DCHECK(destination.IsDoubleStackSlot()) << destination;
5845 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5846 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005847 } else {
5848 DCHECK(source.IsConstant()) << source;
5849 HConstant* constant = source.GetConstant();
5850 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5851 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5852 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005853 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005854 } else {
5855 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005856 vixl32::Register temp = temps.Acquire();
5857 __ Mov(temp, value);
5858 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5859 }
5860 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005861 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01005862 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005863 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5864 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005865 } else {
5866 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005867 vixl32::Register temp = temps.Acquire();
5868 __ Mov(temp, Low32Bits(value));
5869 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5870 __ Mov(temp, High32Bits(value));
5871 GetAssembler()->StoreToOffset(kStoreWord,
5872 temp,
5873 sp,
5874 destination.GetHighStackIndex(kArmWordSize));
5875 }
5876 } else if (constant->IsDoubleConstant()) {
5877 double value = constant->AsDoubleConstant()->GetValue();
5878 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005879 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005880 } else {
5881 DCHECK(destination.IsDoubleStackSlot()) << destination;
5882 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005883 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005884 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005885 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005886 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005887 GetAssembler()->StoreToOffset(kStoreWord,
5888 temp,
5889 sp,
5890 destination.GetHighStackIndex(kArmWordSize));
5891 }
5892 } else {
5893 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5894 float value = constant->AsFloatConstant()->GetValue();
5895 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005896 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005897 } else {
5898 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005899 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005900 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005901 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5902 }
5903 }
5904 }
5905}
5906
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005907void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5908 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5909 vixl32::Register temp = temps.Acquire();
5910 __ Mov(temp, reg);
5911 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5912 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005913}
5914
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005915void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5916 // TODO(VIXL32): Double check the performance of this implementation.
5917 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Artem Serov4593f7d2016-12-29 16:21:49 +00005918 vixl32::SRegister temp_1 = temps.AcquireS();
5919 vixl32::SRegister temp_2 = temps.AcquireS();
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005920
Artem Serov4593f7d2016-12-29 16:21:49 +00005921 __ Vldr(temp_1, MemOperand(sp, mem1));
5922 __ Vldr(temp_2, MemOperand(sp, mem2));
5923 __ Vstr(temp_1, MemOperand(sp, mem2));
5924 __ Vstr(temp_2, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005925}
5926
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005927void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5928 MoveOperands* move = moves_[index];
5929 Location source = move->GetSource();
5930 Location destination = move->GetDestination();
5931 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5932
5933 if (source.IsRegister() && destination.IsRegister()) {
5934 vixl32::Register temp = temps.Acquire();
5935 DCHECK(!RegisterFrom(source).Is(temp));
5936 DCHECK(!RegisterFrom(destination).Is(temp));
5937 __ Mov(temp, RegisterFrom(destination));
5938 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5939 __ Mov(RegisterFrom(source), temp);
5940 } else if (source.IsRegister() && destination.IsStackSlot()) {
5941 Exchange(RegisterFrom(source), destination.GetStackIndex());
5942 } else if (source.IsStackSlot() && destination.IsRegister()) {
5943 Exchange(RegisterFrom(destination), source.GetStackIndex());
5944 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005945 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005946 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005947 vixl32::SRegister temp = temps.AcquireS();
5948 __ Vmov(temp, SRegisterFrom(source));
5949 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5950 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005951 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5952 vixl32::DRegister temp = temps.AcquireD();
5953 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5954 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5955 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5956 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5957 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5958 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5959 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5960 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5961 vixl32::DRegister temp = temps.AcquireD();
5962 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5963 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5964 GetAssembler()->StoreDToOffset(temp, sp, mem);
5965 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005966 vixl32::DRegister first = DRegisterFrom(source);
5967 vixl32::DRegister second = DRegisterFrom(destination);
5968 vixl32::DRegister temp = temps.AcquireD();
5969 __ Vmov(temp, first);
5970 __ Vmov(first, second);
5971 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005972 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005973 vixl32::DRegister reg = source.IsFpuRegisterPair()
5974 ? DRegisterFrom(source)
5975 : DRegisterFrom(destination);
5976 int mem = source.IsFpuRegisterPair()
5977 ? destination.GetStackIndex()
5978 : source.GetStackIndex();
5979 vixl32::DRegister temp = temps.AcquireD();
5980 __ Vmov(temp, reg);
5981 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5982 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005983 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005984 vixl32::SRegister reg = source.IsFpuRegister()
5985 ? SRegisterFrom(source)
5986 : SRegisterFrom(destination);
5987 int mem = source.IsFpuRegister()
5988 ? destination.GetStackIndex()
5989 : source.GetStackIndex();
5990 vixl32::Register temp = temps.Acquire();
5991 __ Vmov(temp, reg);
5992 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5993 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005994 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5995 vixl32::DRegister temp1 = temps.AcquireD();
5996 vixl32::DRegister temp2 = temps.AcquireD();
5997 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
5998 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
5999 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6000 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6001 } else {
6002 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6003 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006004}
6005
6006void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
6007 TODO_VIXL32(FATAL);
6008}
6009
6010void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
6011 TODO_VIXL32(FATAL);
6012}
6013
Artem Serov02d37832016-10-25 15:25:33 +01006014HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006015 HLoadClass::LoadKind desired_class_load_kind) {
6016 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006017 case HLoadClass::LoadKind::kInvalid:
6018 LOG(FATAL) << "UNREACHABLE";
6019 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006020 case HLoadClass::LoadKind::kReferrersClass:
6021 break;
6022 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006023 DCHECK(!GetCompilerOptions().GetCompilePic());
6024 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006025 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6026 DCHECK(GetCompilerOptions().GetCompilePic());
6027 break;
6028 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006029 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006030 case HLoadClass::LoadKind::kBssEntry:
6031 DCHECK(!Runtime::Current()->UseJitCompilation());
6032 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006033 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006034 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006035 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006036 case HLoadClass::LoadKind::kDexCacheViaMethod:
6037 break;
6038 }
6039 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006040}
6041
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006042void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006043 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6044 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006045 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006046 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006047 cls,
6048 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006049 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006050 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006051 return;
6052 }
Vladimir Marko41559982017-01-06 14:04:23 +00006053 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006054
Artem Serovd4cc5b22016-11-04 11:19:09 +00006055 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6056 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006057 ? LocationSummary::kCallOnSlowPath
6058 : LocationSummary::kNoCall;
6059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006060 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006061 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006062 }
6063
Vladimir Marko41559982017-01-06 14:04:23 +00006064 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006065 locations->SetInAt(0, Location::RequiresRegister());
6066 }
6067 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006068 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6069 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6070 // Rely on the type resolution or initialization and marking to save everything we need.
6071 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6072 // to the custom calling convention) or by marking, so we request a different temp.
6073 locations->AddTemp(Location::RequiresRegister());
6074 RegisterSet caller_saves = RegisterSet::Empty();
6075 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6076 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6077 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6078 // that the the kPrimNot result register is the same as the first argument register.
6079 locations->SetCustomSlowPathCallerSaves(caller_saves);
6080 } else {
6081 // For non-Baker read barrier we have a temp-clobbering call.
6082 }
6083 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006084}
6085
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006086// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6087// move.
6088void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006089 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6090 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6091 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006092 return;
6093 }
Vladimir Marko41559982017-01-06 14:04:23 +00006094 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006095
Vladimir Marko41559982017-01-06 14:04:23 +00006096 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006097 Location out_loc = locations->Out();
6098 vixl32::Register out = OutputRegister(cls);
6099
Artem Serovd4cc5b22016-11-04 11:19:09 +00006100 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6101 ? kWithoutReadBarrier
6102 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006103 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006104 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006105 case HLoadClass::LoadKind::kReferrersClass: {
6106 DCHECK(!cls->CanCallRuntime());
6107 DCHECK(!cls->MustGenerateClinitCheck());
6108 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6109 vixl32::Register current_method = InputRegisterAt(cls, 0);
6110 GenerateGcRootFieldLoad(cls,
6111 out_loc,
6112 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006113 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006114 read_barrier_option);
6115 break;
6116 }
6117 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006118 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006119 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6120 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6121 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006122 break;
6123 }
6124 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006125 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006126 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6127 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6128 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6129 codegen_->EmitMovwMovtPlaceholder(labels, out);
6130 break;
6131 }
6132 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006133 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006134 uint32_t address = dchecked_integral_cast<uint32_t>(
6135 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6136 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006137 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006138 break;
6139 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006140 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006141 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6142 ? RegisterFrom(locations->GetTemp(0))
6143 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006144 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006145 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006146 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6147 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006148 generate_null_check = true;
6149 break;
6150 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006151 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006152 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6153 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006154 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006155 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006156 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006157 break;
6158 }
Vladimir Marko41559982017-01-06 14:04:23 +00006159 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006160 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006161 LOG(FATAL) << "UNREACHABLE";
6162 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006163 }
6164
6165 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6166 DCHECK(cls->CanCallRuntime());
6167 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6168 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6169 codegen_->AddSlowPath(slow_path);
6170 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006171 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006172 }
6173 if (cls->MustGenerateClinitCheck()) {
6174 GenerateClassInitializationCheck(slow_path, out);
6175 } else {
6176 __ Bind(slow_path->GetExitLabel());
6177 }
6178 }
6179}
6180
Artem Serov02d37832016-10-25 15:25:33 +01006181void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6182 LocationSummary* locations =
6183 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6184 locations->SetInAt(0, Location::RequiresRegister());
6185 if (check->HasUses()) {
6186 locations->SetOut(Location::SameAsFirstInput());
6187 }
6188}
6189
6190void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6191 // We assume the class is not null.
6192 LoadClassSlowPathARMVIXL* slow_path =
6193 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6194 check,
6195 check->GetDexPc(),
6196 /* do_clinit */ true);
6197 codegen_->AddSlowPath(slow_path);
6198 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6199}
6200
6201void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6202 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6203 UseScratchRegisterScope temps(GetVIXLAssembler());
6204 vixl32::Register temp = temps.Acquire();
6205 GetAssembler()->LoadFromOffset(kLoadWord,
6206 temp,
6207 class_reg,
6208 mirror::Class::StatusOffset().Int32Value());
6209 __ Cmp(temp, mirror::Class::kStatusInitialized);
6210 __ B(lt, slow_path->GetEntryLabel());
6211 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6212 // properly. Therefore, we do a memory fence.
6213 __ Dmb(ISH);
6214 __ Bind(slow_path->GetExitLabel());
6215}
6216
Artem Serov02d37832016-10-25 15:25:33 +01006217HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006218 HLoadString::LoadKind desired_string_load_kind) {
6219 switch (desired_string_load_kind) {
6220 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006221 DCHECK(!GetCompilerOptions().GetCompilePic());
6222 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006223 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6224 DCHECK(GetCompilerOptions().GetCompilePic());
6225 break;
6226 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006227 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006228 case HLoadString::LoadKind::kBssEntry:
6229 DCHECK(!Runtime::Current()->UseJitCompilation());
6230 break;
6231 case HLoadString::LoadKind::kJitTableAddress:
6232 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006233 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006234 case HLoadString::LoadKind::kDexCacheViaMethod:
6235 break;
6236 }
6237 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006238}
6239
6240void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006241 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006243 HLoadString::LoadKind load_kind = load->GetLoadKind();
6244 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006245 locations->SetOut(LocationFrom(r0));
6246 } else {
6247 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006248 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6249 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006250 // Rely on the pResolveString and marking to save everything we need, including temps.
6251 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6252 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006253 locations->AddTemp(Location::RequiresRegister());
6254 RegisterSet caller_saves = RegisterSet::Empty();
6255 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6256 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6257 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6258 // that the the kPrimNot result register is the same as the first argument register.
6259 locations->SetCustomSlowPathCallerSaves(caller_saves);
6260 } else {
6261 // For non-Baker read barrier we have a temp-clobbering call.
6262 }
6263 }
Artem Serov02d37832016-10-25 15:25:33 +01006264 }
6265}
6266
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006267// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6268// move.
6269void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006270 LocationSummary* locations = load->GetLocations();
6271 Location out_loc = locations->Out();
6272 vixl32::Register out = OutputRegister(load);
6273 HLoadString::LoadKind load_kind = load->GetLoadKind();
6274
6275 switch (load_kind) {
6276 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006277 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6278 load->GetStringIndex()));
6279 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006280 }
6281 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6282 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6283 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006284 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006285 codegen_->EmitMovwMovtPlaceholder(labels, out);
6286 return; // No dex cache slow path.
6287 }
6288 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006289 uint32_t address = dchecked_integral_cast<uint32_t>(
6290 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6291 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006292 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6293 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006294 }
6295 case HLoadString::LoadKind::kBssEntry: {
6296 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006297 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6298 ? RegisterFrom(locations->GetTemp(0))
6299 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006300 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006301 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006302 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6303 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6304 LoadStringSlowPathARMVIXL* slow_path =
6305 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6306 codegen_->AddSlowPath(slow_path);
6307 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6308 __ Bind(slow_path->GetExitLabel());
6309 return;
6310 }
6311 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006312 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006313 load->GetStringIndex(),
6314 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006315 // /* GcRoot<mirror::String> */ out = *out
6316 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6317 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006318 }
6319 default:
6320 break;
6321 }
Artem Serov02d37832016-10-25 15:25:33 +01006322
6323 // TODO: Re-add the compiler code to do string dex cache lookup again.
6324 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6325 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006326 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006327 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6328 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6329}
6330
6331static int32_t GetExceptionTlsOffset() {
6332 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6333}
6334
6335void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6336 LocationSummary* locations =
6337 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6338 locations->SetOut(Location::RequiresRegister());
6339}
6340
6341void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6342 vixl32::Register out = OutputRegister(load);
6343 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6344}
6345
6346
6347void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6348 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6349}
6350
6351void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6352 UseScratchRegisterScope temps(GetVIXLAssembler());
6353 vixl32::Register temp = temps.Acquire();
6354 __ Mov(temp, 0);
6355 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6356}
6357
6358void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6359 LocationSummary* locations =
6360 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6361 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6362 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6363}
6364
6365void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6366 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6367 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6368}
6369
Artem Serov657022c2016-11-23 14:19:38 +00006370// Temp is used for read barrier.
6371static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6372 if (kEmitCompilerReadBarrier &&
6373 (kUseBakerReadBarrier ||
6374 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6375 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6376 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6377 return 1;
6378 }
6379 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006380}
6381
Artem Serov657022c2016-11-23 14:19:38 +00006382// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6383// interface pointer, one for loading the current interface.
6384// The other checks have one temp for loading the object's class.
6385static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6386 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6387 return 3;
6388 }
6389 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6390}
Artem Serovcfbe9132016-10-14 15:58:56 +01006391
6392void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6393 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6394 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6395 bool baker_read_barrier_slow_path = false;
6396 switch (type_check_kind) {
6397 case TypeCheckKind::kExactCheck:
6398 case TypeCheckKind::kAbstractClassCheck:
6399 case TypeCheckKind::kClassHierarchyCheck:
6400 case TypeCheckKind::kArrayObjectCheck:
6401 call_kind =
6402 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6403 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6404 break;
6405 case TypeCheckKind::kArrayCheck:
6406 case TypeCheckKind::kUnresolvedCheck:
6407 case TypeCheckKind::kInterfaceCheck:
6408 call_kind = LocationSummary::kCallOnSlowPath;
6409 break;
6410 }
6411
6412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6413 if (baker_read_barrier_slow_path) {
6414 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6415 }
6416 locations->SetInAt(0, Location::RequiresRegister());
6417 locations->SetInAt(1, Location::RequiresRegister());
6418 // The "out" register is used as a temporary, so it overlaps with the inputs.
6419 // Note that TypeCheckSlowPathARM uses this register too.
6420 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006421 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006422}
6423
6424void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6425 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6426 LocationSummary* locations = instruction->GetLocations();
6427 Location obj_loc = locations->InAt(0);
6428 vixl32::Register obj = InputRegisterAt(instruction, 0);
6429 vixl32::Register cls = InputRegisterAt(instruction, 1);
6430 Location out_loc = locations->Out();
6431 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006432 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6433 DCHECK_LE(num_temps, 1u);
6434 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006435 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6436 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6437 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6438 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6439 vixl32::Label done, zero;
6440 SlowPathCodeARMVIXL* slow_path = nullptr;
6441
6442 // Return 0 if `obj` is null.
6443 // avoid null check if we know obj is not null.
6444 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006445 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006446 }
6447
Artem Serovcfbe9132016-10-14 15:58:56 +01006448 switch (type_check_kind) {
6449 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006450 // /* HeapReference<Class> */ out = obj->klass_
6451 GenerateReferenceLoadTwoRegisters(instruction,
6452 out_loc,
6453 obj_loc,
6454 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006455 maybe_temp_loc,
6456 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006457 __ Cmp(out, cls);
6458 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00006459 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006460 __ Mov(out, 1);
6461 __ B(&done);
6462 break;
6463 }
6464
6465 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006466 // /* HeapReference<Class> */ out = obj->klass_
6467 GenerateReferenceLoadTwoRegisters(instruction,
6468 out_loc,
6469 obj_loc,
6470 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006471 maybe_temp_loc,
6472 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006473 // If the class is abstract, we eagerly fetch the super class of the
6474 // object to avoid doing a comparison we know will fail.
6475 vixl32::Label loop;
6476 __ Bind(&loop);
6477 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006478 GenerateReferenceLoadOneRegister(instruction,
6479 out_loc,
6480 super_offset,
6481 maybe_temp_loc,
6482 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006483 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006484 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006485 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006486 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006487 __ Mov(out, 1);
6488 if (zero.IsReferenced()) {
6489 __ B(&done);
6490 }
6491 break;
6492 }
6493
6494 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006495 // /* HeapReference<Class> */ out = obj->klass_
6496 GenerateReferenceLoadTwoRegisters(instruction,
6497 out_loc,
6498 obj_loc,
6499 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006500 maybe_temp_loc,
6501 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006502 // Walk over the class hierarchy to find a match.
6503 vixl32::Label loop, success;
6504 __ Bind(&loop);
6505 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006506 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006507 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006508 GenerateReferenceLoadOneRegister(instruction,
6509 out_loc,
6510 super_offset,
6511 maybe_temp_loc,
6512 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006513 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006514 // If `out` is null, we use it for the result, and jump to `done`.
6515 __ B(&done);
6516 __ Bind(&success);
6517 __ Mov(out, 1);
6518 if (zero.IsReferenced()) {
6519 __ B(&done);
6520 }
6521 break;
6522 }
6523
6524 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006525 // /* HeapReference<Class> */ out = obj->klass_
6526 GenerateReferenceLoadTwoRegisters(instruction,
6527 out_loc,
6528 obj_loc,
6529 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006530 maybe_temp_loc,
6531 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006532 // Do an exact check.
6533 vixl32::Label exact_check;
6534 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006535 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006536 // Otherwise, we need to check that the object's class is a non-primitive array.
6537 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006538 GenerateReferenceLoadOneRegister(instruction,
6539 out_loc,
6540 component_offset,
6541 maybe_temp_loc,
6542 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006543 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006544 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006545 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6546 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006547 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006548 __ Bind(&exact_check);
6549 __ Mov(out, 1);
6550 __ B(&done);
6551 break;
6552 }
6553
6554 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006555 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006556 // /* HeapReference<Class> */ out = obj->klass_
6557 GenerateReferenceLoadTwoRegisters(instruction,
6558 out_loc,
6559 obj_loc,
6560 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006561 maybe_temp_loc,
6562 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006563 __ Cmp(out, cls);
6564 DCHECK(locations->OnlyCallsOnSlowPath());
6565 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6566 /* is_fatal */ false);
6567 codegen_->AddSlowPath(slow_path);
6568 __ B(ne, slow_path->GetEntryLabel());
6569 __ Mov(out, 1);
6570 if (zero.IsReferenced()) {
6571 __ B(&done);
6572 }
6573 break;
6574 }
6575
6576 case TypeCheckKind::kUnresolvedCheck:
6577 case TypeCheckKind::kInterfaceCheck: {
6578 // Note that we indeed only call on slow path, but we always go
6579 // into the slow path for the unresolved and interface check
6580 // cases.
6581 //
6582 // We cannot directly call the InstanceofNonTrivial runtime
6583 // entry point without resorting to a type checking slow path
6584 // here (i.e. by calling InvokeRuntime directly), as it would
6585 // require to assign fixed registers for the inputs of this
6586 // HInstanceOf instruction (following the runtime calling
6587 // convention), which might be cluttered by the potential first
6588 // read barrier emission at the beginning of this method.
6589 //
6590 // TODO: Introduce a new runtime entry point taking the object
6591 // to test (instead of its class) as argument, and let it deal
6592 // with the read barrier issues. This will let us refactor this
6593 // case of the `switch` code as it was previously (with a direct
6594 // call to the runtime not using a type checking slow path).
6595 // This should also be beneficial for the other cases above.
6596 DCHECK(locations->OnlyCallsOnSlowPath());
6597 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6598 /* is_fatal */ false);
6599 codegen_->AddSlowPath(slow_path);
6600 __ B(slow_path->GetEntryLabel());
6601 if (zero.IsReferenced()) {
6602 __ B(&done);
6603 }
6604 break;
6605 }
6606 }
6607
6608 if (zero.IsReferenced()) {
6609 __ Bind(&zero);
6610 __ Mov(out, 0);
6611 }
6612
6613 if (done.IsReferenced()) {
6614 __ Bind(&done);
6615 }
6616
6617 if (slow_path != nullptr) {
6618 __ Bind(slow_path->GetExitLabel());
6619 }
6620}
6621
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006622void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6623 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6624 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6625
6626 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6627 switch (type_check_kind) {
6628 case TypeCheckKind::kExactCheck:
6629 case TypeCheckKind::kAbstractClassCheck:
6630 case TypeCheckKind::kClassHierarchyCheck:
6631 case TypeCheckKind::kArrayObjectCheck:
6632 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6633 LocationSummary::kCallOnSlowPath :
6634 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6635 break;
6636 case TypeCheckKind::kArrayCheck:
6637 case TypeCheckKind::kUnresolvedCheck:
6638 case TypeCheckKind::kInterfaceCheck:
6639 call_kind = LocationSummary::kCallOnSlowPath;
6640 break;
6641 }
6642
6643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6644 locations->SetInAt(0, Location::RequiresRegister());
6645 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006646 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006647}
6648
6649void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6650 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6651 LocationSummary* locations = instruction->GetLocations();
6652 Location obj_loc = locations->InAt(0);
6653 vixl32::Register obj = InputRegisterAt(instruction, 0);
6654 vixl32::Register cls = InputRegisterAt(instruction, 1);
6655 Location temp_loc = locations->GetTemp(0);
6656 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006657 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6658 DCHECK_LE(num_temps, 3u);
6659 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6660 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6661 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6662 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6663 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6664 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6665 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6666 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6667 const uint32_t object_array_data_offset =
6668 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006669
Artem Serov657022c2016-11-23 14:19:38 +00006670 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6671 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6672 // read barriers is done for performance and code size reasons.
6673 bool is_type_check_slow_path_fatal = false;
6674 if (!kEmitCompilerReadBarrier) {
6675 is_type_check_slow_path_fatal =
6676 (type_check_kind == TypeCheckKind::kExactCheck ||
6677 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6678 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6679 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6680 !instruction->CanThrowIntoCatchBlock();
6681 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006682 SlowPathCodeARMVIXL* type_check_slow_path =
6683 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6684 is_type_check_slow_path_fatal);
6685 codegen_->AddSlowPath(type_check_slow_path);
6686
6687 vixl32::Label done;
6688 // Avoid null check if we know obj is not null.
6689 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006690 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006691 }
6692
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006693 switch (type_check_kind) {
6694 case TypeCheckKind::kExactCheck:
6695 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006696 // /* HeapReference<Class> */ temp = obj->klass_
6697 GenerateReferenceLoadTwoRegisters(instruction,
6698 temp_loc,
6699 obj_loc,
6700 class_offset,
6701 maybe_temp2_loc,
6702 kWithoutReadBarrier);
6703
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006704 __ Cmp(temp, cls);
6705 // Jump to slow path for throwing the exception or doing a
6706 // more involved array check.
6707 __ B(ne, type_check_slow_path->GetEntryLabel());
6708 break;
6709 }
6710
6711 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006712 // /* HeapReference<Class> */ temp = obj->klass_
6713 GenerateReferenceLoadTwoRegisters(instruction,
6714 temp_loc,
6715 obj_loc,
6716 class_offset,
6717 maybe_temp2_loc,
6718 kWithoutReadBarrier);
6719
Artem Serovcfbe9132016-10-14 15:58:56 +01006720 // If the class is abstract, we eagerly fetch the super class of the
6721 // object to avoid doing a comparison we know will fail.
6722 vixl32::Label loop;
6723 __ Bind(&loop);
6724 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006725 GenerateReferenceLoadOneRegister(instruction,
6726 temp_loc,
6727 super_offset,
6728 maybe_temp2_loc,
6729 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006730
6731 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6732 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006733 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006734
6735 // Otherwise, compare the classes.
6736 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006737 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006738 break;
6739 }
6740
6741 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006742 // /* HeapReference<Class> */ temp = obj->klass_
6743 GenerateReferenceLoadTwoRegisters(instruction,
6744 temp_loc,
6745 obj_loc,
6746 class_offset,
6747 maybe_temp2_loc,
6748 kWithoutReadBarrier);
6749
Artem Serovcfbe9132016-10-14 15:58:56 +01006750 // Walk over the class hierarchy to find a match.
6751 vixl32::Label loop;
6752 __ Bind(&loop);
6753 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006754 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006755
6756 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006757 GenerateReferenceLoadOneRegister(instruction,
6758 temp_loc,
6759 super_offset,
6760 maybe_temp2_loc,
6761 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006762
6763 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6764 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006765 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006766 // Otherwise, jump to the beginning of the loop.
6767 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006768 break;
6769 }
6770
Artem Serovcfbe9132016-10-14 15:58:56 +01006771 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006772 // /* HeapReference<Class> */ temp = obj->klass_
6773 GenerateReferenceLoadTwoRegisters(instruction,
6774 temp_loc,
6775 obj_loc,
6776 class_offset,
6777 maybe_temp2_loc,
6778 kWithoutReadBarrier);
6779
Artem Serovcfbe9132016-10-14 15:58:56 +01006780 // Do an exact check.
6781 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006782 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006783
6784 // Otherwise, we need to check that the object's class is a non-primitive array.
6785 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006786 GenerateReferenceLoadOneRegister(instruction,
6787 temp_loc,
6788 component_offset,
6789 maybe_temp2_loc,
6790 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006791 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006792 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006793 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6794 // to further check that this component type is not a primitive type.
6795 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6796 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006797 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006798 break;
6799 }
6800
6801 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006802 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006803 // We cannot directly call the CheckCast runtime entry point
6804 // without resorting to a type checking slow path here (i.e. by
6805 // calling InvokeRuntime directly), as it would require to
6806 // assign fixed registers for the inputs of this HInstanceOf
6807 // instruction (following the runtime calling convention), which
6808 // might be cluttered by the potential first read barrier
6809 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006810
Artem Serovcfbe9132016-10-14 15:58:56 +01006811 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006812 break;
Artem Serov657022c2016-11-23 14:19:38 +00006813
6814 case TypeCheckKind::kInterfaceCheck: {
6815 // Avoid read barriers to improve performance of the fast path. We can not get false
6816 // positives by doing this.
6817 // /* HeapReference<Class> */ temp = obj->klass_
6818 GenerateReferenceLoadTwoRegisters(instruction,
6819 temp_loc,
6820 obj_loc,
6821 class_offset,
6822 maybe_temp2_loc,
6823 kWithoutReadBarrier);
6824
6825 // /* HeapReference<Class> */ temp = temp->iftable_
6826 GenerateReferenceLoadTwoRegisters(instruction,
6827 temp_loc,
6828 temp_loc,
6829 iftable_offset,
6830 maybe_temp2_loc,
6831 kWithoutReadBarrier);
6832 // Iftable is never null.
6833 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6834 // Loop through the iftable and check if any class matches.
6835 vixl32::Label start_loop;
6836 __ Bind(&start_loop);
6837 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6838 type_check_slow_path->GetEntryLabel());
6839 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6840 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6841 // Go to next interface.
6842 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6843 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6844 // Compare the classes and continue the loop if they do not match.
6845 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00006846 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00006847 break;
6848 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006849 }
6850 __ Bind(&done);
6851
6852 __ Bind(type_check_slow_path->GetExitLabel());
6853}
6854
Artem Serov551b28f2016-10-18 19:11:30 +01006855void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6856 LocationSummary* locations =
6857 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6858 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6859 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6860}
6861
6862void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6863 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6864 instruction,
6865 instruction->GetDexPc());
6866 if (instruction->IsEnter()) {
6867 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6868 } else {
6869 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6870 }
6871}
6872
Artem Serov02109dd2016-09-23 17:17:54 +01006873void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6874 HandleBitwiseOperation(instruction, AND);
6875}
6876
6877void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6878 HandleBitwiseOperation(instruction, ORR);
6879}
6880
6881void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6882 HandleBitwiseOperation(instruction, EOR);
6883}
6884
6885void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6886 LocationSummary* locations =
6887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6888 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6889 || instruction->GetResultType() == Primitive::kPrimLong);
6890 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6891 locations->SetInAt(0, Location::RequiresRegister());
6892 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6894}
6895
6896void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6897 HandleBitwiseOperation(instruction);
6898}
6899
6900void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6901 HandleBitwiseOperation(instruction);
6902}
6903
6904void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6905 HandleBitwiseOperation(instruction);
6906}
6907
Artem Serov2bbc9532016-10-21 11:51:50 +01006908void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6909 LocationSummary* locations =
6910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6911 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6912 || instruction->GetResultType() == Primitive::kPrimLong);
6913
6914 locations->SetInAt(0, Location::RequiresRegister());
6915 locations->SetInAt(1, Location::RequiresRegister());
6916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6917}
6918
6919void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6920 LocationSummary* locations = instruction->GetLocations();
6921 Location first = locations->InAt(0);
6922 Location second = locations->InAt(1);
6923 Location out = locations->Out();
6924
6925 if (instruction->GetResultType() == Primitive::kPrimInt) {
6926 vixl32::Register first_reg = RegisterFrom(first);
6927 vixl32::Register second_reg = RegisterFrom(second);
6928 vixl32::Register out_reg = RegisterFrom(out);
6929
6930 switch (instruction->GetOpKind()) {
6931 case HInstruction::kAnd:
6932 __ Bic(out_reg, first_reg, second_reg);
6933 break;
6934 case HInstruction::kOr:
6935 __ Orn(out_reg, first_reg, second_reg);
6936 break;
6937 // There is no EON on arm.
6938 case HInstruction::kXor:
6939 default:
6940 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6941 UNREACHABLE();
6942 }
6943 return;
6944
6945 } else {
6946 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6947 vixl32::Register first_low = LowRegisterFrom(first);
6948 vixl32::Register first_high = HighRegisterFrom(first);
6949 vixl32::Register second_low = LowRegisterFrom(second);
6950 vixl32::Register second_high = HighRegisterFrom(second);
6951 vixl32::Register out_low = LowRegisterFrom(out);
6952 vixl32::Register out_high = HighRegisterFrom(out);
6953
6954 switch (instruction->GetOpKind()) {
6955 case HInstruction::kAnd:
6956 __ Bic(out_low, first_low, second_low);
6957 __ Bic(out_high, first_high, second_high);
6958 break;
6959 case HInstruction::kOr:
6960 __ Orn(out_low, first_low, second_low);
6961 __ Orn(out_high, first_high, second_high);
6962 break;
6963 // There is no EON on arm.
6964 case HInstruction::kXor:
6965 default:
6966 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6967 UNREACHABLE();
6968 }
6969 }
6970}
6971
Anton Kirilov74234da2017-01-13 14:42:47 +00006972void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
6973 HDataProcWithShifterOp* instruction) {
6974 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
6975 instruction->GetType() == Primitive::kPrimLong);
6976 LocationSummary* locations =
6977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6978 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
6979 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
6980
6981 locations->SetInAt(0, Location::RequiresRegister());
6982 locations->SetInAt(1, Location::RequiresRegister());
6983 locations->SetOut(Location::RequiresRegister(),
6984 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6985}
6986
6987void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
6988 HDataProcWithShifterOp* instruction) {
6989 const LocationSummary* const locations = instruction->GetLocations();
6990 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
6991 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
6992
6993 if (instruction->GetType() == Primitive::kPrimInt) {
6994 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
6995
6996 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
6997 ? LowRegisterFrom(locations->InAt(1))
6998 : InputRegisterAt(instruction, 1);
6999
7000 GenerateDataProcInstruction(kind,
7001 OutputRegister(instruction),
7002 InputRegisterAt(instruction, 0),
7003 Operand(second,
7004 ShiftFromOpKind(op_kind),
7005 instruction->GetShiftAmount()),
7006 codegen_);
7007 } else {
7008 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7009
7010 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7011 const vixl32::Register second = InputRegisterAt(instruction, 1);
7012
7013 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7014 GenerateDataProc(kind,
7015 locations->Out(),
7016 locations->InAt(0),
7017 second,
7018 Operand(second, ShiftType::ASR, 31),
7019 codegen_);
7020 } else {
7021 GenerateLongDataProc(instruction, codegen_);
7022 }
7023 }
7024}
7025
Artem Serov02109dd2016-09-23 17:17:54 +01007026// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7027void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7028 vixl32::Register first,
7029 uint32_t value) {
7030 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7031 if (value == 0xffffffffu) {
7032 if (!out.Is(first)) {
7033 __ Mov(out, first);
7034 }
7035 return;
7036 }
7037 if (value == 0u) {
7038 __ Mov(out, 0);
7039 return;
7040 }
7041 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
7042 __ And(out, first, value);
7043 } else {
7044 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
7045 __ Bic(out, first, ~value);
7046 }
7047}
7048
7049// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7050void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7051 vixl32::Register first,
7052 uint32_t value) {
7053 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7054 if (value == 0u) {
7055 if (!out.Is(first)) {
7056 __ Mov(out, first);
7057 }
7058 return;
7059 }
7060 if (value == 0xffffffffu) {
7061 __ Mvn(out, 0);
7062 return;
7063 }
7064 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7065 __ Orr(out, first, value);
7066 } else {
7067 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7068 __ Orn(out, first, ~value);
7069 }
7070}
7071
7072// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7073void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7074 vixl32::Register first,
7075 uint32_t value) {
7076 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7077 if (value == 0u) {
7078 if (!out.Is(first)) {
7079 __ Mov(out, first);
7080 }
7081 return;
7082 }
7083 __ Eor(out, first, value);
7084}
7085
Anton Kirilovdda43962016-11-21 19:55:20 +00007086void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7087 Location first,
7088 uint64_t value) {
7089 vixl32::Register out_low = LowRegisterFrom(out);
7090 vixl32::Register out_high = HighRegisterFrom(out);
7091 vixl32::Register first_low = LowRegisterFrom(first);
7092 vixl32::Register first_high = HighRegisterFrom(first);
7093 uint32_t value_low = Low32Bits(value);
7094 uint32_t value_high = High32Bits(value);
7095 if (value_low == 0u) {
7096 if (!out_low.Is(first_low)) {
7097 __ Mov(out_low, first_low);
7098 }
7099 __ Add(out_high, first_high, value_high);
7100 return;
7101 }
7102 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007103 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007104 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007105 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007106 __ Sbc(out_high, first_high, ~value_high);
7107 } else {
7108 LOG(FATAL) << "Unexpected constant " << value_high;
7109 UNREACHABLE();
7110 }
7111}
7112
Artem Serov02109dd2016-09-23 17:17:54 +01007113void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7114 LocationSummary* locations = instruction->GetLocations();
7115 Location first = locations->InAt(0);
7116 Location second = locations->InAt(1);
7117 Location out = locations->Out();
7118
7119 if (second.IsConstant()) {
7120 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7121 uint32_t value_low = Low32Bits(value);
7122 if (instruction->GetResultType() == Primitive::kPrimInt) {
7123 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7124 vixl32::Register out_reg = OutputRegister(instruction);
7125 if (instruction->IsAnd()) {
7126 GenerateAndConst(out_reg, first_reg, value_low);
7127 } else if (instruction->IsOr()) {
7128 GenerateOrrConst(out_reg, first_reg, value_low);
7129 } else {
7130 DCHECK(instruction->IsXor());
7131 GenerateEorConst(out_reg, first_reg, value_low);
7132 }
7133 } else {
7134 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7135 uint32_t value_high = High32Bits(value);
7136 vixl32::Register first_low = LowRegisterFrom(first);
7137 vixl32::Register first_high = HighRegisterFrom(first);
7138 vixl32::Register out_low = LowRegisterFrom(out);
7139 vixl32::Register out_high = HighRegisterFrom(out);
7140 if (instruction->IsAnd()) {
7141 GenerateAndConst(out_low, first_low, value_low);
7142 GenerateAndConst(out_high, first_high, value_high);
7143 } else if (instruction->IsOr()) {
7144 GenerateOrrConst(out_low, first_low, value_low);
7145 GenerateOrrConst(out_high, first_high, value_high);
7146 } else {
7147 DCHECK(instruction->IsXor());
7148 GenerateEorConst(out_low, first_low, value_low);
7149 GenerateEorConst(out_high, first_high, value_high);
7150 }
7151 }
7152 return;
7153 }
7154
7155 if (instruction->GetResultType() == Primitive::kPrimInt) {
7156 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7157 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7158 vixl32::Register out_reg = OutputRegister(instruction);
7159 if (instruction->IsAnd()) {
7160 __ And(out_reg, first_reg, second_reg);
7161 } else if (instruction->IsOr()) {
7162 __ Orr(out_reg, first_reg, second_reg);
7163 } else {
7164 DCHECK(instruction->IsXor());
7165 __ Eor(out_reg, first_reg, second_reg);
7166 }
7167 } else {
7168 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7169 vixl32::Register first_low = LowRegisterFrom(first);
7170 vixl32::Register first_high = HighRegisterFrom(first);
7171 vixl32::Register second_low = LowRegisterFrom(second);
7172 vixl32::Register second_high = HighRegisterFrom(second);
7173 vixl32::Register out_low = LowRegisterFrom(out);
7174 vixl32::Register out_high = HighRegisterFrom(out);
7175 if (instruction->IsAnd()) {
7176 __ And(out_low, first_low, second_low);
7177 __ And(out_high, first_high, second_high);
7178 } else if (instruction->IsOr()) {
7179 __ Orr(out_low, first_low, second_low);
7180 __ Orr(out_high, first_high, second_high);
7181 } else {
7182 DCHECK(instruction->IsXor());
7183 __ Eor(out_low, first_low, second_low);
7184 __ Eor(out_high, first_high, second_high);
7185 }
7186 }
7187}
7188
Artem Serovcfbe9132016-10-14 15:58:56 +01007189void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007190 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007191 Location out,
7192 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007193 Location maybe_temp,
7194 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007195 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007196 if (read_barrier_option == kWithReadBarrier) {
7197 CHECK(kEmitCompilerReadBarrier);
7198 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7199 if (kUseBakerReadBarrier) {
7200 // Load with fast path based Baker's read barrier.
7201 // /* HeapReference<Object> */ out = *(out + offset)
7202 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7203 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7204 } else {
7205 // Load with slow path based read barrier.
7206 // Save the value of `out` into `maybe_temp` before overwriting it
7207 // in the following move operation, as we will need it for the
7208 // read barrier below.
7209 __ Mov(RegisterFrom(maybe_temp), out_reg);
7210 // /* HeapReference<Object> */ out = *(out + offset)
7211 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7212 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7213 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007214 } else {
7215 // Plain load with no read barrier.
7216 // /* HeapReference<Object> */ out = *(out + offset)
7217 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7218 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7219 }
7220}
7221
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007222void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007223 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007224 Location out,
7225 Location obj,
7226 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007227 Location maybe_temp,
7228 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007229 vixl32::Register out_reg = RegisterFrom(out);
7230 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007231 if (read_barrier_option == kWithReadBarrier) {
7232 CHECK(kEmitCompilerReadBarrier);
7233 if (kUseBakerReadBarrier) {
7234 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7235 // Load with fast path based Baker's read barrier.
7236 // /* HeapReference<Object> */ out = *(obj + offset)
7237 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7238 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7239 } else {
7240 // Load with slow path based read barrier.
7241 // /* HeapReference<Object> */ out = *(obj + offset)
7242 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7243 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7244 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007245 } else {
7246 // Plain load with no read barrier.
7247 // /* HeapReference<Object> */ out = *(obj + offset)
7248 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7249 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7250 }
7251}
7252
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007253void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007254 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007255 Location root,
7256 vixl32::Register obj,
7257 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007258 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007259 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007260 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007261 DCHECK(kEmitCompilerReadBarrier);
7262 if (kUseBakerReadBarrier) {
7263 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillain35345a52017-02-27 14:32:08 +00007264 // Baker's read barrier are used:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007265 //
Roland Levillain35345a52017-02-27 14:32:08 +00007266 // root = obj.field;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007267 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain35345a52017-02-27 14:32:08 +00007268 // if (temp != null) {
7269 // root = temp(root)
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007270 // }
7271
7272 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7273 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7274 static_assert(
7275 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7276 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7277 "have different sizes.");
7278 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7279 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7280 "have different sizes.");
7281
Roland Levillain35345a52017-02-27 14:32:08 +00007282 // Slow path marking the GC root `root`.
7283 Location temp = LocationFrom(lr);
7284 SlowPathCodeARMVIXL* slow_path =
7285 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
7286 instruction,
7287 root,
7288 /*entrypoint*/ temp);
7289 codegen_->AddSlowPath(slow_path);
7290
7291 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7292 const int32_t entry_point_offset =
7293 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7294 // Loading the entrypoint does not require a load acquire since it is only changed when
7295 // threads are suspended or running a checkpoint.
7296 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007297 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7298 // checking GetIsGcMarking.
7299 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7300 __ Bind(slow_path->GetExitLabel());
7301 } else {
7302 // GC root loaded through a slow path for read barriers other
7303 // than Baker's.
7304 // /* GcRoot<mirror::Object>* */ root = obj + offset
7305 __ Add(root_reg, obj, offset);
7306 // /* mirror::Object* */ root = root->Read()
7307 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7308 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007309 } else {
7310 // Plain GC root load with no read barrier.
7311 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7312 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7313 // Note that GC roots are not affected by heap poisoning, thus we
7314 // do not have to unpoison `root_reg` here.
7315 }
7316}
7317
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007318void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7319 Location ref,
7320 vixl32::Register obj,
7321 uint32_t offset,
7322 Location temp,
7323 bool needs_null_check) {
7324 DCHECK(kEmitCompilerReadBarrier);
7325 DCHECK(kUseBakerReadBarrier);
7326
7327 // /* HeapReference<Object> */ ref = *(obj + offset)
7328 Location no_index = Location::NoLocation();
7329 ScaleFactor no_scale_factor = TIMES_1;
7330 GenerateReferenceLoadWithBakerReadBarrier(
7331 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007332}
7333
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007334void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7335 Location ref,
7336 vixl32::Register obj,
7337 uint32_t data_offset,
7338 Location index,
7339 Location temp,
7340 bool needs_null_check) {
7341 DCHECK(kEmitCompilerReadBarrier);
7342 DCHECK(kUseBakerReadBarrier);
7343
7344 static_assert(
7345 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7346 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7347 // /* HeapReference<Object> */ ref =
7348 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7349 ScaleFactor scale_factor = TIMES_4;
7350 GenerateReferenceLoadWithBakerReadBarrier(
7351 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007352}
7353
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007354void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7355 Location ref,
7356 vixl32::Register obj,
7357 uint32_t offset,
7358 Location index,
7359 ScaleFactor scale_factor,
7360 Location temp,
7361 bool needs_null_check,
7362 bool always_update_field,
7363 vixl32::Register* temp2) {
7364 DCHECK(kEmitCompilerReadBarrier);
7365 DCHECK(kUseBakerReadBarrier);
7366
Roland Levillain35345a52017-02-27 14:32:08 +00007367 // In slow path based read barriers, the read barrier call is
7368 // inserted after the original load. However, in fast path based
7369 // Baker's read barriers, we need to perform the load of
7370 // mirror::Object::monitor_ *before* the original reference load.
7371 // This load-load ordering is required by the read barrier.
7372 // The fast path/slow path (for Baker's algorithm) should look like:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007373 //
Roland Levillain35345a52017-02-27 14:32:08 +00007374 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7375 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7376 // HeapReference<Object> ref = *src; // Original reference load.
7377 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7378 // if (is_gray) {
7379 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007380 // }
Roland Levillain35345a52017-02-27 14:32:08 +00007381 //
7382 // Note: the original implementation in ReadBarrier::Barrier is
7383 // slightly more complex as it performs additional checks that we do
7384 // not do here for performance reasons.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007385
Roland Levillain35345a52017-02-27 14:32:08 +00007386 vixl32::Register ref_reg = RegisterFrom(ref);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007387 vixl32::Register temp_reg = RegisterFrom(temp);
Roland Levillain35345a52017-02-27 14:32:08 +00007388 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007389
Roland Levillain35345a52017-02-27 14:32:08 +00007390 // /* int32_t */ monitor = obj->monitor_
7391 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7392 if (needs_null_check) {
7393 MaybeRecordImplicitNullCheck(instruction);
7394 }
7395 // /* LockWord */ lock_word = LockWord(monitor)
7396 static_assert(sizeof(LockWord) == sizeof(int32_t),
7397 "art::LockWord and int32_t have different sizes.");
7398
7399 // Introduce a dependency on the lock_word including the rb_state,
7400 // which shall prevent load-load reordering without using
7401 // a memory barrier (which would be more expensive).
7402 // `obj` is unchanged by this operation, but its value now depends
7403 // on `temp_reg`.
7404 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7405
7406 // The actual reference load.
7407 if (index.IsValid()) {
7408 // Load types involving an "index": ArrayGet,
7409 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7410 // intrinsics.
7411 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7412 if (index.IsConstant()) {
7413 size_t computed_offset =
7414 (Int32ConstantFrom(index) << scale_factor) + offset;
7415 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7416 } else {
7417 // Handle the special case of the
7418 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7419 // intrinsics, which use a register pair as index ("long
7420 // offset"), of which only the low part contains data.
7421 vixl32::Register index_reg = index.IsRegisterPair()
7422 ? LowRegisterFrom(index)
7423 : RegisterFrom(index);
7424 UseScratchRegisterScope temps(GetVIXLAssembler());
7425 const vixl32::Register temp3 = temps.Acquire();
7426 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7427 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7428 }
7429 } else {
7430 // /* HeapReference<Object> */ ref = *(obj + offset)
7431 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7432 }
7433
7434 // Object* ref = ref_addr->AsMirrorPtr()
7435 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7436
7437 // Slow path marking the object `ref` when it is gray.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007438 SlowPathCodeARMVIXL* slow_path;
7439 if (always_update_field) {
7440 DCHECK(temp2 != nullptr);
Roland Levillain47b3ab22017-02-27 14:31:35 +00007441 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7442 // of the form `obj + field_offset`, where `obj` is a register and
7443 // `field_offset` is a register pair (of which only the lower half
7444 // is used). Thus `offset` and `scale_factor` above are expected
7445 // to be null in this code path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007446 DCHECK_EQ(offset, 0u);
7447 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain47b3ab22017-02-27 14:31:35 +00007448 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
Roland Levillain35345a52017-02-27 14:32:08 +00007449 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007450 } else {
Roland Levillain35345a52017-02-27 14:32:08 +00007451 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007452 }
7453 AddSlowPath(slow_path);
7454
Roland Levillain35345a52017-02-27 14:32:08 +00007455 // if (rb_state == ReadBarrier::GrayState())
7456 // ref = ReadBarrier::Mark(ref);
7457 // Given the numeric representation, it's enough to check the low bit of the
7458 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7459 // which can be a 16-bit instruction unlike the TST immediate.
7460 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7461 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7462 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7463 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007464 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007465}
7466
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007467void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7468 Location out,
7469 Location ref,
7470 Location obj,
7471 uint32_t offset,
7472 Location index) {
7473 DCHECK(kEmitCompilerReadBarrier);
7474
7475 // Insert a slow path based read barrier *after* the reference load.
7476 //
7477 // If heap poisoning is enabled, the unpoisoning of the loaded
7478 // reference will be carried out by the runtime within the slow
7479 // path.
7480 //
7481 // Note that `ref` currently does not get unpoisoned (when heap
7482 // poisoning is enabled), which is alright as the `ref` argument is
7483 // not used by the artReadBarrierSlow entry point.
7484 //
7485 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7486 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7487 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7488 AddSlowPath(slow_path);
7489
7490 __ B(slow_path->GetEntryLabel());
7491 __ Bind(slow_path->GetExitLabel());
7492}
7493
7494void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007495 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007496 Location ref,
7497 Location obj,
7498 uint32_t offset,
7499 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007500 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007501 // Baker's read barriers shall be handled by the fast path
7502 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007503 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007504 // If heap poisoning is enabled, unpoisoning will be taken care of
7505 // by the runtime within the slow path.
7506 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007507 } else if (kPoisonHeapReferences) {
7508 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7509 }
7510}
7511
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007512void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7513 Location out,
7514 Location root) {
7515 DCHECK(kEmitCompilerReadBarrier);
7516
7517 // Insert a slow path based read barrier *after* the GC root load.
7518 //
7519 // Note that GC roots are not affected by heap poisoning, so we do
7520 // not need to do anything special for this here.
7521 SlowPathCodeARMVIXL* slow_path =
7522 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7523 AddSlowPath(slow_path);
7524
7525 __ B(slow_path->GetEntryLabel());
7526 __ Bind(slow_path->GetExitLabel());
7527}
7528
Artem Serov02d37832016-10-25 15:25:33 +01007529// Check if the desired_dispatch_info is supported. If it is, return it,
7530// otherwise return a fall-back info that should be used instead.
7531HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007532 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007533 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007534 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007535}
7536
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007537vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7538 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7539 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7540 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7541 if (!invoke->GetLocations()->Intrinsified()) {
7542 return RegisterFrom(location);
7543 }
7544 // For intrinsics we allow any location, so it may be on the stack.
7545 if (!location.IsRegister()) {
7546 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7547 return temp;
7548 }
7549 // For register locations, check if the register was saved. If so, get it from the stack.
7550 // Note: There is a chance that the register was saved but not overwritten, so we could
7551 // save one load. However, since this is just an intrinsic slow path we prefer this
7552 // simple and more robust approach rather that trying to determine if that's the case.
7553 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00007554 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007555 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7556 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7557 return temp;
7558 }
7559 return RegisterFrom(location);
7560}
7561
TatWai Chongd8c052a2016-11-02 16:12:48 +08007562Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007563 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007564 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007565 switch (invoke->GetMethodLoadKind()) {
7566 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7567 uint32_t offset =
7568 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7569 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007570 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7571 break;
7572 }
7573 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7574 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7575 break;
7576 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7577 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7578 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007579 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7580 HArmDexCacheArraysBase* base =
7581 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7582 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7583 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7584 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007585 break;
7586 }
7587 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7588 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7589 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007590 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007591 if (current_method.IsRegister()) {
7592 method_reg = RegisterFrom(current_method);
7593 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007594 DCHECK(invoke->GetLocations()->Intrinsified());
7595 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007596 method_reg = reg;
7597 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007598 }
7599 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7600 GetAssembler()->LoadFromOffset(
7601 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007602 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007603 method_reg,
7604 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7605 // temp = temp[index_in_cache];
7606 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7607 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7608 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007609 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007610 break;
7611 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007612 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007613 return callee_method;
7614}
7615
7616void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7617 Location temp) {
7618 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007619
Artem Serovd4cc5b22016-11-04 11:19:09 +00007620 switch (invoke->GetCodePtrLocation()) {
7621 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7622 __ Bl(GetFrameEntryLabel());
7623 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007624 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7625 // LR = callee_method->entry_point_from_quick_compiled_code_
7626 GetAssembler()->LoadFromOffset(
7627 kLoadWord,
7628 lr,
7629 RegisterFrom(callee_method),
7630 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007631 {
7632 // 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 +00007633 ExactAssemblyScope aas(GetVIXLAssembler(),
7634 vixl32::k16BitT32InstructionSizeInBytes,
7635 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007636 // LR()
7637 __ blx(lr);
7638 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007639 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007640 }
7641
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007642 DCHECK(!IsLeafMethod());
7643}
7644
7645void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7646 vixl32::Register temp = RegisterFrom(temp_location);
7647 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7648 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7649
7650 // Use the calling convention instead of the location of the receiver, as
7651 // intrinsics may have put the receiver in a different register. In the intrinsics
7652 // slow path, the arguments have been moved to the right place, so here we are
7653 // guaranteed that the receiver is the first register of the calling convention.
7654 InvokeDexCallingConventionARMVIXL calling_convention;
7655 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7656 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007657 {
7658 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007659 ExactAssemblyScope aas(GetVIXLAssembler(),
7660 vixl32::kMaxInstructionSizeInBytes,
7661 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007662 // /* HeapReference<Class> */ temp = receiver->klass_
7663 __ ldr(temp, MemOperand(receiver, class_offset));
7664 MaybeRecordImplicitNullCheck(invoke);
7665 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007666 // Instead of simply (possibly) unpoisoning `temp` here, we should
7667 // emit a read barrier for the previous class reference load.
7668 // However this is not required in practice, as this is an
7669 // intermediate/temporary reference and because the current
7670 // concurrent copying collector keeps the from-space memory
7671 // intact/accessible until the end of the marking phase (the
7672 // concurrent copying collector may not in the future).
7673 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7674
7675 // temp = temp->GetMethodAt(method_offset);
7676 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7677 kArmPointerSize).Int32Value();
7678 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7679 // LR = temp->GetEntryPoint();
7680 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7681 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007682 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7683 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7684 // that.
7685 // 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 +00007686 ExactAssemblyScope aas(GetVIXLAssembler(),
7687 vixl32::k16BitT32InstructionSizeInBytes,
7688 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007689 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007690}
7691
Artem Serovd4cc5b22016-11-04 11:19:09 +00007692CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007693 const DexFile& dex_file, dex::StringIndex string_index) {
7694 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007695}
7696
7697CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7698 const DexFile& dex_file, dex::TypeIndex type_index) {
7699 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7700}
7701
Vladimir Marko1998cd02017-01-13 13:02:58 +00007702CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
7703 const DexFile& dex_file, dex::TypeIndex type_index) {
7704 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7705}
7706
Artem Serovd4cc5b22016-11-04 11:19:09 +00007707CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7708 const DexFile& dex_file, uint32_t element_offset) {
7709 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7710}
7711
7712CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7713 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7714 patches->emplace_back(dex_file, offset_or_index);
7715 return &patches->back();
7716}
7717
Artem Serovc5fcb442016-12-02 19:19:58 +00007718VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7719 const DexFile& dex_file,
7720 dex::StringIndex string_index) {
7721 return boot_image_string_patches_.GetOrCreate(
7722 StringReference(&dex_file, string_index),
7723 [this]() {
7724 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7725 });
7726}
7727
7728VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7729 const DexFile& dex_file,
7730 dex::TypeIndex type_index) {
7731 return boot_image_type_patches_.GetOrCreate(
7732 TypeReference(&dex_file, type_index),
7733 [this]() {
7734 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7735 });
7736}
7737
7738VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00007739 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00007740}
7741
7742VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7743 return DeduplicateUint32Literal(address, &uint32_literals_);
7744}
7745
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007746VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
7747 const DexFile& dex_file,
7748 dex::StringIndex string_index,
7749 Handle<mirror::String> handle) {
7750 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7751 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007752 return jit_string_patches_.GetOrCreate(
7753 StringReference(&dex_file, string_index),
7754 [this]() {
7755 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7756 });
7757}
7758
7759VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7760 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007761 Handle<mirror::Class> handle) {
7762 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7763 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007764 return jit_class_patches_.GetOrCreate(
7765 TypeReference(&dex_file, type_index),
7766 [this]() {
7767 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7768 });
7769}
7770
Artem Serovd4cc5b22016-11-04 11:19:09 +00007771template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7772inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7773 const ArenaDeque<PcRelativePatchInfo>& infos,
7774 ArenaVector<LinkerPatch>* linker_patches) {
7775 for (const PcRelativePatchInfo& info : infos) {
7776 const DexFile& dex_file = info.target_dex_file;
7777 size_t offset_or_index = info.offset_or_index;
7778 DCHECK(info.add_pc_label.IsBound());
7779 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7780 // Add MOVW patch.
7781 DCHECK(info.movw_label.IsBound());
7782 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7783 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7784 // Add MOVT patch.
7785 DCHECK(info.movt_label.IsBound());
7786 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7787 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7788 }
7789}
7790
7791void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7792 DCHECK(linker_patches->empty());
7793 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00007794 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007795 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007796 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007797 boot_image_type_patches_.size() +
7798 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00007799 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007800 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007801 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7802 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007803 for (const auto& entry : boot_image_string_patches_) {
7804 const StringReference& target_string = entry.first;
7805 VIXLUInt32Literal* literal = entry.second;
7806 DCHECK(literal->IsBound());
7807 uint32_t literal_offset = literal->GetLocation();
7808 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7809 target_string.dex_file,
7810 target_string.string_index.index_));
7811 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007812 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007813 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007814 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7815 linker_patches);
7816 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007817 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7818 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007819 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7820 linker_patches);
7821 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007822 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7823 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007824 for (const auto& entry : boot_image_type_patches_) {
7825 const TypeReference& target_type = entry.first;
7826 VIXLUInt32Literal* literal = entry.second;
7827 DCHECK(literal->IsBound());
7828 uint32_t literal_offset = literal->GetLocation();
7829 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7830 target_type.dex_file,
7831 target_type.type_index.index_));
7832 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007833 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00007834}
7835
7836VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7837 uint32_t value,
7838 Uint32ToLiteralMap* map) {
7839 return map->GetOrCreate(
7840 value,
7841 [this, value]() {
7842 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7843 });
7844}
7845
7846VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7847 MethodReference target_method,
7848 MethodToLiteralMap* map) {
7849 return map->GetOrCreate(
7850 target_method,
7851 [this]() {
7852 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7853 });
7854}
7855
Artem Serov2bbc9532016-10-21 11:51:50 +01007856void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7857 LocationSummary* locations =
7858 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7859 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7860 Location::RequiresRegister());
7861 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7862 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7863 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7864}
7865
7866void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7867 vixl32::Register res = OutputRegister(instr);
7868 vixl32::Register accumulator =
7869 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7870 vixl32::Register mul_left =
7871 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7872 vixl32::Register mul_right =
7873 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7874
7875 if (instr->GetOpKind() == HInstruction::kAdd) {
7876 __ Mla(res, mul_left, mul_right, accumulator);
7877 } else {
7878 __ Mls(res, mul_left, mul_right, accumulator);
7879 }
7880}
7881
Artem Serov551b28f2016-10-18 19:11:30 +01007882void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7883 // Nothing to do, this should be removed during prepare for register allocator.
7884 LOG(FATAL) << "Unreachable";
7885}
7886
7887void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7888 // Nothing to do, this should be removed during prepare for register allocator.
7889 LOG(FATAL) << "Unreachable";
7890}
7891
7892// Simple implementation of packed switch - generate cascaded compare/jumps.
7893void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7894 LocationSummary* locations =
7895 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7896 locations->SetInAt(0, Location::RequiresRegister());
7897 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7898 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7899 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7900 if (switch_instr->GetStartValue() != 0) {
7901 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7902 }
7903 }
7904}
7905
7906// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7907void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7908 int32_t lower_bound = switch_instr->GetStartValue();
7909 uint32_t num_entries = switch_instr->GetNumEntries();
7910 LocationSummary* locations = switch_instr->GetLocations();
7911 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7912 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7913
7914 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7915 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7916 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007917 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007918 vixl32::Register temp_reg = temps.Acquire();
7919 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7920 // the immediate, because IP is used as the destination register. For the other
7921 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7922 // and they can be encoded in the instruction without making use of IP register.
7923 __ Adds(temp_reg, value_reg, -lower_bound);
7924
7925 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7926 // Jump to successors[0] if value == lower_bound.
7927 __ B(eq, codegen_->GetLabelOf(successors[0]));
7928 int32_t last_index = 0;
7929 for (; num_entries - last_index > 2; last_index += 2) {
7930 __ Adds(temp_reg, temp_reg, -2);
7931 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7932 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7933 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7934 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7935 }
7936 if (num_entries - last_index == 2) {
7937 // The last missing case_value.
7938 __ Cmp(temp_reg, 1);
7939 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7940 }
7941
7942 // And the default for any other value.
7943 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7944 __ B(codegen_->GetLabelOf(default_block));
7945 }
7946 } else {
7947 // Create a table lookup.
7948 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7949
7950 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7951
7952 // Remove the bias.
7953 vixl32::Register key_reg;
7954 if (lower_bound != 0) {
7955 key_reg = RegisterFrom(locations->GetTemp(1));
7956 __ Sub(key_reg, value_reg, lower_bound);
7957 } else {
7958 key_reg = value_reg;
7959 }
7960
7961 // Check whether the value is in the table, jump to default block if not.
7962 __ Cmp(key_reg, num_entries - 1);
7963 __ B(hi, codegen_->GetLabelOf(default_block));
7964
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007965 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007966 vixl32::Register jump_offset = temps.Acquire();
7967
7968 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00007969 {
7970 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
7971 ExactAssemblyScope aas(GetVIXLAssembler(),
7972 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
7973 CodeBufferCheckScope::kMaximumSize);
7974 __ adr(table_base, jump_table->GetTableStartLabel());
7975 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01007976
Scott Wakeling86e9d262017-01-18 15:59:24 +00007977 // Jump to target block by branching to table_base(pc related) + offset.
7978 vixl32::Register target_address = table_base;
7979 __ add(target_address, table_base, jump_offset);
7980 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007981
Scott Wakeling86e9d262017-01-18 15:59:24 +00007982 jump_table->EmitTable(codegen_);
7983 }
Artem Serov551b28f2016-10-18 19:11:30 +01007984 }
7985}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007986void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7988 locations->SetOut(Location::RequiresRegister());
7989}
7990
7991void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7992 vixl32::Register base_reg = OutputRegister(base);
7993 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7994 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7995 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
7996}
Artem Serov551b28f2016-10-18 19:11:30 +01007997
Artem Serov02d37832016-10-25 15:25:33 +01007998// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007999void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8000 if (!trg.IsValid()) {
8001 DCHECK_EQ(type, Primitive::kPrimVoid);
8002 return;
8003 }
8004
8005 DCHECK_NE(type, Primitive::kPrimVoid);
8006
Artem Serovd4cc5b22016-11-04 11:19:09 +00008007 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008008 if (return_loc.Equals(trg)) {
8009 return;
8010 }
8011
8012 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8013 // with the last branch.
8014 if (type == Primitive::kPrimLong) {
8015 TODO_VIXL32(FATAL);
8016 } else if (type == Primitive::kPrimDouble) {
8017 TODO_VIXL32(FATAL);
8018 } else {
8019 // Let the parallel move resolver take care of all of this.
8020 HParallelMove parallel_move(GetGraph()->GetArena());
8021 parallel_move.AddMove(return_loc, trg, type, nullptr);
8022 GetMoveResolver()->EmitNativeCode(&parallel_move);
8023 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008024}
Scott Wakelingfe885462016-09-22 10:24:38 +01008025
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008026void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8027 LocationSummary* locations =
8028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8029 locations->SetInAt(0, Location::RequiresRegister());
8030 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008031}
8032
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008033void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8034 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8035 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8036 instruction->GetIndex(), kArmPointerSize).SizeValue();
8037 GetAssembler()->LoadFromOffset(kLoadWord,
8038 OutputRegister(instruction),
8039 InputRegisterAt(instruction, 0),
8040 method_offset);
8041 } else {
8042 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8043 instruction->GetIndex(), kArmPointerSize));
8044 GetAssembler()->LoadFromOffset(kLoadWord,
8045 OutputRegister(instruction),
8046 InputRegisterAt(instruction, 0),
8047 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8048 GetAssembler()->LoadFromOffset(kLoadWord,
8049 OutputRegister(instruction),
8050 OutputRegister(instruction),
8051 method_offset);
8052 }
Artem Serov551b28f2016-10-18 19:11:30 +01008053}
8054
Artem Serovc5fcb442016-12-02 19:19:58 +00008055static void PatchJitRootUse(uint8_t* code,
8056 const uint8_t* roots_data,
8057 VIXLUInt32Literal* literal,
8058 uint64_t index_in_table) {
8059 DCHECK(literal->IsBound());
8060 uint32_t literal_offset = literal->GetLocation();
8061 uintptr_t address =
8062 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8063 uint8_t* data = code + literal_offset;
8064 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8065}
8066
8067void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8068 for (const auto& entry : jit_string_patches_) {
8069 const auto& it = jit_string_roots_.find(entry.first);
8070 DCHECK(it != jit_string_roots_.end());
8071 PatchJitRootUse(code, roots_data, entry.second, it->second);
8072 }
8073 for (const auto& entry : jit_class_patches_) {
8074 const auto& it = jit_class_roots_.find(entry.first);
8075 DCHECK(it != jit_class_roots_.end());
8076 PatchJitRootUse(code, roots_data, entry.second, it->second);
8077 }
8078}
8079
Artem Serovd4cc5b22016-11-04 11:19:09 +00008080void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8081 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8082 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008083 ExactAssemblyScope aas(GetVIXLAssembler(),
8084 3 * vixl32::kMaxInstructionSizeInBytes,
8085 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008086 // TODO(VIXL): Think about using mov instead of movw.
8087 __ bind(&labels->movw_label);
8088 __ movw(out, /* placeholder */ 0u);
8089 __ bind(&labels->movt_label);
8090 __ movt(out, /* placeholder */ 0u);
8091 __ bind(&labels->add_pc_label);
8092 __ add(out, out, pc);
8093}
8094
Scott Wakelingfe885462016-09-22 10:24:38 +01008095#undef __
8096#undef QUICK_ENTRY_POINT
8097#undef TODO_VIXL32
8098
8099} // namespace arm
8100} // namespace art