blob: 6bfbe4a9c94d0562e63c4cc521c88c950bf604a6 [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
Anton Kirilovedb2ac32016-11-30 15:14:10 +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).
664//
665// 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`).
671class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
672 public:
673 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
674 Location ref,
675 Location entrypoint = Location::NoLocation())
676 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
677 DCHECK(kEmitCompilerReadBarrier);
678 }
679
680 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
681
682 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
683 LocationSummary* locations = instruction_->GetLocations();
684 vixl32::Register ref_reg = RegisterFrom(ref_);
685 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 {
735 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`).
763class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
764 public:
765 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
766 Location ref,
767 vixl32::Register obj,
768 Location field_offset,
769 vixl32::Register temp1,
770 vixl32::Register temp2)
771 : SlowPathCodeARMVIXL(instruction),
772 ref_(ref),
773 obj_(obj),
774 field_offset_(field_offset),
775 temp1_(temp1),
776 temp2_(temp2) {
777 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);
805
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 //
831 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);
835
836 // If the new reference is different from the old reference,
837 // update the field in the holder (`*(obj_ + field_offset_)`).
838 //
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.
844 vixl32::Label done;
845 __ Cmp(temp1_, ref_reg);
Artem Serov517d9f62016-12-12 15:51:15 +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.
860 vixl32::Register offset = LowRegisterFrom(field_offset_);
861 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
916 __ Bind(&done);
917 __ B(GetExitLabel());
918 }
919
920 private:
921 // The location (register) of the marked object reference.
922 const Location ref_;
923 // The register containing the object holding the marked object reference field.
924 const vixl32::Register obj_;
925 // The location of the offset of the marked reference field within `obj_`.
926 Location field_offset_;
927
928 const vixl32::Register temp1_;
929 const vixl32::Register temp2_;
930
931 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
932};
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 boot_image_address_patches_(std::less<uint32_t>(),
1485 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1486 jit_string_patches_(StringReferenceValueComparator(),
1487 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1488 jit_class_patches_(TypeReferenceValueComparator(),
1489 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001490 // Always save the LR register to mimic Quick.
1491 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001492 // Give d14 and d15 as scratch registers to VIXL.
1493 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1494 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1495 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1496 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1497 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1498 // also want to investigate giving those 14 other D registers to the allocator.
1499 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1500 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001501}
1502
Artem Serov551b28f2016-10-18 19:11:30 +01001503void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1504 uint32_t num_entries = switch_instr_->GetNumEntries();
1505 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1506
1507 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001508 // underlying code buffer and we have generated a jump table of the right size, using
1509 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001510 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1511 num_entries * sizeof(int32_t),
1512 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001513 // TODO(VIXL): Check that using lower case bind is fine here.
1514 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001515 for (uint32_t i = 0; i < num_entries; i++) {
1516 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1517 }
1518}
1519
1520void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1521 uint32_t num_entries = switch_instr_->GetNumEntries();
1522 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1523
Artem Serov551b28f2016-10-18 19:11:30 +01001524 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1525 for (uint32_t i = 0; i < num_entries; i++) {
1526 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1527 DCHECK(target_label->IsBound());
1528 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1529 // When doing BX to address we need to have lower bit set to 1 in T32.
1530 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1531 jump_offset++;
1532 }
1533 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1534 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001535
Scott Wakelingb77051e2016-11-21 19:46:00 +00001536 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001537 }
1538}
1539
Artem Serov09a940d2016-11-11 16:15:11 +00001540void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001541 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001542 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001543 }
1544}
1545
Andreas Gampeca620d72016-11-08 08:09:33 -08001546#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001547
1548void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001549 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001550 GetAssembler()->FinalizeCode();
1551 CodeGenerator::Finalize(allocator);
1552}
1553
1554void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001555 // Stack register, LR and PC are always reserved.
1556 blocked_core_registers_[SP] = true;
1557 blocked_core_registers_[LR] = true;
1558 blocked_core_registers_[PC] = true;
1559
1560 // Reserve thread register.
1561 blocked_core_registers_[TR] = true;
1562
1563 // Reserve temp register.
1564 blocked_core_registers_[IP] = true;
1565
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001566 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1567 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1568 blocked_fpu_registers_[28] = true;
1569 blocked_fpu_registers_[29] = true;
1570 blocked_fpu_registers_[30] = true;
1571 blocked_fpu_registers_[31] = true;
1572
Scott Wakelingfe885462016-09-22 10:24:38 +01001573 if (GetGraph()->IsDebuggable()) {
1574 // Stubs do not save callee-save floating point registers. If the graph
1575 // is debuggable, we need to deal with these registers differently. For
1576 // now, just block them.
1577 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1578 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1579 ++i) {
1580 blocked_fpu_registers_[i] = true;
1581 }
1582 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001583}
1584
Scott Wakelingfe885462016-09-22 10:24:38 +01001585InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1586 CodeGeneratorARMVIXL* codegen)
1587 : InstructionCodeGenerator(graph, codegen),
1588 assembler_(codegen->GetAssembler()),
1589 codegen_(codegen) {}
1590
1591void CodeGeneratorARMVIXL::ComputeSpillMask() {
1592 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1593 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1594 // There is no easy instruction to restore just the PC on thumb2. We spill and
1595 // restore another arbitrary register.
1596 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1597 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1598 // We use vpush and vpop for saving and restoring floating point registers, which take
1599 // a SRegister and the number of registers to save/restore after that SRegister. We
1600 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1601 // but in the range.
1602 if (fpu_spill_mask_ != 0) {
1603 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1604 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1605 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1606 fpu_spill_mask_ |= (1 << i);
1607 }
1608 }
1609}
1610
1611void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1612 bool skip_overflow_check =
1613 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1614 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1615 __ Bind(&frame_entry_label_);
1616
1617 if (HasEmptyFrame()) {
1618 return;
1619 }
1620
Scott Wakelingfe885462016-09-22 10:24:38 +01001621 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001622 UseScratchRegisterScope temps(GetVIXLAssembler());
1623 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001624 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001625 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001626 ExactAssemblyScope aas(GetVIXLAssembler(),
1627 vixl32::kMaxInstructionSizeInBytes,
1628 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001629 __ ldr(temp, MemOperand(temp));
1630 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001631 }
1632
1633 __ Push(RegisterList(core_spill_mask_));
1634 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1635 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1636 0,
1637 core_spill_mask_,
1638 kArmWordSize);
1639 if (fpu_spill_mask_ != 0) {
1640 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1641
1642 // Check that list is contiguous.
1643 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1644
1645 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1646 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001647 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001648 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001649
1650 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1651 UseScratchRegisterScope temps(GetVIXLAssembler());
1652 vixl32::Register temp = temps.Acquire();
1653 // Initialize should_deoptimize flag to 0.
1654 __ Mov(temp, 0);
1655 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1656 }
1657
Scott Wakelingfe885462016-09-22 10:24:38 +01001658 int adjust = GetFrameSize() - FrameEntrySpillSize();
1659 __ Sub(sp, sp, adjust);
1660 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001661
1662 // Save the current method if we need it. Note that we do not
1663 // do this in HCurrentMethod, as the instruction might have been removed
1664 // in the SSA graph.
1665 if (RequiresCurrentMethod()) {
1666 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1667 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001668}
1669
1670void CodeGeneratorARMVIXL::GenerateFrameExit() {
1671 if (HasEmptyFrame()) {
1672 __ Bx(lr);
1673 return;
1674 }
1675 GetAssembler()->cfi().RememberState();
1676 int adjust = GetFrameSize() - FrameEntrySpillSize();
1677 __ Add(sp, sp, adjust);
1678 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1679 if (fpu_spill_mask_ != 0) {
1680 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1681
1682 // Check that list is contiguous.
1683 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1684
1685 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1686 GetAssembler()->cfi().AdjustCFAOffset(
1687 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001688 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001689 }
1690 // Pop LR into PC to return.
1691 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1692 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1693 __ Pop(RegisterList(pop_mask));
1694 GetAssembler()->cfi().RestoreState();
1695 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1696}
1697
1698void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1699 __ Bind(GetLabelOf(block));
1700}
1701
Artem Serovd4cc5b22016-11-04 11:19:09 +00001702Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1703 switch (type) {
1704 case Primitive::kPrimBoolean:
1705 case Primitive::kPrimByte:
1706 case Primitive::kPrimChar:
1707 case Primitive::kPrimShort:
1708 case Primitive::kPrimInt:
1709 case Primitive::kPrimNot: {
1710 uint32_t index = gp_index_++;
1711 uint32_t stack_index = stack_index_++;
1712 if (index < calling_convention.GetNumberOfRegisters()) {
1713 return LocationFrom(calling_convention.GetRegisterAt(index));
1714 } else {
1715 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1716 }
1717 }
1718
1719 case Primitive::kPrimLong: {
1720 uint32_t index = gp_index_;
1721 uint32_t stack_index = stack_index_;
1722 gp_index_ += 2;
1723 stack_index_ += 2;
1724 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1725 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1726 // Skip R1, and use R2_R3 instead.
1727 gp_index_++;
1728 index++;
1729 }
1730 }
1731 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1732 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1733 calling_convention.GetRegisterAt(index + 1).GetCode());
1734
1735 return LocationFrom(calling_convention.GetRegisterAt(index),
1736 calling_convention.GetRegisterAt(index + 1));
1737 } else {
1738 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1739 }
1740 }
1741
1742 case Primitive::kPrimFloat: {
1743 uint32_t stack_index = stack_index_++;
1744 if (float_index_ % 2 == 0) {
1745 float_index_ = std::max(double_index_, float_index_);
1746 }
1747 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1748 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1749 } else {
1750 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1751 }
1752 }
1753
1754 case Primitive::kPrimDouble: {
1755 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1756 uint32_t stack_index = stack_index_;
1757 stack_index_ += 2;
1758 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1759 uint32_t index = double_index_;
1760 double_index_ += 2;
1761 Location result = LocationFrom(
1762 calling_convention.GetFpuRegisterAt(index),
1763 calling_convention.GetFpuRegisterAt(index + 1));
1764 DCHECK(ExpectedPairLayout(result));
1765 return result;
1766 } else {
1767 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1768 }
1769 }
1770
1771 case Primitive::kPrimVoid:
1772 LOG(FATAL) << "Unexpected parameter type " << type;
1773 break;
1774 }
1775 return Location::NoLocation();
1776}
1777
1778Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1779 switch (type) {
1780 case Primitive::kPrimBoolean:
1781 case Primitive::kPrimByte:
1782 case Primitive::kPrimChar:
1783 case Primitive::kPrimShort:
1784 case Primitive::kPrimInt:
1785 case Primitive::kPrimNot: {
1786 return LocationFrom(r0);
1787 }
1788
1789 case Primitive::kPrimFloat: {
1790 return LocationFrom(s0);
1791 }
1792
1793 case Primitive::kPrimLong: {
1794 return LocationFrom(r0, r1);
1795 }
1796
1797 case Primitive::kPrimDouble: {
1798 return LocationFrom(s0, s1);
1799 }
1800
1801 case Primitive::kPrimVoid:
1802 return Location::NoLocation();
1803 }
1804
1805 UNREACHABLE();
1806}
1807
1808Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1809 return LocationFrom(kMethodRegister);
1810}
1811
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001812void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1813 if (source.Equals(destination)) {
1814 return;
1815 }
1816 if (destination.IsRegister()) {
1817 if (source.IsRegister()) {
1818 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1819 } else if (source.IsFpuRegister()) {
1820 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1821 } else {
1822 GetAssembler()->LoadFromOffset(kLoadWord,
1823 RegisterFrom(destination),
1824 sp,
1825 source.GetStackIndex());
1826 }
1827 } else if (destination.IsFpuRegister()) {
1828 if (source.IsRegister()) {
1829 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1830 } else if (source.IsFpuRegister()) {
1831 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1832 } else {
1833 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1834 }
1835 } else {
1836 DCHECK(destination.IsStackSlot()) << destination;
1837 if (source.IsRegister()) {
1838 GetAssembler()->StoreToOffset(kStoreWord,
1839 RegisterFrom(source),
1840 sp,
1841 destination.GetStackIndex());
1842 } else if (source.IsFpuRegister()) {
1843 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1844 } else {
1845 DCHECK(source.IsStackSlot()) << source;
1846 UseScratchRegisterScope temps(GetVIXLAssembler());
1847 vixl32::Register temp = temps.Acquire();
1848 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1849 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1850 }
1851 }
1852}
1853
Artem Serovcfbe9132016-10-14 15:58:56 +01001854void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1855 DCHECK(location.IsRegister());
1856 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001857}
1858
1859void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001860 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1861 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1862 HParallelMove move(GetGraph()->GetArena());
1863 move.AddMove(src, dst, dst_type, nullptr);
1864 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001865}
1866
Artem Serovcfbe9132016-10-14 15:58:56 +01001867void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1868 if (location.IsRegister()) {
1869 locations->AddTemp(location);
1870 } else if (location.IsRegisterPair()) {
1871 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1872 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1873 } else {
1874 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1875 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001876}
1877
1878void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1879 HInstruction* instruction,
1880 uint32_t dex_pc,
1881 SlowPathCode* slow_path) {
1882 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001883 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1884 // Ensure the pc position is recorded immediately after the `blx` instruction.
1885 // 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 +00001886 ExactAssemblyScope aas(GetVIXLAssembler(),
1887 vixl32::k16BitT32InstructionSizeInBytes,
1888 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001889 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001890 if (EntrypointRequiresStackMap(entrypoint)) {
1891 RecordPcInfo(instruction, dex_pc, slow_path);
1892 }
1893}
1894
1895void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1896 HInstruction* instruction,
1897 SlowPathCode* slow_path) {
1898 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001899 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001900 __ Blx(lr);
1901}
1902
Scott Wakelingfe885462016-09-22 10:24:38 +01001903void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1904 DCHECK(!successor->IsExitBlock());
1905 HBasicBlock* block = got->GetBlock();
1906 HInstruction* previous = got->GetPrevious();
1907 HLoopInformation* info = block->GetLoopInformation();
1908
1909 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1910 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1911 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1912 return;
1913 }
1914 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1915 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1916 }
1917 if (!codegen_->GoesToNextBlock(block, successor)) {
1918 __ B(codegen_->GetLabelOf(successor));
1919 }
1920}
1921
1922void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1923 got->SetLocations(nullptr);
1924}
1925
1926void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1927 HandleGoto(got, got->GetSuccessor());
1928}
1929
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001930void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1931 try_boundary->SetLocations(nullptr);
1932}
1933
1934void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1935 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1936 if (!successor->IsExitBlock()) {
1937 HandleGoto(try_boundary, successor);
1938 }
1939}
1940
Scott Wakelingfe885462016-09-22 10:24:38 +01001941void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1942 exit->SetLocations(nullptr);
1943}
1944
1945void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1946}
1947
1948void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1949 Primitive::Type type = instruction->InputAt(0)->GetType();
1950 Location lhs_loc = instruction->GetLocations()->InAt(0);
1951 Location rhs_loc = instruction->GetLocations()->InAt(1);
1952 if (rhs_loc.IsConstant()) {
1953 // 0.0 is the only immediate that can be encoded directly in
1954 // a VCMP instruction.
1955 //
1956 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1957 // specify that in a floating-point comparison, positive zero
1958 // and negative zero are considered equal, so we can use the
1959 // literal 0.0 for both cases here.
1960 //
1961 // Note however that some methods (Float.equal, Float.compare,
1962 // Float.compareTo, Double.equal, Double.compare,
1963 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1964 // StrictMath.min) consider 0.0 to be (strictly) greater than
1965 // -0.0. So if we ever translate calls to these methods into a
1966 // HCompare instruction, we must handle the -0.0 case with
1967 // care here.
1968 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1969 if (type == Primitive::kPrimFloat) {
1970 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1971 } else {
1972 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001973 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001974 }
1975 } else {
1976 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001977 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001978 } else {
1979 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001980 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001981 }
1982 }
1983}
1984
1985void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1986 vixl32::Label* true_label,
1987 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1988 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1989 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1990 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1991}
1992
1993void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1994 vixl32::Label* true_label,
1995 vixl32::Label* false_label) {
1996 LocationSummary* locations = cond->GetLocations();
1997 Location left = locations->InAt(0);
1998 Location right = locations->InAt(1);
1999 IfCondition if_cond = cond->GetCondition();
2000
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002001 vixl32::Register left_high = HighRegisterFrom(left);
2002 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002003 IfCondition true_high_cond = if_cond;
2004 IfCondition false_high_cond = cond->GetOppositeCondition();
2005 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2006
2007 // Set the conditions for the test, remembering that == needs to be
2008 // decided using the low words.
2009 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
2010 switch (if_cond) {
2011 case kCondEQ:
2012 case kCondNE:
2013 // Nothing to do.
2014 break;
2015 case kCondLT:
2016 false_high_cond = kCondGT;
2017 break;
2018 case kCondLE:
2019 true_high_cond = kCondLT;
2020 break;
2021 case kCondGT:
2022 false_high_cond = kCondLT;
2023 break;
2024 case kCondGE:
2025 true_high_cond = kCondGT;
2026 break;
2027 case kCondB:
2028 false_high_cond = kCondA;
2029 break;
2030 case kCondBE:
2031 true_high_cond = kCondB;
2032 break;
2033 case kCondA:
2034 false_high_cond = kCondB;
2035 break;
2036 case kCondAE:
2037 true_high_cond = kCondA;
2038 break;
2039 }
2040 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002041 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002042 int32_t val_low = Low32Bits(value);
2043 int32_t val_high = High32Bits(value);
2044
2045 __ Cmp(left_high, val_high);
2046 if (if_cond == kCondNE) {
2047 __ B(ARMCondition(true_high_cond), true_label);
2048 } else if (if_cond == kCondEQ) {
2049 __ B(ARMCondition(false_high_cond), false_label);
2050 } else {
2051 __ B(ARMCondition(true_high_cond), true_label);
2052 __ B(ARMCondition(false_high_cond), false_label);
2053 }
2054 // Must be equal high, so compare the lows.
2055 __ Cmp(left_low, val_low);
2056 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002057 vixl32::Register right_high = HighRegisterFrom(right);
2058 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002059
2060 __ Cmp(left_high, right_high);
2061 if (if_cond == kCondNE) {
2062 __ B(ARMCondition(true_high_cond), true_label);
2063 } else if (if_cond == kCondEQ) {
2064 __ B(ARMCondition(false_high_cond), false_label);
2065 } else {
2066 __ B(ARMCondition(true_high_cond), true_label);
2067 __ B(ARMCondition(false_high_cond), false_label);
2068 }
2069 // Must be equal high, so compare the lows.
2070 __ Cmp(left_low, right_low);
2071 }
2072 // The last comparison might be unsigned.
2073 // TODO: optimize cases where this is always true/false
2074 __ B(final_condition, true_label);
2075}
2076
2077void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2078 vixl32::Label* true_target_in,
2079 vixl32::Label* false_target_in) {
2080 // Generated branching requires both targets to be explicit. If either of the
2081 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2082 vixl32::Label fallthrough;
2083 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2084 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2085
2086 Primitive::Type type = condition->InputAt(0)->GetType();
2087 switch (type) {
2088 case Primitive::kPrimLong:
2089 GenerateLongComparesAndJumps(condition, true_target, false_target);
2090 break;
2091 case Primitive::kPrimFloat:
2092 case Primitive::kPrimDouble:
2093 GenerateVcmp(condition);
2094 GenerateFPJumps(condition, true_target, false_target);
2095 break;
2096 default:
2097 LOG(FATAL) << "Unexpected compare type " << type;
2098 }
2099
2100 if (false_target != &fallthrough) {
2101 __ B(false_target);
2102 }
2103
2104 if (true_target_in == nullptr || false_target_in == nullptr) {
2105 __ Bind(&fallthrough);
2106 }
2107}
2108
2109void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2110 size_t condition_input_index,
2111 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002112 vixl32::Label* false_target,
2113 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002114 HInstruction* cond = instruction->InputAt(condition_input_index);
2115
2116 if (true_target == nullptr && false_target == nullptr) {
2117 // Nothing to do. The code always falls through.
2118 return;
2119 } else if (cond->IsIntConstant()) {
2120 // Constant condition, statically compared against "true" (integer value 1).
2121 if (cond->AsIntConstant()->IsTrue()) {
2122 if (true_target != nullptr) {
2123 __ B(true_target);
2124 }
2125 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002126 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002127 if (false_target != nullptr) {
2128 __ B(false_target);
2129 }
2130 }
2131 return;
2132 }
2133
2134 // The following code generates these patterns:
2135 // (1) true_target == nullptr && false_target != nullptr
2136 // - opposite condition true => branch to false_target
2137 // (2) true_target != nullptr && false_target == nullptr
2138 // - condition true => branch to true_target
2139 // (3) true_target != nullptr && false_target != nullptr
2140 // - condition true => branch to true_target
2141 // - branch to false_target
2142 if (IsBooleanValueOrMaterializedCondition(cond)) {
2143 // Condition has been materialized, compare the output to 0.
2144 if (kIsDebugBuild) {
2145 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2146 DCHECK(cond_val.IsRegister());
2147 }
2148 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002149 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2150 false_target,
2151 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002152 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002153 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2154 true_target,
2155 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002156 }
2157 } else {
2158 // Condition has not been materialized. Use its inputs as the comparison and
2159 // its condition as the branch condition.
2160 HCondition* condition = cond->AsCondition();
2161
2162 // If this is a long or FP comparison that has been folded into
2163 // the HCondition, generate the comparison directly.
2164 Primitive::Type type = condition->InputAt(0)->GetType();
2165 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2166 GenerateCompareTestAndBranch(condition, true_target, false_target);
2167 return;
2168 }
2169
2170 LocationSummary* locations = cond->GetLocations();
2171 DCHECK(locations->InAt(0).IsRegister());
2172 vixl32::Register left = InputRegisterAt(cond, 0);
2173 Location right = locations->InAt(1);
2174 if (right.IsRegister()) {
2175 __ Cmp(left, InputRegisterAt(cond, 1));
2176 } else {
2177 DCHECK(right.IsConstant());
2178 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2179 }
2180 if (true_target == nullptr) {
2181 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
2182 } else {
2183 __ B(ARMCondition(condition->GetCondition()), true_target);
2184 }
2185 }
2186
2187 // If neither branch falls through (case 3), the conditional branch to `true_target`
2188 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2189 if (true_target != nullptr && false_target != nullptr) {
2190 __ B(false_target);
2191 }
2192}
2193
2194void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2196 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2197 locations->SetInAt(0, Location::RequiresRegister());
2198 }
2199}
2200
2201void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2202 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2203 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002204 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2205 nullptr : codegen_->GetLabelOf(true_successor);
2206 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2207 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002208 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2209}
2210
Scott Wakelingc34dba72016-10-03 10:14:44 +01002211void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2212 LocationSummary* locations = new (GetGraph()->GetArena())
2213 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2214 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2215 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2216 locations->SetInAt(0, Location::RequiresRegister());
2217 }
2218}
2219
2220void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2221 SlowPathCodeARMVIXL* slow_path =
2222 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2223 GenerateTestAndBranch(deoptimize,
2224 /* condition_input_index */ 0,
2225 slow_path->GetEntryLabel(),
2226 /* false_target */ nullptr);
2227}
2228
Artem Serovd4cc5b22016-11-04 11:19:09 +00002229void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2230 LocationSummary* locations = new (GetGraph()->GetArena())
2231 LocationSummary(flag, LocationSummary::kNoCall);
2232 locations->SetOut(Location::RequiresRegister());
2233}
2234
2235void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2236 GetAssembler()->LoadFromOffset(kLoadWord,
2237 OutputRegister(flag),
2238 sp,
2239 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2240}
2241
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002242void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2244 if (Primitive::IsFloatingPointType(select->GetType())) {
2245 locations->SetInAt(0, Location::RequiresFpuRegister());
2246 locations->SetInAt(1, Location::RequiresFpuRegister());
2247 } else {
2248 locations->SetInAt(0, Location::RequiresRegister());
2249 locations->SetInAt(1, Location::RequiresRegister());
2250 }
2251 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2252 locations->SetInAt(2, Location::RequiresRegister());
2253 }
2254 locations->SetOut(Location::SameAsFirstInput());
2255}
2256
2257void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2258 LocationSummary* locations = select->GetLocations();
2259 vixl32::Label false_target;
2260 GenerateTestAndBranch(select,
2261 /* condition_input_index */ 2,
2262 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002263 &false_target,
2264 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002265 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2266 __ Bind(&false_target);
2267}
2268
Artem Serov551b28f2016-10-18 19:11:30 +01002269void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2270 new (GetGraph()->GetArena()) LocationSummary(info);
2271}
2272
2273void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2274 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2275}
2276
Scott Wakelingfe885462016-09-22 10:24:38 +01002277void CodeGeneratorARMVIXL::GenerateNop() {
2278 __ Nop();
2279}
2280
2281void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2282 LocationSummary* locations =
2283 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2284 // Handle the long/FP comparisons made in instruction simplification.
2285 switch (cond->InputAt(0)->GetType()) {
2286 case Primitive::kPrimLong:
2287 locations->SetInAt(0, Location::RequiresRegister());
2288 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2289 if (!cond->IsEmittedAtUseSite()) {
2290 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2291 }
2292 break;
2293
Scott Wakelingfe885462016-09-22 10:24:38 +01002294 case Primitive::kPrimFloat:
2295 case Primitive::kPrimDouble:
2296 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002297 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002298 if (!cond->IsEmittedAtUseSite()) {
2299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2300 }
2301 break;
2302
2303 default:
2304 locations->SetInAt(0, Location::RequiresRegister());
2305 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2306 if (!cond->IsEmittedAtUseSite()) {
2307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2308 }
2309 }
2310}
2311
2312void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2313 if (cond->IsEmittedAtUseSite()) {
2314 return;
2315 }
2316
Artem Serov657022c2016-11-23 14:19:38 +00002317 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002318 vixl32::Register out = OutputRegister(cond);
2319 vixl32::Label true_label, false_label;
2320
2321 switch (cond->InputAt(0)->GetType()) {
2322 default: {
2323 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002324 if (right.IsRegister()) {
2325 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2326 } else {
2327 DCHECK(right.IsConstant());
2328 __ Cmp(InputRegisterAt(cond, 0),
2329 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2330 }
Artem Serov0fb37192016-12-06 18:13:40 +00002331 ExactAssemblyScope aas(GetVIXLAssembler(),
2332 3 * vixl32::kMaxInstructionSizeInBytes,
2333 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002334 __ ite(ARMCondition(cond->GetCondition()));
2335 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2336 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002337 return;
2338 }
2339 case Primitive::kPrimLong:
2340 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2341 break;
2342 case Primitive::kPrimFloat:
2343 case Primitive::kPrimDouble:
2344 GenerateVcmp(cond);
2345 GenerateFPJumps(cond, &true_label, &false_label);
2346 break;
2347 }
2348
2349 // Convert the jumps into the result.
2350 vixl32::Label done_label;
2351
2352 // False case: result = 0.
2353 __ Bind(&false_label);
2354 __ Mov(out, 0);
2355 __ B(&done_label);
2356
2357 // True case: result = 1.
2358 __ Bind(&true_label);
2359 __ Mov(out, 1);
2360 __ Bind(&done_label);
2361}
2362
2363void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2364 HandleCondition(comp);
2365}
2366
2367void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2368 HandleCondition(comp);
2369}
2370
2371void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2372 HandleCondition(comp);
2373}
2374
2375void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2376 HandleCondition(comp);
2377}
2378
2379void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2380 HandleCondition(comp);
2381}
2382
2383void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2384 HandleCondition(comp);
2385}
2386
2387void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2388 HandleCondition(comp);
2389}
2390
2391void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2392 HandleCondition(comp);
2393}
2394
2395void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2396 HandleCondition(comp);
2397}
2398
2399void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2400 HandleCondition(comp);
2401}
2402
2403void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2404 HandleCondition(comp);
2405}
2406
2407void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2408 HandleCondition(comp);
2409}
2410
2411void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2412 HandleCondition(comp);
2413}
2414
2415void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2416 HandleCondition(comp);
2417}
2418
2419void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2420 HandleCondition(comp);
2421}
2422
2423void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2424 HandleCondition(comp);
2425}
2426
2427void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2428 HandleCondition(comp);
2429}
2430
2431void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2432 HandleCondition(comp);
2433}
2434
2435void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2436 HandleCondition(comp);
2437}
2438
2439void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2440 HandleCondition(comp);
2441}
2442
2443void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2444 LocationSummary* locations =
2445 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2446 locations->SetOut(Location::ConstantLocation(constant));
2447}
2448
2449void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2450 // Will be generated at use site.
2451}
2452
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002453void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2454 LocationSummary* locations =
2455 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2456 locations->SetOut(Location::ConstantLocation(constant));
2457}
2458
2459void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2460 // Will be generated at use site.
2461}
2462
Scott Wakelingfe885462016-09-22 10:24:38 +01002463void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2464 LocationSummary* locations =
2465 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2466 locations->SetOut(Location::ConstantLocation(constant));
2467}
2468
2469void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2470 // Will be generated at use site.
2471}
2472
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002473void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2474 LocationSummary* locations =
2475 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2476 locations->SetOut(Location::ConstantLocation(constant));
2477}
2478
Scott Wakelingc34dba72016-10-03 10:14:44 +01002479void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2480 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002481 // Will be generated at use site.
2482}
2483
2484void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2485 LocationSummary* locations =
2486 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2487 locations->SetOut(Location::ConstantLocation(constant));
2488}
2489
Scott Wakelingc34dba72016-10-03 10:14:44 +01002490void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2491 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002492 // Will be generated at use site.
2493}
2494
Scott Wakelingfe885462016-09-22 10:24:38 +01002495void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2496 memory_barrier->SetLocations(nullptr);
2497}
2498
2499void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2500 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2501}
2502
2503void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2504 ret->SetLocations(nullptr);
2505}
2506
2507void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2508 codegen_->GenerateFrameExit();
2509}
2510
2511void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2512 LocationSummary* locations =
2513 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2514 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2515}
2516
2517void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2518 codegen_->GenerateFrameExit();
2519}
2520
Artem Serovcfbe9132016-10-14 15:58:56 +01002521void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2522 // The trampoline uses the same calling convention as dex calling conventions,
2523 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2524 // the method_idx.
2525 HandleInvoke(invoke);
2526}
2527
2528void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2529 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2530}
2531
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002532void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2533 // Explicit clinit checks triggered by static invokes must have been pruned by
2534 // art::PrepareForRegisterAllocation.
2535 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2536
Anton Kirilov5ec62182016-10-13 20:16:02 +01002537 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2538 if (intrinsic.TryDispatch(invoke)) {
2539 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2540 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2541 }
2542 return;
2543 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002544
2545 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002546
Artem Serovd4cc5b22016-11-04 11:19:09 +00002547 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2548 if (invoke->HasPcRelativeDexCache()) {
2549 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2550 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002551}
2552
Anton Kirilov5ec62182016-10-13 20:16:02 +01002553static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2554 if (invoke->GetLocations()->Intrinsified()) {
2555 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2556 intrinsic.Dispatch(invoke);
2557 return true;
2558 }
2559 return false;
2560}
2561
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002562void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2563 // Explicit clinit checks triggered by static invokes must have been pruned by
2564 // art::PrepareForRegisterAllocation.
2565 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2566
Anton Kirilov5ec62182016-10-13 20:16:02 +01002567 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2568 return;
2569 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002570
2571 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002572 codegen_->GenerateStaticOrDirectCall(
2573 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002574 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2575}
2576
2577void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002578 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002579 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2580}
2581
2582void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002583 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2584 if (intrinsic.TryDispatch(invoke)) {
2585 return;
2586 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002587
2588 HandleInvoke(invoke);
2589}
2590
2591void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002592 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2593 return;
2594 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002595
2596 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002597 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002598 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002599}
2600
Artem Serovcfbe9132016-10-14 15:58:56 +01002601void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2602 HandleInvoke(invoke);
2603 // Add the hidden argument.
2604 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2605}
2606
2607void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2608 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2609 LocationSummary* locations = invoke->GetLocations();
2610 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2611 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2612 Location receiver = locations->InAt(0);
2613 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2614
2615 DCHECK(!receiver.IsStackSlot());
2616
Alexandre Rames374ddf32016-11-04 10:40:49 +00002617 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2618 {
Artem Serov0fb37192016-12-06 18:13:40 +00002619 ExactAssemblyScope aas(GetVIXLAssembler(),
2620 vixl32::kMaxInstructionSizeInBytes,
2621 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002622 // /* HeapReference<Class> */ temp = receiver->klass_
2623 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2624 codegen_->MaybeRecordImplicitNullCheck(invoke);
2625 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002626 // Instead of simply (possibly) unpoisoning `temp` here, we should
2627 // emit a read barrier for the previous class reference load.
2628 // However this is not required in practice, as this is an
2629 // intermediate/temporary reference and because the current
2630 // concurrent copying collector keeps the from-space memory
2631 // intact/accessible until the end of the marking phase (the
2632 // concurrent copying collector may not in the future).
2633 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2634 GetAssembler()->LoadFromOffset(kLoadWord,
2635 temp,
2636 temp,
2637 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2638 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2639 invoke->GetImtIndex(), kArmPointerSize));
2640 // temp = temp->GetImtEntryAt(method_offset);
2641 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2642 uint32_t entry_point =
2643 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2644 // LR = temp->GetEntryPoint();
2645 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2646
2647 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2648 // instruction from clobbering it as they might use r12 as a scratch register.
2649 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002650
2651 {
2652 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2653 // so it checks if the application is using them (by passing them to the macro assembler
2654 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2655 // what is available, and is the opposite of the standard usage: Instead of requesting a
2656 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2657 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2658 // (to materialize the constant), since the destination register becomes available for such use
2659 // internally for the duration of the macro instruction.
2660 UseScratchRegisterScope temps(GetVIXLAssembler());
2661 temps.Exclude(hidden_reg);
2662 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2663 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002664 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002665 // Ensure the pc position is recorded immediately after the `blx` instruction.
2666 // 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 +00002667 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002668 vixl32::k16BitT32InstructionSizeInBytes,
2669 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002670 // LR();
2671 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002672 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002673 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002674 }
2675}
2676
Orion Hodsonac141392017-01-13 11:53:47 +00002677void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2678 HandleInvoke(invoke);
2679}
2680
2681void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2682 codegen_->GenerateInvokePolymorphicCall(invoke);
2683}
2684
Artem Serov02109dd2016-09-23 17:17:54 +01002685void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2688 switch (neg->GetResultType()) {
2689 case Primitive::kPrimInt: {
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2692 break;
2693 }
2694 case Primitive::kPrimLong: {
2695 locations->SetInAt(0, Location::RequiresRegister());
2696 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2697 break;
2698 }
2699
2700 case Primitive::kPrimFloat:
2701 case Primitive::kPrimDouble:
2702 locations->SetInAt(0, Location::RequiresFpuRegister());
2703 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2708 }
2709}
2710
2711void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2712 LocationSummary* locations = neg->GetLocations();
2713 Location out = locations->Out();
2714 Location in = locations->InAt(0);
2715 switch (neg->GetResultType()) {
2716 case Primitive::kPrimInt:
2717 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2718 break;
2719
2720 case Primitive::kPrimLong:
2721 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2722 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2723 // We cannot emit an RSC (Reverse Subtract with Carry)
2724 // instruction here, as it does not exist in the Thumb-2
2725 // instruction set. We use the following approach
2726 // using SBC and SUB instead.
2727 //
2728 // out.hi = -C
2729 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2730 // out.hi = out.hi - in.hi
2731 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2732 break;
2733
2734 case Primitive::kPrimFloat:
2735 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00002736 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01002737 break;
2738
2739 default:
2740 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2741 }
2742}
2743
Scott Wakelingfe885462016-09-22 10:24:38 +01002744void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2745 Primitive::Type result_type = conversion->GetResultType();
2746 Primitive::Type input_type = conversion->GetInputType();
2747 DCHECK_NE(result_type, input_type);
2748
2749 // The float-to-long, double-to-long and long-to-float type conversions
2750 // rely on a call to the runtime.
2751 LocationSummary::CallKind call_kind =
2752 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2753 && result_type == Primitive::kPrimLong)
2754 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2755 ? LocationSummary::kCallOnMainOnly
2756 : LocationSummary::kNoCall;
2757 LocationSummary* locations =
2758 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2759
2760 // The Java language does not allow treating boolean as an integral type but
2761 // our bit representation makes it safe.
2762
2763 switch (result_type) {
2764 case Primitive::kPrimByte:
2765 switch (input_type) {
2766 case Primitive::kPrimLong:
2767 // Type conversion from long to byte is a result of code transformations.
2768 case Primitive::kPrimBoolean:
2769 // Boolean input is a result of code transformations.
2770 case Primitive::kPrimShort:
2771 case Primitive::kPrimInt:
2772 case Primitive::kPrimChar:
2773 // Processing a Dex `int-to-byte' instruction.
2774 locations->SetInAt(0, Location::RequiresRegister());
2775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
2782 break;
2783
2784 case Primitive::kPrimShort:
2785 switch (input_type) {
2786 case Primitive::kPrimLong:
2787 // Type conversion from long to short is a result of code transformations.
2788 case Primitive::kPrimBoolean:
2789 // Boolean input is a result of code transformations.
2790 case Primitive::kPrimByte:
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimChar:
2793 // Processing a Dex `int-to-short' instruction.
2794 locations->SetInAt(0, Location::RequiresRegister());
2795 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2796 break;
2797
2798 default:
2799 LOG(FATAL) << "Unexpected type conversion from " << input_type
2800 << " to " << result_type;
2801 }
2802 break;
2803
2804 case Primitive::kPrimInt:
2805 switch (input_type) {
2806 case Primitive::kPrimLong:
2807 // Processing a Dex `long-to-int' instruction.
2808 locations->SetInAt(0, Location::Any());
2809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2810 break;
2811
2812 case Primitive::kPrimFloat:
2813 // Processing a Dex `float-to-int' instruction.
2814 locations->SetInAt(0, Location::RequiresFpuRegister());
2815 locations->SetOut(Location::RequiresRegister());
2816 locations->AddTemp(Location::RequiresFpuRegister());
2817 break;
2818
2819 case Primitive::kPrimDouble:
2820 // Processing a Dex `double-to-int' instruction.
2821 locations->SetInAt(0, Location::RequiresFpuRegister());
2822 locations->SetOut(Location::RequiresRegister());
2823 locations->AddTemp(Location::RequiresFpuRegister());
2824 break;
2825
2826 default:
2827 LOG(FATAL) << "Unexpected type conversion from " << input_type
2828 << " to " << result_type;
2829 }
2830 break;
2831
2832 case Primitive::kPrimLong:
2833 switch (input_type) {
2834 case Primitive::kPrimBoolean:
2835 // Boolean input is a result of code transformations.
2836 case Primitive::kPrimByte:
2837 case Primitive::kPrimShort:
2838 case Primitive::kPrimInt:
2839 case Primitive::kPrimChar:
2840 // Processing a Dex `int-to-long' instruction.
2841 locations->SetInAt(0, Location::RequiresRegister());
2842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2843 break;
2844
2845 case Primitive::kPrimFloat: {
2846 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2848 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2849 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002850 break;
2851 }
2852
2853 case Primitive::kPrimDouble: {
2854 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002855 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2856 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2857 calling_convention.GetFpuRegisterAt(1)));
2858 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002859 break;
2860 }
2861
2862 default:
2863 LOG(FATAL) << "Unexpected type conversion from " << input_type
2864 << " to " << result_type;
2865 }
2866 break;
2867
2868 case Primitive::kPrimChar:
2869 switch (input_type) {
2870 case Primitive::kPrimLong:
2871 // Type conversion from long to char is a result of code transformations.
2872 case Primitive::kPrimBoolean:
2873 // Boolean input is a result of code transformations.
2874 case Primitive::kPrimByte:
2875 case Primitive::kPrimShort:
2876 case Primitive::kPrimInt:
2877 // Processing a Dex `int-to-char' instruction.
2878 locations->SetInAt(0, Location::RequiresRegister());
2879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2880 break;
2881
2882 default:
2883 LOG(FATAL) << "Unexpected type conversion from " << input_type
2884 << " to " << result_type;
2885 }
2886 break;
2887
2888 case Primitive::kPrimFloat:
2889 switch (input_type) {
2890 case Primitive::kPrimBoolean:
2891 // Boolean input is a result of code transformations.
2892 case Primitive::kPrimByte:
2893 case Primitive::kPrimShort:
2894 case Primitive::kPrimInt:
2895 case Primitive::kPrimChar:
2896 // Processing a Dex `int-to-float' instruction.
2897 locations->SetInAt(0, Location::RequiresRegister());
2898 locations->SetOut(Location::RequiresFpuRegister());
2899 break;
2900
2901 case Primitive::kPrimLong: {
2902 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002903 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2904 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2905 calling_convention.GetRegisterAt(1)));
2906 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002907 break;
2908 }
2909
2910 case Primitive::kPrimDouble:
2911 // Processing a Dex `double-to-float' instruction.
2912 locations->SetInAt(0, Location::RequiresFpuRegister());
2913 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 };
2920 break;
2921
2922 case Primitive::kPrimDouble:
2923 switch (input_type) {
2924 case Primitive::kPrimBoolean:
2925 // Boolean input is a result of code transformations.
2926 case Primitive::kPrimByte:
2927 case Primitive::kPrimShort:
2928 case Primitive::kPrimInt:
2929 case Primitive::kPrimChar:
2930 // Processing a Dex `int-to-double' instruction.
2931 locations->SetInAt(0, Location::RequiresRegister());
2932 locations->SetOut(Location::RequiresFpuRegister());
2933 break;
2934
2935 case Primitive::kPrimLong:
2936 // Processing a Dex `long-to-double' instruction.
2937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetOut(Location::RequiresFpuRegister());
2939 locations->AddTemp(Location::RequiresFpuRegister());
2940 locations->AddTemp(Location::RequiresFpuRegister());
2941 break;
2942
2943 case Primitive::kPrimFloat:
2944 // Processing a Dex `float-to-double' instruction.
2945 locations->SetInAt(0, Location::RequiresFpuRegister());
2946 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2947 break;
2948
2949 default:
2950 LOG(FATAL) << "Unexpected type conversion from " << input_type
2951 << " to " << result_type;
2952 };
2953 break;
2954
2955 default:
2956 LOG(FATAL) << "Unexpected type conversion from " << input_type
2957 << " to " << result_type;
2958 }
2959}
2960
2961void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2962 LocationSummary* locations = conversion->GetLocations();
2963 Location out = locations->Out();
2964 Location in = locations->InAt(0);
2965 Primitive::Type result_type = conversion->GetResultType();
2966 Primitive::Type input_type = conversion->GetInputType();
2967 DCHECK_NE(result_type, input_type);
2968 switch (result_type) {
2969 case Primitive::kPrimByte:
2970 switch (input_type) {
2971 case Primitive::kPrimLong:
2972 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002973 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002974 break;
2975 case Primitive::kPrimBoolean:
2976 // Boolean input is a result of code transformations.
2977 case Primitive::kPrimShort:
2978 case Primitive::kPrimInt:
2979 case Primitive::kPrimChar:
2980 // Processing a Dex `int-to-byte' instruction.
2981 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2982 break;
2983
2984 default:
2985 LOG(FATAL) << "Unexpected type conversion from " << input_type
2986 << " to " << result_type;
2987 }
2988 break;
2989
2990 case Primitive::kPrimShort:
2991 switch (input_type) {
2992 case Primitive::kPrimLong:
2993 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002994 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002995 break;
2996 case Primitive::kPrimBoolean:
2997 // Boolean input is a result of code transformations.
2998 case Primitive::kPrimByte:
2999 case Primitive::kPrimInt:
3000 case Primitive::kPrimChar:
3001 // Processing a Dex `int-to-short' instruction.
3002 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3003 break;
3004
3005 default:
3006 LOG(FATAL) << "Unexpected type conversion from " << input_type
3007 << " to " << result_type;
3008 }
3009 break;
3010
3011 case Primitive::kPrimInt:
3012 switch (input_type) {
3013 case Primitive::kPrimLong:
3014 // Processing a Dex `long-to-int' instruction.
3015 DCHECK(out.IsRegister());
3016 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003017 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003018 } else if (in.IsDoubleStackSlot()) {
3019 GetAssembler()->LoadFromOffset(kLoadWord,
3020 OutputRegister(conversion),
3021 sp,
3022 in.GetStackIndex());
3023 } else {
3024 DCHECK(in.IsConstant());
3025 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00003026 int32_t value = Int32ConstantFrom(in);
3027 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003028 }
3029 break;
3030
3031 case Primitive::kPrimFloat: {
3032 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003033 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003034 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003035 __ Vmov(OutputRegister(conversion), temp);
3036 break;
3037 }
3038
3039 case Primitive::kPrimDouble: {
3040 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003041 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003042 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003043 __ Vmov(OutputRegister(conversion), temp_s);
3044 break;
3045 }
3046
3047 default:
3048 LOG(FATAL) << "Unexpected type conversion from " << input_type
3049 << " to " << result_type;
3050 }
3051 break;
3052
3053 case Primitive::kPrimLong:
3054 switch (input_type) {
3055 case Primitive::kPrimBoolean:
3056 // Boolean input is a result of code transformations.
3057 case Primitive::kPrimByte:
3058 case Primitive::kPrimShort:
3059 case Primitive::kPrimInt:
3060 case Primitive::kPrimChar:
3061 // Processing a Dex `int-to-long' instruction.
3062 DCHECK(out.IsRegisterPair());
3063 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003064 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003065 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003066 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003067 break;
3068
3069 case Primitive::kPrimFloat:
3070 // Processing a Dex `float-to-long' instruction.
3071 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3072 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3073 break;
3074
3075 case Primitive::kPrimDouble:
3076 // Processing a Dex `double-to-long' instruction.
3077 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3078 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3079 break;
3080
3081 default:
3082 LOG(FATAL) << "Unexpected type conversion from " << input_type
3083 << " to " << result_type;
3084 }
3085 break;
3086
3087 case Primitive::kPrimChar:
3088 switch (input_type) {
3089 case Primitive::kPrimLong:
3090 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003091 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003092 break;
3093 case Primitive::kPrimBoolean:
3094 // Boolean input is a result of code transformations.
3095 case Primitive::kPrimByte:
3096 case Primitive::kPrimShort:
3097 case Primitive::kPrimInt:
3098 // Processing a Dex `int-to-char' instruction.
3099 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3100 break;
3101
3102 default:
3103 LOG(FATAL) << "Unexpected type conversion from " << input_type
3104 << " to " << result_type;
3105 }
3106 break;
3107
3108 case Primitive::kPrimFloat:
3109 switch (input_type) {
3110 case Primitive::kPrimBoolean:
3111 // Boolean input is a result of code transformations.
3112 case Primitive::kPrimByte:
3113 case Primitive::kPrimShort:
3114 case Primitive::kPrimInt:
3115 case Primitive::kPrimChar: {
3116 // Processing a Dex `int-to-float' instruction.
3117 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003118 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003119 break;
3120 }
3121
3122 case Primitive::kPrimLong:
3123 // Processing a Dex `long-to-float' instruction.
3124 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3125 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3126 break;
3127
3128 case Primitive::kPrimDouble:
3129 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003130 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003131 break;
3132
3133 default:
3134 LOG(FATAL) << "Unexpected type conversion from " << input_type
3135 << " to " << result_type;
3136 };
3137 break;
3138
3139 case Primitive::kPrimDouble:
3140 switch (input_type) {
3141 case Primitive::kPrimBoolean:
3142 // Boolean input is a result of code transformations.
3143 case Primitive::kPrimByte:
3144 case Primitive::kPrimShort:
3145 case Primitive::kPrimInt:
3146 case Primitive::kPrimChar: {
3147 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003148 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003149 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003150 break;
3151 }
3152
3153 case Primitive::kPrimLong: {
3154 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155 vixl32::Register low = LowRegisterFrom(in);
3156 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003157 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003158 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003159 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003160 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003161 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003162
3163 // temp_d = int-to-double(high)
3164 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003165 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003166 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003167 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003168 // out_d = unsigned-to-double(low)
3169 __ Vmov(out_s, low);
3170 __ Vcvt(F64, U32, out_d, out_s);
3171 // out_d += temp_d * constant_d
3172 __ Vmla(F64, out_d, temp_d, constant_d);
3173 break;
3174 }
3175
3176 case Primitive::kPrimFloat:
3177 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003178 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003179 break;
3180
3181 default:
3182 LOG(FATAL) << "Unexpected type conversion from " << input_type
3183 << " to " << result_type;
3184 };
3185 break;
3186
3187 default:
3188 LOG(FATAL) << "Unexpected type conversion from " << input_type
3189 << " to " << result_type;
3190 }
3191}
3192
3193void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3194 LocationSummary* locations =
3195 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3196 switch (add->GetResultType()) {
3197 case Primitive::kPrimInt: {
3198 locations->SetInAt(0, Location::RequiresRegister());
3199 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3201 break;
3202 }
3203
Scott Wakelingfe885462016-09-22 10:24:38 +01003204 case Primitive::kPrimLong: {
3205 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003206 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3208 break;
3209 }
3210
3211 case Primitive::kPrimFloat:
3212 case Primitive::kPrimDouble: {
3213 locations->SetInAt(0, Location::RequiresFpuRegister());
3214 locations->SetInAt(1, Location::RequiresFpuRegister());
3215 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3216 break;
3217 }
3218
3219 default:
3220 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3221 }
3222}
3223
3224void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3225 LocationSummary* locations = add->GetLocations();
3226 Location out = locations->Out();
3227 Location first = locations->InAt(0);
3228 Location second = locations->InAt(1);
3229
3230 switch (add->GetResultType()) {
3231 case Primitive::kPrimInt: {
3232 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3233 }
3234 break;
3235
Scott Wakelingfe885462016-09-22 10:24:38 +01003236 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003237 if (second.IsConstant()) {
3238 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3239 GenerateAddLongConst(out, first, value);
3240 } else {
3241 DCHECK(second.IsRegisterPair());
3242 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3243 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3244 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003245 break;
3246 }
3247
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003248 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003249 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003250 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003251 break;
3252
3253 default:
3254 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3255 }
3256}
3257
3258void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3259 LocationSummary* locations =
3260 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3261 switch (sub->GetResultType()) {
3262 case Primitive::kPrimInt: {
3263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3266 break;
3267 }
3268
Scott Wakelingfe885462016-09-22 10:24:38 +01003269 case Primitive::kPrimLong: {
3270 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003271 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3273 break;
3274 }
3275 case Primitive::kPrimFloat:
3276 case Primitive::kPrimDouble: {
3277 locations->SetInAt(0, Location::RequiresFpuRegister());
3278 locations->SetInAt(1, Location::RequiresFpuRegister());
3279 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3280 break;
3281 }
3282 default:
3283 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3284 }
3285}
3286
3287void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3288 LocationSummary* locations = sub->GetLocations();
3289 Location out = locations->Out();
3290 Location first = locations->InAt(0);
3291 Location second = locations->InAt(1);
3292 switch (sub->GetResultType()) {
3293 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003294 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003295 break;
3296 }
3297
Scott Wakelingfe885462016-09-22 10:24:38 +01003298 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003299 if (second.IsConstant()) {
3300 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3301 GenerateAddLongConst(out, first, -value);
3302 } else {
3303 DCHECK(second.IsRegisterPair());
3304 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3305 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3306 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003307 break;
3308 }
3309
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003310 case Primitive::kPrimFloat:
3311 case Primitive::kPrimDouble:
3312 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003313 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003314
3315 default:
3316 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3317 }
3318}
3319
3320void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3321 LocationSummary* locations =
3322 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3323 switch (mul->GetResultType()) {
3324 case Primitive::kPrimInt:
3325 case Primitive::kPrimLong: {
3326 locations->SetInAt(0, Location::RequiresRegister());
3327 locations->SetInAt(1, Location::RequiresRegister());
3328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3329 break;
3330 }
3331
3332 case Primitive::kPrimFloat:
3333 case Primitive::kPrimDouble: {
3334 locations->SetInAt(0, Location::RequiresFpuRegister());
3335 locations->SetInAt(1, Location::RequiresFpuRegister());
3336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3337 break;
3338 }
3339
3340 default:
3341 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3342 }
3343}
3344
3345void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3346 LocationSummary* locations = mul->GetLocations();
3347 Location out = locations->Out();
3348 Location first = locations->InAt(0);
3349 Location second = locations->InAt(1);
3350 switch (mul->GetResultType()) {
3351 case Primitive::kPrimInt: {
3352 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3353 break;
3354 }
3355 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003356 vixl32::Register out_hi = HighRegisterFrom(out);
3357 vixl32::Register out_lo = LowRegisterFrom(out);
3358 vixl32::Register in1_hi = HighRegisterFrom(first);
3359 vixl32::Register in1_lo = LowRegisterFrom(first);
3360 vixl32::Register in2_hi = HighRegisterFrom(second);
3361 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003362
3363 // Extra checks to protect caused by the existence of R1_R2.
3364 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3365 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003366 DCHECK(!out_hi.Is(in1_lo));
3367 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003368
3369 // input: in1 - 64 bits, in2 - 64 bits
3370 // output: out
3371 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3372 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3373 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3374
3375 UseScratchRegisterScope temps(GetVIXLAssembler());
3376 vixl32::Register temp = temps.Acquire();
3377 // temp <- in1.lo * in2.hi
3378 __ Mul(temp, in1_lo, in2_hi);
3379 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3380 __ Mla(out_hi, in1_hi, in2_lo, temp);
3381 // out.lo <- (in1.lo * in2.lo)[31:0];
3382 __ Umull(out_lo, temp, in1_lo, in2_lo);
3383 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003384 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003385 break;
3386 }
3387
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003388 case Primitive::kPrimFloat:
3389 case Primitive::kPrimDouble:
3390 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003391 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003392
3393 default:
3394 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3395 }
3396}
3397
Scott Wakelingfe885462016-09-22 10:24:38 +01003398void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3399 DCHECK(instruction->IsDiv() || instruction->IsRem());
3400 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3401
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003402 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003403 DCHECK(second.IsConstant());
3404
3405 vixl32::Register out = OutputRegister(instruction);
3406 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003407 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003408 DCHECK(imm == 1 || imm == -1);
3409
3410 if (instruction->IsRem()) {
3411 __ Mov(out, 0);
3412 } else {
3413 if (imm == 1) {
3414 __ Mov(out, dividend);
3415 } else {
3416 __ Rsb(out, dividend, 0);
3417 }
3418 }
3419}
3420
3421void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3422 DCHECK(instruction->IsDiv() || instruction->IsRem());
3423 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3424
3425 LocationSummary* locations = instruction->GetLocations();
3426 Location second = locations->InAt(1);
3427 DCHECK(second.IsConstant());
3428
3429 vixl32::Register out = OutputRegister(instruction);
3430 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003431 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003432 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003433 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3434 int ctz_imm = CTZ(abs_imm);
3435
3436 if (ctz_imm == 1) {
3437 __ Lsr(temp, dividend, 32 - ctz_imm);
3438 } else {
3439 __ Asr(temp, dividend, 31);
3440 __ Lsr(temp, temp, 32 - ctz_imm);
3441 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003442 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003443
3444 if (instruction->IsDiv()) {
3445 __ Asr(out, out, ctz_imm);
3446 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003447 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003448 }
3449 } else {
3450 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003451 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003452 }
3453}
3454
3455void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3456 DCHECK(instruction->IsDiv() || instruction->IsRem());
3457 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3458
3459 LocationSummary* locations = instruction->GetLocations();
3460 Location second = locations->InAt(1);
3461 DCHECK(second.IsConstant());
3462
3463 vixl32::Register out = OutputRegister(instruction);
3464 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003465 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3466 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003467 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003468
3469 int64_t magic;
3470 int shift;
3471 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3472
Anton Kirilovdda43962016-11-21 19:55:20 +00003473 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3474 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003475 __ Smull(temp2, temp1, dividend, temp1);
3476
3477 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003478 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003479 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003480 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003481 }
3482
3483 if (shift != 0) {
3484 __ Asr(temp1, temp1, shift);
3485 }
3486
3487 if (instruction->IsDiv()) {
3488 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3489 } else {
3490 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3491 // TODO: Strength reduction for mls.
3492 __ Mov(temp2, imm);
3493 __ Mls(out, temp1, temp2, dividend);
3494 }
3495}
3496
3497void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3498 HBinaryOperation* instruction) {
3499 DCHECK(instruction->IsDiv() || instruction->IsRem());
3500 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3501
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003502 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003503 DCHECK(second.IsConstant());
3504
Anton Kirilov644032c2016-12-06 17:51:43 +00003505 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003506 if (imm == 0) {
3507 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3508 } else if (imm == 1 || imm == -1) {
3509 DivRemOneOrMinusOne(instruction);
3510 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3511 DivRemByPowerOfTwo(instruction);
3512 } else {
3513 DCHECK(imm <= -2 || imm >= 2);
3514 GenerateDivRemWithAnyConstant(instruction);
3515 }
3516}
3517
3518void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3519 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3520 if (div->GetResultType() == Primitive::kPrimLong) {
3521 // pLdiv runtime call.
3522 call_kind = LocationSummary::kCallOnMainOnly;
3523 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3524 // sdiv will be replaced by other instruction sequence.
3525 } else if (div->GetResultType() == Primitive::kPrimInt &&
3526 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3527 // pIdivmod runtime call.
3528 call_kind = LocationSummary::kCallOnMainOnly;
3529 }
3530
3531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3532
3533 switch (div->GetResultType()) {
3534 case Primitive::kPrimInt: {
3535 if (div->InputAt(1)->IsConstant()) {
3536 locations->SetInAt(0, Location::RequiresRegister());
3537 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003539 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003540 if (value == 1 || value == 0 || value == -1) {
3541 // No temp register required.
3542 } else {
3543 locations->AddTemp(Location::RequiresRegister());
3544 if (!IsPowerOfTwo(AbsOrMin(value))) {
3545 locations->AddTemp(Location::RequiresRegister());
3546 }
3547 }
3548 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3549 locations->SetInAt(0, Location::RequiresRegister());
3550 locations->SetInAt(1, Location::RequiresRegister());
3551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3552 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003553 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3554 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3555 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003556 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003557 // we only need the former.
3558 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003559 }
3560 break;
3561 }
3562 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003563 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3564 locations->SetInAt(0, LocationFrom(
3565 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3566 locations->SetInAt(1, LocationFrom(
3567 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3568 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003569 break;
3570 }
3571 case Primitive::kPrimFloat:
3572 case Primitive::kPrimDouble: {
3573 locations->SetInAt(0, Location::RequiresFpuRegister());
3574 locations->SetInAt(1, Location::RequiresFpuRegister());
3575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3576 break;
3577 }
3578
3579 default:
3580 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3581 }
3582}
3583
3584void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003585 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003586 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003587
3588 switch (div->GetResultType()) {
3589 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003590 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003591 GenerateDivRemConstantIntegral(div);
3592 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3593 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3594 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003595 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3596 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3597 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3598 DCHECK(r0.Is(OutputRegister(div)));
3599
3600 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3601 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003602 }
3603 break;
3604 }
3605
3606 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003607 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3608 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3609 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3610 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3611 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3612 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3613 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3614
3615 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3616 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003617 break;
3618 }
3619
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003620 case Primitive::kPrimFloat:
3621 case Primitive::kPrimDouble:
3622 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003623 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003624
3625 default:
3626 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3627 }
3628}
3629
Artem Serov551b28f2016-10-18 19:11:30 +01003630void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3631 Primitive::Type type = rem->GetResultType();
3632
3633 // Most remainders are implemented in the runtime.
3634 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3635 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3636 // sdiv will be replaced by other instruction sequence.
3637 call_kind = LocationSummary::kNoCall;
3638 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3639 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3640 // Have hardware divide instruction for int, do it with three instructions.
3641 call_kind = LocationSummary::kNoCall;
3642 }
3643
3644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3645
3646 switch (type) {
3647 case Primitive::kPrimInt: {
3648 if (rem->InputAt(1)->IsConstant()) {
3649 locations->SetInAt(0, Location::RequiresRegister());
3650 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003652 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01003653 if (value == 1 || value == 0 || value == -1) {
3654 // No temp register required.
3655 } else {
3656 locations->AddTemp(Location::RequiresRegister());
3657 if (!IsPowerOfTwo(AbsOrMin(value))) {
3658 locations->AddTemp(Location::RequiresRegister());
3659 }
3660 }
3661 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3662 locations->SetInAt(0, Location::RequiresRegister());
3663 locations->SetInAt(1, Location::RequiresRegister());
3664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3665 locations->AddTemp(Location::RequiresRegister());
3666 } else {
3667 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3668 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3669 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003670 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003671 // we only need the latter.
3672 locations->SetOut(LocationFrom(r1));
3673 }
3674 break;
3675 }
3676 case Primitive::kPrimLong: {
3677 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3678 locations->SetInAt(0, LocationFrom(
3679 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3680 locations->SetInAt(1, LocationFrom(
3681 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3682 // The runtime helper puts the output in R2,R3.
3683 locations->SetOut(LocationFrom(r2, r3));
3684 break;
3685 }
3686 case Primitive::kPrimFloat: {
3687 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3688 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3689 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3690 locations->SetOut(LocationFrom(s0));
3691 break;
3692 }
3693
3694 case Primitive::kPrimDouble: {
3695 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3696 locations->SetInAt(0, LocationFrom(
3697 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3698 locations->SetInAt(1, LocationFrom(
3699 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3700 locations->SetOut(LocationFrom(s0, s1));
3701 break;
3702 }
3703
3704 default:
3705 LOG(FATAL) << "Unexpected rem type " << type;
3706 }
3707}
3708
3709void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3710 LocationSummary* locations = rem->GetLocations();
3711 Location second = locations->InAt(1);
3712
3713 Primitive::Type type = rem->GetResultType();
3714 switch (type) {
3715 case Primitive::kPrimInt: {
3716 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3717 vixl32::Register out_reg = OutputRegister(rem);
3718 if (second.IsConstant()) {
3719 GenerateDivRemConstantIntegral(rem);
3720 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3721 vixl32::Register reg2 = RegisterFrom(second);
3722 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3723
3724 // temp = reg1 / reg2 (integer division)
3725 // dest = reg1 - temp * reg2
3726 __ Sdiv(temp, reg1, reg2);
3727 __ Mls(out_reg, temp, reg2, reg1);
3728 } else {
3729 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3730 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3731 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3732 DCHECK(out_reg.Is(r1));
3733
3734 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3735 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3736 }
3737 break;
3738 }
3739
3740 case Primitive::kPrimLong: {
3741 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3742 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3743 break;
3744 }
3745
3746 case Primitive::kPrimFloat: {
3747 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3748 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3749 break;
3750 }
3751
3752 case Primitive::kPrimDouble: {
3753 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3754 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3755 break;
3756 }
3757
3758 default:
3759 LOG(FATAL) << "Unexpected rem type " << type;
3760 }
3761}
3762
3763
Scott Wakelingfe885462016-09-22 10:24:38 +01003764void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003765 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003766 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003767}
3768
3769void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3770 DivZeroCheckSlowPathARMVIXL* slow_path =
3771 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3772 codegen_->AddSlowPath(slow_path);
3773
3774 LocationSummary* locations = instruction->GetLocations();
3775 Location value = locations->InAt(0);
3776
3777 switch (instruction->GetType()) {
3778 case Primitive::kPrimBoolean:
3779 case Primitive::kPrimByte:
3780 case Primitive::kPrimChar:
3781 case Primitive::kPrimShort:
3782 case Primitive::kPrimInt: {
3783 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003784 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003785 } else {
3786 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003787 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003788 __ B(slow_path->GetEntryLabel());
3789 }
3790 }
3791 break;
3792 }
3793 case Primitive::kPrimLong: {
3794 if (value.IsRegisterPair()) {
3795 UseScratchRegisterScope temps(GetVIXLAssembler());
3796 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003797 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003798 __ B(eq, slow_path->GetEntryLabel());
3799 } else {
3800 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003801 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003802 __ B(slow_path->GetEntryLabel());
3803 }
3804 }
3805 break;
3806 }
3807 default:
3808 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3809 }
3810}
3811
Artem Serov02109dd2016-09-23 17:17:54 +01003812void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3813 LocationSummary* locations = ror->GetLocations();
3814 vixl32::Register in = InputRegisterAt(ror, 0);
3815 Location rhs = locations->InAt(1);
3816 vixl32::Register out = OutputRegister(ror);
3817
3818 if (rhs.IsConstant()) {
3819 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3820 // so map all rotations to a +ve. equivalent in that range.
3821 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3822 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3823 if (rot) {
3824 // Rotate, mapping left rotations to right equivalents if necessary.
3825 // (e.g. left by 2 bits == right by 30.)
3826 __ Ror(out, in, rot);
3827 } else if (!out.Is(in)) {
3828 __ Mov(out, in);
3829 }
3830 } else {
3831 __ Ror(out, in, RegisterFrom(rhs));
3832 }
3833}
3834
3835// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3836// rotates by swapping input regs (effectively rotating by the first 32-bits of
3837// a larger rotation) or flipping direction (thus treating larger right/left
3838// rotations as sub-word sized rotations in the other direction) as appropriate.
3839void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3840 LocationSummary* locations = ror->GetLocations();
3841 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3842 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3843 Location rhs = locations->InAt(1);
3844 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3845 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3846
3847 if (rhs.IsConstant()) {
3848 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3849 // Map all rotations to +ve. equivalents on the interval [0,63].
3850 rot &= kMaxLongShiftDistance;
3851 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3852 // logic below to a simple pair of binary orr.
3853 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3854 if (rot >= kArmBitsPerWord) {
3855 rot -= kArmBitsPerWord;
3856 std::swap(in_reg_hi, in_reg_lo);
3857 }
3858 // Rotate, or mov to out for zero or word size rotations.
3859 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003860 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003861 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003862 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003863 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3864 } else {
3865 __ Mov(out_reg_lo, in_reg_lo);
3866 __ Mov(out_reg_hi, in_reg_hi);
3867 }
3868 } else {
3869 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3870 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3871 vixl32::Label end;
3872 vixl32::Label shift_by_32_plus_shift_right;
3873
3874 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3875 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003876 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00003877 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01003878
3879 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3880 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3881 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3882 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3883 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3884 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3885 __ Lsr(shift_left, in_reg_hi, shift_right);
3886 __ Add(out_reg_lo, out_reg_lo, shift_left);
3887 __ B(&end);
3888
3889 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3890 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3891 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3892 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3893 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3894 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3895 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3896 __ Lsl(shift_right, in_reg_hi, shift_left);
3897 __ Add(out_reg_lo, out_reg_lo, shift_right);
3898
3899 __ Bind(&end);
3900 }
3901}
3902
3903void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3904 LocationSummary* locations =
3905 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3906 switch (ror->GetResultType()) {
3907 case Primitive::kPrimInt: {
3908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3910 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3911 break;
3912 }
3913 case Primitive::kPrimLong: {
3914 locations->SetInAt(0, Location::RequiresRegister());
3915 if (ror->InputAt(1)->IsConstant()) {
3916 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3917 } else {
3918 locations->SetInAt(1, Location::RequiresRegister());
3919 locations->AddTemp(Location::RequiresRegister());
3920 locations->AddTemp(Location::RequiresRegister());
3921 }
3922 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3923 break;
3924 }
3925 default:
3926 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3927 }
3928}
3929
3930void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3931 Primitive::Type type = ror->GetResultType();
3932 switch (type) {
3933 case Primitive::kPrimInt: {
3934 HandleIntegerRotate(ror);
3935 break;
3936 }
3937 case Primitive::kPrimLong: {
3938 HandleLongRotate(ror);
3939 break;
3940 }
3941 default:
3942 LOG(FATAL) << "Unexpected operation type " << type;
3943 UNREACHABLE();
3944 }
3945}
3946
Artem Serov02d37832016-10-25 15:25:33 +01003947void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3948 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3949
3950 LocationSummary* locations =
3951 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3952
3953 switch (op->GetResultType()) {
3954 case Primitive::kPrimInt: {
3955 locations->SetInAt(0, Location::RequiresRegister());
3956 if (op->InputAt(1)->IsConstant()) {
3957 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3959 } else {
3960 locations->SetInAt(1, Location::RequiresRegister());
3961 // Make the output overlap, as it will be used to hold the masked
3962 // second input.
3963 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3964 }
3965 break;
3966 }
3967 case Primitive::kPrimLong: {
3968 locations->SetInAt(0, Location::RequiresRegister());
3969 if (op->InputAt(1)->IsConstant()) {
3970 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3971 // For simplicity, use kOutputOverlap even though we only require that low registers
3972 // don't clash with high registers which the register allocator currently guarantees.
3973 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3974 } else {
3975 locations->SetInAt(1, Location::RequiresRegister());
3976 locations->AddTemp(Location::RequiresRegister());
3977 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3978 }
3979 break;
3980 }
3981 default:
3982 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3983 }
3984}
3985
3986void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3987 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3988
3989 LocationSummary* locations = op->GetLocations();
3990 Location out = locations->Out();
3991 Location first = locations->InAt(0);
3992 Location second = locations->InAt(1);
3993
3994 Primitive::Type type = op->GetResultType();
3995 switch (type) {
3996 case Primitive::kPrimInt: {
3997 vixl32::Register out_reg = OutputRegister(op);
3998 vixl32::Register first_reg = InputRegisterAt(op, 0);
3999 if (second.IsRegister()) {
4000 vixl32::Register second_reg = RegisterFrom(second);
4001 // ARM doesn't mask the shift count so we need to do it ourselves.
4002 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4003 if (op->IsShl()) {
4004 __ Lsl(out_reg, first_reg, out_reg);
4005 } else if (op->IsShr()) {
4006 __ Asr(out_reg, first_reg, out_reg);
4007 } else {
4008 __ Lsr(out_reg, first_reg, out_reg);
4009 }
4010 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004011 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004012 uint32_t shift_value = cst & kMaxIntShiftDistance;
4013 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4014 __ Mov(out_reg, first_reg);
4015 } else if (op->IsShl()) {
4016 __ Lsl(out_reg, first_reg, shift_value);
4017 } else if (op->IsShr()) {
4018 __ Asr(out_reg, first_reg, shift_value);
4019 } else {
4020 __ Lsr(out_reg, first_reg, shift_value);
4021 }
4022 }
4023 break;
4024 }
4025 case Primitive::kPrimLong: {
4026 vixl32::Register o_h = HighRegisterFrom(out);
4027 vixl32::Register o_l = LowRegisterFrom(out);
4028
4029 vixl32::Register high = HighRegisterFrom(first);
4030 vixl32::Register low = LowRegisterFrom(first);
4031
4032 if (second.IsRegister()) {
4033 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4034
4035 vixl32::Register second_reg = RegisterFrom(second);
4036
4037 if (op->IsShl()) {
4038 __ And(o_l, second_reg, kMaxLongShiftDistance);
4039 // Shift the high part
4040 __ Lsl(o_h, high, o_l);
4041 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004042 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004043 __ Lsr(temp, low, temp);
4044 __ Orr(o_h, o_h, temp);
4045 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004046 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004047 {
Artem Serov0fb37192016-12-06 18:13:40 +00004048 ExactAssemblyScope guard(GetVIXLAssembler(),
4049 2 * vixl32::kMaxInstructionSizeInBytes,
4050 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004051 __ it(pl);
4052 __ lsl(pl, o_h, low, temp);
4053 }
4054 // Shift the low part
4055 __ Lsl(o_l, low, o_l);
4056 } else if (op->IsShr()) {
4057 __ And(o_h, second_reg, kMaxLongShiftDistance);
4058 // Shift the low part
4059 __ Lsr(o_l, low, o_h);
4060 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004061 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004062 __ Lsl(temp, high, temp);
4063 __ Orr(o_l, o_l, temp);
4064 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004065 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004066 {
Artem Serov0fb37192016-12-06 18:13:40 +00004067 ExactAssemblyScope guard(GetVIXLAssembler(),
4068 2 * vixl32::kMaxInstructionSizeInBytes,
4069 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004070 __ it(pl);
4071 __ asr(pl, o_l, high, temp);
4072 }
4073 // Shift the high part
4074 __ Asr(o_h, high, o_h);
4075 } else {
4076 __ And(o_h, second_reg, kMaxLongShiftDistance);
4077 // same as Shr except we use `Lsr`s and not `Asr`s
4078 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004079 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004080 __ Lsl(temp, high, temp);
4081 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004082 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004083 {
Artem Serov0fb37192016-12-06 18:13:40 +00004084 ExactAssemblyScope guard(GetVIXLAssembler(),
4085 2 * vixl32::kMaxInstructionSizeInBytes,
4086 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004087 __ it(pl);
4088 __ lsr(pl, o_l, high, temp);
4089 }
4090 __ Lsr(o_h, high, o_h);
4091 }
4092 } else {
4093 // Register allocator doesn't create partial overlap.
4094 DCHECK(!o_l.Is(high));
4095 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004096 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004097 uint32_t shift_value = cst & kMaxLongShiftDistance;
4098 if (shift_value > 32) {
4099 if (op->IsShl()) {
4100 __ Lsl(o_h, low, shift_value - 32);
4101 __ Mov(o_l, 0);
4102 } else if (op->IsShr()) {
4103 __ Asr(o_l, high, shift_value - 32);
4104 __ Asr(o_h, high, 31);
4105 } else {
4106 __ Lsr(o_l, high, shift_value - 32);
4107 __ Mov(o_h, 0);
4108 }
4109 } else if (shift_value == 32) {
4110 if (op->IsShl()) {
4111 __ Mov(o_h, low);
4112 __ Mov(o_l, 0);
4113 } else if (op->IsShr()) {
4114 __ Mov(o_l, high);
4115 __ Asr(o_h, high, 31);
4116 } else {
4117 __ Mov(o_l, high);
4118 __ Mov(o_h, 0);
4119 }
4120 } else if (shift_value == 1) {
4121 if (op->IsShl()) {
4122 __ Lsls(o_l, low, 1);
4123 __ Adc(o_h, high, high);
4124 } else if (op->IsShr()) {
4125 __ Asrs(o_h, high, 1);
4126 __ Rrx(o_l, low);
4127 } else {
4128 __ Lsrs(o_h, high, 1);
4129 __ Rrx(o_l, low);
4130 }
4131 } else {
4132 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4133 if (op->IsShl()) {
4134 __ Lsl(o_h, high, shift_value);
4135 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4136 __ Lsl(o_l, low, shift_value);
4137 } else if (op->IsShr()) {
4138 __ Lsr(o_l, low, shift_value);
4139 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4140 __ Asr(o_h, high, shift_value);
4141 } else {
4142 __ Lsr(o_l, low, shift_value);
4143 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4144 __ Lsr(o_h, high, shift_value);
4145 }
4146 }
4147 }
4148 break;
4149 }
4150 default:
4151 LOG(FATAL) << "Unexpected operation type " << type;
4152 UNREACHABLE();
4153 }
4154}
4155
4156void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4157 HandleShift(shl);
4158}
4159
4160void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4161 HandleShift(shl);
4162}
4163
4164void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4165 HandleShift(shr);
4166}
4167
4168void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4169 HandleShift(shr);
4170}
4171
4172void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4173 HandleShift(ushr);
4174}
4175
4176void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4177 HandleShift(ushr);
4178}
4179
4180void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4181 LocationSummary* locations =
4182 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4183 if (instruction->IsStringAlloc()) {
4184 locations->AddTemp(LocationFrom(kMethodRegister));
4185 } else {
4186 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4187 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004188 }
4189 locations->SetOut(LocationFrom(r0));
4190}
4191
4192void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4193 // Note: if heap poisoning is enabled, the entry point takes cares
4194 // of poisoning the reference.
4195 if (instruction->IsStringAlloc()) {
4196 // String is allocated through StringFactory. Call NewEmptyString entry point.
4197 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4198 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4199 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4200 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004201 // 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 +00004202 ExactAssemblyScope aas(GetVIXLAssembler(),
4203 vixl32::k16BitT32InstructionSizeInBytes,
4204 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004205 __ blx(lr);
4206 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4207 } else {
4208 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004209 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004210 }
4211}
4212
4213void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4214 LocationSummary* locations =
4215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4216 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004217 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004218 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4219 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004220}
4221
4222void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004223 // Note: if heap poisoning is enabled, the entry point takes cares
4224 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004225 QuickEntrypointEnum entrypoint =
4226 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4227 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004228 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004229 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004230}
4231
4232void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4233 LocationSummary* locations =
4234 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4235 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4236 if (location.IsStackSlot()) {
4237 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4238 } else if (location.IsDoubleStackSlot()) {
4239 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4240 }
4241 locations->SetOut(location);
4242}
4243
4244void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4245 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4246 // Nothing to do, the parameter is already at its location.
4247}
4248
4249void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4250 LocationSummary* locations =
4251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4252 locations->SetOut(LocationFrom(kMethodRegister));
4253}
4254
4255void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4256 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4257 // Nothing to do, the method is already at its location.
4258}
4259
4260void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4261 LocationSummary* locations =
4262 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4263 locations->SetInAt(0, Location::RequiresRegister());
4264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4265}
4266
4267void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4268 LocationSummary* locations = not_->GetLocations();
4269 Location out = locations->Out();
4270 Location in = locations->InAt(0);
4271 switch (not_->GetResultType()) {
4272 case Primitive::kPrimInt:
4273 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4274 break;
4275
4276 case Primitive::kPrimLong:
4277 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4278 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4279 break;
4280
4281 default:
4282 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4283 }
4284}
4285
Scott Wakelingc34dba72016-10-03 10:14:44 +01004286void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4287 LocationSummary* locations =
4288 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4289 locations->SetInAt(0, Location::RequiresRegister());
4290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4291}
4292
4293void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4294 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4295}
4296
Artem Serov02d37832016-10-25 15:25:33 +01004297void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4298 LocationSummary* locations =
4299 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4300 switch (compare->InputAt(0)->GetType()) {
4301 case Primitive::kPrimBoolean:
4302 case Primitive::kPrimByte:
4303 case Primitive::kPrimShort:
4304 case Primitive::kPrimChar:
4305 case Primitive::kPrimInt:
4306 case Primitive::kPrimLong: {
4307 locations->SetInAt(0, Location::RequiresRegister());
4308 locations->SetInAt(1, Location::RequiresRegister());
4309 // Output overlaps because it is written before doing the low comparison.
4310 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4311 break;
4312 }
4313 case Primitive::kPrimFloat:
4314 case Primitive::kPrimDouble: {
4315 locations->SetInAt(0, Location::RequiresFpuRegister());
4316 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4317 locations->SetOut(Location::RequiresRegister());
4318 break;
4319 }
4320 default:
4321 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4322 }
4323}
4324
4325void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4326 LocationSummary* locations = compare->GetLocations();
4327 vixl32::Register out = OutputRegister(compare);
4328 Location left = locations->InAt(0);
4329 Location right = locations->InAt(1);
4330
4331 vixl32::Label less, greater, done;
4332 Primitive::Type type = compare->InputAt(0)->GetType();
4333 vixl32::Condition less_cond = vixl32::Condition(kNone);
4334 switch (type) {
4335 case Primitive::kPrimBoolean:
4336 case Primitive::kPrimByte:
4337 case Primitive::kPrimShort:
4338 case Primitive::kPrimChar:
4339 case Primitive::kPrimInt: {
4340 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4341 __ Mov(out, 0);
4342 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4343 less_cond = lt;
4344 break;
4345 }
4346 case Primitive::kPrimLong: {
4347 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004348 __ B(lt, &less, /* far_target */ false);
4349 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004350 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4351 __ Mov(out, 0);
4352 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4353 less_cond = lo;
4354 break;
4355 }
4356 case Primitive::kPrimFloat:
4357 case Primitive::kPrimDouble: {
4358 __ Mov(out, 0);
4359 GenerateVcmp(compare);
4360 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4361 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4362 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4363 break;
4364 }
4365 default:
4366 LOG(FATAL) << "Unexpected compare type " << type;
4367 UNREACHABLE();
4368 }
4369
Artem Serov517d9f62016-12-12 15:51:15 +00004370 __ B(eq, &done, /* far_target */ false);
4371 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004372
4373 __ Bind(&greater);
4374 __ Mov(out, 1);
4375 __ B(&done);
4376
4377 __ Bind(&less);
4378 __ Mov(out, -1);
4379
4380 __ Bind(&done);
4381}
4382
4383void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4384 LocationSummary* locations =
4385 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4386 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4387 locations->SetInAt(i, Location::Any());
4388 }
4389 locations->SetOut(Location::Any());
4390}
4391
4392void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4393 LOG(FATAL) << "Unreachable";
4394}
4395
4396void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4397 // TODO (ported from quick): revisit ARM barrier kinds.
4398 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4399 switch (kind) {
4400 case MemBarrierKind::kAnyStore:
4401 case MemBarrierKind::kLoadAny:
4402 case MemBarrierKind::kAnyAny: {
4403 flavor = DmbOptions::ISH;
4404 break;
4405 }
4406 case MemBarrierKind::kStoreStore: {
4407 flavor = DmbOptions::ISHST;
4408 break;
4409 }
4410 default:
4411 LOG(FATAL) << "Unexpected memory barrier " << kind;
4412 }
4413 __ Dmb(flavor);
4414}
4415
4416void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4417 uint32_t offset,
4418 vixl32::Register out_lo,
4419 vixl32::Register out_hi) {
4420 UseScratchRegisterScope temps(GetVIXLAssembler());
4421 if (offset != 0) {
4422 vixl32::Register temp = temps.Acquire();
4423 __ Add(temp, addr, offset);
4424 addr = temp;
4425 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004426 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004427}
4428
4429void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4430 uint32_t offset,
4431 vixl32::Register value_lo,
4432 vixl32::Register value_hi,
4433 vixl32::Register temp1,
4434 vixl32::Register temp2,
4435 HInstruction* instruction) {
4436 UseScratchRegisterScope temps(GetVIXLAssembler());
4437 vixl32::Label fail;
4438 if (offset != 0) {
4439 vixl32::Register temp = temps.Acquire();
4440 __ Add(temp, addr, offset);
4441 addr = temp;
4442 }
4443 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004444 {
4445 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004446 ExactAssemblyScope aas(GetVIXLAssembler(),
4447 vixl32::kMaxInstructionSizeInBytes,
4448 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004449 // We need a load followed by store. (The address used in a STREX instruction must
4450 // be the same as the address in the most recently executed LDREX instruction.)
4451 __ ldrexd(temp1, temp2, MemOperand(addr));
4452 codegen_->MaybeRecordImplicitNullCheck(instruction);
4453 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004454 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004455 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004456}
Artem Serov02109dd2016-09-23 17:17:54 +01004457
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004458void LocationsBuilderARMVIXL::HandleFieldSet(
4459 HInstruction* instruction, const FieldInfo& field_info) {
4460 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4461
4462 LocationSummary* locations =
4463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4464 locations->SetInAt(0, Location::RequiresRegister());
4465
4466 Primitive::Type field_type = field_info.GetFieldType();
4467 if (Primitive::IsFloatingPointType(field_type)) {
4468 locations->SetInAt(1, Location::RequiresFpuRegister());
4469 } else {
4470 locations->SetInAt(1, Location::RequiresRegister());
4471 }
4472
4473 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4474 bool generate_volatile = field_info.IsVolatile()
4475 && is_wide
4476 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4477 bool needs_write_barrier =
4478 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4479 // Temporary registers for the write barrier.
4480 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4481 if (needs_write_barrier) {
4482 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4483 locations->AddTemp(Location::RequiresRegister());
4484 } else if (generate_volatile) {
4485 // ARM encoding have some additional constraints for ldrexd/strexd:
4486 // - registers need to be consecutive
4487 // - the first register should be even but not R14.
4488 // We don't test for ARM yet, and the assertion makes sure that we
4489 // revisit this if we ever enable ARM encoding.
4490 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4491
4492 locations->AddTemp(Location::RequiresRegister());
4493 locations->AddTemp(Location::RequiresRegister());
4494 if (field_type == Primitive::kPrimDouble) {
4495 // For doubles we need two more registers to copy the value.
4496 locations->AddTemp(LocationFrom(r2));
4497 locations->AddTemp(LocationFrom(r3));
4498 }
4499 }
4500}
4501
4502void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4503 const FieldInfo& field_info,
4504 bool value_can_be_null) {
4505 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4506
4507 LocationSummary* locations = instruction->GetLocations();
4508 vixl32::Register base = InputRegisterAt(instruction, 0);
4509 Location value = locations->InAt(1);
4510
4511 bool is_volatile = field_info.IsVolatile();
4512 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4513 Primitive::Type field_type = field_info.GetFieldType();
4514 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4515 bool needs_write_barrier =
4516 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4517
4518 if (is_volatile) {
4519 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4520 }
4521
4522 switch (field_type) {
4523 case Primitive::kPrimBoolean:
4524 case Primitive::kPrimByte: {
4525 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4526 break;
4527 }
4528
4529 case Primitive::kPrimShort:
4530 case Primitive::kPrimChar: {
4531 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4532 break;
4533 }
4534
4535 case Primitive::kPrimInt:
4536 case Primitive::kPrimNot: {
4537 if (kPoisonHeapReferences && needs_write_barrier) {
4538 // Note that in the case where `value` is a null reference,
4539 // we do not enter this block, as a null reference does not
4540 // need poisoning.
4541 DCHECK_EQ(field_type, Primitive::kPrimNot);
4542 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4543 __ Mov(temp, RegisterFrom(value));
4544 GetAssembler()->PoisonHeapReference(temp);
4545 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4546 } else {
4547 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4548 }
4549 break;
4550 }
4551
4552 case Primitive::kPrimLong: {
4553 if (is_volatile && !atomic_ldrd_strd) {
4554 GenerateWideAtomicStore(base,
4555 offset,
4556 LowRegisterFrom(value),
4557 HighRegisterFrom(value),
4558 RegisterFrom(locations->GetTemp(0)),
4559 RegisterFrom(locations->GetTemp(1)),
4560 instruction);
4561 } else {
4562 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4563 codegen_->MaybeRecordImplicitNullCheck(instruction);
4564 }
4565 break;
4566 }
4567
4568 case Primitive::kPrimFloat: {
4569 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4570 break;
4571 }
4572
4573 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004574 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004575 if (is_volatile && !atomic_ldrd_strd) {
4576 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4577 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4578
4579 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4580
4581 GenerateWideAtomicStore(base,
4582 offset,
4583 value_reg_lo,
4584 value_reg_hi,
4585 RegisterFrom(locations->GetTemp(2)),
4586 RegisterFrom(locations->GetTemp(3)),
4587 instruction);
4588 } else {
4589 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4590 codegen_->MaybeRecordImplicitNullCheck(instruction);
4591 }
4592 break;
4593 }
4594
4595 case Primitive::kPrimVoid:
4596 LOG(FATAL) << "Unreachable type " << field_type;
4597 UNREACHABLE();
4598 }
4599
4600 // Longs and doubles are handled in the switch.
4601 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004602 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4603 // should use a scope and the assembler to emit the store instruction to guarantee that we
4604 // record the pc at the correct position. But the `Assembler` does not automatically handle
4605 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4606 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004607 codegen_->MaybeRecordImplicitNullCheck(instruction);
4608 }
4609
4610 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4611 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4612 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4613 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4614 }
4615
4616 if (is_volatile) {
4617 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4618 }
4619}
4620
Artem Serov02d37832016-10-25 15:25:33 +01004621void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4622 const FieldInfo& field_info) {
4623 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4624
4625 bool object_field_get_with_read_barrier =
4626 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4627 LocationSummary* locations =
4628 new (GetGraph()->GetArena()) LocationSummary(instruction,
4629 object_field_get_with_read_barrier ?
4630 LocationSummary::kCallOnSlowPath :
4631 LocationSummary::kNoCall);
4632 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4633 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4634 }
4635 locations->SetInAt(0, Location::RequiresRegister());
4636
4637 bool volatile_for_double = field_info.IsVolatile()
4638 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4639 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4640 // The output overlaps in case of volatile long: we don't want the
4641 // code generated by GenerateWideAtomicLoad to overwrite the
4642 // object's location. Likewise, in the case of an object field get
4643 // with read barriers enabled, we do not want the load to overwrite
4644 // the object's location, as we need it to emit the read barrier.
4645 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4646 object_field_get_with_read_barrier;
4647
4648 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4649 locations->SetOut(Location::RequiresFpuRegister());
4650 } else {
4651 locations->SetOut(Location::RequiresRegister(),
4652 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4653 }
4654 if (volatile_for_double) {
4655 // ARM encoding have some additional constraints for ldrexd/strexd:
4656 // - registers need to be consecutive
4657 // - the first register should be even but not R14.
4658 // We don't test for ARM yet, and the assertion makes sure that we
4659 // revisit this if we ever enable ARM encoding.
4660 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4661 locations->AddTemp(Location::RequiresRegister());
4662 locations->AddTemp(Location::RequiresRegister());
4663 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4664 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004665 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004666 locations->AddTemp(Location::RequiresRegister());
4667 }
4668}
4669
4670Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4671 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4672 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4673 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4674 return Location::ConstantLocation(input->AsConstant());
4675 } else {
4676 return Location::RequiresFpuRegister();
4677 }
4678}
4679
Artem Serov02109dd2016-09-23 17:17:54 +01004680Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4681 Opcode opcode) {
4682 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4683 if (constant->IsConstant() &&
4684 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4685 return Location::ConstantLocation(constant->AsConstant());
4686 }
4687 return Location::RequiresRegister();
4688}
4689
4690bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4691 Opcode opcode) {
4692 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4693 if (Primitive::Is64BitType(input_cst->GetType())) {
4694 Opcode high_opcode = opcode;
4695 SetCc low_set_cc = kCcDontCare;
4696 switch (opcode) {
4697 case SUB:
4698 // Flip the operation to an ADD.
4699 value = -value;
4700 opcode = ADD;
4701 FALLTHROUGH_INTENDED;
4702 case ADD:
4703 if (Low32Bits(value) == 0u) {
4704 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4705 }
4706 high_opcode = ADC;
4707 low_set_cc = kCcSet;
4708 break;
4709 default:
4710 break;
4711 }
4712 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4713 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4714 } else {
4715 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4716 }
4717}
4718
4719// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4720// enabled.
4721bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4722 Opcode opcode,
4723 SetCc set_cc) {
4724 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4725 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4726 return true;
4727 }
4728 Opcode neg_opcode = kNoOperand;
4729 switch (opcode) {
4730 case AND: neg_opcode = BIC; value = ~value; break;
4731 case ORR: neg_opcode = ORN; value = ~value; break;
4732 case ADD: neg_opcode = SUB; value = -value; break;
4733 case ADC: neg_opcode = SBC; value = ~value; break;
4734 case SUB: neg_opcode = ADD; value = -value; break;
4735 case SBC: neg_opcode = ADC; value = ~value; break;
4736 default:
4737 return false;
4738 }
4739 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4740}
4741
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004742void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4743 const FieldInfo& field_info) {
4744 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4745
4746 LocationSummary* locations = instruction->GetLocations();
4747 vixl32::Register base = InputRegisterAt(instruction, 0);
4748 Location out = locations->Out();
4749 bool is_volatile = field_info.IsVolatile();
4750 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4751 Primitive::Type field_type = field_info.GetFieldType();
4752 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4753
4754 switch (field_type) {
4755 case Primitive::kPrimBoolean:
4756 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4757 break;
4758
4759 case Primitive::kPrimByte:
4760 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4761 break;
4762
4763 case Primitive::kPrimShort:
4764 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4765 break;
4766
4767 case Primitive::kPrimChar:
4768 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4769 break;
4770
4771 case Primitive::kPrimInt:
4772 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4773 break;
4774
4775 case Primitive::kPrimNot: {
4776 // /* HeapReference<Object> */ out = *(base + offset)
4777 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004778 Location temp_loc = locations->GetTemp(0);
4779 // Note that a potential implicit null check is handled in this
4780 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4781 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4782 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4783 if (is_volatile) {
4784 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4785 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004786 } else {
4787 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004788 codegen_->MaybeRecordImplicitNullCheck(instruction);
4789 if (is_volatile) {
4790 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4791 }
4792 // If read barriers are enabled, emit read barriers other than
4793 // Baker's using a slow path (and also unpoison the loaded
4794 // reference, if heap poisoning is enabled).
4795 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4796 }
4797 break;
4798 }
4799
4800 case Primitive::kPrimLong:
4801 if (is_volatile && !atomic_ldrd_strd) {
4802 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4803 } else {
4804 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4805 }
4806 break;
4807
4808 case Primitive::kPrimFloat:
4809 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4810 break;
4811
4812 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004813 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004814 if (is_volatile && !atomic_ldrd_strd) {
4815 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4816 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4817 GenerateWideAtomicLoad(base, offset, lo, hi);
4818 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4819 // scope.
4820 codegen_->MaybeRecordImplicitNullCheck(instruction);
4821 __ Vmov(out_dreg, lo, hi);
4822 } else {
4823 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 }
4826 break;
4827 }
4828
4829 case Primitive::kPrimVoid:
4830 LOG(FATAL) << "Unreachable type " << field_type;
4831 UNREACHABLE();
4832 }
4833
4834 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4835 // Potential implicit null checks, in the case of reference or
4836 // double fields, are handled in the previous switch statement.
4837 } else {
4838 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004839 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4840 // should use a scope and the assembler to emit the load instruction to guarantee that we
4841 // record the pc at the correct position. But the `Assembler` does not automatically handle
4842 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4843 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004844 codegen_->MaybeRecordImplicitNullCheck(instruction);
4845 }
4846
4847 if (is_volatile) {
4848 if (field_type == Primitive::kPrimNot) {
4849 // Memory barriers, in the case of references, are also handled
4850 // in the previous switch statement.
4851 } else {
4852 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4853 }
4854 }
4855}
4856
4857void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4858 HandleFieldSet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4862 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4863}
4864
4865void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4866 HandleFieldGet(instruction, instruction->GetFieldInfo());
4867}
4868
4869void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4870 HandleFieldGet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4874 HandleFieldGet(instruction, instruction->GetFieldInfo());
4875}
4876
4877void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4878 HandleFieldGet(instruction, instruction->GetFieldInfo());
4879}
4880
Scott Wakelingc34dba72016-10-03 10:14:44 +01004881void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4882 HandleFieldSet(instruction, instruction->GetFieldInfo());
4883}
4884
4885void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4886 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4887}
4888
Artem Serovcfbe9132016-10-14 15:58:56 +01004889void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4890 HUnresolvedInstanceFieldGet* instruction) {
4891 FieldAccessCallingConventionARMVIXL calling_convention;
4892 codegen_->CreateUnresolvedFieldLocationSummary(
4893 instruction, instruction->GetFieldType(), calling_convention);
4894}
4895
4896void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4897 HUnresolvedInstanceFieldGet* instruction) {
4898 FieldAccessCallingConventionARMVIXL calling_convention;
4899 codegen_->GenerateUnresolvedFieldAccess(instruction,
4900 instruction->GetFieldType(),
4901 instruction->GetFieldIndex(),
4902 instruction->GetDexPc(),
4903 calling_convention);
4904}
4905
4906void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4907 HUnresolvedInstanceFieldSet* instruction) {
4908 FieldAccessCallingConventionARMVIXL calling_convention;
4909 codegen_->CreateUnresolvedFieldLocationSummary(
4910 instruction, instruction->GetFieldType(), calling_convention);
4911}
4912
4913void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4914 HUnresolvedInstanceFieldSet* instruction) {
4915 FieldAccessCallingConventionARMVIXL calling_convention;
4916 codegen_->GenerateUnresolvedFieldAccess(instruction,
4917 instruction->GetFieldType(),
4918 instruction->GetFieldIndex(),
4919 instruction->GetDexPc(),
4920 calling_convention);
4921}
4922
4923void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4924 HUnresolvedStaticFieldGet* instruction) {
4925 FieldAccessCallingConventionARMVIXL calling_convention;
4926 codegen_->CreateUnresolvedFieldLocationSummary(
4927 instruction, instruction->GetFieldType(), calling_convention);
4928}
4929
4930void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4931 HUnresolvedStaticFieldGet* instruction) {
4932 FieldAccessCallingConventionARMVIXL calling_convention;
4933 codegen_->GenerateUnresolvedFieldAccess(instruction,
4934 instruction->GetFieldType(),
4935 instruction->GetFieldIndex(),
4936 instruction->GetDexPc(),
4937 calling_convention);
4938}
4939
4940void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4941 HUnresolvedStaticFieldSet* instruction) {
4942 FieldAccessCallingConventionARMVIXL calling_convention;
4943 codegen_->CreateUnresolvedFieldLocationSummary(
4944 instruction, instruction->GetFieldType(), calling_convention);
4945}
4946
4947void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4948 HUnresolvedStaticFieldSet* instruction) {
4949 FieldAccessCallingConventionARMVIXL calling_convention;
4950 codegen_->GenerateUnresolvedFieldAccess(instruction,
4951 instruction->GetFieldType(),
4952 instruction->GetFieldIndex(),
4953 instruction->GetDexPc(),
4954 calling_convention);
4955}
4956
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004957void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004958 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004959 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004960}
4961
4962void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4963 if (CanMoveNullCheckToUser(instruction)) {
4964 return;
4965 }
4966
4967 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004968 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004969 ExactAssemblyScope aas(GetVIXLAssembler(),
4970 vixl32::kMaxInstructionSizeInBytes,
4971 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004972 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4973 RecordPcInfo(instruction, instruction->GetDexPc());
4974}
4975
4976void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4977 NullCheckSlowPathARMVIXL* slow_path =
4978 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4979 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004980 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004981}
4982
4983void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4984 codegen_->GenerateNullCheck(instruction);
4985}
4986
Scott Wakelingc34dba72016-10-03 10:14:44 +01004987static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4988 switch (type) {
4989 case Primitive::kPrimNot:
4990 return kLoadWord;
4991 case Primitive::kPrimBoolean:
4992 return kLoadUnsignedByte;
4993 case Primitive::kPrimByte:
4994 return kLoadSignedByte;
4995 case Primitive::kPrimChar:
4996 return kLoadUnsignedHalfword;
4997 case Primitive::kPrimShort:
4998 return kLoadSignedHalfword;
4999 case Primitive::kPrimInt:
5000 return kLoadWord;
5001 case Primitive::kPrimLong:
5002 return kLoadWordPair;
5003 case Primitive::kPrimFloat:
5004 return kLoadSWord;
5005 case Primitive::kPrimDouble:
5006 return kLoadDWord;
5007 default:
5008 LOG(FATAL) << "Unreachable type " << type;
5009 UNREACHABLE();
5010 }
5011}
5012
5013static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5014 switch (type) {
5015 case Primitive::kPrimNot:
5016 return kStoreWord;
5017 case Primitive::kPrimBoolean:
5018 case Primitive::kPrimByte:
5019 return kStoreByte;
5020 case Primitive::kPrimChar:
5021 case Primitive::kPrimShort:
5022 return kStoreHalfword;
5023 case Primitive::kPrimInt:
5024 return kStoreWord;
5025 case Primitive::kPrimLong:
5026 return kStoreWordPair;
5027 case Primitive::kPrimFloat:
5028 return kStoreSWord;
5029 case Primitive::kPrimDouble:
5030 return kStoreDWord;
5031 default:
5032 LOG(FATAL) << "Unreachable type " << type;
5033 UNREACHABLE();
5034 }
5035}
5036
5037void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5038 Location out_loc,
5039 vixl32::Register base,
5040 vixl32::Register reg_index,
5041 vixl32::Condition cond) {
5042 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5043 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5044
5045 switch (type) {
5046 case Primitive::kPrimByte:
5047 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5048 break;
5049 case Primitive::kPrimBoolean:
5050 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5051 break;
5052 case Primitive::kPrimShort:
5053 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5054 break;
5055 case Primitive::kPrimChar:
5056 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5057 break;
5058 case Primitive::kPrimNot:
5059 case Primitive::kPrimInt:
5060 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5061 break;
5062 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5063 case Primitive::kPrimLong:
5064 case Primitive::kPrimFloat:
5065 case Primitive::kPrimDouble:
5066 default:
5067 LOG(FATAL) << "Unreachable type " << type;
5068 UNREACHABLE();
5069 }
5070}
5071
5072void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5073 Location loc,
5074 vixl32::Register base,
5075 vixl32::Register reg_index,
5076 vixl32::Condition cond) {
5077 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5078 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5079
5080 switch (type) {
5081 case Primitive::kPrimByte:
5082 case Primitive::kPrimBoolean:
5083 __ Strb(cond, RegisterFrom(loc), mem_address);
5084 break;
5085 case Primitive::kPrimShort:
5086 case Primitive::kPrimChar:
5087 __ Strh(cond, RegisterFrom(loc), mem_address);
5088 break;
5089 case Primitive::kPrimNot:
5090 case Primitive::kPrimInt:
5091 __ Str(cond, RegisterFrom(loc), mem_address);
5092 break;
5093 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5094 case Primitive::kPrimLong:
5095 case Primitive::kPrimFloat:
5096 case Primitive::kPrimDouble:
5097 default:
5098 LOG(FATAL) << "Unreachable type " << type;
5099 UNREACHABLE();
5100 }
5101}
5102
5103void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5104 bool object_array_get_with_read_barrier =
5105 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5106 LocationSummary* locations =
5107 new (GetGraph()->GetArena()) LocationSummary(instruction,
5108 object_array_get_with_read_barrier ?
5109 LocationSummary::kCallOnSlowPath :
5110 LocationSummary::kNoCall);
5111 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005112 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005113 }
5114 locations->SetInAt(0, Location::RequiresRegister());
5115 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5116 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5118 } else {
5119 // The output overlaps in the case of an object array get with
5120 // read barriers enabled: we do not want the move to overwrite the
5121 // array's location, as we need it to emit the read barrier.
5122 locations->SetOut(
5123 Location::RequiresRegister(),
5124 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5125 }
5126 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005127 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005128 // Also need for String compression feature.
5129 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5130 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005131 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005132 }
5133}
5134
5135void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005136 LocationSummary* locations = instruction->GetLocations();
5137 Location obj_loc = locations->InAt(0);
5138 vixl32::Register obj = InputRegisterAt(instruction, 0);
5139 Location index = locations->InAt(1);
5140 Location out_loc = locations->Out();
5141 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5142 Primitive::Type type = instruction->GetType();
5143 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5144 instruction->IsStringCharAt();
5145 HInstruction* array_instr = instruction->GetArray();
5146 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005147
5148 switch (type) {
5149 case Primitive::kPrimBoolean:
5150 case Primitive::kPrimByte:
5151 case Primitive::kPrimShort:
5152 case Primitive::kPrimChar:
5153 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005154 vixl32::Register length;
5155 if (maybe_compressed_char_at) {
5156 length = RegisterFrom(locations->GetTemp(0));
5157 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5158 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5159 codegen_->MaybeRecordImplicitNullCheck(instruction);
5160 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005161 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005162 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005163 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005164 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005165 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5166 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5167 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005168 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005169 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5170 RegisterFrom(out_loc),
5171 obj,
5172 data_offset + const_index);
5173 __ B(&done);
5174 __ Bind(&uncompressed_load);
5175 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5176 RegisterFrom(out_loc),
5177 obj,
5178 data_offset + (const_index << 1));
5179 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005180 } else {
5181 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5182
5183 LoadOperandType load_type = GetLoadOperandType(type);
5184 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5185 }
5186 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005187 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005188 vixl32::Register temp = temps.Acquire();
5189
5190 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005191 // We do not need to compute the intermediate address from the array: the
5192 // input instruction has done it already. See the comment in
5193 // `TryExtractArrayAccessAddress()`.
5194 if (kIsDebugBuild) {
5195 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005196 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005197 }
5198 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005199 } else {
5200 __ Add(temp, obj, data_offset);
5201 }
5202 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005203 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005204 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5205 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5206 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005207 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005208 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
5209 __ B(&done);
5210 __ Bind(&uncompressed_load);
5211 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
5212 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005213 } else {
5214 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5215 }
5216 }
5217 break;
5218 }
5219
5220 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005221 // The read barrier instrumentation of object ArrayGet
5222 // instructions does not support the HIntermediateAddress
5223 // instruction.
5224 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5225
Scott Wakelingc34dba72016-10-03 10:14:44 +01005226 static_assert(
5227 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5228 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5229 // /* HeapReference<Object> */ out =
5230 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5231 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005232 Location temp = locations->GetTemp(0);
5233 // Note that a potential implicit null check is handled in this
5234 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5235 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5236 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005237 } else {
5238 vixl32::Register out = OutputRegister(instruction);
5239 if (index.IsConstant()) {
5240 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005241 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005242 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005243 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5244 // we should use a scope and the assembler to emit the load instruction to guarantee that
5245 // we record the pc at the correct position. But the `Assembler` does not automatically
5246 // handle unencodable offsets. Practically, everything is fine because the helper and
5247 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005248 codegen_->MaybeRecordImplicitNullCheck(instruction);
5249 // If read barriers are enabled, emit read barriers other than
5250 // Baker's using a slow path (and also unpoison the loaded
5251 // reference, if heap poisoning is enabled).
5252 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5253 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005254 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005255 vixl32::Register temp = temps.Acquire();
5256
5257 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005258 // We do not need to compute the intermediate address from the array: the
5259 // input instruction has done it already. See the comment in
5260 // `TryExtractArrayAccessAddress()`.
5261 if (kIsDebugBuild) {
5262 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005263 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005264 }
5265 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005266 } else {
5267 __ Add(temp, obj, data_offset);
5268 }
5269 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005270 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005271 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5272 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5273 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005274 codegen_->MaybeRecordImplicitNullCheck(instruction);
5275 // If read barriers are enabled, emit read barriers other than
5276 // Baker's using a slow path (and also unpoison the loaded
5277 // reference, if heap poisoning is enabled).
5278 codegen_->MaybeGenerateReadBarrierSlow(
5279 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5280 }
5281 }
5282 break;
5283 }
5284
5285 case Primitive::kPrimLong: {
5286 if (index.IsConstant()) {
5287 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005288 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005289 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5290 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005291 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005292 vixl32::Register temp = temps.Acquire();
5293 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5294 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5295 }
5296 break;
5297 }
5298
5299 case Primitive::kPrimFloat: {
5300 vixl32::SRegister out = SRegisterFrom(out_loc);
5301 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005302 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005303 GetAssembler()->LoadSFromOffset(out, obj, offset);
5304 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005305 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005306 vixl32::Register temp = temps.Acquire();
5307 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5308 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5309 }
5310 break;
5311 }
5312
5313 case Primitive::kPrimDouble: {
5314 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005315 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005316 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5317 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005318 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005319 vixl32::Register temp = temps.Acquire();
5320 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5321 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5322 }
5323 break;
5324 }
5325
5326 case Primitive::kPrimVoid:
5327 LOG(FATAL) << "Unreachable type " << type;
5328 UNREACHABLE();
5329 }
5330
5331 if (type == Primitive::kPrimNot) {
5332 // Potential implicit null checks, in the case of reference
5333 // arrays, are handled in the previous switch statement.
5334 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005335 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5336 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005337 codegen_->MaybeRecordImplicitNullCheck(instruction);
5338 }
5339}
5340
5341void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5342 Primitive::Type value_type = instruction->GetComponentType();
5343
5344 bool needs_write_barrier =
5345 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5346 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5347
5348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5349 instruction,
5350 may_need_runtime_call_for_type_check ?
5351 LocationSummary::kCallOnSlowPath :
5352 LocationSummary::kNoCall);
5353
5354 locations->SetInAt(0, Location::RequiresRegister());
5355 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5356 if (Primitive::IsFloatingPointType(value_type)) {
5357 locations->SetInAt(2, Location::RequiresFpuRegister());
5358 } else {
5359 locations->SetInAt(2, Location::RequiresRegister());
5360 }
5361 if (needs_write_barrier) {
5362 // Temporary registers for the write barrier.
5363 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5364 locations->AddTemp(Location::RequiresRegister());
5365 }
5366}
5367
5368void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005369 LocationSummary* locations = instruction->GetLocations();
5370 vixl32::Register array = InputRegisterAt(instruction, 0);
5371 Location index = locations->InAt(1);
5372 Primitive::Type value_type = instruction->GetComponentType();
5373 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5374 bool needs_write_barrier =
5375 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5376 uint32_t data_offset =
5377 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5378 Location value_loc = locations->InAt(2);
5379 HInstruction* array_instr = instruction->GetArray();
5380 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005381
5382 switch (value_type) {
5383 case Primitive::kPrimBoolean:
5384 case Primitive::kPrimByte:
5385 case Primitive::kPrimShort:
5386 case Primitive::kPrimChar:
5387 case Primitive::kPrimInt: {
5388 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005389 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005390 uint32_t full_offset =
5391 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5392 StoreOperandType store_type = GetStoreOperandType(value_type);
5393 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5394 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005395 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005396 vixl32::Register temp = temps.Acquire();
5397
5398 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005399 // We do not need to compute the intermediate address from the array: the
5400 // input instruction has done it already. See the comment in
5401 // `TryExtractArrayAccessAddress()`.
5402 if (kIsDebugBuild) {
5403 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005404 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005405 }
5406 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005407 } else {
5408 __ Add(temp, array, data_offset);
5409 }
5410 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5411 }
5412 break;
5413 }
5414
5415 case Primitive::kPrimNot: {
5416 vixl32::Register value = RegisterFrom(value_loc);
5417 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5418 // See the comment in instruction_simplifier_shared.cc.
5419 DCHECK(!has_intermediate_address);
5420
5421 if (instruction->InputAt(2)->IsNullConstant()) {
5422 // Just setting null.
5423 if (index.IsConstant()) {
5424 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005425 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005426 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5427 } else {
5428 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005429 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005430 vixl32::Register temp = temps.Acquire();
5431 __ Add(temp, array, data_offset);
5432 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5433 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005434 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5435 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005436 codegen_->MaybeRecordImplicitNullCheck(instruction);
5437 DCHECK(!needs_write_barrier);
5438 DCHECK(!may_need_runtime_call_for_type_check);
5439 break;
5440 }
5441
5442 DCHECK(needs_write_barrier);
5443 Location temp1_loc = locations->GetTemp(0);
5444 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5445 Location temp2_loc = locations->GetTemp(1);
5446 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5447 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5448 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5449 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5450 vixl32::Label done;
5451 SlowPathCodeARMVIXL* slow_path = nullptr;
5452
5453 if (may_need_runtime_call_for_type_check) {
5454 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5455 codegen_->AddSlowPath(slow_path);
5456 if (instruction->GetValueCanBeNull()) {
5457 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005458 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005459 if (index.IsConstant()) {
5460 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005461 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005462 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5463 } else {
5464 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005465 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005466 vixl32::Register temp = temps.Acquire();
5467 __ Add(temp, array, data_offset);
5468 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5469 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005470 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5471 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005472 codegen_->MaybeRecordImplicitNullCheck(instruction);
5473 __ B(&done);
5474 __ Bind(&non_zero);
5475 }
5476
5477 // Note that when read barriers are enabled, the type checks
5478 // are performed without read barriers. This is fine, even in
5479 // the case where a class object is in the from-space after
5480 // the flip, as a comparison involving such a type would not
5481 // produce a false positive; it may of course produce a false
5482 // negative, in which case we would take the ArraySet slow
5483 // path.
5484
Alexandre Rames374ddf32016-11-04 10:40:49 +00005485 {
5486 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005487 ExactAssemblyScope aas(GetVIXLAssembler(),
5488 vixl32::kMaxInstructionSizeInBytes,
5489 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005490 // /* HeapReference<Class> */ temp1 = array->klass_
5491 __ ldr(temp1, MemOperand(array, class_offset));
5492 codegen_->MaybeRecordImplicitNullCheck(instruction);
5493 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005494 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5495
5496 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5497 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5498 // /* HeapReference<Class> */ temp2 = value->klass_
5499 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5500 // If heap poisoning is enabled, no need to unpoison `temp1`
5501 // nor `temp2`, as we are comparing two poisoned references.
5502 __ Cmp(temp1, temp2);
5503
5504 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5505 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00005506 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005507 // If heap poisoning is enabled, the `temp1` reference has
5508 // not been unpoisoned yet; unpoison it now.
5509 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5510
5511 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5512 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5513 // If heap poisoning is enabled, no need to unpoison
5514 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005515 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005516 __ Bind(&do_put);
5517 } else {
5518 __ B(ne, slow_path->GetEntryLabel());
5519 }
5520 }
5521
5522 vixl32::Register source = value;
5523 if (kPoisonHeapReferences) {
5524 // Note that in the case where `value` is a null reference,
5525 // we do not enter this block, as a null reference does not
5526 // need poisoning.
5527 DCHECK_EQ(value_type, Primitive::kPrimNot);
5528 __ Mov(temp1, value);
5529 GetAssembler()->PoisonHeapReference(temp1);
5530 source = temp1;
5531 }
5532
5533 if (index.IsConstant()) {
5534 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005535 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005536 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5537 } else {
5538 DCHECK(index.IsRegister()) << index;
5539
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005540 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005541 vixl32::Register temp = temps.Acquire();
5542 __ Add(temp, array, data_offset);
5543 codegen_->StoreToShiftedRegOffset(value_type,
5544 LocationFrom(source),
5545 temp,
5546 RegisterFrom(index));
5547 }
5548
5549 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005550 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5551 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005552 codegen_->MaybeRecordImplicitNullCheck(instruction);
5553 }
5554
5555 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5556
5557 if (done.IsReferenced()) {
5558 __ Bind(&done);
5559 }
5560
5561 if (slow_path != nullptr) {
5562 __ Bind(slow_path->GetExitLabel());
5563 }
5564
5565 break;
5566 }
5567
5568 case Primitive::kPrimLong: {
5569 Location value = locations->InAt(2);
5570 if (index.IsConstant()) {
5571 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005572 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005573 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5574 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005575 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005576 vixl32::Register temp = temps.Acquire();
5577 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5578 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5579 }
5580 break;
5581 }
5582
5583 case Primitive::kPrimFloat: {
5584 Location value = locations->InAt(2);
5585 DCHECK(value.IsFpuRegister());
5586 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005587 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005588 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5589 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005590 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005591 vixl32::Register temp = temps.Acquire();
5592 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5593 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5594 }
5595 break;
5596 }
5597
5598 case Primitive::kPrimDouble: {
5599 Location value = locations->InAt(2);
5600 DCHECK(value.IsFpuRegisterPair());
5601 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005602 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005603 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5604 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005605 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005606 vixl32::Register temp = temps.Acquire();
5607 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5608 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5609 }
5610 break;
5611 }
5612
5613 case Primitive::kPrimVoid:
5614 LOG(FATAL) << "Unreachable type " << value_type;
5615 UNREACHABLE();
5616 }
5617
5618 // Objects are handled in the switch.
5619 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005620 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5621 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005622 codegen_->MaybeRecordImplicitNullCheck(instruction);
5623 }
5624}
5625
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005626void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5627 LocationSummary* locations =
5628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5629 locations->SetInAt(0, Location::RequiresRegister());
5630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5631}
5632
5633void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5634 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5635 vixl32::Register obj = InputRegisterAt(instruction, 0);
5636 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005637 {
Artem Serov0fb37192016-12-06 18:13:40 +00005638 ExactAssemblyScope aas(GetVIXLAssembler(),
5639 vixl32::kMaxInstructionSizeInBytes,
5640 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005641 __ ldr(out, MemOperand(obj, offset));
5642 codegen_->MaybeRecordImplicitNullCheck(instruction);
5643 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005644 // Mask out compression flag from String's array length.
5645 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005646 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005647 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005648}
5649
Artem Serov2bbc9532016-10-21 11:51:50 +01005650void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005651 LocationSummary* locations =
5652 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5653
5654 locations->SetInAt(0, Location::RequiresRegister());
5655 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5657}
5658
5659void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5660 vixl32::Register out = OutputRegister(instruction);
5661 vixl32::Register first = InputRegisterAt(instruction, 0);
5662 Location second = instruction->GetLocations()->InAt(1);
5663
Artem Serov2bbc9532016-10-21 11:51:50 +01005664 if (second.IsRegister()) {
5665 __ Add(out, first, RegisterFrom(second));
5666 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005667 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01005668 }
5669}
5670
Scott Wakelingc34dba72016-10-03 10:14:44 +01005671void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5672 RegisterSet caller_saves = RegisterSet::Empty();
5673 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5674 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5675 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5676 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5677 locations->SetInAt(0, Location::RequiresRegister());
5678 locations->SetInAt(1, Location::RequiresRegister());
5679}
5680
5681void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5682 SlowPathCodeARMVIXL* slow_path =
5683 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5684 codegen_->AddSlowPath(slow_path);
5685
5686 vixl32::Register index = InputRegisterAt(instruction, 0);
5687 vixl32::Register length = InputRegisterAt(instruction, 1);
5688
5689 __ Cmp(index, length);
5690 __ B(hs, slow_path->GetEntryLabel());
5691}
5692
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005693void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5694 vixl32::Register card,
5695 vixl32::Register object,
5696 vixl32::Register value,
5697 bool can_be_null) {
5698 vixl32::Label is_null;
5699 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005700 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005701 }
5702 GetAssembler()->LoadFromOffset(
5703 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005704 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005705 __ Strb(card, MemOperand(card, temp));
5706 if (can_be_null) {
5707 __ Bind(&is_null);
5708 }
5709}
5710
Scott Wakelingfe885462016-09-22 10:24:38 +01005711void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5712 LOG(FATAL) << "Unreachable";
5713}
5714
5715void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5716 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5717}
5718
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005719void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005720 LocationSummary* locations =
5721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5722 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005723}
5724
5725void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5726 HBasicBlock* block = instruction->GetBlock();
5727 if (block->GetLoopInformation() != nullptr) {
5728 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5729 // The back edge will generate the suspend check.
5730 return;
5731 }
5732 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5733 // The goto will generate the suspend check.
5734 return;
5735 }
5736 GenerateSuspendCheck(instruction, nullptr);
5737}
5738
5739void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5740 HBasicBlock* successor) {
5741 SuspendCheckSlowPathARMVIXL* slow_path =
5742 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5743 if (slow_path == nullptr) {
5744 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5745 instruction->SetSlowPath(slow_path);
5746 codegen_->AddSlowPath(slow_path);
5747 if (successor != nullptr) {
5748 DCHECK(successor->IsLoopHeader());
5749 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5750 }
5751 } else {
5752 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5753 }
5754
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005755 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005756 vixl32::Register temp = temps.Acquire();
5757 GetAssembler()->LoadFromOffset(
5758 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5759 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005760 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 __ Bind(slow_path->GetReturnLabel());
5762 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005763 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005764 __ B(slow_path->GetEntryLabel());
5765 }
5766}
5767
Scott Wakelingfe885462016-09-22 10:24:38 +01005768ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5769 return codegen_->GetAssembler();
5770}
5771
5772void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005773 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005774 MoveOperands* move = moves_[index];
5775 Location source = move->GetSource();
5776 Location destination = move->GetDestination();
5777
5778 if (source.IsRegister()) {
5779 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005780 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005781 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005782 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005783 } else {
5784 DCHECK(destination.IsStackSlot());
5785 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005786 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005787 sp,
5788 destination.GetStackIndex());
5789 }
5790 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005791 if (destination.IsRegister()) {
5792 GetAssembler()->LoadFromOffset(kLoadWord,
5793 RegisterFrom(destination),
5794 sp,
5795 source.GetStackIndex());
5796 } else if (destination.IsFpuRegister()) {
5797 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5798 } else {
5799 DCHECK(destination.IsStackSlot());
5800 vixl32::Register temp = temps.Acquire();
5801 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5802 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5803 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005804 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005805 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005806 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005807 } else if (destination.IsFpuRegister()) {
5808 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5809 } else {
5810 DCHECK(destination.IsStackSlot());
5811 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5812 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005813 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005814 if (destination.IsDoubleStackSlot()) {
5815 vixl32::DRegister temp = temps.AcquireD();
5816 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5817 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5818 } else if (destination.IsRegisterPair()) {
5819 DCHECK(ExpectedPairLayout(destination));
5820 GetAssembler()->LoadFromOffset(
5821 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5822 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005823 DCHECK(destination.IsFpuRegisterPair()) << destination;
5824 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005825 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005826 } else if (source.IsRegisterPair()) {
5827 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005828 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5829 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005830 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005831 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005832 } else {
5833 DCHECK(destination.IsDoubleStackSlot()) << destination;
5834 DCHECK(ExpectedPairLayout(source));
5835 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005836 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005837 sp,
5838 destination.GetStackIndex());
5839 }
5840 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005841 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005842 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005843 } else if (destination.IsFpuRegisterPair()) {
5844 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5845 } else {
5846 DCHECK(destination.IsDoubleStackSlot()) << destination;
5847 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5848 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005849 } else {
5850 DCHECK(source.IsConstant()) << source;
5851 HConstant* constant = source.GetConstant();
5852 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5853 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5854 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005855 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005856 } else {
5857 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005858 vixl32::Register temp = temps.Acquire();
5859 __ Mov(temp, value);
5860 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5861 }
5862 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005863 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01005864 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005865 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5866 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005867 } else {
5868 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005869 vixl32::Register temp = temps.Acquire();
5870 __ Mov(temp, Low32Bits(value));
5871 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5872 __ Mov(temp, High32Bits(value));
5873 GetAssembler()->StoreToOffset(kStoreWord,
5874 temp,
5875 sp,
5876 destination.GetHighStackIndex(kArmWordSize));
5877 }
5878 } else if (constant->IsDoubleConstant()) {
5879 double value = constant->AsDoubleConstant()->GetValue();
5880 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005881 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005882 } else {
5883 DCHECK(destination.IsDoubleStackSlot()) << destination;
5884 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005885 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005886 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005887 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005888 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005889 GetAssembler()->StoreToOffset(kStoreWord,
5890 temp,
5891 sp,
5892 destination.GetHighStackIndex(kArmWordSize));
5893 }
5894 } else {
5895 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5896 float value = constant->AsFloatConstant()->GetValue();
5897 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005898 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005899 } else {
5900 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005901 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005902 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005903 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5904 }
5905 }
5906 }
5907}
5908
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005909void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5910 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5911 vixl32::Register temp = temps.Acquire();
5912 __ Mov(temp, reg);
5913 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5914 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005915}
5916
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005917void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5918 // TODO(VIXL32): Double check the performance of this implementation.
5919 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Artem Serov4593f7d2016-12-29 16:21:49 +00005920 vixl32::SRegister temp_1 = temps.AcquireS();
5921 vixl32::SRegister temp_2 = temps.AcquireS();
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005922
Artem Serov4593f7d2016-12-29 16:21:49 +00005923 __ Vldr(temp_1, MemOperand(sp, mem1));
5924 __ Vldr(temp_2, MemOperand(sp, mem2));
5925 __ Vstr(temp_1, MemOperand(sp, mem2));
5926 __ Vstr(temp_2, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005927}
5928
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005929void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5930 MoveOperands* move = moves_[index];
5931 Location source = move->GetSource();
5932 Location destination = move->GetDestination();
5933 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5934
5935 if (source.IsRegister() && destination.IsRegister()) {
5936 vixl32::Register temp = temps.Acquire();
5937 DCHECK(!RegisterFrom(source).Is(temp));
5938 DCHECK(!RegisterFrom(destination).Is(temp));
5939 __ Mov(temp, RegisterFrom(destination));
5940 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5941 __ Mov(RegisterFrom(source), temp);
5942 } else if (source.IsRegister() && destination.IsStackSlot()) {
5943 Exchange(RegisterFrom(source), destination.GetStackIndex());
5944 } else if (source.IsStackSlot() && destination.IsRegister()) {
5945 Exchange(RegisterFrom(destination), source.GetStackIndex());
5946 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005947 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005948 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005949 vixl32::SRegister temp = temps.AcquireS();
5950 __ Vmov(temp, SRegisterFrom(source));
5951 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5952 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005953 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5954 vixl32::DRegister temp = temps.AcquireD();
5955 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5956 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5957 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5958 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5959 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5960 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5961 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5962 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5963 vixl32::DRegister temp = temps.AcquireD();
5964 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5965 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5966 GetAssembler()->StoreDToOffset(temp, sp, mem);
5967 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005968 vixl32::DRegister first = DRegisterFrom(source);
5969 vixl32::DRegister second = DRegisterFrom(destination);
5970 vixl32::DRegister temp = temps.AcquireD();
5971 __ Vmov(temp, first);
5972 __ Vmov(first, second);
5973 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005974 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005975 vixl32::DRegister reg = source.IsFpuRegisterPair()
5976 ? DRegisterFrom(source)
5977 : DRegisterFrom(destination);
5978 int mem = source.IsFpuRegisterPair()
5979 ? destination.GetStackIndex()
5980 : source.GetStackIndex();
5981 vixl32::DRegister temp = temps.AcquireD();
5982 __ Vmov(temp, reg);
5983 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5984 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005985 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005986 vixl32::SRegister reg = source.IsFpuRegister()
5987 ? SRegisterFrom(source)
5988 : SRegisterFrom(destination);
5989 int mem = source.IsFpuRegister()
5990 ? destination.GetStackIndex()
5991 : source.GetStackIndex();
5992 vixl32::Register temp = temps.Acquire();
5993 __ Vmov(temp, reg);
5994 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5995 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005996 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5997 vixl32::DRegister temp1 = temps.AcquireD();
5998 vixl32::DRegister temp2 = temps.AcquireD();
5999 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6000 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6001 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6002 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6003 } else {
6004 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6005 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006006}
6007
6008void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
6009 TODO_VIXL32(FATAL);
6010}
6011
6012void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
6013 TODO_VIXL32(FATAL);
6014}
6015
Artem Serov02d37832016-10-25 15:25:33 +01006016HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006017 HLoadClass::LoadKind desired_class_load_kind) {
6018 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006019 case HLoadClass::LoadKind::kInvalid:
6020 LOG(FATAL) << "UNREACHABLE";
6021 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006022 case HLoadClass::LoadKind::kReferrersClass:
6023 break;
6024 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006025 DCHECK(!GetCompilerOptions().GetCompilePic());
6026 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006027 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6028 DCHECK(GetCompilerOptions().GetCompilePic());
6029 break;
6030 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006031 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006032 case HLoadClass::LoadKind::kBssEntry:
6033 DCHECK(!Runtime::Current()->UseJitCompilation());
6034 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006035 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006036 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006037 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006038 case HLoadClass::LoadKind::kDexCacheViaMethod:
6039 break;
6040 }
6041 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006042}
6043
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006044void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006045 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6046 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006047 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006048 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006049 cls,
6050 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006051 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006052 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006053 return;
6054 }
Vladimir Marko41559982017-01-06 14:04:23 +00006055 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006056
Artem Serovd4cc5b22016-11-04 11:19:09 +00006057 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6058 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006059 ? LocationSummary::kCallOnSlowPath
6060 : LocationSummary::kNoCall;
6061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006062 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006063 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006064 }
6065
Vladimir Marko41559982017-01-06 14:04:23 +00006066 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006067 locations->SetInAt(0, Location::RequiresRegister());
6068 }
6069 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006070 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6071 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6072 // Rely on the type resolution or initialization and marking to save everything we need.
6073 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6074 // to the custom calling convention) or by marking, so we request a different temp.
6075 locations->AddTemp(Location::RequiresRegister());
6076 RegisterSet caller_saves = RegisterSet::Empty();
6077 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6078 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6079 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6080 // that the the kPrimNot result register is the same as the first argument register.
6081 locations->SetCustomSlowPathCallerSaves(caller_saves);
6082 } else {
6083 // For non-Baker read barrier we have a temp-clobbering call.
6084 }
6085 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006086}
6087
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006088// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6089// move.
6090void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006091 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6092 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6093 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006094 return;
6095 }
Vladimir Marko41559982017-01-06 14:04:23 +00006096 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006097
Vladimir Marko41559982017-01-06 14:04:23 +00006098 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006099 Location out_loc = locations->Out();
6100 vixl32::Register out = OutputRegister(cls);
6101
Artem Serovd4cc5b22016-11-04 11:19:09 +00006102 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6103 ? kWithoutReadBarrier
6104 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006105 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006106 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006107 case HLoadClass::LoadKind::kReferrersClass: {
6108 DCHECK(!cls->CanCallRuntime());
6109 DCHECK(!cls->MustGenerateClinitCheck());
6110 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6111 vixl32::Register current_method = InputRegisterAt(cls, 0);
6112 GenerateGcRootFieldLoad(cls,
6113 out_loc,
6114 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006115 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006116 read_barrier_option);
6117 break;
6118 }
6119 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006120 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006121 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6122 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6123 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006124 break;
6125 }
6126 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006127 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006128 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6129 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6130 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6131 codegen_->EmitMovwMovtPlaceholder(labels, out);
6132 break;
6133 }
6134 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006135 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006136 uint32_t address = dchecked_integral_cast<uint32_t>(
6137 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6138 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006139 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006140 break;
6141 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006142 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006143 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6144 ? RegisterFrom(locations->GetTemp(0))
6145 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006147 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006148 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6149 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006150 generate_null_check = true;
6151 break;
6152 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006153 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006154 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6155 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006156 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006157 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006158 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006159 break;
6160 }
Vladimir Marko41559982017-01-06 14:04:23 +00006161 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006162 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006163 LOG(FATAL) << "UNREACHABLE";
6164 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006165 }
6166
6167 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6168 DCHECK(cls->CanCallRuntime());
6169 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6170 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6171 codegen_->AddSlowPath(slow_path);
6172 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006173 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006174 }
6175 if (cls->MustGenerateClinitCheck()) {
6176 GenerateClassInitializationCheck(slow_path, out);
6177 } else {
6178 __ Bind(slow_path->GetExitLabel());
6179 }
6180 }
6181}
6182
Artem Serov02d37832016-10-25 15:25:33 +01006183void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6184 LocationSummary* locations =
6185 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6186 locations->SetInAt(0, Location::RequiresRegister());
6187 if (check->HasUses()) {
6188 locations->SetOut(Location::SameAsFirstInput());
6189 }
6190}
6191
6192void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6193 // We assume the class is not null.
6194 LoadClassSlowPathARMVIXL* slow_path =
6195 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6196 check,
6197 check->GetDexPc(),
6198 /* do_clinit */ true);
6199 codegen_->AddSlowPath(slow_path);
6200 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6201}
6202
6203void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6204 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6205 UseScratchRegisterScope temps(GetVIXLAssembler());
6206 vixl32::Register temp = temps.Acquire();
6207 GetAssembler()->LoadFromOffset(kLoadWord,
6208 temp,
6209 class_reg,
6210 mirror::Class::StatusOffset().Int32Value());
6211 __ Cmp(temp, mirror::Class::kStatusInitialized);
6212 __ B(lt, slow_path->GetEntryLabel());
6213 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6214 // properly. Therefore, we do a memory fence.
6215 __ Dmb(ISH);
6216 __ Bind(slow_path->GetExitLabel());
6217}
6218
Artem Serov02d37832016-10-25 15:25:33 +01006219HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006220 HLoadString::LoadKind desired_string_load_kind) {
6221 switch (desired_string_load_kind) {
6222 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006223 DCHECK(!GetCompilerOptions().GetCompilePic());
6224 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006225 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6226 DCHECK(GetCompilerOptions().GetCompilePic());
6227 break;
6228 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006229 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006230 case HLoadString::LoadKind::kBssEntry:
6231 DCHECK(!Runtime::Current()->UseJitCompilation());
6232 break;
6233 case HLoadString::LoadKind::kJitTableAddress:
6234 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006235 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006236 case HLoadString::LoadKind::kDexCacheViaMethod:
6237 break;
6238 }
6239 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006240}
6241
6242void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006243 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006245 HLoadString::LoadKind load_kind = load->GetLoadKind();
6246 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006247 locations->SetOut(LocationFrom(r0));
6248 } else {
6249 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006250 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6251 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006252 // Rely on the pResolveString and marking to save everything we need, including temps.
6253 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6254 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006255 locations->AddTemp(Location::RequiresRegister());
6256 RegisterSet caller_saves = RegisterSet::Empty();
6257 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6258 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6259 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6260 // that the the kPrimNot result register is the same as the first argument register.
6261 locations->SetCustomSlowPathCallerSaves(caller_saves);
6262 } else {
6263 // For non-Baker read barrier we have a temp-clobbering call.
6264 }
6265 }
Artem Serov02d37832016-10-25 15:25:33 +01006266 }
6267}
6268
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006269// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6270// move.
6271void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006272 LocationSummary* locations = load->GetLocations();
6273 Location out_loc = locations->Out();
6274 vixl32::Register out = OutputRegister(load);
6275 HLoadString::LoadKind load_kind = load->GetLoadKind();
6276
6277 switch (load_kind) {
6278 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006279 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6280 load->GetStringIndex()));
6281 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006282 }
6283 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6284 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6285 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006286 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006287 codegen_->EmitMovwMovtPlaceholder(labels, out);
6288 return; // No dex cache slow path.
6289 }
6290 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006291 uint32_t address = dchecked_integral_cast<uint32_t>(
6292 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6293 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006294 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6295 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006296 }
6297 case HLoadString::LoadKind::kBssEntry: {
6298 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006299 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6300 ? RegisterFrom(locations->GetTemp(0))
6301 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006302 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006303 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006304 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6305 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6306 LoadStringSlowPathARMVIXL* slow_path =
6307 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6308 codegen_->AddSlowPath(slow_path);
6309 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6310 __ Bind(slow_path->GetExitLabel());
6311 return;
6312 }
6313 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006314 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006315 load->GetStringIndex(),
6316 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006317 // /* GcRoot<mirror::String> */ out = *out
6318 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6319 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006320 }
6321 default:
6322 break;
6323 }
Artem Serov02d37832016-10-25 15:25:33 +01006324
6325 // TODO: Re-add the compiler code to do string dex cache lookup again.
6326 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6327 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006328 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006329 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6330 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6331}
6332
6333static int32_t GetExceptionTlsOffset() {
6334 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6335}
6336
6337void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6338 LocationSummary* locations =
6339 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6340 locations->SetOut(Location::RequiresRegister());
6341}
6342
6343void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6344 vixl32::Register out = OutputRegister(load);
6345 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6346}
6347
6348
6349void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6350 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6351}
6352
6353void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6354 UseScratchRegisterScope temps(GetVIXLAssembler());
6355 vixl32::Register temp = temps.Acquire();
6356 __ Mov(temp, 0);
6357 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6358}
6359
6360void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6361 LocationSummary* locations =
6362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6363 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6364 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6365}
6366
6367void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6368 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6369 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6370}
6371
Artem Serov657022c2016-11-23 14:19:38 +00006372// Temp is used for read barrier.
6373static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6374 if (kEmitCompilerReadBarrier &&
6375 (kUseBakerReadBarrier ||
6376 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6378 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6379 return 1;
6380 }
6381 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006382}
6383
Artem Serov657022c2016-11-23 14:19:38 +00006384// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6385// interface pointer, one for loading the current interface.
6386// The other checks have one temp for loading the object's class.
6387static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6388 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6389 return 3;
6390 }
6391 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6392}
Artem Serovcfbe9132016-10-14 15:58:56 +01006393
6394void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6395 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6396 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6397 bool baker_read_barrier_slow_path = false;
6398 switch (type_check_kind) {
6399 case TypeCheckKind::kExactCheck:
6400 case TypeCheckKind::kAbstractClassCheck:
6401 case TypeCheckKind::kClassHierarchyCheck:
6402 case TypeCheckKind::kArrayObjectCheck:
6403 call_kind =
6404 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6405 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6406 break;
6407 case TypeCheckKind::kArrayCheck:
6408 case TypeCheckKind::kUnresolvedCheck:
6409 case TypeCheckKind::kInterfaceCheck:
6410 call_kind = LocationSummary::kCallOnSlowPath;
6411 break;
6412 }
6413
6414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6415 if (baker_read_barrier_slow_path) {
6416 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6417 }
6418 locations->SetInAt(0, Location::RequiresRegister());
6419 locations->SetInAt(1, Location::RequiresRegister());
6420 // The "out" register is used as a temporary, so it overlaps with the inputs.
6421 // Note that TypeCheckSlowPathARM uses this register too.
6422 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006423 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006424}
6425
6426void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6427 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6428 LocationSummary* locations = instruction->GetLocations();
6429 Location obj_loc = locations->InAt(0);
6430 vixl32::Register obj = InputRegisterAt(instruction, 0);
6431 vixl32::Register cls = InputRegisterAt(instruction, 1);
6432 Location out_loc = locations->Out();
6433 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006434 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6435 DCHECK_LE(num_temps, 1u);
6436 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006437 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6438 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6439 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6440 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6441 vixl32::Label done, zero;
6442 SlowPathCodeARMVIXL* slow_path = nullptr;
6443
6444 // Return 0 if `obj` is null.
6445 // avoid null check if we know obj is not null.
6446 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006447 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006448 }
6449
Artem Serovcfbe9132016-10-14 15:58:56 +01006450 switch (type_check_kind) {
6451 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006452 // /* HeapReference<Class> */ out = obj->klass_
6453 GenerateReferenceLoadTwoRegisters(instruction,
6454 out_loc,
6455 obj_loc,
6456 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006457 maybe_temp_loc,
6458 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006459 __ Cmp(out, cls);
6460 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00006461 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006462 __ Mov(out, 1);
6463 __ B(&done);
6464 break;
6465 }
6466
6467 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006468 // /* HeapReference<Class> */ out = obj->klass_
6469 GenerateReferenceLoadTwoRegisters(instruction,
6470 out_loc,
6471 obj_loc,
6472 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006473 maybe_temp_loc,
6474 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006475 // If the class is abstract, we eagerly fetch the super class of the
6476 // object to avoid doing a comparison we know will fail.
6477 vixl32::Label loop;
6478 __ Bind(&loop);
6479 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006480 GenerateReferenceLoadOneRegister(instruction,
6481 out_loc,
6482 super_offset,
6483 maybe_temp_loc,
6484 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006485 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006486 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006487 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006488 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006489 __ Mov(out, 1);
6490 if (zero.IsReferenced()) {
6491 __ B(&done);
6492 }
6493 break;
6494 }
6495
6496 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006497 // /* HeapReference<Class> */ out = obj->klass_
6498 GenerateReferenceLoadTwoRegisters(instruction,
6499 out_loc,
6500 obj_loc,
6501 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006502 maybe_temp_loc,
6503 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006504 // Walk over the class hierarchy to find a match.
6505 vixl32::Label loop, success;
6506 __ Bind(&loop);
6507 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006508 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006509 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006510 GenerateReferenceLoadOneRegister(instruction,
6511 out_loc,
6512 super_offset,
6513 maybe_temp_loc,
6514 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006515 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006516 // If `out` is null, we use it for the result, and jump to `done`.
6517 __ B(&done);
6518 __ Bind(&success);
6519 __ Mov(out, 1);
6520 if (zero.IsReferenced()) {
6521 __ B(&done);
6522 }
6523 break;
6524 }
6525
6526 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006527 // /* HeapReference<Class> */ out = obj->klass_
6528 GenerateReferenceLoadTwoRegisters(instruction,
6529 out_loc,
6530 obj_loc,
6531 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006532 maybe_temp_loc,
6533 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006534 // Do an exact check.
6535 vixl32::Label exact_check;
6536 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006537 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006538 // Otherwise, we need to check that the object's class is a non-primitive array.
6539 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006540 GenerateReferenceLoadOneRegister(instruction,
6541 out_loc,
6542 component_offset,
6543 maybe_temp_loc,
6544 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006545 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006546 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006547 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6548 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006549 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006550 __ Bind(&exact_check);
6551 __ Mov(out, 1);
6552 __ B(&done);
6553 break;
6554 }
6555
6556 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006557 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006558 // /* HeapReference<Class> */ out = obj->klass_
6559 GenerateReferenceLoadTwoRegisters(instruction,
6560 out_loc,
6561 obj_loc,
6562 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006563 maybe_temp_loc,
6564 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006565 __ Cmp(out, cls);
6566 DCHECK(locations->OnlyCallsOnSlowPath());
6567 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6568 /* is_fatal */ false);
6569 codegen_->AddSlowPath(slow_path);
6570 __ B(ne, slow_path->GetEntryLabel());
6571 __ Mov(out, 1);
6572 if (zero.IsReferenced()) {
6573 __ B(&done);
6574 }
6575 break;
6576 }
6577
6578 case TypeCheckKind::kUnresolvedCheck:
6579 case TypeCheckKind::kInterfaceCheck: {
6580 // Note that we indeed only call on slow path, but we always go
6581 // into the slow path for the unresolved and interface check
6582 // cases.
6583 //
6584 // We cannot directly call the InstanceofNonTrivial runtime
6585 // entry point without resorting to a type checking slow path
6586 // here (i.e. by calling InvokeRuntime directly), as it would
6587 // require to assign fixed registers for the inputs of this
6588 // HInstanceOf instruction (following the runtime calling
6589 // convention), which might be cluttered by the potential first
6590 // read barrier emission at the beginning of this method.
6591 //
6592 // TODO: Introduce a new runtime entry point taking the object
6593 // to test (instead of its class) as argument, and let it deal
6594 // with the read barrier issues. This will let us refactor this
6595 // case of the `switch` code as it was previously (with a direct
6596 // call to the runtime not using a type checking slow path).
6597 // This should also be beneficial for the other cases above.
6598 DCHECK(locations->OnlyCallsOnSlowPath());
6599 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6600 /* is_fatal */ false);
6601 codegen_->AddSlowPath(slow_path);
6602 __ B(slow_path->GetEntryLabel());
6603 if (zero.IsReferenced()) {
6604 __ B(&done);
6605 }
6606 break;
6607 }
6608 }
6609
6610 if (zero.IsReferenced()) {
6611 __ Bind(&zero);
6612 __ Mov(out, 0);
6613 }
6614
6615 if (done.IsReferenced()) {
6616 __ Bind(&done);
6617 }
6618
6619 if (slow_path != nullptr) {
6620 __ Bind(slow_path->GetExitLabel());
6621 }
6622}
6623
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006624void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6625 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6626 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6627
6628 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6629 switch (type_check_kind) {
6630 case TypeCheckKind::kExactCheck:
6631 case TypeCheckKind::kAbstractClassCheck:
6632 case TypeCheckKind::kClassHierarchyCheck:
6633 case TypeCheckKind::kArrayObjectCheck:
6634 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6635 LocationSummary::kCallOnSlowPath :
6636 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6637 break;
6638 case TypeCheckKind::kArrayCheck:
6639 case TypeCheckKind::kUnresolvedCheck:
6640 case TypeCheckKind::kInterfaceCheck:
6641 call_kind = LocationSummary::kCallOnSlowPath;
6642 break;
6643 }
6644
6645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6646 locations->SetInAt(0, Location::RequiresRegister());
6647 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006648 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006649}
6650
6651void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6652 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6653 LocationSummary* locations = instruction->GetLocations();
6654 Location obj_loc = locations->InAt(0);
6655 vixl32::Register obj = InputRegisterAt(instruction, 0);
6656 vixl32::Register cls = InputRegisterAt(instruction, 1);
6657 Location temp_loc = locations->GetTemp(0);
6658 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006659 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6660 DCHECK_LE(num_temps, 3u);
6661 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6662 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6663 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6664 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6665 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6666 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6667 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6668 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6669 const uint32_t object_array_data_offset =
6670 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006671
Artem Serov657022c2016-11-23 14:19:38 +00006672 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6673 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6674 // read barriers is done for performance and code size reasons.
6675 bool is_type_check_slow_path_fatal = false;
6676 if (!kEmitCompilerReadBarrier) {
6677 is_type_check_slow_path_fatal =
6678 (type_check_kind == TypeCheckKind::kExactCheck ||
6679 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6680 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6681 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6682 !instruction->CanThrowIntoCatchBlock();
6683 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006684 SlowPathCodeARMVIXL* type_check_slow_path =
6685 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6686 is_type_check_slow_path_fatal);
6687 codegen_->AddSlowPath(type_check_slow_path);
6688
6689 vixl32::Label done;
6690 // Avoid null check if we know obj is not null.
6691 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006692 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006693 }
6694
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006695 switch (type_check_kind) {
6696 case TypeCheckKind::kExactCheck:
6697 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006698 // /* HeapReference<Class> */ temp = obj->klass_
6699 GenerateReferenceLoadTwoRegisters(instruction,
6700 temp_loc,
6701 obj_loc,
6702 class_offset,
6703 maybe_temp2_loc,
6704 kWithoutReadBarrier);
6705
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006706 __ Cmp(temp, cls);
6707 // Jump to slow path for throwing the exception or doing a
6708 // more involved array check.
6709 __ B(ne, type_check_slow_path->GetEntryLabel());
6710 break;
6711 }
6712
6713 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006714 // /* HeapReference<Class> */ temp = obj->klass_
6715 GenerateReferenceLoadTwoRegisters(instruction,
6716 temp_loc,
6717 obj_loc,
6718 class_offset,
6719 maybe_temp2_loc,
6720 kWithoutReadBarrier);
6721
Artem Serovcfbe9132016-10-14 15:58:56 +01006722 // If the class is abstract, we eagerly fetch the super class of the
6723 // object to avoid doing a comparison we know will fail.
6724 vixl32::Label loop;
6725 __ Bind(&loop);
6726 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006727 GenerateReferenceLoadOneRegister(instruction,
6728 temp_loc,
6729 super_offset,
6730 maybe_temp2_loc,
6731 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006732
6733 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6734 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006735 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006736
6737 // Otherwise, compare the classes.
6738 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006739 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006740 break;
6741 }
6742
6743 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006744 // /* HeapReference<Class> */ temp = obj->klass_
6745 GenerateReferenceLoadTwoRegisters(instruction,
6746 temp_loc,
6747 obj_loc,
6748 class_offset,
6749 maybe_temp2_loc,
6750 kWithoutReadBarrier);
6751
Artem Serovcfbe9132016-10-14 15:58:56 +01006752 // Walk over the class hierarchy to find a match.
6753 vixl32::Label loop;
6754 __ Bind(&loop);
6755 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006756 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006757
6758 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006759 GenerateReferenceLoadOneRegister(instruction,
6760 temp_loc,
6761 super_offset,
6762 maybe_temp2_loc,
6763 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006764
6765 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6766 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006767 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006768 // Otherwise, jump to the beginning of the loop.
6769 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006770 break;
6771 }
6772
Artem Serovcfbe9132016-10-14 15:58:56 +01006773 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006774 // /* HeapReference<Class> */ temp = obj->klass_
6775 GenerateReferenceLoadTwoRegisters(instruction,
6776 temp_loc,
6777 obj_loc,
6778 class_offset,
6779 maybe_temp2_loc,
6780 kWithoutReadBarrier);
6781
Artem Serovcfbe9132016-10-14 15:58:56 +01006782 // Do an exact check.
6783 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006784 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006785
6786 // Otherwise, we need to check that the object's class is a non-primitive array.
6787 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006788 GenerateReferenceLoadOneRegister(instruction,
6789 temp_loc,
6790 component_offset,
6791 maybe_temp2_loc,
6792 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006793 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006794 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006795 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6796 // to further check that this component type is not a primitive type.
6797 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6798 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006799 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006800 break;
6801 }
6802
6803 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006804 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006805 // We cannot directly call the CheckCast runtime entry point
6806 // without resorting to a type checking slow path here (i.e. by
6807 // calling InvokeRuntime directly), as it would require to
6808 // assign fixed registers for the inputs of this HInstanceOf
6809 // instruction (following the runtime calling convention), which
6810 // might be cluttered by the potential first read barrier
6811 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006812
Artem Serovcfbe9132016-10-14 15:58:56 +01006813 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006814 break;
Artem Serov657022c2016-11-23 14:19:38 +00006815
6816 case TypeCheckKind::kInterfaceCheck: {
6817 // Avoid read barriers to improve performance of the fast path. We can not get false
6818 // positives by doing this.
6819 // /* HeapReference<Class> */ temp = obj->klass_
6820 GenerateReferenceLoadTwoRegisters(instruction,
6821 temp_loc,
6822 obj_loc,
6823 class_offset,
6824 maybe_temp2_loc,
6825 kWithoutReadBarrier);
6826
6827 // /* HeapReference<Class> */ temp = temp->iftable_
6828 GenerateReferenceLoadTwoRegisters(instruction,
6829 temp_loc,
6830 temp_loc,
6831 iftable_offset,
6832 maybe_temp2_loc,
6833 kWithoutReadBarrier);
6834 // Iftable is never null.
6835 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6836 // Loop through the iftable and check if any class matches.
6837 vixl32::Label start_loop;
6838 __ Bind(&start_loop);
6839 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6840 type_check_slow_path->GetEntryLabel());
6841 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6842 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6843 // Go to next interface.
6844 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6845 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6846 // Compare the classes and continue the loop if they do not match.
6847 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00006848 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00006849 break;
6850 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006851 }
6852 __ Bind(&done);
6853
6854 __ Bind(type_check_slow_path->GetExitLabel());
6855}
6856
Artem Serov551b28f2016-10-18 19:11:30 +01006857void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6858 LocationSummary* locations =
6859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6860 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6861 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6862}
6863
6864void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6865 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6866 instruction,
6867 instruction->GetDexPc());
6868 if (instruction->IsEnter()) {
6869 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6870 } else {
6871 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6872 }
6873}
6874
Artem Serov02109dd2016-09-23 17:17:54 +01006875void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6876 HandleBitwiseOperation(instruction, AND);
6877}
6878
6879void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6880 HandleBitwiseOperation(instruction, ORR);
6881}
6882
6883void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6884 HandleBitwiseOperation(instruction, EOR);
6885}
6886
6887void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6888 LocationSummary* locations =
6889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6890 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6891 || instruction->GetResultType() == Primitive::kPrimLong);
6892 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6893 locations->SetInAt(0, Location::RequiresRegister());
6894 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6896}
6897
6898void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6899 HandleBitwiseOperation(instruction);
6900}
6901
6902void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6903 HandleBitwiseOperation(instruction);
6904}
6905
6906void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6907 HandleBitwiseOperation(instruction);
6908}
6909
Artem Serov2bbc9532016-10-21 11:51:50 +01006910void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6911 LocationSummary* locations =
6912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6913 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6914 || instruction->GetResultType() == Primitive::kPrimLong);
6915
6916 locations->SetInAt(0, Location::RequiresRegister());
6917 locations->SetInAt(1, Location::RequiresRegister());
6918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6919}
6920
6921void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6922 LocationSummary* locations = instruction->GetLocations();
6923 Location first = locations->InAt(0);
6924 Location second = locations->InAt(1);
6925 Location out = locations->Out();
6926
6927 if (instruction->GetResultType() == Primitive::kPrimInt) {
6928 vixl32::Register first_reg = RegisterFrom(first);
6929 vixl32::Register second_reg = RegisterFrom(second);
6930 vixl32::Register out_reg = RegisterFrom(out);
6931
6932 switch (instruction->GetOpKind()) {
6933 case HInstruction::kAnd:
6934 __ Bic(out_reg, first_reg, second_reg);
6935 break;
6936 case HInstruction::kOr:
6937 __ Orn(out_reg, first_reg, second_reg);
6938 break;
6939 // There is no EON on arm.
6940 case HInstruction::kXor:
6941 default:
6942 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6943 UNREACHABLE();
6944 }
6945 return;
6946
6947 } else {
6948 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6949 vixl32::Register first_low = LowRegisterFrom(first);
6950 vixl32::Register first_high = HighRegisterFrom(first);
6951 vixl32::Register second_low = LowRegisterFrom(second);
6952 vixl32::Register second_high = HighRegisterFrom(second);
6953 vixl32::Register out_low = LowRegisterFrom(out);
6954 vixl32::Register out_high = HighRegisterFrom(out);
6955
6956 switch (instruction->GetOpKind()) {
6957 case HInstruction::kAnd:
6958 __ Bic(out_low, first_low, second_low);
6959 __ Bic(out_high, first_high, second_high);
6960 break;
6961 case HInstruction::kOr:
6962 __ Orn(out_low, first_low, second_low);
6963 __ Orn(out_high, first_high, second_high);
6964 break;
6965 // There is no EON on arm.
6966 case HInstruction::kXor:
6967 default:
6968 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6969 UNREACHABLE();
6970 }
6971 }
6972}
6973
Anton Kirilov74234da2017-01-13 14:42:47 +00006974void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
6975 HDataProcWithShifterOp* instruction) {
6976 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
6977 instruction->GetType() == Primitive::kPrimLong);
6978 LocationSummary* locations =
6979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6980 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
6981 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
6982
6983 locations->SetInAt(0, Location::RequiresRegister());
6984 locations->SetInAt(1, Location::RequiresRegister());
6985 locations->SetOut(Location::RequiresRegister(),
6986 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6987}
6988
6989void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
6990 HDataProcWithShifterOp* instruction) {
6991 const LocationSummary* const locations = instruction->GetLocations();
6992 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
6993 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
6994
6995 if (instruction->GetType() == Primitive::kPrimInt) {
6996 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
6997
6998 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
6999 ? LowRegisterFrom(locations->InAt(1))
7000 : InputRegisterAt(instruction, 1);
7001
7002 GenerateDataProcInstruction(kind,
7003 OutputRegister(instruction),
7004 InputRegisterAt(instruction, 0),
7005 Operand(second,
7006 ShiftFromOpKind(op_kind),
7007 instruction->GetShiftAmount()),
7008 codegen_);
7009 } else {
7010 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7011
7012 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7013 const vixl32::Register second = InputRegisterAt(instruction, 1);
7014
7015 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7016 GenerateDataProc(kind,
7017 locations->Out(),
7018 locations->InAt(0),
7019 second,
7020 Operand(second, ShiftType::ASR, 31),
7021 codegen_);
7022 } else {
7023 GenerateLongDataProc(instruction, codegen_);
7024 }
7025 }
7026}
7027
Artem Serov02109dd2016-09-23 17:17:54 +01007028// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7029void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7030 vixl32::Register first,
7031 uint32_t value) {
7032 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7033 if (value == 0xffffffffu) {
7034 if (!out.Is(first)) {
7035 __ Mov(out, first);
7036 }
7037 return;
7038 }
7039 if (value == 0u) {
7040 __ Mov(out, 0);
7041 return;
7042 }
7043 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
7044 __ And(out, first, value);
7045 } else {
7046 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
7047 __ Bic(out, first, ~value);
7048 }
7049}
7050
7051// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7052void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7053 vixl32::Register first,
7054 uint32_t value) {
7055 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7056 if (value == 0u) {
7057 if (!out.Is(first)) {
7058 __ Mov(out, first);
7059 }
7060 return;
7061 }
7062 if (value == 0xffffffffu) {
7063 __ Mvn(out, 0);
7064 return;
7065 }
7066 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7067 __ Orr(out, first, value);
7068 } else {
7069 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7070 __ Orn(out, first, ~value);
7071 }
7072}
7073
7074// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7075void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7076 vixl32::Register first,
7077 uint32_t value) {
7078 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7079 if (value == 0u) {
7080 if (!out.Is(first)) {
7081 __ Mov(out, first);
7082 }
7083 return;
7084 }
7085 __ Eor(out, first, value);
7086}
7087
Anton Kirilovdda43962016-11-21 19:55:20 +00007088void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7089 Location first,
7090 uint64_t value) {
7091 vixl32::Register out_low = LowRegisterFrom(out);
7092 vixl32::Register out_high = HighRegisterFrom(out);
7093 vixl32::Register first_low = LowRegisterFrom(first);
7094 vixl32::Register first_high = HighRegisterFrom(first);
7095 uint32_t value_low = Low32Bits(value);
7096 uint32_t value_high = High32Bits(value);
7097 if (value_low == 0u) {
7098 if (!out_low.Is(first_low)) {
7099 __ Mov(out_low, first_low);
7100 }
7101 __ Add(out_high, first_high, value_high);
7102 return;
7103 }
7104 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007105 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007106 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007107 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007108 __ Sbc(out_high, first_high, ~value_high);
7109 } else {
7110 LOG(FATAL) << "Unexpected constant " << value_high;
7111 UNREACHABLE();
7112 }
7113}
7114
Artem Serov02109dd2016-09-23 17:17:54 +01007115void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7116 LocationSummary* locations = instruction->GetLocations();
7117 Location first = locations->InAt(0);
7118 Location second = locations->InAt(1);
7119 Location out = locations->Out();
7120
7121 if (second.IsConstant()) {
7122 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7123 uint32_t value_low = Low32Bits(value);
7124 if (instruction->GetResultType() == Primitive::kPrimInt) {
7125 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7126 vixl32::Register out_reg = OutputRegister(instruction);
7127 if (instruction->IsAnd()) {
7128 GenerateAndConst(out_reg, first_reg, value_low);
7129 } else if (instruction->IsOr()) {
7130 GenerateOrrConst(out_reg, first_reg, value_low);
7131 } else {
7132 DCHECK(instruction->IsXor());
7133 GenerateEorConst(out_reg, first_reg, value_low);
7134 }
7135 } else {
7136 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7137 uint32_t value_high = High32Bits(value);
7138 vixl32::Register first_low = LowRegisterFrom(first);
7139 vixl32::Register first_high = HighRegisterFrom(first);
7140 vixl32::Register out_low = LowRegisterFrom(out);
7141 vixl32::Register out_high = HighRegisterFrom(out);
7142 if (instruction->IsAnd()) {
7143 GenerateAndConst(out_low, first_low, value_low);
7144 GenerateAndConst(out_high, first_high, value_high);
7145 } else if (instruction->IsOr()) {
7146 GenerateOrrConst(out_low, first_low, value_low);
7147 GenerateOrrConst(out_high, first_high, value_high);
7148 } else {
7149 DCHECK(instruction->IsXor());
7150 GenerateEorConst(out_low, first_low, value_low);
7151 GenerateEorConst(out_high, first_high, value_high);
7152 }
7153 }
7154 return;
7155 }
7156
7157 if (instruction->GetResultType() == Primitive::kPrimInt) {
7158 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7159 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7160 vixl32::Register out_reg = OutputRegister(instruction);
7161 if (instruction->IsAnd()) {
7162 __ And(out_reg, first_reg, second_reg);
7163 } else if (instruction->IsOr()) {
7164 __ Orr(out_reg, first_reg, second_reg);
7165 } else {
7166 DCHECK(instruction->IsXor());
7167 __ Eor(out_reg, first_reg, second_reg);
7168 }
7169 } else {
7170 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7171 vixl32::Register first_low = LowRegisterFrom(first);
7172 vixl32::Register first_high = HighRegisterFrom(first);
7173 vixl32::Register second_low = LowRegisterFrom(second);
7174 vixl32::Register second_high = HighRegisterFrom(second);
7175 vixl32::Register out_low = LowRegisterFrom(out);
7176 vixl32::Register out_high = HighRegisterFrom(out);
7177 if (instruction->IsAnd()) {
7178 __ And(out_low, first_low, second_low);
7179 __ And(out_high, first_high, second_high);
7180 } else if (instruction->IsOr()) {
7181 __ Orr(out_low, first_low, second_low);
7182 __ Orr(out_high, first_high, second_high);
7183 } else {
7184 DCHECK(instruction->IsXor());
7185 __ Eor(out_low, first_low, second_low);
7186 __ Eor(out_high, first_high, second_high);
7187 }
7188 }
7189}
7190
Artem Serovcfbe9132016-10-14 15:58:56 +01007191void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007192 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007193 Location out,
7194 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007195 Location maybe_temp,
7196 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007197 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007198 if (read_barrier_option == kWithReadBarrier) {
7199 CHECK(kEmitCompilerReadBarrier);
7200 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7201 if (kUseBakerReadBarrier) {
7202 // Load with fast path based Baker's read barrier.
7203 // /* HeapReference<Object> */ out = *(out + offset)
7204 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7205 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7206 } else {
7207 // Load with slow path based read barrier.
7208 // Save the value of `out` into `maybe_temp` before overwriting it
7209 // in the following move operation, as we will need it for the
7210 // read barrier below.
7211 __ Mov(RegisterFrom(maybe_temp), out_reg);
7212 // /* HeapReference<Object> */ out = *(out + offset)
7213 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7214 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7215 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007216 } else {
7217 // Plain load with no read barrier.
7218 // /* HeapReference<Object> */ out = *(out + offset)
7219 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7220 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7221 }
7222}
7223
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007224void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007225 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007226 Location out,
7227 Location obj,
7228 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007229 Location maybe_temp,
7230 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007231 vixl32::Register out_reg = RegisterFrom(out);
7232 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007233 if (read_barrier_option == kWithReadBarrier) {
7234 CHECK(kEmitCompilerReadBarrier);
7235 if (kUseBakerReadBarrier) {
7236 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7237 // Load with fast path based Baker's read barrier.
7238 // /* HeapReference<Object> */ out = *(obj + offset)
7239 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7240 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7241 } else {
7242 // Load with slow path based read barrier.
7243 // /* HeapReference<Object> */ out = *(obj + offset)
7244 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7245 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7246 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007247 } else {
7248 // Plain load with no read barrier.
7249 // /* HeapReference<Object> */ out = *(obj + offset)
7250 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7251 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7252 }
7253}
7254
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007255void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007256 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007257 Location root,
7258 vixl32::Register obj,
7259 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007260 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007261 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007262 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007263 DCHECK(kEmitCompilerReadBarrier);
7264 if (kUseBakerReadBarrier) {
7265 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7266 // Baker's read barrier are used:
7267 //
7268 // root = obj.field;
7269 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7270 // if (temp != null) {
7271 // root = temp(root)
7272 // }
7273
7274 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7275 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7276 static_assert(
7277 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7278 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7279 "have different sizes.");
7280 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7281 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7282 "have different sizes.");
7283
7284 // Slow path marking the GC root `root`.
7285 Location temp = LocationFrom(lr);
7286 SlowPathCodeARMVIXL* slow_path =
7287 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
7288 instruction,
7289 root,
7290 /*entrypoint*/ temp);
7291 codegen_->AddSlowPath(slow_path);
7292
7293 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7294 const int32_t entry_point_offset =
7295 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7296 // Loading the entrypoint does not require a load acquire since it is only changed when
7297 // threads are suspended or running a checkpoint.
7298 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
7299 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7300 // checking GetIsGcMarking.
7301 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7302 __ Bind(slow_path->GetExitLabel());
7303 } else {
7304 // GC root loaded through a slow path for read barriers other
7305 // than Baker's.
7306 // /* GcRoot<mirror::Object>* */ root = obj + offset
7307 __ Add(root_reg, obj, offset);
7308 // /* mirror::Object* */ root = root->Read()
7309 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7310 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007311 } else {
7312 // Plain GC root load with no read barrier.
7313 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7314 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7315 // Note that GC roots are not affected by heap poisoning, thus we
7316 // do not have to unpoison `root_reg` here.
7317 }
7318}
7319
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007320void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7321 Location ref,
7322 vixl32::Register obj,
7323 uint32_t offset,
7324 Location temp,
7325 bool needs_null_check) {
7326 DCHECK(kEmitCompilerReadBarrier);
7327 DCHECK(kUseBakerReadBarrier);
7328
7329 // /* HeapReference<Object> */ ref = *(obj + offset)
7330 Location no_index = Location::NoLocation();
7331 ScaleFactor no_scale_factor = TIMES_1;
7332 GenerateReferenceLoadWithBakerReadBarrier(
7333 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007334}
7335
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007336void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7337 Location ref,
7338 vixl32::Register obj,
7339 uint32_t data_offset,
7340 Location index,
7341 Location temp,
7342 bool needs_null_check) {
7343 DCHECK(kEmitCompilerReadBarrier);
7344 DCHECK(kUseBakerReadBarrier);
7345
7346 static_assert(
7347 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7348 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7349 // /* HeapReference<Object> */ ref =
7350 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7351 ScaleFactor scale_factor = TIMES_4;
7352 GenerateReferenceLoadWithBakerReadBarrier(
7353 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007354}
7355
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007356void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7357 Location ref,
7358 vixl32::Register obj,
7359 uint32_t offset,
7360 Location index,
7361 ScaleFactor scale_factor,
7362 Location temp,
7363 bool needs_null_check,
7364 bool always_update_field,
7365 vixl32::Register* temp2) {
7366 DCHECK(kEmitCompilerReadBarrier);
7367 DCHECK(kUseBakerReadBarrier);
7368
7369 // In slow path based read barriers, the read barrier call is
7370 // inserted after the original load. However, in fast path based
7371 // Baker's read barriers, we need to perform the load of
7372 // mirror::Object::monitor_ *before* the original reference load.
7373 // This load-load ordering is required by the read barrier.
7374 // The fast path/slow path (for Baker's algorithm) should look like:
7375 //
7376 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7377 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7378 // HeapReference<Object> ref = *src; // Original reference load.
7379 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7380 // if (is_gray) {
7381 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7382 // }
7383 //
7384 // Note: the original implementation in ReadBarrier::Barrier is
7385 // slightly more complex as it performs additional checks that we do
7386 // not do here for performance reasons.
7387
7388 vixl32::Register ref_reg = RegisterFrom(ref);
7389 vixl32::Register temp_reg = RegisterFrom(temp);
7390 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7391
7392 // /* int32_t */ monitor = obj->monitor_
7393 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7394 if (needs_null_check) {
7395 MaybeRecordImplicitNullCheck(instruction);
7396 }
7397 // /* LockWord */ lock_word = LockWord(monitor)
7398 static_assert(sizeof(LockWord) == sizeof(int32_t),
7399 "art::LockWord and int32_t have different sizes.");
7400
7401 // Introduce a dependency on the lock_word including the rb_state,
7402 // which shall prevent load-load reordering without using
7403 // a memory barrier (which would be more expensive).
7404 // `obj` is unchanged by this operation, but its value now depends
7405 // on `temp_reg`.
7406 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7407
7408 // The actual reference load.
7409 if (index.IsValid()) {
7410 // Load types involving an "index": ArrayGet,
7411 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7412 // intrinsics.
7413 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7414 if (index.IsConstant()) {
7415 size_t computed_offset =
7416 (Int32ConstantFrom(index) << scale_factor) + offset;
7417 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7418 } else {
7419 // Handle the special case of the
7420 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7421 // intrinsics, which use a register pair as index ("long
7422 // offset"), of which only the low part contains data.
7423 vixl32::Register index_reg = index.IsRegisterPair()
7424 ? LowRegisterFrom(index)
7425 : RegisterFrom(index);
7426 UseScratchRegisterScope temps(GetVIXLAssembler());
7427 const vixl32::Register temp3 = temps.Acquire();
7428 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7429 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7430 }
7431 } else {
7432 // /* HeapReference<Object> */ ref = *(obj + offset)
7433 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7434 }
7435
7436 // Object* ref = ref_addr->AsMirrorPtr()
7437 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7438
7439 // Slow path marking the object `ref` when it is gray.
7440 SlowPathCodeARMVIXL* slow_path;
7441 if (always_update_field) {
7442 DCHECK(temp2 != nullptr);
7443 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7444 // of the form `obj + field_offset`, where `obj` is a register and
7445 // `field_offset` is a register pair (of which only the lower half
7446 // is used). Thus `offset` and `scale_factor` above are expected
7447 // to be null in this code path.
7448 DCHECK_EQ(offset, 0u);
7449 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7450 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
7451 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7452 } else {
7453 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
7454 }
7455 AddSlowPath(slow_path);
7456
7457 // if (rb_state == ReadBarrier::GrayState())
7458 // ref = ReadBarrier::Mark(ref);
7459 // Given the numeric representation, it's enough to check the low bit of the
7460 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7461 // which can be a 16-bit instruction unlike the TST immediate.
7462 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7463 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7464 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7465 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
7466 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007467}
7468
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007469void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7470 Location out,
7471 Location ref,
7472 Location obj,
7473 uint32_t offset,
7474 Location index) {
7475 DCHECK(kEmitCompilerReadBarrier);
7476
7477 // Insert a slow path based read barrier *after* the reference load.
7478 //
7479 // If heap poisoning is enabled, the unpoisoning of the loaded
7480 // reference will be carried out by the runtime within the slow
7481 // path.
7482 //
7483 // Note that `ref` currently does not get unpoisoned (when heap
7484 // poisoning is enabled), which is alright as the `ref` argument is
7485 // not used by the artReadBarrierSlow entry point.
7486 //
7487 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7488 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7489 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7490 AddSlowPath(slow_path);
7491
7492 __ B(slow_path->GetEntryLabel());
7493 __ Bind(slow_path->GetExitLabel());
7494}
7495
7496void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007497 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007498 Location ref,
7499 Location obj,
7500 uint32_t offset,
7501 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007502 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007503 // Baker's read barriers shall be handled by the fast path
7504 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007505 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007506 // If heap poisoning is enabled, unpoisoning will be taken care of
7507 // by the runtime within the slow path.
7508 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007509 } else if (kPoisonHeapReferences) {
7510 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7511 }
7512}
7513
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007514void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7515 Location out,
7516 Location root) {
7517 DCHECK(kEmitCompilerReadBarrier);
7518
7519 // Insert a slow path based read barrier *after* the GC root load.
7520 //
7521 // Note that GC roots are not affected by heap poisoning, so we do
7522 // not need to do anything special for this here.
7523 SlowPathCodeARMVIXL* slow_path =
7524 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7525 AddSlowPath(slow_path);
7526
7527 __ B(slow_path->GetEntryLabel());
7528 __ Bind(slow_path->GetExitLabel());
7529}
7530
Artem Serov02d37832016-10-25 15:25:33 +01007531// Check if the desired_dispatch_info is supported. If it is, return it,
7532// otherwise return a fall-back info that should be used instead.
7533HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007534 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007535 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007536 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007537}
7538
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007539vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7540 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7541 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7542 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7543 if (!invoke->GetLocations()->Intrinsified()) {
7544 return RegisterFrom(location);
7545 }
7546 // For intrinsics we allow any location, so it may be on the stack.
7547 if (!location.IsRegister()) {
7548 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7549 return temp;
7550 }
7551 // For register locations, check if the register was saved. If so, get it from the stack.
7552 // Note: There is a chance that the register was saved but not overwritten, so we could
7553 // save one load. However, since this is just an intrinsic slow path we prefer this
7554 // simple and more robust approach rather that trying to determine if that's the case.
7555 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00007556 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007557 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7558 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7559 return temp;
7560 }
7561 return RegisterFrom(location);
7562}
7563
TatWai Chongd8c052a2016-11-02 16:12:48 +08007564Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007565 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007566 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007567 switch (invoke->GetMethodLoadKind()) {
7568 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7569 uint32_t offset =
7570 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7571 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007572 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7573 break;
7574 }
7575 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7576 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7577 break;
7578 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7579 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7580 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007581 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7582 HArmDexCacheArraysBase* base =
7583 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7584 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7585 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7586 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007587 break;
7588 }
7589 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7590 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7591 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007592 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007593 if (current_method.IsRegister()) {
7594 method_reg = RegisterFrom(current_method);
7595 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007596 DCHECK(invoke->GetLocations()->Intrinsified());
7597 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007598 method_reg = reg;
7599 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007600 }
7601 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7602 GetAssembler()->LoadFromOffset(
7603 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007604 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007605 method_reg,
7606 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7607 // temp = temp[index_in_cache];
7608 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7609 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7610 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007611 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007612 break;
7613 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007614 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007615 return callee_method;
7616}
7617
7618void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7619 Location temp) {
7620 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007621
Artem Serovd4cc5b22016-11-04 11:19:09 +00007622 switch (invoke->GetCodePtrLocation()) {
7623 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7624 __ Bl(GetFrameEntryLabel());
7625 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007626 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7627 // LR = callee_method->entry_point_from_quick_compiled_code_
7628 GetAssembler()->LoadFromOffset(
7629 kLoadWord,
7630 lr,
7631 RegisterFrom(callee_method),
7632 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007633 {
7634 // 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 +00007635 ExactAssemblyScope aas(GetVIXLAssembler(),
7636 vixl32::k16BitT32InstructionSizeInBytes,
7637 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007638 // LR()
7639 __ blx(lr);
7640 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007641 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007642 }
7643
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007644 DCHECK(!IsLeafMethod());
7645}
7646
7647void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7648 vixl32::Register temp = RegisterFrom(temp_location);
7649 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7650 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7651
7652 // Use the calling convention instead of the location of the receiver, as
7653 // intrinsics may have put the receiver in a different register. In the intrinsics
7654 // slow path, the arguments have been moved to the right place, so here we are
7655 // guaranteed that the receiver is the first register of the calling convention.
7656 InvokeDexCallingConventionARMVIXL calling_convention;
7657 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7658 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007659 {
7660 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007661 ExactAssemblyScope aas(GetVIXLAssembler(),
7662 vixl32::kMaxInstructionSizeInBytes,
7663 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007664 // /* HeapReference<Class> */ temp = receiver->klass_
7665 __ ldr(temp, MemOperand(receiver, class_offset));
7666 MaybeRecordImplicitNullCheck(invoke);
7667 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007668 // Instead of simply (possibly) unpoisoning `temp` here, we should
7669 // emit a read barrier for the previous class reference load.
7670 // However this is not required in practice, as this is an
7671 // intermediate/temporary reference and because the current
7672 // concurrent copying collector keeps the from-space memory
7673 // intact/accessible until the end of the marking phase (the
7674 // concurrent copying collector may not in the future).
7675 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7676
7677 // temp = temp->GetMethodAt(method_offset);
7678 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7679 kArmPointerSize).Int32Value();
7680 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7681 // LR = temp->GetEntryPoint();
7682 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7683 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007684 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7685 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7686 // that.
7687 // 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 +00007688 ExactAssemblyScope aas(GetVIXLAssembler(),
7689 vixl32::k16BitT32InstructionSizeInBytes,
7690 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007691 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007692}
7693
Artem Serovd4cc5b22016-11-04 11:19:09 +00007694CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007695 const DexFile& dex_file, dex::StringIndex string_index) {
7696 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007697}
7698
7699CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7700 const DexFile& dex_file, dex::TypeIndex type_index) {
7701 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7702}
7703
Vladimir Marko1998cd02017-01-13 13:02:58 +00007704CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
7705 const DexFile& dex_file, dex::TypeIndex type_index) {
7706 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7707}
7708
Artem Serovd4cc5b22016-11-04 11:19:09 +00007709CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7710 const DexFile& dex_file, uint32_t element_offset) {
7711 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7712}
7713
7714CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7715 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7716 patches->emplace_back(dex_file, offset_or_index);
7717 return &patches->back();
7718}
7719
Artem Serovc5fcb442016-12-02 19:19:58 +00007720VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7721 const DexFile& dex_file,
7722 dex::StringIndex string_index) {
7723 return boot_image_string_patches_.GetOrCreate(
7724 StringReference(&dex_file, string_index),
7725 [this]() {
7726 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7727 });
7728}
7729
7730VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7731 const DexFile& dex_file,
7732 dex::TypeIndex type_index) {
7733 return boot_image_type_patches_.GetOrCreate(
7734 TypeReference(&dex_file, type_index),
7735 [this]() {
7736 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7737 });
7738}
7739
7740VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
7741 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7742 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7743 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7744}
7745
7746VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7747 return DeduplicateUint32Literal(address, &uint32_literals_);
7748}
7749
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007750VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
7751 const DexFile& dex_file,
7752 dex::StringIndex string_index,
7753 Handle<mirror::String> handle) {
7754 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7755 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007756 return jit_string_patches_.GetOrCreate(
7757 StringReference(&dex_file, string_index),
7758 [this]() {
7759 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7760 });
7761}
7762
7763VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7764 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007765 Handle<mirror::Class> handle) {
7766 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7767 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007768 return jit_class_patches_.GetOrCreate(
7769 TypeReference(&dex_file, type_index),
7770 [this]() {
7771 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7772 });
7773}
7774
Artem Serovd4cc5b22016-11-04 11:19:09 +00007775template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7776inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7777 const ArenaDeque<PcRelativePatchInfo>& infos,
7778 ArenaVector<LinkerPatch>* linker_patches) {
7779 for (const PcRelativePatchInfo& info : infos) {
7780 const DexFile& dex_file = info.target_dex_file;
7781 size_t offset_or_index = info.offset_or_index;
7782 DCHECK(info.add_pc_label.IsBound());
7783 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7784 // Add MOVW patch.
7785 DCHECK(info.movw_label.IsBound());
7786 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7787 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7788 // Add MOVT patch.
7789 DCHECK(info.movt_label.IsBound());
7790 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7791 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7792 }
7793}
7794
7795void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7796 DCHECK(linker_patches->empty());
7797 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00007798 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007799 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007800 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007801 boot_image_type_patches_.size() +
7802 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007803 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007804 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007805 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007806 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7807 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007808 for (const auto& entry : boot_image_string_patches_) {
7809 const StringReference& target_string = entry.first;
7810 VIXLUInt32Literal* literal = entry.second;
7811 DCHECK(literal->IsBound());
7812 uint32_t literal_offset = literal->GetLocation();
7813 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7814 target_string.dex_file,
7815 target_string.string_index.index_));
7816 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007817 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007818 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007819 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7820 linker_patches);
7821 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007822 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7823 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007824 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7825 linker_patches);
7826 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007827 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7828 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007829 for (const auto& entry : boot_image_type_patches_) {
7830 const TypeReference& target_type = entry.first;
7831 VIXLUInt32Literal* literal = entry.second;
7832 DCHECK(literal->IsBound());
7833 uint32_t literal_offset = literal->GetLocation();
7834 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7835 target_type.dex_file,
7836 target_type.type_index.index_));
7837 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007838 for (const auto& entry : boot_image_address_patches_) {
7839 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7840 VIXLUInt32Literal* literal = entry.second;
7841 DCHECK(literal->IsBound());
7842 uint32_t literal_offset = literal->GetLocation();
7843 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7844 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007845 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00007846}
7847
7848VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7849 uint32_t value,
7850 Uint32ToLiteralMap* map) {
7851 return map->GetOrCreate(
7852 value,
7853 [this, value]() {
7854 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7855 });
7856}
7857
7858VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7859 MethodReference target_method,
7860 MethodToLiteralMap* map) {
7861 return map->GetOrCreate(
7862 target_method,
7863 [this]() {
7864 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7865 });
7866}
7867
Artem Serov2bbc9532016-10-21 11:51:50 +01007868void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7869 LocationSummary* locations =
7870 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7871 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7872 Location::RequiresRegister());
7873 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7874 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7876}
7877
7878void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7879 vixl32::Register res = OutputRegister(instr);
7880 vixl32::Register accumulator =
7881 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7882 vixl32::Register mul_left =
7883 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7884 vixl32::Register mul_right =
7885 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7886
7887 if (instr->GetOpKind() == HInstruction::kAdd) {
7888 __ Mla(res, mul_left, mul_right, accumulator);
7889 } else {
7890 __ Mls(res, mul_left, mul_right, accumulator);
7891 }
7892}
7893
Artem Serov551b28f2016-10-18 19:11:30 +01007894void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7895 // Nothing to do, this should be removed during prepare for register allocator.
7896 LOG(FATAL) << "Unreachable";
7897}
7898
7899void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7900 // Nothing to do, this should be removed during prepare for register allocator.
7901 LOG(FATAL) << "Unreachable";
7902}
7903
7904// Simple implementation of packed switch - generate cascaded compare/jumps.
7905void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7906 LocationSummary* locations =
7907 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7908 locations->SetInAt(0, Location::RequiresRegister());
7909 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7910 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7911 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7912 if (switch_instr->GetStartValue() != 0) {
7913 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7914 }
7915 }
7916}
7917
7918// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7919void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7920 int32_t lower_bound = switch_instr->GetStartValue();
7921 uint32_t num_entries = switch_instr->GetNumEntries();
7922 LocationSummary* locations = switch_instr->GetLocations();
7923 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7924 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7925
7926 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7927 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7928 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007929 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007930 vixl32::Register temp_reg = temps.Acquire();
7931 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7932 // the immediate, because IP is used as the destination register. For the other
7933 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7934 // and they can be encoded in the instruction without making use of IP register.
7935 __ Adds(temp_reg, value_reg, -lower_bound);
7936
7937 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7938 // Jump to successors[0] if value == lower_bound.
7939 __ B(eq, codegen_->GetLabelOf(successors[0]));
7940 int32_t last_index = 0;
7941 for (; num_entries - last_index > 2; last_index += 2) {
7942 __ Adds(temp_reg, temp_reg, -2);
7943 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7944 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7945 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7946 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7947 }
7948 if (num_entries - last_index == 2) {
7949 // The last missing case_value.
7950 __ Cmp(temp_reg, 1);
7951 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7952 }
7953
7954 // And the default for any other value.
7955 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7956 __ B(codegen_->GetLabelOf(default_block));
7957 }
7958 } else {
7959 // Create a table lookup.
7960 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7961
7962 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7963
7964 // Remove the bias.
7965 vixl32::Register key_reg;
7966 if (lower_bound != 0) {
7967 key_reg = RegisterFrom(locations->GetTemp(1));
7968 __ Sub(key_reg, value_reg, lower_bound);
7969 } else {
7970 key_reg = value_reg;
7971 }
7972
7973 // Check whether the value is in the table, jump to default block if not.
7974 __ Cmp(key_reg, num_entries - 1);
7975 __ B(hi, codegen_->GetLabelOf(default_block));
7976
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007977 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007978 vixl32::Register jump_offset = temps.Acquire();
7979
7980 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00007981 {
7982 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
7983 ExactAssemblyScope aas(GetVIXLAssembler(),
7984 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
7985 CodeBufferCheckScope::kMaximumSize);
7986 __ adr(table_base, jump_table->GetTableStartLabel());
7987 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01007988
Scott Wakeling86e9d262017-01-18 15:59:24 +00007989 // Jump to target block by branching to table_base(pc related) + offset.
7990 vixl32::Register target_address = table_base;
7991 __ add(target_address, table_base, jump_offset);
7992 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007993
Scott Wakeling86e9d262017-01-18 15:59:24 +00007994 jump_table->EmitTable(codegen_);
7995 }
Artem Serov551b28f2016-10-18 19:11:30 +01007996 }
7997}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007998void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8000 locations->SetOut(Location::RequiresRegister());
8001}
8002
8003void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8004 vixl32::Register base_reg = OutputRegister(base);
8005 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8006 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
8007 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
8008}
Artem Serov551b28f2016-10-18 19:11:30 +01008009
Artem Serov02d37832016-10-25 15:25:33 +01008010// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008011void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8012 if (!trg.IsValid()) {
8013 DCHECK_EQ(type, Primitive::kPrimVoid);
8014 return;
8015 }
8016
8017 DCHECK_NE(type, Primitive::kPrimVoid);
8018
Artem Serovd4cc5b22016-11-04 11:19:09 +00008019 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008020 if (return_loc.Equals(trg)) {
8021 return;
8022 }
8023
8024 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8025 // with the last branch.
8026 if (type == Primitive::kPrimLong) {
8027 TODO_VIXL32(FATAL);
8028 } else if (type == Primitive::kPrimDouble) {
8029 TODO_VIXL32(FATAL);
8030 } else {
8031 // Let the parallel move resolver take care of all of this.
8032 HParallelMove parallel_move(GetGraph()->GetArena());
8033 parallel_move.AddMove(return_loc, trg, type, nullptr);
8034 GetMoveResolver()->EmitNativeCode(&parallel_move);
8035 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008036}
Scott Wakelingfe885462016-09-22 10:24:38 +01008037
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008038void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8039 LocationSummary* locations =
8040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8041 locations->SetInAt(0, Location::RequiresRegister());
8042 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008043}
8044
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008045void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8046 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8047 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8048 instruction->GetIndex(), kArmPointerSize).SizeValue();
8049 GetAssembler()->LoadFromOffset(kLoadWord,
8050 OutputRegister(instruction),
8051 InputRegisterAt(instruction, 0),
8052 method_offset);
8053 } else {
8054 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8055 instruction->GetIndex(), kArmPointerSize));
8056 GetAssembler()->LoadFromOffset(kLoadWord,
8057 OutputRegister(instruction),
8058 InputRegisterAt(instruction, 0),
8059 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8060 GetAssembler()->LoadFromOffset(kLoadWord,
8061 OutputRegister(instruction),
8062 OutputRegister(instruction),
8063 method_offset);
8064 }
Artem Serov551b28f2016-10-18 19:11:30 +01008065}
8066
Artem Serovc5fcb442016-12-02 19:19:58 +00008067static void PatchJitRootUse(uint8_t* code,
8068 const uint8_t* roots_data,
8069 VIXLUInt32Literal* literal,
8070 uint64_t index_in_table) {
8071 DCHECK(literal->IsBound());
8072 uint32_t literal_offset = literal->GetLocation();
8073 uintptr_t address =
8074 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8075 uint8_t* data = code + literal_offset;
8076 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8077}
8078
8079void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8080 for (const auto& entry : jit_string_patches_) {
8081 const auto& it = jit_string_roots_.find(entry.first);
8082 DCHECK(it != jit_string_roots_.end());
8083 PatchJitRootUse(code, roots_data, entry.second, it->second);
8084 }
8085 for (const auto& entry : jit_class_patches_) {
8086 const auto& it = jit_class_roots_.find(entry.first);
8087 DCHECK(it != jit_class_roots_.end());
8088 PatchJitRootUse(code, roots_data, entry.second, it->second);
8089 }
8090}
8091
Artem Serovd4cc5b22016-11-04 11:19:09 +00008092void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8093 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8094 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008095 ExactAssemblyScope aas(GetVIXLAssembler(),
8096 3 * vixl32::kMaxInstructionSizeInBytes,
8097 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008098 // TODO(VIXL): Think about using mov instead of movw.
8099 __ bind(&labels->movw_label);
8100 __ movw(out, /* placeholder */ 0u);
8101 __ bind(&labels->movt_label);
8102 __ movt(out, /* placeholder */ 0u);
8103 __ bind(&labels->add_pc_label);
8104 __ add(out, out, pc);
8105}
8106
Scott Wakelingfe885462016-09-22 10:24:38 +01008107#undef __
8108#undef QUICK_ENTRY_POINT
8109#undef TODO_VIXL32
8110
8111} // namespace arm
8112} // namespace art