blob: 4b6be32bdec69c407ca840375c3bbfe2244a90f5 [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;
Donghui Bai426b49c2016-11-08 14:55:38 +080045using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010046using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010047using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000050using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010051using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000052using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000053using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010054using helpers::LocationFrom;
55using helpers::LowRegisterFrom;
56using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080057using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::OutputRegister;
59using helpers::OutputSRegister;
60using helpers::OutputVRegister;
61using helpers::RegisterFrom;
62using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000063using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010064
Artem Serov0fb37192016-12-06 18:13:40 +000065using vixl::ExactAssemblyScope;
66using vixl::CodeBufferCheckScope;
67
Scott Wakelingfe885462016-09-22 10:24:38 +010068using RegisterList = vixl32::RegisterList;
69
70static bool ExpectedPairLayout(Location location) {
71 // We expected this for both core and fpu register pairs.
72 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
73}
Artem Serovd4cc5b22016-11-04 11:19:09 +000074// Use a local definition to prevent copying mistakes.
75static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
76static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010077static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010079
80#ifdef __
81#error "ARM Codegen VIXL macro-assembler macro already defined."
82#endif
83
Scott Wakelingfe885462016-09-22 10:24:38 +010084// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
85#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
86#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
87
88// Marker that code is yet to be, and must, be implemented.
89#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
90
Scott Wakelinga7812ae2016-10-17 10:03:36 +010091// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
92// for each live D registers they treat two corresponding S registers as live ones.
93//
94// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
95// from a list of contiguous S registers a list of contiguous D registers (processing first/last
96// S registers corner cases) and save/restore this new list treating them as D registers.
97// - decreasing code size
98// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
99// restored and then used in regular non SlowPath code as D register.
100//
101// For the following example (v means the S register is live):
102// D names: | D0 | D1 | D2 | D4 | ...
103// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
104// Live? | | v | v | v | v | v | v | | ...
105//
106// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
107// as D registers.
108//
109// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
110// for lists of floating-point registers.
111static size_t SaveContiguousSRegisterList(size_t first,
112 size_t last,
113 CodeGenerator* codegen,
114 size_t stack_offset) {
115 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
116 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
117 DCHECK_LE(first, last);
118 if ((first == last) && (first == 0)) {
119 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
120 return stack_offset + kSRegSizeInBytes;
121 }
122 if (first % 2 == 1) {
123 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
124 stack_offset += kSRegSizeInBytes;
125 }
126
127 bool save_last = false;
128 if (last % 2 == 0) {
129 save_last = true;
130 --last;
131 }
132
133 if (first < last) {
134 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
135 DCHECK_EQ((last - first + 1) % 2, 0u);
136 size_t number_of_d_regs = (last - first + 1) / 2;
137
138 if (number_of_d_regs == 1) {
139 __ Vstr(d_reg, MemOperand(sp, stack_offset));
140 } else if (number_of_d_regs > 1) {
141 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
142 vixl32::Register base = sp;
143 if (stack_offset != 0) {
144 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000145 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100146 }
147 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
148 }
149 stack_offset += number_of_d_regs * kDRegSizeInBytes;
150 }
151
152 if (save_last) {
153 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
154 stack_offset += kSRegSizeInBytes;
155 }
156
157 return stack_offset;
158}
159
160static size_t RestoreContiguousSRegisterList(size_t first,
161 size_t last,
162 CodeGenerator* codegen,
163 size_t stack_offset) {
164 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
165 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
166 DCHECK_LE(first, last);
167 if ((first == last) && (first == 0)) {
168 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
169 return stack_offset + kSRegSizeInBytes;
170 }
171 if (first % 2 == 1) {
172 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
173 stack_offset += kSRegSizeInBytes;
174 }
175
176 bool restore_last = false;
177 if (last % 2 == 0) {
178 restore_last = true;
179 --last;
180 }
181
182 if (first < last) {
183 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
184 DCHECK_EQ((last - first + 1) % 2, 0u);
185 size_t number_of_d_regs = (last - first + 1) / 2;
186 if (number_of_d_regs == 1) {
187 __ Vldr(d_reg, MemOperand(sp, stack_offset));
188 } else if (number_of_d_regs > 1) {
189 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
190 vixl32::Register base = sp;
191 if (stack_offset != 0) {
192 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000193 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100194 }
195 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
196 }
197 stack_offset += number_of_d_regs * kDRegSizeInBytes;
198 }
199
200 if (restore_last) {
201 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
202 stack_offset += kSRegSizeInBytes;
203 }
204
205 return stack_offset;
206}
207
208void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
209 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
210 size_t orig_offset = stack_offset;
211
212 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
213 for (uint32_t i : LowToHighBits(core_spills)) {
214 // If the register holds an object, update the stack mask.
215 if (locations->RegisterContainsObject(i)) {
216 locations->SetStackBit(stack_offset / kVRegSize);
217 }
218 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
219 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
220 saved_core_stack_offsets_[i] = stack_offset;
221 stack_offset += kArmWordSize;
222 }
223
224 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
225 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
226
227 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
228 orig_offset = stack_offset;
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
231 saved_fpu_stack_offsets_[i] = stack_offset;
232 stack_offset += kArmWordSize;
233 }
234
235 stack_offset = orig_offset;
236 while (fp_spills != 0u) {
237 uint32_t begin = CTZ(fp_spills);
238 uint32_t tmp = fp_spills + (1u << begin);
239 fp_spills &= tmp; // Clear the contiguous range of 1s.
240 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
241 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
242 }
243 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
244}
245
246void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
247 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
248 size_t orig_offset = stack_offset;
249
250 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
251 for (uint32_t i : LowToHighBits(core_spills)) {
252 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
253 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
254 stack_offset += kArmWordSize;
255 }
256
257 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
258 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
259 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
260
261 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
262 while (fp_spills != 0u) {
263 uint32_t begin = CTZ(fp_spills);
264 uint32_t tmp = fp_spills + (1u << begin);
265 fp_spills &= tmp; // Clear the contiguous range of 1s.
266 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
267 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
268 }
269 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
270}
271
272class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
273 public:
274 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
278 __ Bind(GetEntryLabel());
279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
283 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
290 bool IsFatal() const OVERRIDE { return true; }
291
292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
293
294 private:
295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
296};
297
Scott Wakelingfe885462016-09-22 10:24:38 +0100298class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
299 public:
300 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
301 : SlowPathCodeARMVIXL(instruction) {}
302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100304 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100305 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100306 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100307 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
308 }
309
310 bool IsFatal() const OVERRIDE { return true; }
311
312 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
313
314 private:
315 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
316};
317
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100318class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
319 public:
320 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
321 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
325 __ Bind(GetEntryLabel());
326 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
327 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
328 if (successor_ == nullptr) {
329 __ B(GetReturnLabel());
330 } else {
331 __ B(arm_codegen->GetLabelOf(successor_));
332 }
333 }
334
335 vixl32::Label* GetReturnLabel() {
336 DCHECK(successor_ == nullptr);
337 return &return_label_;
338 }
339
340 HBasicBlock* GetSuccessor() const {
341 return successor_;
342 }
343
344 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
345
346 private:
347 // If not null, the block to branch to after the suspend check.
348 HBasicBlock* const successor_;
349
350 // If `successor_` is null, the label to branch to after the suspend check.
351 vixl32::Label return_label_;
352
353 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
354};
355
Scott Wakelingc34dba72016-10-03 10:14:44 +0100356class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
357 public:
358 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
359 : SlowPathCodeARMVIXL(instruction) {}
360
361 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
362 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
363 LocationSummary* locations = instruction_->GetLocations();
364
365 __ Bind(GetEntryLabel());
366 if (instruction_->CanThrowIntoCatchBlock()) {
367 // Live registers will be restored in the catch block if caught.
368 SaveLiveRegisters(codegen, instruction_->GetLocations());
369 }
370 // We're moving two locations to locations that could overlap, so we need a parallel
371 // move resolver.
372 InvokeRuntimeCallingConventionARMVIXL calling_convention;
373 codegen->EmitParallelMoves(
374 locations->InAt(0),
375 LocationFrom(calling_convention.GetRegisterAt(0)),
376 Primitive::kPrimInt,
377 locations->InAt(1),
378 LocationFrom(calling_convention.GetRegisterAt(1)),
379 Primitive::kPrimInt);
380 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
381 ? kQuickThrowStringBounds
382 : kQuickThrowArrayBounds;
383 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
384 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
385 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
386 }
387
388 bool IsFatal() const OVERRIDE { return true; }
389
390 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
391
392 private:
393 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
394};
395
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100396class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
397 public:
398 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000399 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100400 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
401 }
402
403 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000404 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000405 Location out = locations->Out();
406 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100407
408 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000413 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
414 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
415 bool is_load_class_bss_entry =
416 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
417 vixl32::Register entry_address;
418 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
419 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
420 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
421 // the kSaveEverything call.
422 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
423 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
424 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
425 if (temp_is_r0) {
426 __ Mov(entry_address, temp);
427 }
428 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000429 dex::TypeIndex type_index = cls_->GetTypeIndex();
430 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100431 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
432 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000433 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100434 if (do_clinit_) {
435 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
436 } else {
437 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
438 }
439
Vladimir Markoea4c1262017-02-06 19:59:33 +0000440 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
441 if (is_load_class_bss_entry) {
442 if (call_saves_everything_except_r0) {
443 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
444 __ Str(r0, MemOperand(entry_address));
445 } else {
446 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
447 UseScratchRegisterScope temps(
448 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
449 vixl32::Register temp = temps.Acquire();
450 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
451 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
452 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
453 __ Str(r0, MemOperand(temp));
454 }
455 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100456 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100457 if (out.IsValid()) {
458 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
459 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
460 }
461 RestoreLiveRegisters(codegen, locations);
462 __ B(GetExitLabel());
463 }
464
465 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
466
467 private:
468 // The class this slow path will load.
469 HLoadClass* const cls_;
470
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100471 // The dex PC of `at_`.
472 const uint32_t dex_pc_;
473
474 // Whether to initialize the class.
475 const bool do_clinit_;
476
477 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
478};
479
Artem Serovd4cc5b22016-11-04 11:19:09 +0000480class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
481 public:
482 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
483 : SlowPathCodeARMVIXL(instruction) {}
484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000486 DCHECK(instruction_->IsLoadString());
487 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000488 LocationSummary* locations = instruction_->GetLocations();
489 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
490 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000491 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000492 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000493 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
494
495 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, locations);
498
499 InvokeRuntimeCallingConventionARMVIXL calling_convention;
500 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000501 // the kSaveEverything call.
502 vixl32::Register entry_address;
503 if (call_saves_everything_except_r0) {
504 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
505 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
506 entry_address = temp_is_r0 ? out : temp;
507 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
508 if (temp_is_r0) {
509 __ Mov(entry_address, temp);
510 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000511 }
512
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000513 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000514 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
515 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
516
517 // Store the resolved String to the .bss entry.
518 if (call_saves_everything_except_r0) {
519 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
520 __ Str(r0, MemOperand(entry_address));
521 } else {
522 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000523 UseScratchRegisterScope temps(
524 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
525 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000526 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
527 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000528 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
529 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000530 }
531
532 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
533 RestoreLiveRegisters(codegen, locations);
534
535 __ B(GetExitLabel());
536 }
537
538 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
539
540 private:
541 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
542};
543
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100544class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
545 public:
546 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
547 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
548
549 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
550 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100551 DCHECK(instruction_->IsCheckCast()
552 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
553
554 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
555 __ Bind(GetEntryLabel());
556
557 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100558 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100559 }
560
561 // We're moving two locations to locations that could overlap, so we need a parallel
562 // move resolver.
563 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100564
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800565 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800566 LocationFrom(calling_convention.GetRegisterAt(0)),
567 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800568 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800569 LocationFrom(calling_convention.GetRegisterAt(1)),
570 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100571 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100572 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
573 instruction_,
574 instruction_->GetDexPc(),
575 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800576 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100577 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100578 } else {
579 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800580 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
581 instruction_,
582 instruction_->GetDexPc(),
583 this);
584 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100585 }
586
587 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100588 RestoreLiveRegisters(codegen, locations);
589 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100590 }
591 }
592
593 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
594
595 bool IsFatal() const OVERRIDE { return is_fatal_; }
596
597 private:
598 const bool is_fatal_;
599
600 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
601};
602
Scott Wakelingc34dba72016-10-03 10:14:44 +0100603class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
604 public:
605 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
606 : SlowPathCodeARMVIXL(instruction) {}
607
608 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
609 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
610 __ Bind(GetEntryLabel());
611 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
612 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
613 }
614
615 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
616
617 private:
618 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
619};
620
621class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
622 public:
623 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 __ Bind(GetEntryLabel());
628 SaveLiveRegisters(codegen, locations);
629
630 InvokeRuntimeCallingConventionARMVIXL calling_convention;
631 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
632 parallel_move.AddMove(
633 locations->InAt(0),
634 LocationFrom(calling_convention.GetRegisterAt(0)),
635 Primitive::kPrimNot,
636 nullptr);
637 parallel_move.AddMove(
638 locations->InAt(1),
639 LocationFrom(calling_convention.GetRegisterAt(1)),
640 Primitive::kPrimInt,
641 nullptr);
642 parallel_move.AddMove(
643 locations->InAt(2),
644 LocationFrom(calling_convention.GetRegisterAt(2)),
645 Primitive::kPrimNot,
646 nullptr);
647 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
648
649 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
650 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
651 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
652 RestoreLiveRegisters(codegen, locations);
653 __ B(GetExitLabel());
654 }
655
656 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
657
658 private:
659 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
660};
661
Roland Levillain47b3ab22017-02-27 14:31:35 +0000662// Slow path marking an object reference `ref` during a read
663// barrier. The field `obj.field` in the object `obj` holding this
664// reference does not get updated by this slow path after marking (see
665// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000666//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000667// This means that after the execution of this slow path, `ref` will
668// always be up-to-date, but `obj.field` may not; i.e., after the
669// flip, `ref` will be a to-space reference, but `obj.field` will
670// probably still be a from-space reference (unless it gets updated by
671// another thread, or if another thread installed another object
672// reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000673class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
674 public:
675 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
676 Location ref,
677 Location entrypoint = Location::NoLocation())
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
679 DCHECK(kEmitCompilerReadBarrier);
680 }
681
Roland Levillain47b3ab22017-02-27 14:31:35 +0000682 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000683
Roland Levillain47b3ab22017-02-27 14:31:35 +0000684 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
685 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000687 DCHECK(locations->CanCall());
688 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
689 DCHECK(instruction_->IsInstanceFieldGet() ||
690 instruction_->IsStaticFieldGet() ||
691 instruction_->IsArrayGet() ||
692 instruction_->IsArraySet() ||
693 instruction_->IsLoadClass() ||
694 instruction_->IsLoadString() ||
695 instruction_->IsInstanceOf() ||
696 instruction_->IsCheckCast() ||
697 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
698 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
699 << "Unexpected instruction in read barrier marking slow path: "
700 << instruction_->DebugName();
701 // The read barrier instrumentation of object ArrayGet
702 // instructions does not support the HIntermediateAddress
703 // instruction.
704 DCHECK(!(instruction_->IsArrayGet() &&
705 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
706
707 __ Bind(GetEntryLabel());
708 // No need to save live registers; it's taken care of by the
709 // entrypoint. Also, there is no need to update the stack mask,
710 // as this runtime call will not trigger a garbage collection.
711 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
712 DCHECK(!ref_reg.Is(sp));
713 DCHECK(!ref_reg.Is(lr));
714 DCHECK(!ref_reg.Is(pc));
715 // IP is used internally by the ReadBarrierMarkRegX entry point
716 // as a temporary, it cannot be the entry point's input/output.
717 DCHECK(!ref_reg.Is(ip));
718 DCHECK(ref_reg.IsRegister()) << ref_reg;
719 // "Compact" slow path, saving two moves.
720 //
721 // Instead of using the standard runtime calling convention (input
722 // and output in R0):
723 //
724 // R0 <- ref
725 // R0 <- ReadBarrierMark(R0)
726 // ref <- R0
727 //
728 // we just use rX (the register containing `ref`) as input and output
729 // of a dedicated entrypoint:
730 //
731 // rX <- ReadBarrierMarkRegX(rX)
732 //
733 if (entrypoint_.IsValid()) {
734 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
735 __ Blx(RegisterFrom(entrypoint_));
736 } else {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000737 int32_t entry_point_offset =
738 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
739 // This runtime call does not require a stack map.
740 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
741 }
742 __ B(GetExitLabel());
743 }
744
745 private:
746 // The location (register) of the marked object reference.
747 const Location ref_;
748
749 // The location of the entrypoint if already loaded.
750 const Location entrypoint_;
751
752 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
753};
754
755// Slow path marking an object reference `ref` during a read barrier,
756// and if needed, atomically updating the field `obj.field` in the
757// object `obj` holding this reference after marking (contrary to
758// ReadBarrierMarkSlowPathARM above, which never tries to update
759// `obj.field`).
760//
761// This means that after the execution of this slow path, both `ref`
762// and `obj.field` will be up-to-date; i.e., after the flip, both will
763// hold the same to-space reference (unless another thread installed
764// another object reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000765class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
766 public:
767 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
768 Location ref,
769 vixl32::Register obj,
770 Location field_offset,
771 vixl32::Register temp1,
Roland Levillain35345a52017-02-27 14:32:08 +0000772 vixl32::Register temp2)
Roland Levillain47b3ab22017-02-27 14:31:35 +0000773 : SlowPathCodeARMVIXL(instruction),
774 ref_(ref),
775 obj_(obj),
776 field_offset_(field_offset),
777 temp1_(temp1),
Roland Levillain35345a52017-02-27 14:32:08 +0000778 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000779 DCHECK(kEmitCompilerReadBarrier);
780 }
781
782 const char* GetDescription() const OVERRIDE {
783 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
784 }
785
786 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
787 LocationSummary* locations = instruction_->GetLocations();
788 vixl32::Register ref_reg = RegisterFrom(ref_);
789 DCHECK(locations->CanCall());
790 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
791 // This slow path is only used by the UnsafeCASObject intrinsic.
792 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
793 << "Unexpected instruction in read barrier marking and field updating slow path: "
794 << instruction_->DebugName();
795 DCHECK(instruction_->GetLocations()->Intrinsified());
796 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
797 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
798
799 __ Bind(GetEntryLabel());
800
801 // Save the old reference.
802 // Note that we cannot use IP to save the old reference, as IP is
803 // used internally by the ReadBarrierMarkRegX entry point, and we
804 // need the old reference after the call to that entry point.
805 DCHECK(!temp1_.Is(ip));
806 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000807
808 // No need to save live registers; it's taken care of by the
809 // entrypoint. Also, there is no need to update the stack mask,
810 // as this runtime call will not trigger a garbage collection.
811 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
812 DCHECK(!ref_reg.Is(sp));
813 DCHECK(!ref_reg.Is(lr));
814 DCHECK(!ref_reg.Is(pc));
815 // IP is used internally by the ReadBarrierMarkRegX entry point
816 // as a temporary, it cannot be the entry point's input/output.
817 DCHECK(!ref_reg.Is(ip));
818 DCHECK(ref_reg.IsRegister()) << ref_reg;
819 // "Compact" slow path, saving two moves.
820 //
821 // Instead of using the standard runtime calling convention (input
822 // and output in R0):
823 //
824 // R0 <- ref
825 // R0 <- ReadBarrierMark(R0)
826 // ref <- R0
827 //
828 // we just use rX (the register containing `ref`) as input and output
829 // of a dedicated entrypoint:
830 //
831 // rX <- ReadBarrierMarkRegX(rX)
832 //
Roland Levillain35345a52017-02-27 14:32:08 +0000833 int32_t entry_point_offset =
834 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
835 // This runtime call does not require a stack map.
836 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000837
838 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000839 // update the field in the holder (`*(obj_ + field_offset_)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000840 //
841 // Note that this field could also hold a different object, if
842 // another thread had concurrently changed it. In that case, the
843 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
844 // (CAS) operation below would abort the CAS, leaving the field
845 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000846 vixl32::Label done;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000847 __ Cmp(temp1_, ref_reg);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000848 __ B(eq, &done, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000849
850 // Update the the holder's field atomically. This may fail if
851 // mutator updates before us, but it's OK. This is achieved
852 // using a strong compare-and-set (CAS) operation with relaxed
853 // memory synchronization ordering, where the expected value is
854 // the old reference and the desired value is the new reference.
855
856 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
857 // Convenience aliases.
858 vixl32::Register base = obj_;
859 // The UnsafeCASObject intrinsic uses a register pair as field
860 // offset ("long offset"), of which only the low part contains
861 // data.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000862 vixl32::Register offset = LowRegisterFrom(field_offset_);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000863 vixl32::Register expected = temp1_;
864 vixl32::Register value = ref_reg;
865 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
866 vixl32::Register tmp = temp2_; // Value in memory.
867
868 __ Add(tmp_ptr, base, offset);
869
870 if (kPoisonHeapReferences) {
871 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
872 if (value.Is(expected)) {
873 // Do not poison `value`, as it is the same register as
874 // `expected`, which has just been poisoned.
875 } else {
876 arm_codegen->GetAssembler()->PoisonHeapReference(value);
877 }
878 }
879
880 // do {
881 // tmp = [r_ptr] - expected;
882 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
883
884 vixl32::Label loop_head, exit_loop;
885 __ Bind(&loop_head);
886
887 __ Ldrex(tmp, MemOperand(tmp_ptr));
888
889 __ Subs(tmp, tmp, expected);
890
891 {
Artem Serov0fb37192016-12-06 18:13:40 +0000892 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
893 2 * kMaxInstructionSizeInBytes,
894 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000895
896 __ it(ne);
897 __ clrex(ne);
898 }
899
Artem Serov517d9f62016-12-12 15:51:15 +0000900 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000901
902 __ Strex(tmp, value, MemOperand(tmp_ptr));
903 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +0000904 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000905
906 __ Bind(&exit_loop);
907
908 if (kPoisonHeapReferences) {
909 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
910 if (value.Is(expected)) {
911 // Do not unpoison `value`, as it is the same register as
912 // `expected`, which has just been unpoisoned.
913 } else {
914 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
915 }
916 }
917
Roland Levillain47b3ab22017-02-27 14:31:35 +0000918 __ Bind(&done);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000919 __ B(GetExitLabel());
920 }
921
922 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000923 // The location (register) of the marked object reference.
924 const Location ref_;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000925 // The register containing the object holding the marked object reference field.
926 const vixl32::Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000927 // The location of the offset of the marked reference field within `obj_`.
928 Location field_offset_;
929
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000930 const vixl32::Register temp1_;
931 const vixl32::Register temp2_;
932
Roland Levillain47b3ab22017-02-27 14:31:35 +0000933 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000934};
935
936// Slow path generating a read barrier for a heap reference.
937class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
938 public:
939 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
940 Location out,
941 Location ref,
942 Location obj,
943 uint32_t offset,
944 Location index)
945 : SlowPathCodeARMVIXL(instruction),
946 out_(out),
947 ref_(ref),
948 obj_(obj),
949 offset_(offset),
950 index_(index) {
951 DCHECK(kEmitCompilerReadBarrier);
952 // If `obj` is equal to `out` or `ref`, it means the initial object
953 // has been overwritten by (or after) the heap object reference load
954 // to be instrumented, e.g.:
955 //
956 // __ LoadFromOffset(kLoadWord, out, out, offset);
957 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
958 //
959 // In that case, we have lost the information about the original
960 // object, and the emitted read barrier cannot work properly.
961 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
962 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
963 }
964
965 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
966 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
967 LocationSummary* locations = instruction_->GetLocations();
968 vixl32::Register reg_out = RegisterFrom(out_);
969 DCHECK(locations->CanCall());
970 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
971 DCHECK(instruction_->IsInstanceFieldGet() ||
972 instruction_->IsStaticFieldGet() ||
973 instruction_->IsArrayGet() ||
974 instruction_->IsInstanceOf() ||
975 instruction_->IsCheckCast() ||
976 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
977 << "Unexpected instruction in read barrier for heap reference slow path: "
978 << instruction_->DebugName();
979 // The read barrier instrumentation of object ArrayGet
980 // instructions does not support the HIntermediateAddress
981 // instruction.
982 DCHECK(!(instruction_->IsArrayGet() &&
983 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
984
985 __ Bind(GetEntryLabel());
986 SaveLiveRegisters(codegen, locations);
987
988 // We may have to change the index's value, but as `index_` is a
989 // constant member (like other "inputs" of this slow path),
990 // introduce a copy of it, `index`.
991 Location index = index_;
992 if (index_.IsValid()) {
993 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
994 if (instruction_->IsArrayGet()) {
995 // Compute the actual memory offset and store it in `index`.
996 vixl32::Register index_reg = RegisterFrom(index_);
997 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
998 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
999 // We are about to change the value of `index_reg` (see the
1000 // calls to art::arm::Thumb2Assembler::Lsl and
1001 // art::arm::Thumb2Assembler::AddConstant below), but it has
1002 // not been saved by the previous call to
1003 // art::SlowPathCode::SaveLiveRegisters, as it is a
1004 // callee-save register --
1005 // art::SlowPathCode::SaveLiveRegisters does not consider
1006 // callee-save registers, as it has been designed with the
1007 // assumption that callee-save registers are supposed to be
1008 // handled by the called function. So, as a callee-save
1009 // register, `index_reg` _would_ eventually be saved onto
1010 // the stack, but it would be too late: we would have
1011 // changed its value earlier. Therefore, we manually save
1012 // it here into another freely available register,
1013 // `free_reg`, chosen of course among the caller-save
1014 // registers (as a callee-save `free_reg` register would
1015 // exhibit the same problem).
1016 //
1017 // Note we could have requested a temporary register from
1018 // the register allocator instead; but we prefer not to, as
1019 // this is a slow path, and we know we can find a
1020 // caller-save register that is available.
1021 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1022 __ Mov(free_reg, index_reg);
1023 index_reg = free_reg;
1024 index = LocationFrom(index_reg);
1025 } else {
1026 // The initial register stored in `index_` has already been
1027 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1028 // (as it is not a callee-save register), so we can freely
1029 // use it.
1030 }
1031 // Shifting the index value contained in `index_reg` by the scale
1032 // factor (2) cannot overflow in practice, as the runtime is
1033 // unable to allocate object arrays with a size larger than
1034 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1035 __ Lsl(index_reg, index_reg, TIMES_4);
1036 static_assert(
1037 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1038 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1039 __ Add(index_reg, index_reg, offset_);
1040 } else {
1041 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1042 // intrinsics, `index_` is not shifted by a scale factor of 2
1043 // (as in the case of ArrayGet), as it is actually an offset
1044 // to an object field within an object.
1045 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1046 DCHECK(instruction_->GetLocations()->Intrinsified());
1047 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1048 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1049 << instruction_->AsInvoke()->GetIntrinsic();
1050 DCHECK_EQ(offset_, 0U);
1051 DCHECK(index_.IsRegisterPair());
1052 // UnsafeGet's offset location is a register pair, the low
1053 // part contains the correct offset.
1054 index = index_.ToLow();
1055 }
1056 }
1057
1058 // We're moving two or three locations to locations that could
1059 // overlap, so we need a parallel move resolver.
1060 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1061 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1062 parallel_move.AddMove(ref_,
1063 LocationFrom(calling_convention.GetRegisterAt(0)),
1064 Primitive::kPrimNot,
1065 nullptr);
1066 parallel_move.AddMove(obj_,
1067 LocationFrom(calling_convention.GetRegisterAt(1)),
1068 Primitive::kPrimNot,
1069 nullptr);
1070 if (index.IsValid()) {
1071 parallel_move.AddMove(index,
1072 LocationFrom(calling_convention.GetRegisterAt(2)),
1073 Primitive::kPrimInt,
1074 nullptr);
1075 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1076 } else {
1077 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1078 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1079 }
1080 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1081 CheckEntrypointTypes<
1082 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1083 arm_codegen->Move32(out_, LocationFrom(r0));
1084
1085 RestoreLiveRegisters(codegen, locations);
1086 __ B(GetExitLabel());
1087 }
1088
1089 const char* GetDescription() const OVERRIDE {
1090 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1091 }
1092
1093 private:
1094 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1095 uint32_t ref = RegisterFrom(ref_).GetCode();
1096 uint32_t obj = RegisterFrom(obj_).GetCode();
1097 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1098 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1099 return vixl32::Register(i);
1100 }
1101 }
1102 // We shall never fail to find a free caller-save register, as
1103 // there are more than two core caller-save registers on ARM
1104 // (meaning it is possible to find one which is different from
1105 // `ref` and `obj`).
1106 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1107 LOG(FATAL) << "Could not find a free caller-save register";
1108 UNREACHABLE();
1109 }
1110
1111 const Location out_;
1112 const Location ref_;
1113 const Location obj_;
1114 const uint32_t offset_;
1115 // An additional location containing an index to an array.
1116 // Only used for HArrayGet and the UnsafeGetObject &
1117 // UnsafeGetObjectVolatile intrinsics.
1118 const Location index_;
1119
1120 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1121};
1122
1123// Slow path generating a read barrier for a GC root.
1124class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1125 public:
1126 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1127 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1128 DCHECK(kEmitCompilerReadBarrier);
1129 }
1130
1131 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1132 LocationSummary* locations = instruction_->GetLocations();
1133 vixl32::Register reg_out = RegisterFrom(out_);
1134 DCHECK(locations->CanCall());
1135 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1136 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1137 << "Unexpected instruction in read barrier for GC root slow path: "
1138 << instruction_->DebugName();
1139
1140 __ Bind(GetEntryLabel());
1141 SaveLiveRegisters(codegen, locations);
1142
1143 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1144 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1145 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1146 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1147 instruction_,
1148 instruction_->GetDexPc(),
1149 this);
1150 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1151 arm_codegen->Move32(out_, LocationFrom(r0));
1152
1153 RestoreLiveRegisters(codegen, locations);
1154 __ B(GetExitLabel());
1155 }
1156
1157 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1158
1159 private:
1160 const Location out_;
1161 const Location root_;
1162
1163 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1164};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001165
Scott Wakelingfe885462016-09-22 10:24:38 +01001166inline vixl32::Condition ARMCondition(IfCondition cond) {
1167 switch (cond) {
1168 case kCondEQ: return eq;
1169 case kCondNE: return ne;
1170 case kCondLT: return lt;
1171 case kCondLE: return le;
1172 case kCondGT: return gt;
1173 case kCondGE: return ge;
1174 case kCondB: return lo;
1175 case kCondBE: return ls;
1176 case kCondA: return hi;
1177 case kCondAE: return hs;
1178 }
1179 LOG(FATAL) << "Unreachable";
1180 UNREACHABLE();
1181}
1182
1183// Maps signed condition to unsigned condition.
1184inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1185 switch (cond) {
1186 case kCondEQ: return eq;
1187 case kCondNE: return ne;
1188 // Signed to unsigned.
1189 case kCondLT: return lo;
1190 case kCondLE: return ls;
1191 case kCondGT: return hi;
1192 case kCondGE: return hs;
1193 // Unsigned remain unchanged.
1194 case kCondB: return lo;
1195 case kCondBE: return ls;
1196 case kCondA: return hi;
1197 case kCondAE: return hs;
1198 }
1199 LOG(FATAL) << "Unreachable";
1200 UNREACHABLE();
1201}
1202
1203inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1204 // The ARM condition codes can express all the necessary branches, see the
1205 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1206 // There is no dex instruction or HIR that would need the missing conditions
1207 // "equal or unordered" or "not equal".
1208 switch (cond) {
1209 case kCondEQ: return eq;
1210 case kCondNE: return ne /* unordered */;
1211 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1212 case kCondLE: return gt_bias ? ls : le /* unordered */;
1213 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1214 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1215 default:
1216 LOG(FATAL) << "UNREACHABLE";
1217 UNREACHABLE();
1218 }
1219}
1220
Anton Kirilov74234da2017-01-13 14:42:47 +00001221inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1222 switch (op_kind) {
1223 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1224 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1225 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1226 default:
1227 LOG(FATAL) << "Unexpected op kind " << op_kind;
1228 UNREACHABLE();
1229 }
1230}
1231
Scott Wakelingfe885462016-09-22 10:24:38 +01001232void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1233 stream << vixl32::Register(reg);
1234}
1235
1236void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1237 stream << vixl32::SRegister(reg);
1238}
1239
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001240static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001241 uint32_t mask = 0;
1242 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1243 i <= regs.GetLastSRegister().GetCode();
1244 ++i) {
1245 mask |= (1 << i);
1246 }
1247 return mask;
1248}
1249
Artem Serovd4cc5b22016-11-04 11:19:09 +00001250// Saves the register in the stack. Returns the size taken on stack.
1251size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1252 uint32_t reg_id ATTRIBUTE_UNUSED) {
1253 TODO_VIXL32(FATAL);
1254 return 0;
1255}
1256
1257// Restores the register from the stack. Returns the size taken on stack.
1258size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1259 uint32_t reg_id ATTRIBUTE_UNUSED) {
1260 TODO_VIXL32(FATAL);
1261 return 0;
1262}
1263
1264size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1265 uint32_t reg_id ATTRIBUTE_UNUSED) {
1266 TODO_VIXL32(FATAL);
1267 return 0;
1268}
1269
1270size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1271 uint32_t reg_id ATTRIBUTE_UNUSED) {
1272 TODO_VIXL32(FATAL);
1273 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001274}
1275
Anton Kirilov74234da2017-01-13 14:42:47 +00001276static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1277 vixl32::Register out,
1278 vixl32::Register first,
1279 const Operand& second,
1280 CodeGeneratorARMVIXL* codegen) {
1281 if (second.IsImmediate() && second.GetImmediate() == 0) {
1282 const Operand in = kind == HInstruction::kAnd
1283 ? Operand(0)
1284 : Operand(first);
1285
1286 __ Mov(out, in);
1287 } else {
1288 switch (kind) {
1289 case HInstruction::kAdd:
1290 __ Add(out, first, second);
1291 break;
1292 case HInstruction::kAnd:
1293 __ And(out, first, second);
1294 break;
1295 case HInstruction::kOr:
1296 __ Orr(out, first, second);
1297 break;
1298 case HInstruction::kSub:
1299 __ Sub(out, first, second);
1300 break;
1301 case HInstruction::kXor:
1302 __ Eor(out, first, second);
1303 break;
1304 default:
1305 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1306 UNREACHABLE();
1307 }
1308 }
1309}
1310
1311static void GenerateDataProc(HInstruction::InstructionKind kind,
1312 const Location& out,
1313 const Location& first,
1314 const Operand& second_lo,
1315 const Operand& second_hi,
1316 CodeGeneratorARMVIXL* codegen) {
1317 const vixl32::Register first_hi = HighRegisterFrom(first);
1318 const vixl32::Register first_lo = LowRegisterFrom(first);
1319 const vixl32::Register out_hi = HighRegisterFrom(out);
1320 const vixl32::Register out_lo = LowRegisterFrom(out);
1321
1322 if (kind == HInstruction::kAdd) {
1323 __ Adds(out_lo, first_lo, second_lo);
1324 __ Adc(out_hi, first_hi, second_hi);
1325 } else if (kind == HInstruction::kSub) {
1326 __ Subs(out_lo, first_lo, second_lo);
1327 __ Sbc(out_hi, first_hi, second_hi);
1328 } else {
1329 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1330 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1331 }
1332}
1333
1334static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1335 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1336}
1337
1338static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1339 CodeGeneratorARMVIXL* codegen) {
1340 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1341 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1342
1343 const LocationSummary* const locations = instruction->GetLocations();
1344 const uint32_t shift_value = instruction->GetShiftAmount();
1345 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1346 const Location first = locations->InAt(0);
1347 const Location second = locations->InAt(1);
1348 const Location out = locations->Out();
1349 const vixl32::Register first_hi = HighRegisterFrom(first);
1350 const vixl32::Register first_lo = LowRegisterFrom(first);
1351 const vixl32::Register out_hi = HighRegisterFrom(out);
1352 const vixl32::Register out_lo = LowRegisterFrom(out);
1353 const vixl32::Register second_hi = HighRegisterFrom(second);
1354 const vixl32::Register second_lo = LowRegisterFrom(second);
1355 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1356
1357 if (shift_value >= 32) {
1358 if (shift == ShiftType::LSL) {
1359 GenerateDataProcInstruction(kind,
1360 out_hi,
1361 first_hi,
1362 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1363 codegen);
1364 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1365 } else if (shift == ShiftType::ASR) {
1366 GenerateDataProc(kind,
1367 out,
1368 first,
1369 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1370 Operand(second_hi, ShiftType::ASR, 31),
1371 codegen);
1372 } else {
1373 DCHECK_EQ(shift, ShiftType::LSR);
1374 GenerateDataProc(kind,
1375 out,
1376 first,
1377 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1378 0,
1379 codegen);
1380 }
1381 } else {
1382 DCHECK_GT(shift_value, 1U);
1383 DCHECK_LT(shift_value, 32U);
1384
1385 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1386
1387 if (shift == ShiftType::LSL) {
1388 // We are not doing this for HInstruction::kAdd because the output will require
1389 // Location::kOutputOverlap; not applicable to other cases.
1390 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1391 GenerateDataProcInstruction(kind,
1392 out_hi,
1393 first_hi,
1394 Operand(second_hi, ShiftType::LSL, shift_value),
1395 codegen);
1396 GenerateDataProcInstruction(kind,
1397 out_hi,
1398 out_hi,
1399 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1400 codegen);
1401 GenerateDataProcInstruction(kind,
1402 out_lo,
1403 first_lo,
1404 Operand(second_lo, ShiftType::LSL, shift_value),
1405 codegen);
1406 } else {
1407 const vixl32::Register temp = temps.Acquire();
1408
1409 __ Lsl(temp, second_hi, shift_value);
1410 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1411 GenerateDataProc(kind,
1412 out,
1413 first,
1414 Operand(second_lo, ShiftType::LSL, shift_value),
1415 temp,
1416 codegen);
1417 }
1418 } else {
1419 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1420
1421 // We are not doing this for HInstruction::kAdd because the output will require
1422 // Location::kOutputOverlap; not applicable to other cases.
1423 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1424 GenerateDataProcInstruction(kind,
1425 out_lo,
1426 first_lo,
1427 Operand(second_lo, ShiftType::LSR, shift_value),
1428 codegen);
1429 GenerateDataProcInstruction(kind,
1430 out_lo,
1431 out_lo,
1432 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1433 codegen);
1434 GenerateDataProcInstruction(kind,
1435 out_hi,
1436 first_hi,
1437 Operand(second_hi, shift, shift_value),
1438 codegen);
1439 } else {
1440 const vixl32::Register temp = temps.Acquire();
1441
1442 __ Lsr(temp, second_lo, shift_value);
1443 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1444 GenerateDataProc(kind,
1445 out,
1446 first,
1447 temp,
1448 Operand(second_hi, shift, shift_value),
1449 codegen);
1450 }
1451 }
1452 }
1453}
1454
Donghui Bai426b49c2016-11-08 14:55:38 +08001455static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1456 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1457 if (rhs_loc.IsConstant()) {
1458 // 0.0 is the only immediate that can be encoded directly in
1459 // a VCMP instruction.
1460 //
1461 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1462 // specify that in a floating-point comparison, positive zero
1463 // and negative zero are considered equal, so we can use the
1464 // literal 0.0 for both cases here.
1465 //
1466 // Note however that some methods (Float.equal, Float.compare,
1467 // Float.compareTo, Double.equal, Double.compare,
1468 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1469 // StrictMath.min) consider 0.0 to be (strictly) greater than
1470 // -0.0. So if we ever translate calls to these methods into a
1471 // HCompare instruction, we must handle the -0.0 case with
1472 // care here.
1473 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1474
1475 const Primitive::Type type = instruction->InputAt(0)->GetType();
1476
1477 if (type == Primitive::kPrimFloat) {
1478 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1479 } else {
1480 DCHECK_EQ(type, Primitive::kPrimDouble);
1481 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1482 }
1483 } else {
1484 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1485 }
1486}
1487
1488static vixl32::Condition GenerateLongTestConstant(HCondition* condition,
1489 bool invert,
1490 CodeGeneratorARMVIXL* codegen) {
1491 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1492
1493 const LocationSummary* const locations = condition->GetLocations();
1494 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1495 vixl32::Condition ret = eq;
1496 const Location left = locations->InAt(0);
1497 const Location right = locations->InAt(1);
1498
1499 DCHECK(right.IsConstant());
1500
1501 const vixl32::Register left_high = HighRegisterFrom(left);
1502 const vixl32::Register left_low = LowRegisterFrom(left);
1503 int64_t value = Int64ConstantFrom(right);
1504
1505 switch (cond) {
1506 case kCondEQ:
1507 case kCondNE:
1508 case kCondB:
1509 case kCondBE:
1510 case kCondA:
1511 case kCondAE: {
1512 __ Cmp(left_high, High32Bits(value));
1513
1514 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1515 2 * vixl32::k16BitT32InstructionSizeInBytes,
1516 CodeBufferCheckScope::kExactSize);
1517
1518 __ it(eq);
1519 __ cmp(eq, left_low, Low32Bits(value));
1520 ret = ARMUnsignedCondition(cond);
1521 break;
1522 }
1523 case kCondLE:
1524 case kCondGT:
1525 // Trivially true or false.
1526 if (value == std::numeric_limits<int64_t>::max()) {
1527 __ Cmp(left_low, left_low);
1528 ret = cond == kCondLE ? eq : ne;
1529 break;
1530 }
1531
1532 if (cond == kCondLE) {
1533 cond = kCondLT;
1534 } else {
1535 DCHECK_EQ(cond, kCondGT);
1536 cond = kCondGE;
1537 }
1538
1539 value++;
1540 FALLTHROUGH_INTENDED;
1541 case kCondGE:
1542 case kCondLT: {
1543 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1544
1545 __ Cmp(left_low, Low32Bits(value));
1546 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
1547 ret = ARMCondition(cond);
1548 break;
1549 }
1550 default:
1551 LOG(FATAL) << "Unreachable";
1552 UNREACHABLE();
1553 }
1554
1555 return ret;
1556}
1557
1558static vixl32::Condition GenerateLongTest(HCondition* condition,
1559 bool invert,
1560 CodeGeneratorARMVIXL* codegen) {
1561 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1562
1563 const LocationSummary* const locations = condition->GetLocations();
1564 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1565 vixl32::Condition ret = eq;
1566 Location left = locations->InAt(0);
1567 Location right = locations->InAt(1);
1568
1569 DCHECK(right.IsRegisterPair());
1570
1571 switch (cond) {
1572 case kCondEQ:
1573 case kCondNE:
1574 case kCondB:
1575 case kCondBE:
1576 case kCondA:
1577 case kCondAE: {
1578 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1579
1580 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1581 2 * vixl32::k16BitT32InstructionSizeInBytes,
1582 CodeBufferCheckScope::kExactSize);
1583
1584 __ it(eq);
1585 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
1586 ret = ARMUnsignedCondition(cond);
1587 break;
1588 }
1589 case kCondLE:
1590 case kCondGT:
1591 if (cond == kCondLE) {
1592 cond = kCondGE;
1593 } else {
1594 DCHECK_EQ(cond, kCondGT);
1595 cond = kCondLT;
1596 }
1597
1598 std::swap(left, right);
1599 FALLTHROUGH_INTENDED;
1600 case kCondGE:
1601 case kCondLT: {
1602 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1603
1604 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1605 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
1606 ret = ARMCondition(cond);
1607 break;
1608 }
1609 default:
1610 LOG(FATAL) << "Unreachable";
1611 UNREACHABLE();
1612 }
1613
1614 return ret;
1615}
1616
1617static vixl32::Condition GenerateTest(HInstruction* instruction,
1618 Location loc,
1619 bool invert,
1620 CodeGeneratorARMVIXL* codegen) {
1621 DCHECK(!instruction->IsConstant());
1622
1623 vixl32::Condition ret = invert ? eq : ne;
1624
1625 if (IsBooleanValueOrMaterializedCondition(instruction)) {
1626 __ Cmp(RegisterFrom(loc), 0);
1627 } else {
1628 HCondition* const condition = instruction->AsCondition();
1629 const Primitive::Type type = condition->GetLeft()->GetType();
1630 const IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1631
1632 if (type == Primitive::kPrimLong) {
1633 ret = condition->GetLocations()->InAt(1).IsConstant()
1634 ? GenerateLongTestConstant(condition, invert, codegen)
1635 : GenerateLongTest(condition, invert, codegen);
1636 } else if (Primitive::IsFloatingPointType(type)) {
1637 GenerateVcmp(condition, codegen);
1638 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1639 ret = ARMFPCondition(cond, condition->IsGtBias());
1640 } else {
1641 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1642 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1643 ret = ARMCondition(cond);
1644 }
1645 }
1646
1647 return ret;
1648}
1649
1650static bool CanGenerateTest(HInstruction* condition, ArmVIXLAssembler* assembler) {
1651 if (!IsBooleanValueOrMaterializedCondition(condition)) {
1652 const HCondition* const cond = condition->AsCondition();
1653
1654 if (cond->GetLeft()->GetType() == Primitive::kPrimLong) {
1655 const LocationSummary* const locations = cond->GetLocations();
1656 const IfCondition c = cond->GetCondition();
1657
1658 if (locations->InAt(1).IsConstant()) {
1659 const int64_t value = Int64ConstantFrom(locations->InAt(1));
1660
1661 if (c < kCondLT || c > kCondGE) {
1662 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1663 // we check that the least significant half of the first input to be compared
1664 // is in a low register (the other half is read outside an IT block), and
1665 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1666 // encoding can be used.
1667 if (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value))) {
1668 return false;
1669 }
1670 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
1671 // the previous one, but are not strictly necessary.
1672 } else if (c == kCondLE || c == kCondGT) {
1673 if (value < std::numeric_limits<int64_t>::max() &&
1674 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
1675 return false;
1676 }
1677 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
1678 return false;
1679 }
1680 }
1681 }
1682 }
1683
1684 return true;
1685}
1686
1687static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1688 const Primitive::Type type = constant->GetType();
1689 bool ret = false;
1690
1691 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1692
1693 if (type == Primitive::kPrimLong) {
1694 const uint64_t value = Uint64ConstantFrom(constant);
1695
1696 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1697 } else {
1698 ret = IsUint<8>(Int32ConstantFrom(constant));
1699 }
1700
1701 return ret;
1702}
1703
1704static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1705 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1706
1707 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1708 return Location::ConstantLocation(constant->AsConstant());
1709 }
1710
1711 return Location::RequiresRegister();
1712}
1713
1714static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1715 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1716 // we check that we are not dealing with floating-point output (there is no
1717 // 16-bit VMOV encoding).
1718 if (!out.IsRegister() && !out.IsRegisterPair()) {
1719 return false;
1720 }
1721
1722 // For constants, we also check that the output is in one or two low registers,
1723 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1724 // MOV encoding can be used.
1725 if (src.IsConstant()) {
1726 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1727 return false;
1728 }
1729
1730 if (out.IsRegister()) {
1731 if (!RegisterFrom(out).IsLow()) {
1732 return false;
1733 }
1734 } else {
1735 DCHECK(out.IsRegisterPair());
1736
1737 if (!HighRegisterFrom(out).IsLow()) {
1738 return false;
1739 }
1740 }
1741 }
1742
1743 return true;
1744}
1745
Scott Wakelingfe885462016-09-22 10:24:38 +01001746#undef __
1747
Donghui Bai426b49c2016-11-08 14:55:38 +08001748vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1749 vixl32::Label* final_label) {
1750 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
1751
1752 const HBasicBlock* const block = instruction->GetBlock();
1753 const HLoopInformation* const info = block->GetLoopInformation();
1754 HInstruction* const next = instruction->GetNext();
1755
1756 // Avoid a branch to a branch.
1757 if (next->IsGoto() && (info == nullptr ||
1758 !info->IsBackEdge(*block) ||
1759 !info->HasSuspendCheck())) {
1760 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1761 }
1762
1763 return final_label;
1764}
1765
Scott Wakelingfe885462016-09-22 10:24:38 +01001766CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1767 const ArmInstructionSetFeatures& isa_features,
1768 const CompilerOptions& compiler_options,
1769 OptimizingCompilerStats* stats)
1770 : CodeGenerator(graph,
1771 kNumberOfCoreRegisters,
1772 kNumberOfSRegisters,
1773 kNumberOfRegisterPairs,
1774 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001775 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001776 compiler_options,
1777 stats),
1778 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001779 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001780 location_builder_(graph, this),
1781 instruction_visitor_(graph, this),
1782 move_resolver_(graph->GetArena(), this),
1783 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001784 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001785 uint32_literals_(std::less<uint32_t>(),
1786 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001787 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001788 boot_image_string_patches_(StringReferenceValueComparator(),
1789 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001790 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001791 boot_image_type_patches_(TypeReferenceValueComparator(),
1792 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1793 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001794 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001795 boot_image_address_patches_(std::less<uint32_t>(),
1796 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1797 jit_string_patches_(StringReferenceValueComparator(),
1798 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1799 jit_class_patches_(TypeReferenceValueComparator(),
1800 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001801 // Always save the LR register to mimic Quick.
1802 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001803 // Give d14 and d15 as scratch registers to VIXL.
1804 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1805 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1806 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1807 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1808 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1809 // also want to investigate giving those 14 other D registers to the allocator.
1810 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1811 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001812}
1813
Artem Serov551b28f2016-10-18 19:11:30 +01001814void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1815 uint32_t num_entries = switch_instr_->GetNumEntries();
1816 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1817
1818 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001819 // underlying code buffer and we have generated a jump table of the right size, using
1820 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001821 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1822 num_entries * sizeof(int32_t),
1823 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001824 // TODO(VIXL): Check that using lower case bind is fine here.
1825 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001826 for (uint32_t i = 0; i < num_entries; i++) {
1827 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1828 }
1829}
1830
1831void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1832 uint32_t num_entries = switch_instr_->GetNumEntries();
1833 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1834
Artem Serov551b28f2016-10-18 19:11:30 +01001835 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1836 for (uint32_t i = 0; i < num_entries; i++) {
1837 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1838 DCHECK(target_label->IsBound());
1839 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1840 // When doing BX to address we need to have lower bit set to 1 in T32.
1841 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1842 jump_offset++;
1843 }
1844 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1845 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001846
Scott Wakelingb77051e2016-11-21 19:46:00 +00001847 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001848 }
1849}
1850
Artem Serov09a940d2016-11-11 16:15:11 +00001851void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001852 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001853 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001854 }
1855}
1856
Andreas Gampeca620d72016-11-08 08:09:33 -08001857#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001858
1859void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001860 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001861 GetAssembler()->FinalizeCode();
1862 CodeGenerator::Finalize(allocator);
1863}
1864
1865void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001866 // Stack register, LR and PC are always reserved.
1867 blocked_core_registers_[SP] = true;
1868 blocked_core_registers_[LR] = true;
1869 blocked_core_registers_[PC] = true;
1870
1871 // Reserve thread register.
1872 blocked_core_registers_[TR] = true;
1873
1874 // Reserve temp register.
1875 blocked_core_registers_[IP] = true;
1876
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001877 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1878 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1879 blocked_fpu_registers_[28] = true;
1880 blocked_fpu_registers_[29] = true;
1881 blocked_fpu_registers_[30] = true;
1882 blocked_fpu_registers_[31] = true;
1883
Scott Wakelingfe885462016-09-22 10:24:38 +01001884 if (GetGraph()->IsDebuggable()) {
1885 // Stubs do not save callee-save floating point registers. If the graph
1886 // is debuggable, we need to deal with these registers differently. For
1887 // now, just block them.
1888 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1889 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1890 ++i) {
1891 blocked_fpu_registers_[i] = true;
1892 }
1893 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001894}
1895
Scott Wakelingfe885462016-09-22 10:24:38 +01001896InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1897 CodeGeneratorARMVIXL* codegen)
1898 : InstructionCodeGenerator(graph, codegen),
1899 assembler_(codegen->GetAssembler()),
1900 codegen_(codegen) {}
1901
1902void CodeGeneratorARMVIXL::ComputeSpillMask() {
1903 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1904 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1905 // There is no easy instruction to restore just the PC on thumb2. We spill and
1906 // restore another arbitrary register.
1907 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1908 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1909 // We use vpush and vpop for saving and restoring floating point registers, which take
1910 // a SRegister and the number of registers to save/restore after that SRegister. We
1911 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1912 // but in the range.
1913 if (fpu_spill_mask_ != 0) {
1914 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1915 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1916 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1917 fpu_spill_mask_ |= (1 << i);
1918 }
1919 }
1920}
1921
1922void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1923 bool skip_overflow_check =
1924 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1925 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1926 __ Bind(&frame_entry_label_);
1927
1928 if (HasEmptyFrame()) {
1929 return;
1930 }
1931
Scott Wakelingfe885462016-09-22 10:24:38 +01001932 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001933 UseScratchRegisterScope temps(GetVIXLAssembler());
1934 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001935 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001936 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001937 ExactAssemblyScope aas(GetVIXLAssembler(),
1938 vixl32::kMaxInstructionSizeInBytes,
1939 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001940 __ ldr(temp, MemOperand(temp));
1941 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001942 }
1943
1944 __ Push(RegisterList(core_spill_mask_));
1945 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1946 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1947 0,
1948 core_spill_mask_,
1949 kArmWordSize);
1950 if (fpu_spill_mask_ != 0) {
1951 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1952
1953 // Check that list is contiguous.
1954 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1955
1956 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1957 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001958 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001959 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001960
1961 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1962 UseScratchRegisterScope temps(GetVIXLAssembler());
1963 vixl32::Register temp = temps.Acquire();
1964 // Initialize should_deoptimize flag to 0.
1965 __ Mov(temp, 0);
1966 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1967 }
1968
Scott Wakelingfe885462016-09-22 10:24:38 +01001969 int adjust = GetFrameSize() - FrameEntrySpillSize();
1970 __ Sub(sp, sp, adjust);
1971 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001972
1973 // Save the current method if we need it. Note that we do not
1974 // do this in HCurrentMethod, as the instruction might have been removed
1975 // in the SSA graph.
1976 if (RequiresCurrentMethod()) {
1977 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1978 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001979}
1980
1981void CodeGeneratorARMVIXL::GenerateFrameExit() {
1982 if (HasEmptyFrame()) {
1983 __ Bx(lr);
1984 return;
1985 }
1986 GetAssembler()->cfi().RememberState();
1987 int adjust = GetFrameSize() - FrameEntrySpillSize();
1988 __ Add(sp, sp, adjust);
1989 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1990 if (fpu_spill_mask_ != 0) {
1991 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1992
1993 // Check that list is contiguous.
1994 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1995
1996 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1997 GetAssembler()->cfi().AdjustCFAOffset(
1998 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001999 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002000 }
2001 // Pop LR into PC to return.
2002 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2003 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2004 __ Pop(RegisterList(pop_mask));
2005 GetAssembler()->cfi().RestoreState();
2006 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2007}
2008
2009void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2010 __ Bind(GetLabelOf(block));
2011}
2012
Artem Serovd4cc5b22016-11-04 11:19:09 +00002013Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2014 switch (type) {
2015 case Primitive::kPrimBoolean:
2016 case Primitive::kPrimByte:
2017 case Primitive::kPrimChar:
2018 case Primitive::kPrimShort:
2019 case Primitive::kPrimInt:
2020 case Primitive::kPrimNot: {
2021 uint32_t index = gp_index_++;
2022 uint32_t stack_index = stack_index_++;
2023 if (index < calling_convention.GetNumberOfRegisters()) {
2024 return LocationFrom(calling_convention.GetRegisterAt(index));
2025 } else {
2026 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2027 }
2028 }
2029
2030 case Primitive::kPrimLong: {
2031 uint32_t index = gp_index_;
2032 uint32_t stack_index = stack_index_;
2033 gp_index_ += 2;
2034 stack_index_ += 2;
2035 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2036 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2037 // Skip R1, and use R2_R3 instead.
2038 gp_index_++;
2039 index++;
2040 }
2041 }
2042 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2043 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2044 calling_convention.GetRegisterAt(index + 1).GetCode());
2045
2046 return LocationFrom(calling_convention.GetRegisterAt(index),
2047 calling_convention.GetRegisterAt(index + 1));
2048 } else {
2049 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2050 }
2051 }
2052
2053 case Primitive::kPrimFloat: {
2054 uint32_t stack_index = stack_index_++;
2055 if (float_index_ % 2 == 0) {
2056 float_index_ = std::max(double_index_, float_index_);
2057 }
2058 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2059 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2060 } else {
2061 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2062 }
2063 }
2064
2065 case Primitive::kPrimDouble: {
2066 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2067 uint32_t stack_index = stack_index_;
2068 stack_index_ += 2;
2069 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2070 uint32_t index = double_index_;
2071 double_index_ += 2;
2072 Location result = LocationFrom(
2073 calling_convention.GetFpuRegisterAt(index),
2074 calling_convention.GetFpuRegisterAt(index + 1));
2075 DCHECK(ExpectedPairLayout(result));
2076 return result;
2077 } else {
2078 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2079 }
2080 }
2081
2082 case Primitive::kPrimVoid:
2083 LOG(FATAL) << "Unexpected parameter type " << type;
2084 break;
2085 }
2086 return Location::NoLocation();
2087}
2088
2089Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2090 switch (type) {
2091 case Primitive::kPrimBoolean:
2092 case Primitive::kPrimByte:
2093 case Primitive::kPrimChar:
2094 case Primitive::kPrimShort:
2095 case Primitive::kPrimInt:
2096 case Primitive::kPrimNot: {
2097 return LocationFrom(r0);
2098 }
2099
2100 case Primitive::kPrimFloat: {
2101 return LocationFrom(s0);
2102 }
2103
2104 case Primitive::kPrimLong: {
2105 return LocationFrom(r0, r1);
2106 }
2107
2108 case Primitive::kPrimDouble: {
2109 return LocationFrom(s0, s1);
2110 }
2111
2112 case Primitive::kPrimVoid:
2113 return Location::NoLocation();
2114 }
2115
2116 UNREACHABLE();
2117}
2118
2119Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2120 return LocationFrom(kMethodRegister);
2121}
2122
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002123void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2124 if (source.Equals(destination)) {
2125 return;
2126 }
2127 if (destination.IsRegister()) {
2128 if (source.IsRegister()) {
2129 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2130 } else if (source.IsFpuRegister()) {
2131 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2132 } else {
2133 GetAssembler()->LoadFromOffset(kLoadWord,
2134 RegisterFrom(destination),
2135 sp,
2136 source.GetStackIndex());
2137 }
2138 } else if (destination.IsFpuRegister()) {
2139 if (source.IsRegister()) {
2140 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2141 } else if (source.IsFpuRegister()) {
2142 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2143 } else {
2144 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2145 }
2146 } else {
2147 DCHECK(destination.IsStackSlot()) << destination;
2148 if (source.IsRegister()) {
2149 GetAssembler()->StoreToOffset(kStoreWord,
2150 RegisterFrom(source),
2151 sp,
2152 destination.GetStackIndex());
2153 } else if (source.IsFpuRegister()) {
2154 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2155 } else {
2156 DCHECK(source.IsStackSlot()) << source;
2157 UseScratchRegisterScope temps(GetVIXLAssembler());
2158 vixl32::Register temp = temps.Acquire();
2159 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2160 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2161 }
2162 }
2163}
2164
Artem Serovcfbe9132016-10-14 15:58:56 +01002165void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2166 DCHECK(location.IsRegister());
2167 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002168}
2169
2170void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002171 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2172 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2173 HParallelMove move(GetGraph()->GetArena());
2174 move.AddMove(src, dst, dst_type, nullptr);
2175 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002176}
2177
Artem Serovcfbe9132016-10-14 15:58:56 +01002178void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2179 if (location.IsRegister()) {
2180 locations->AddTemp(location);
2181 } else if (location.IsRegisterPair()) {
2182 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2183 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2184 } else {
2185 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2186 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002187}
2188
2189void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2190 HInstruction* instruction,
2191 uint32_t dex_pc,
2192 SlowPathCode* slow_path) {
2193 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002194 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2195 // Ensure the pc position is recorded immediately after the `blx` instruction.
2196 // 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 +00002197 ExactAssemblyScope aas(GetVIXLAssembler(),
2198 vixl32::k16BitT32InstructionSizeInBytes,
2199 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002200 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002201 if (EntrypointRequiresStackMap(entrypoint)) {
2202 RecordPcInfo(instruction, dex_pc, slow_path);
2203 }
2204}
2205
2206void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2207 HInstruction* instruction,
2208 SlowPathCode* slow_path) {
2209 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002210 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002211 __ Blx(lr);
2212}
2213
Scott Wakelingfe885462016-09-22 10:24:38 +01002214void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2215 DCHECK(!successor->IsExitBlock());
2216 HBasicBlock* block = got->GetBlock();
2217 HInstruction* previous = got->GetPrevious();
2218 HLoopInformation* info = block->GetLoopInformation();
2219
2220 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2221 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2222 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2223 return;
2224 }
2225 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2226 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2227 }
2228 if (!codegen_->GoesToNextBlock(block, successor)) {
2229 __ B(codegen_->GetLabelOf(successor));
2230 }
2231}
2232
2233void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2234 got->SetLocations(nullptr);
2235}
2236
2237void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2238 HandleGoto(got, got->GetSuccessor());
2239}
2240
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002241void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2242 try_boundary->SetLocations(nullptr);
2243}
2244
2245void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2246 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2247 if (!successor->IsExitBlock()) {
2248 HandleGoto(try_boundary, successor);
2249 }
2250}
2251
Scott Wakelingfe885462016-09-22 10:24:38 +01002252void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2253 exit->SetLocations(nullptr);
2254}
2255
2256void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2257}
2258
Scott Wakelingfe885462016-09-22 10:24:38 +01002259void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
2260 vixl32::Label* true_label,
2261 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
2262 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
2263 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2264 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
2265}
2266
2267void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2268 vixl32::Label* true_label,
2269 vixl32::Label* false_label) {
2270 LocationSummary* locations = cond->GetLocations();
2271 Location left = locations->InAt(0);
2272 Location right = locations->InAt(1);
2273 IfCondition if_cond = cond->GetCondition();
2274
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002275 vixl32::Register left_high = HighRegisterFrom(left);
2276 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01002277 IfCondition true_high_cond = if_cond;
2278 IfCondition false_high_cond = cond->GetOppositeCondition();
2279 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2280
2281 // Set the conditions for the test, remembering that == needs to be
2282 // decided using the low words.
2283 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
2284 switch (if_cond) {
2285 case kCondEQ:
2286 case kCondNE:
2287 // Nothing to do.
2288 break;
2289 case kCondLT:
2290 false_high_cond = kCondGT;
2291 break;
2292 case kCondLE:
2293 true_high_cond = kCondLT;
2294 break;
2295 case kCondGT:
2296 false_high_cond = kCondLT;
2297 break;
2298 case kCondGE:
2299 true_high_cond = kCondGT;
2300 break;
2301 case kCondB:
2302 false_high_cond = kCondA;
2303 break;
2304 case kCondBE:
2305 true_high_cond = kCondB;
2306 break;
2307 case kCondA:
2308 false_high_cond = kCondB;
2309 break;
2310 case kCondAE:
2311 true_high_cond = kCondA;
2312 break;
2313 }
2314 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00002315 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002316 int32_t val_low = Low32Bits(value);
2317 int32_t val_high = High32Bits(value);
2318
2319 __ Cmp(left_high, val_high);
2320 if (if_cond == kCondNE) {
2321 __ B(ARMCondition(true_high_cond), true_label);
2322 } else if (if_cond == kCondEQ) {
2323 __ B(ARMCondition(false_high_cond), false_label);
2324 } else {
2325 __ B(ARMCondition(true_high_cond), true_label);
2326 __ B(ARMCondition(false_high_cond), false_label);
2327 }
2328 // Must be equal high, so compare the lows.
2329 __ Cmp(left_low, val_low);
2330 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002331 vixl32::Register right_high = HighRegisterFrom(right);
2332 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01002333
2334 __ Cmp(left_high, right_high);
2335 if (if_cond == kCondNE) {
2336 __ B(ARMCondition(true_high_cond), true_label);
2337 } else if (if_cond == kCondEQ) {
2338 __ B(ARMCondition(false_high_cond), false_label);
2339 } else {
2340 __ B(ARMCondition(true_high_cond), true_label);
2341 __ B(ARMCondition(false_high_cond), false_label);
2342 }
2343 // Must be equal high, so compare the lows.
2344 __ Cmp(left_low, right_low);
2345 }
2346 // The last comparison might be unsigned.
2347 // TODO: optimize cases where this is always true/false
2348 __ B(final_condition, true_label);
2349}
2350
2351void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2352 vixl32::Label* true_target_in,
2353 vixl32::Label* false_target_in) {
2354 // Generated branching requires both targets to be explicit. If either of the
2355 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2356 vixl32::Label fallthrough;
2357 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2358 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2359
2360 Primitive::Type type = condition->InputAt(0)->GetType();
2361 switch (type) {
2362 case Primitive::kPrimLong:
2363 GenerateLongComparesAndJumps(condition, true_target, false_target);
2364 break;
2365 case Primitive::kPrimFloat:
2366 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002367 GenerateVcmp(condition, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002368 GenerateFPJumps(condition, true_target, false_target);
2369 break;
2370 default:
2371 LOG(FATAL) << "Unexpected compare type " << type;
2372 }
2373
2374 if (false_target != &fallthrough) {
2375 __ B(false_target);
2376 }
2377
2378 if (true_target_in == nullptr || false_target_in == nullptr) {
2379 __ Bind(&fallthrough);
2380 }
2381}
2382
2383void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2384 size_t condition_input_index,
2385 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002386 vixl32::Label* false_target,
2387 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002388 HInstruction* cond = instruction->InputAt(condition_input_index);
2389
2390 if (true_target == nullptr && false_target == nullptr) {
2391 // Nothing to do. The code always falls through.
2392 return;
2393 } else if (cond->IsIntConstant()) {
2394 // Constant condition, statically compared against "true" (integer value 1).
2395 if (cond->AsIntConstant()->IsTrue()) {
2396 if (true_target != nullptr) {
2397 __ B(true_target);
2398 }
2399 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002400 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002401 if (false_target != nullptr) {
2402 __ B(false_target);
2403 }
2404 }
2405 return;
2406 }
2407
2408 // The following code generates these patterns:
2409 // (1) true_target == nullptr && false_target != nullptr
2410 // - opposite condition true => branch to false_target
2411 // (2) true_target != nullptr && false_target == nullptr
2412 // - condition true => branch to true_target
2413 // (3) true_target != nullptr && false_target != nullptr
2414 // - condition true => branch to true_target
2415 // - branch to false_target
2416 if (IsBooleanValueOrMaterializedCondition(cond)) {
2417 // Condition has been materialized, compare the output to 0.
2418 if (kIsDebugBuild) {
2419 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2420 DCHECK(cond_val.IsRegister());
2421 }
2422 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002423 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2424 false_target,
2425 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002426 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002427 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2428 true_target,
2429 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002430 }
2431 } else {
2432 // Condition has not been materialized. Use its inputs as the comparison and
2433 // its condition as the branch condition.
2434 HCondition* condition = cond->AsCondition();
2435
2436 // If this is a long or FP comparison that has been folded into
2437 // the HCondition, generate the comparison directly.
2438 Primitive::Type type = condition->InputAt(0)->GetType();
2439 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2440 GenerateCompareTestAndBranch(condition, true_target, false_target);
2441 return;
2442 }
2443
Donghui Bai426b49c2016-11-08 14:55:38 +08002444 vixl32::Label* non_fallthrough_target;
2445 vixl32::Condition arm_cond = vixl32::Condition::None();
2446 const vixl32::Register left = InputRegisterAt(cond, 0);
2447 const Operand right = InputOperandAt(cond, 1);
2448
Scott Wakelingfe885462016-09-22 10:24:38 +01002449 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002450 arm_cond = ARMCondition(condition->GetOppositeCondition());
2451 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002452 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002453 arm_cond = ARMCondition(condition->GetCondition());
2454 non_fallthrough_target = true_target;
2455 }
2456
2457 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2458 if (arm_cond.Is(eq)) {
2459 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2460 } else {
2461 DCHECK(arm_cond.Is(ne));
2462 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2463 }
2464 } else {
2465 __ Cmp(left, right);
2466 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002467 }
2468 }
2469
2470 // If neither branch falls through (case 3), the conditional branch to `true_target`
2471 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2472 if (true_target != nullptr && false_target != nullptr) {
2473 __ B(false_target);
2474 }
2475}
2476
2477void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2479 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2480 locations->SetInAt(0, Location::RequiresRegister());
2481 }
2482}
2483
2484void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2485 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2486 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002487 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2488 nullptr : codegen_->GetLabelOf(true_successor);
2489 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2490 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002491 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2492}
2493
Scott Wakelingc34dba72016-10-03 10:14:44 +01002494void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2495 LocationSummary* locations = new (GetGraph()->GetArena())
2496 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2497 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2498 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2499 locations->SetInAt(0, Location::RequiresRegister());
2500 }
2501}
2502
2503void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2504 SlowPathCodeARMVIXL* slow_path =
2505 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2506 GenerateTestAndBranch(deoptimize,
2507 /* condition_input_index */ 0,
2508 slow_path->GetEntryLabel(),
2509 /* false_target */ nullptr);
2510}
2511
Artem Serovd4cc5b22016-11-04 11:19:09 +00002512void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2513 LocationSummary* locations = new (GetGraph()->GetArena())
2514 LocationSummary(flag, LocationSummary::kNoCall);
2515 locations->SetOut(Location::RequiresRegister());
2516}
2517
2518void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2519 GetAssembler()->LoadFromOffset(kLoadWord,
2520 OutputRegister(flag),
2521 sp,
2522 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2523}
2524
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002525void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002527 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2528
2529 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002530 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002531 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002532 } else {
2533 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002534 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002535 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002536
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002537 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002538 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2539 // The code generator handles overlap with the values, but not with the condition.
2540 locations->SetOut(Location::SameAsFirstInput());
2541 } else if (is_floating_point) {
2542 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2543 } else {
2544 if (!locations->InAt(1).IsConstant()) {
2545 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2546 }
2547
2548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002549 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002550}
2551
2552void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002553 HInstruction* const condition = select->GetCondition();
2554 const LocationSummary* const locations = select->GetLocations();
2555 const Primitive::Type type = select->GetType();
2556 const Location first = locations->InAt(0);
2557 const Location out = locations->Out();
2558 const Location second = locations->InAt(1);
2559 Location src;
2560
2561 if (condition->IsIntConstant()) {
2562 if (condition->AsIntConstant()->IsFalse()) {
2563 src = first;
2564 } else {
2565 src = second;
2566 }
2567
2568 codegen_->MoveLocation(out, src, type);
2569 return;
2570 }
2571
2572 if (!Primitive::IsFloatingPointType(type) &&
2573 CanGenerateTest(condition, codegen_->GetAssembler())) {
2574 bool invert = false;
2575
2576 if (out.Equals(second)) {
2577 src = first;
2578 invert = true;
2579 } else if (out.Equals(first)) {
2580 src = second;
2581 } else if (second.IsConstant()) {
2582 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2583 src = second;
2584 } else if (first.IsConstant()) {
2585 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2586 src = first;
2587 invert = true;
2588 } else {
2589 src = second;
2590 }
2591
2592 if (CanGenerateConditionalMove(out, src)) {
2593 if (!out.Equals(first) && !out.Equals(second)) {
2594 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2595 }
2596
2597 const vixl32::Condition cond = GenerateTest(condition, locations->InAt(2), invert, codegen_);
2598 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
2599 ExactAssemblyScope guard(GetVIXLAssembler(),
2600 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2601 CodeBufferCheckScope::kExactSize);
2602
2603 if (out.IsRegister()) {
2604 __ it(cond);
2605 __ mov(cond, RegisterFrom(out), OperandFrom(src, type));
2606 } else {
2607 DCHECK(out.IsRegisterPair());
2608
2609 Operand operand_high(0);
2610 Operand operand_low(0);
2611
2612 if (src.IsConstant()) {
2613 const int64_t value = Int64ConstantFrom(src);
2614
2615 operand_high = High32Bits(value);
2616 operand_low = Low32Bits(value);
2617 } else {
2618 DCHECK(src.IsRegisterPair());
2619 operand_high = HighRegisterFrom(src);
2620 operand_low = LowRegisterFrom(src);
2621 }
2622
2623 __ it(cond);
2624 __ mov(cond, LowRegisterFrom(out), operand_low);
2625 __ it(cond);
2626 __ mov(cond, HighRegisterFrom(out), operand_high);
2627 }
2628
2629 return;
2630 }
2631 }
2632
2633 vixl32::Label* false_target = nullptr;
2634 vixl32::Label* true_target = nullptr;
2635 vixl32::Label select_end;
2636 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2637
2638 if (out.Equals(second)) {
2639 true_target = target;
2640 src = first;
2641 } else {
2642 false_target = target;
2643 src = second;
2644
2645 if (!out.Equals(first)) {
2646 codegen_->MoveLocation(out, first, type);
2647 }
2648 }
2649
2650 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2651 codegen_->MoveLocation(out, src, type);
2652
2653 if (select_end.IsReferenced()) {
2654 __ Bind(&select_end);
2655 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002656}
2657
Artem Serov551b28f2016-10-18 19:11:30 +01002658void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2659 new (GetGraph()->GetArena()) LocationSummary(info);
2660}
2661
2662void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2663 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2664}
2665
Scott Wakelingfe885462016-09-22 10:24:38 +01002666void CodeGeneratorARMVIXL::GenerateNop() {
2667 __ Nop();
2668}
2669
2670void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2671 LocationSummary* locations =
2672 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2673 // Handle the long/FP comparisons made in instruction simplification.
2674 switch (cond->InputAt(0)->GetType()) {
2675 case Primitive::kPrimLong:
2676 locations->SetInAt(0, Location::RequiresRegister());
2677 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2678 if (!cond->IsEmittedAtUseSite()) {
2679 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2680 }
2681 break;
2682
Scott Wakelingfe885462016-09-22 10:24:38 +01002683 case Primitive::kPrimFloat:
2684 case Primitive::kPrimDouble:
2685 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002686 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002687 if (!cond->IsEmittedAtUseSite()) {
2688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2689 }
2690 break;
2691
2692 default:
2693 locations->SetInAt(0, Location::RequiresRegister());
2694 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2695 if (!cond->IsEmittedAtUseSite()) {
2696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2697 }
2698 }
2699}
2700
2701void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2702 if (cond->IsEmittedAtUseSite()) {
2703 return;
2704 }
2705
Artem Serov657022c2016-11-23 14:19:38 +00002706 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002707 vixl32::Register out = OutputRegister(cond);
2708 vixl32::Label true_label, false_label;
2709
2710 switch (cond->InputAt(0)->GetType()) {
2711 default: {
2712 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002713 if (right.IsRegister()) {
2714 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2715 } else {
2716 DCHECK(right.IsConstant());
2717 __ Cmp(InputRegisterAt(cond, 0),
2718 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2719 }
Artem Serov0fb37192016-12-06 18:13:40 +00002720 ExactAssemblyScope aas(GetVIXLAssembler(),
2721 3 * vixl32::kMaxInstructionSizeInBytes,
2722 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002723 __ ite(ARMCondition(cond->GetCondition()));
2724 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2725 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002726 return;
2727 }
2728 case Primitive::kPrimLong:
2729 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2730 break;
2731 case Primitive::kPrimFloat:
2732 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002733 GenerateVcmp(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002734 GenerateFPJumps(cond, &true_label, &false_label);
2735 break;
2736 }
2737
2738 // Convert the jumps into the result.
2739 vixl32::Label done_label;
2740
2741 // False case: result = 0.
2742 __ Bind(&false_label);
2743 __ Mov(out, 0);
2744 __ B(&done_label);
2745
2746 // True case: result = 1.
2747 __ Bind(&true_label);
2748 __ Mov(out, 1);
2749 __ Bind(&done_label);
2750}
2751
2752void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2753 HandleCondition(comp);
2754}
2755
2756void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2757 HandleCondition(comp);
2758}
2759
2760void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2761 HandleCondition(comp);
2762}
2763
2764void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2765 HandleCondition(comp);
2766}
2767
2768void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2769 HandleCondition(comp);
2770}
2771
2772void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2773 HandleCondition(comp);
2774}
2775
2776void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2777 HandleCondition(comp);
2778}
2779
2780void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2781 HandleCondition(comp);
2782}
2783
2784void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2785 HandleCondition(comp);
2786}
2787
2788void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2789 HandleCondition(comp);
2790}
2791
2792void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2793 HandleCondition(comp);
2794}
2795
2796void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2797 HandleCondition(comp);
2798}
2799
2800void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2801 HandleCondition(comp);
2802}
2803
2804void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2805 HandleCondition(comp);
2806}
2807
2808void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2809 HandleCondition(comp);
2810}
2811
2812void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2813 HandleCondition(comp);
2814}
2815
2816void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2817 HandleCondition(comp);
2818}
2819
2820void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2821 HandleCondition(comp);
2822}
2823
2824void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2825 HandleCondition(comp);
2826}
2827
2828void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2829 HandleCondition(comp);
2830}
2831
2832void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2833 LocationSummary* locations =
2834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2835 locations->SetOut(Location::ConstantLocation(constant));
2836}
2837
2838void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2839 // Will be generated at use site.
2840}
2841
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002842void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2843 LocationSummary* locations =
2844 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2845 locations->SetOut(Location::ConstantLocation(constant));
2846}
2847
2848void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2849 // Will be generated at use site.
2850}
2851
Scott Wakelingfe885462016-09-22 10:24:38 +01002852void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2853 LocationSummary* locations =
2854 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2855 locations->SetOut(Location::ConstantLocation(constant));
2856}
2857
2858void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2859 // Will be generated at use site.
2860}
2861
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002862void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2865 locations->SetOut(Location::ConstantLocation(constant));
2866}
2867
Scott Wakelingc34dba72016-10-03 10:14:44 +01002868void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2869 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002870 // Will be generated at use site.
2871}
2872
2873void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2876 locations->SetOut(Location::ConstantLocation(constant));
2877}
2878
Scott Wakelingc34dba72016-10-03 10:14:44 +01002879void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2880 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002881 // Will be generated at use site.
2882}
2883
Scott Wakelingfe885462016-09-22 10:24:38 +01002884void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2885 memory_barrier->SetLocations(nullptr);
2886}
2887
2888void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2889 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2890}
2891
2892void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2893 ret->SetLocations(nullptr);
2894}
2895
2896void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2897 codegen_->GenerateFrameExit();
2898}
2899
2900void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2901 LocationSummary* locations =
2902 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2903 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2904}
2905
2906void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2907 codegen_->GenerateFrameExit();
2908}
2909
Artem Serovcfbe9132016-10-14 15:58:56 +01002910void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2911 // The trampoline uses the same calling convention as dex calling conventions,
2912 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2913 // the method_idx.
2914 HandleInvoke(invoke);
2915}
2916
2917void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2918 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2919}
2920
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002921void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2922 // Explicit clinit checks triggered by static invokes must have been pruned by
2923 // art::PrepareForRegisterAllocation.
2924 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2925
Anton Kirilov5ec62182016-10-13 20:16:02 +01002926 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2927 if (intrinsic.TryDispatch(invoke)) {
2928 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2929 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2930 }
2931 return;
2932 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002933
2934 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002935
Artem Serovd4cc5b22016-11-04 11:19:09 +00002936 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2937 if (invoke->HasPcRelativeDexCache()) {
2938 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2939 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002940}
2941
Anton Kirilov5ec62182016-10-13 20:16:02 +01002942static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2943 if (invoke->GetLocations()->Intrinsified()) {
2944 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2945 intrinsic.Dispatch(invoke);
2946 return true;
2947 }
2948 return false;
2949}
2950
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002951void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2952 // Explicit clinit checks triggered by static invokes must have been pruned by
2953 // art::PrepareForRegisterAllocation.
2954 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2955
Anton Kirilov5ec62182016-10-13 20:16:02 +01002956 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2957 return;
2958 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002959
2960 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002961 codegen_->GenerateStaticOrDirectCall(
2962 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002963 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2964}
2965
2966void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002967 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002968 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2969}
2970
2971void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002972 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2973 if (intrinsic.TryDispatch(invoke)) {
2974 return;
2975 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002976
2977 HandleInvoke(invoke);
2978}
2979
2980void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002981 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2982 return;
2983 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002984
2985 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002986 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002987 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002988}
2989
Artem Serovcfbe9132016-10-14 15:58:56 +01002990void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2991 HandleInvoke(invoke);
2992 // Add the hidden argument.
2993 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2994}
2995
2996void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2997 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2998 LocationSummary* locations = invoke->GetLocations();
2999 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3000 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3001 Location receiver = locations->InAt(0);
3002 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3003
3004 DCHECK(!receiver.IsStackSlot());
3005
Alexandre Rames374ddf32016-11-04 10:40:49 +00003006 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3007 {
Artem Serov0fb37192016-12-06 18:13:40 +00003008 ExactAssemblyScope aas(GetVIXLAssembler(),
3009 vixl32::kMaxInstructionSizeInBytes,
3010 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003011 // /* HeapReference<Class> */ temp = receiver->klass_
3012 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3013 codegen_->MaybeRecordImplicitNullCheck(invoke);
3014 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003015 // Instead of simply (possibly) unpoisoning `temp` here, we should
3016 // emit a read barrier for the previous class reference load.
3017 // However this is not required in practice, as this is an
3018 // intermediate/temporary reference and because the current
3019 // concurrent copying collector keeps the from-space memory
3020 // intact/accessible until the end of the marking phase (the
3021 // concurrent copying collector may not in the future).
3022 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3023 GetAssembler()->LoadFromOffset(kLoadWord,
3024 temp,
3025 temp,
3026 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3027 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3028 invoke->GetImtIndex(), kArmPointerSize));
3029 // temp = temp->GetImtEntryAt(method_offset);
3030 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3031 uint32_t entry_point =
3032 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3033 // LR = temp->GetEntryPoint();
3034 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3035
3036 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3037 // instruction from clobbering it as they might use r12 as a scratch register.
3038 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003039
3040 {
3041 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3042 // so it checks if the application is using them (by passing them to the macro assembler
3043 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3044 // what is available, and is the opposite of the standard usage: Instead of requesting a
3045 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3046 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3047 // (to materialize the constant), since the destination register becomes available for such use
3048 // internally for the duration of the macro instruction.
3049 UseScratchRegisterScope temps(GetVIXLAssembler());
3050 temps.Exclude(hidden_reg);
3051 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3052 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003053 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003054 // Ensure the pc position is recorded immediately after the `blx` instruction.
3055 // 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 +00003056 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003057 vixl32::k16BitT32InstructionSizeInBytes,
3058 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003059 // LR();
3060 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003061 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003062 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003063 }
3064}
3065
Orion Hodsonac141392017-01-13 11:53:47 +00003066void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3067 HandleInvoke(invoke);
3068}
3069
3070void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3071 codegen_->GenerateInvokePolymorphicCall(invoke);
3072}
3073
Artem Serov02109dd2016-09-23 17:17:54 +01003074void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3075 LocationSummary* locations =
3076 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3077 switch (neg->GetResultType()) {
3078 case Primitive::kPrimInt: {
3079 locations->SetInAt(0, Location::RequiresRegister());
3080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3081 break;
3082 }
3083 case Primitive::kPrimLong: {
3084 locations->SetInAt(0, Location::RequiresRegister());
3085 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3086 break;
3087 }
3088
3089 case Primitive::kPrimFloat:
3090 case Primitive::kPrimDouble:
3091 locations->SetInAt(0, Location::RequiresFpuRegister());
3092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3093 break;
3094
3095 default:
3096 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3097 }
3098}
3099
3100void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3101 LocationSummary* locations = neg->GetLocations();
3102 Location out = locations->Out();
3103 Location in = locations->InAt(0);
3104 switch (neg->GetResultType()) {
3105 case Primitive::kPrimInt:
3106 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3107 break;
3108
3109 case Primitive::kPrimLong:
3110 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3111 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3112 // We cannot emit an RSC (Reverse Subtract with Carry)
3113 // instruction here, as it does not exist in the Thumb-2
3114 // instruction set. We use the following approach
3115 // using SBC and SUB instead.
3116 //
3117 // out.hi = -C
3118 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3119 // out.hi = out.hi - in.hi
3120 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3121 break;
3122
3123 case Primitive::kPrimFloat:
3124 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003125 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003126 break;
3127
3128 default:
3129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3130 }
3131}
3132
Scott Wakelingfe885462016-09-22 10:24:38 +01003133void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3134 Primitive::Type result_type = conversion->GetResultType();
3135 Primitive::Type input_type = conversion->GetInputType();
3136 DCHECK_NE(result_type, input_type);
3137
3138 // The float-to-long, double-to-long and long-to-float type conversions
3139 // rely on a call to the runtime.
3140 LocationSummary::CallKind call_kind =
3141 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3142 && result_type == Primitive::kPrimLong)
3143 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3144 ? LocationSummary::kCallOnMainOnly
3145 : LocationSummary::kNoCall;
3146 LocationSummary* locations =
3147 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3148
3149 // The Java language does not allow treating boolean as an integral type but
3150 // our bit representation makes it safe.
3151
3152 switch (result_type) {
3153 case Primitive::kPrimByte:
3154 switch (input_type) {
3155 case Primitive::kPrimLong:
3156 // Type conversion from long to byte is a result of code transformations.
3157 case Primitive::kPrimBoolean:
3158 // Boolean input is a result of code transformations.
3159 case Primitive::kPrimShort:
3160 case Primitive::kPrimInt:
3161 case Primitive::kPrimChar:
3162 // Processing a Dex `int-to-byte' instruction.
3163 locations->SetInAt(0, Location::RequiresRegister());
3164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3165 break;
3166
3167 default:
3168 LOG(FATAL) << "Unexpected type conversion from " << input_type
3169 << " to " << result_type;
3170 }
3171 break;
3172
3173 case Primitive::kPrimShort:
3174 switch (input_type) {
3175 case Primitive::kPrimLong:
3176 // Type conversion from long to short is a result of code transformations.
3177 case Primitive::kPrimBoolean:
3178 // Boolean input is a result of code transformations.
3179 case Primitive::kPrimByte:
3180 case Primitive::kPrimInt:
3181 case Primitive::kPrimChar:
3182 // Processing a Dex `int-to-short' instruction.
3183 locations->SetInAt(0, Location::RequiresRegister());
3184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3185 break;
3186
3187 default:
3188 LOG(FATAL) << "Unexpected type conversion from " << input_type
3189 << " to " << result_type;
3190 }
3191 break;
3192
3193 case Primitive::kPrimInt:
3194 switch (input_type) {
3195 case Primitive::kPrimLong:
3196 // Processing a Dex `long-to-int' instruction.
3197 locations->SetInAt(0, Location::Any());
3198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3199 break;
3200
3201 case Primitive::kPrimFloat:
3202 // Processing a Dex `float-to-int' instruction.
3203 locations->SetInAt(0, Location::RequiresFpuRegister());
3204 locations->SetOut(Location::RequiresRegister());
3205 locations->AddTemp(Location::RequiresFpuRegister());
3206 break;
3207
3208 case Primitive::kPrimDouble:
3209 // Processing a Dex `double-to-int' instruction.
3210 locations->SetInAt(0, Location::RequiresFpuRegister());
3211 locations->SetOut(Location::RequiresRegister());
3212 locations->AddTemp(Location::RequiresFpuRegister());
3213 break;
3214
3215 default:
3216 LOG(FATAL) << "Unexpected type conversion from " << input_type
3217 << " to " << result_type;
3218 }
3219 break;
3220
3221 case Primitive::kPrimLong:
3222 switch (input_type) {
3223 case Primitive::kPrimBoolean:
3224 // Boolean input is a result of code transformations.
3225 case Primitive::kPrimByte:
3226 case Primitive::kPrimShort:
3227 case Primitive::kPrimInt:
3228 case Primitive::kPrimChar:
3229 // Processing a Dex `int-to-long' instruction.
3230 locations->SetInAt(0, Location::RequiresRegister());
3231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3232 break;
3233
3234 case Primitive::kPrimFloat: {
3235 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003236 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3237 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3238 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003239 break;
3240 }
3241
3242 case Primitive::kPrimDouble: {
3243 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003244 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3245 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3246 calling_convention.GetFpuRegisterAt(1)));
3247 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003248 break;
3249 }
3250
3251 default:
3252 LOG(FATAL) << "Unexpected type conversion from " << input_type
3253 << " to " << result_type;
3254 }
3255 break;
3256
3257 case Primitive::kPrimChar:
3258 switch (input_type) {
3259 case Primitive::kPrimLong:
3260 // Type conversion from long to char is a result of code transformations.
3261 case Primitive::kPrimBoolean:
3262 // Boolean input is a result of code transformations.
3263 case Primitive::kPrimByte:
3264 case Primitive::kPrimShort:
3265 case Primitive::kPrimInt:
3266 // Processing a Dex `int-to-char' instruction.
3267 locations->SetInAt(0, Location::RequiresRegister());
3268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3269 break;
3270
3271 default:
3272 LOG(FATAL) << "Unexpected type conversion from " << input_type
3273 << " to " << result_type;
3274 }
3275 break;
3276
3277 case Primitive::kPrimFloat:
3278 switch (input_type) {
3279 case Primitive::kPrimBoolean:
3280 // Boolean input is a result of code transformations.
3281 case Primitive::kPrimByte:
3282 case Primitive::kPrimShort:
3283 case Primitive::kPrimInt:
3284 case Primitive::kPrimChar:
3285 // Processing a Dex `int-to-float' instruction.
3286 locations->SetInAt(0, Location::RequiresRegister());
3287 locations->SetOut(Location::RequiresFpuRegister());
3288 break;
3289
3290 case Primitive::kPrimLong: {
3291 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003292 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3293 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3294 calling_convention.GetRegisterAt(1)));
3295 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003296 break;
3297 }
3298
3299 case Primitive::kPrimDouble:
3300 // Processing a Dex `double-to-float' instruction.
3301 locations->SetInAt(0, Location::RequiresFpuRegister());
3302 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3303 break;
3304
3305 default:
3306 LOG(FATAL) << "Unexpected type conversion from " << input_type
3307 << " to " << result_type;
3308 };
3309 break;
3310
3311 case Primitive::kPrimDouble:
3312 switch (input_type) {
3313 case Primitive::kPrimBoolean:
3314 // Boolean input is a result of code transformations.
3315 case Primitive::kPrimByte:
3316 case Primitive::kPrimShort:
3317 case Primitive::kPrimInt:
3318 case Primitive::kPrimChar:
3319 // Processing a Dex `int-to-double' instruction.
3320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetOut(Location::RequiresFpuRegister());
3322 break;
3323
3324 case Primitive::kPrimLong:
3325 // Processing a Dex `long-to-double' instruction.
3326 locations->SetInAt(0, Location::RequiresRegister());
3327 locations->SetOut(Location::RequiresFpuRegister());
3328 locations->AddTemp(Location::RequiresFpuRegister());
3329 locations->AddTemp(Location::RequiresFpuRegister());
3330 break;
3331
3332 case Primitive::kPrimFloat:
3333 // Processing a Dex `float-to-double' instruction.
3334 locations->SetInAt(0, Location::RequiresFpuRegister());
3335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3336 break;
3337
3338 default:
3339 LOG(FATAL) << "Unexpected type conversion from " << input_type
3340 << " to " << result_type;
3341 };
3342 break;
3343
3344 default:
3345 LOG(FATAL) << "Unexpected type conversion from " << input_type
3346 << " to " << result_type;
3347 }
3348}
3349
3350void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3351 LocationSummary* locations = conversion->GetLocations();
3352 Location out = locations->Out();
3353 Location in = locations->InAt(0);
3354 Primitive::Type result_type = conversion->GetResultType();
3355 Primitive::Type input_type = conversion->GetInputType();
3356 DCHECK_NE(result_type, input_type);
3357 switch (result_type) {
3358 case Primitive::kPrimByte:
3359 switch (input_type) {
3360 case Primitive::kPrimLong:
3361 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003362 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003363 break;
3364 case Primitive::kPrimBoolean:
3365 // Boolean input is a result of code transformations.
3366 case Primitive::kPrimShort:
3367 case Primitive::kPrimInt:
3368 case Primitive::kPrimChar:
3369 // Processing a Dex `int-to-byte' instruction.
3370 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3371 break;
3372
3373 default:
3374 LOG(FATAL) << "Unexpected type conversion from " << input_type
3375 << " to " << result_type;
3376 }
3377 break;
3378
3379 case Primitive::kPrimShort:
3380 switch (input_type) {
3381 case Primitive::kPrimLong:
3382 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003383 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003384 break;
3385 case Primitive::kPrimBoolean:
3386 // Boolean input is a result of code transformations.
3387 case Primitive::kPrimByte:
3388 case Primitive::kPrimInt:
3389 case Primitive::kPrimChar:
3390 // Processing a Dex `int-to-short' instruction.
3391 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3392 break;
3393
3394 default:
3395 LOG(FATAL) << "Unexpected type conversion from " << input_type
3396 << " to " << result_type;
3397 }
3398 break;
3399
3400 case Primitive::kPrimInt:
3401 switch (input_type) {
3402 case Primitive::kPrimLong:
3403 // Processing a Dex `long-to-int' instruction.
3404 DCHECK(out.IsRegister());
3405 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003406 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003407 } else if (in.IsDoubleStackSlot()) {
3408 GetAssembler()->LoadFromOffset(kLoadWord,
3409 OutputRegister(conversion),
3410 sp,
3411 in.GetStackIndex());
3412 } else {
3413 DCHECK(in.IsConstant());
3414 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00003415 int32_t value = Int32ConstantFrom(in);
3416 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01003417 }
3418 break;
3419
3420 case Primitive::kPrimFloat: {
3421 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003422 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003423 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003424 __ Vmov(OutputRegister(conversion), temp);
3425 break;
3426 }
3427
3428 case Primitive::kPrimDouble: {
3429 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003430 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003431 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003432 __ Vmov(OutputRegister(conversion), temp_s);
3433 break;
3434 }
3435
3436 default:
3437 LOG(FATAL) << "Unexpected type conversion from " << input_type
3438 << " to " << result_type;
3439 }
3440 break;
3441
3442 case Primitive::kPrimLong:
3443 switch (input_type) {
3444 case Primitive::kPrimBoolean:
3445 // Boolean input is a result of code transformations.
3446 case Primitive::kPrimByte:
3447 case Primitive::kPrimShort:
3448 case Primitive::kPrimInt:
3449 case Primitive::kPrimChar:
3450 // Processing a Dex `int-to-long' instruction.
3451 DCHECK(out.IsRegisterPair());
3452 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003453 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003454 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003455 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003456 break;
3457
3458 case Primitive::kPrimFloat:
3459 // Processing a Dex `float-to-long' instruction.
3460 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3461 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3462 break;
3463
3464 case Primitive::kPrimDouble:
3465 // Processing a Dex `double-to-long' instruction.
3466 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3467 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3468 break;
3469
3470 default:
3471 LOG(FATAL) << "Unexpected type conversion from " << input_type
3472 << " to " << result_type;
3473 }
3474 break;
3475
3476 case Primitive::kPrimChar:
3477 switch (input_type) {
3478 case Primitive::kPrimLong:
3479 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003480 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003481 break;
3482 case Primitive::kPrimBoolean:
3483 // Boolean input is a result of code transformations.
3484 case Primitive::kPrimByte:
3485 case Primitive::kPrimShort:
3486 case Primitive::kPrimInt:
3487 // Processing a Dex `int-to-char' instruction.
3488 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3489 break;
3490
3491 default:
3492 LOG(FATAL) << "Unexpected type conversion from " << input_type
3493 << " to " << result_type;
3494 }
3495 break;
3496
3497 case Primitive::kPrimFloat:
3498 switch (input_type) {
3499 case Primitive::kPrimBoolean:
3500 // Boolean input is a result of code transformations.
3501 case Primitive::kPrimByte:
3502 case Primitive::kPrimShort:
3503 case Primitive::kPrimInt:
3504 case Primitive::kPrimChar: {
3505 // Processing a Dex `int-to-float' instruction.
3506 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003507 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003508 break;
3509 }
3510
3511 case Primitive::kPrimLong:
3512 // Processing a Dex `long-to-float' instruction.
3513 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3514 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3515 break;
3516
3517 case Primitive::kPrimDouble:
3518 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003519 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003520 break;
3521
3522 default:
3523 LOG(FATAL) << "Unexpected type conversion from " << input_type
3524 << " to " << result_type;
3525 };
3526 break;
3527
3528 case Primitive::kPrimDouble:
3529 switch (input_type) {
3530 case Primitive::kPrimBoolean:
3531 // Boolean input is a result of code transformations.
3532 case Primitive::kPrimByte:
3533 case Primitive::kPrimShort:
3534 case Primitive::kPrimInt:
3535 case Primitive::kPrimChar: {
3536 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003537 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003538 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003539 break;
3540 }
3541
3542 case Primitive::kPrimLong: {
3543 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003544 vixl32::Register low = LowRegisterFrom(in);
3545 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003546 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003547 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003548 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003549 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003550 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003551
3552 // temp_d = int-to-double(high)
3553 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003554 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003555 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003556 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003557 // out_d = unsigned-to-double(low)
3558 __ Vmov(out_s, low);
3559 __ Vcvt(F64, U32, out_d, out_s);
3560 // out_d += temp_d * constant_d
3561 __ Vmla(F64, out_d, temp_d, constant_d);
3562 break;
3563 }
3564
3565 case Primitive::kPrimFloat:
3566 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003567 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003568 break;
3569
3570 default:
3571 LOG(FATAL) << "Unexpected type conversion from " << input_type
3572 << " to " << result_type;
3573 };
3574 break;
3575
3576 default:
3577 LOG(FATAL) << "Unexpected type conversion from " << input_type
3578 << " to " << result_type;
3579 }
3580}
3581
3582void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3585 switch (add->GetResultType()) {
3586 case Primitive::kPrimInt: {
3587 locations->SetInAt(0, Location::RequiresRegister());
3588 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3590 break;
3591 }
3592
Scott Wakelingfe885462016-09-22 10:24:38 +01003593 case Primitive::kPrimLong: {
3594 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003595 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3597 break;
3598 }
3599
3600 case Primitive::kPrimFloat:
3601 case Primitive::kPrimDouble: {
3602 locations->SetInAt(0, Location::RequiresFpuRegister());
3603 locations->SetInAt(1, Location::RequiresFpuRegister());
3604 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3605 break;
3606 }
3607
3608 default:
3609 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3610 }
3611}
3612
3613void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3614 LocationSummary* locations = add->GetLocations();
3615 Location out = locations->Out();
3616 Location first = locations->InAt(0);
3617 Location second = locations->InAt(1);
3618
3619 switch (add->GetResultType()) {
3620 case Primitive::kPrimInt: {
3621 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3622 }
3623 break;
3624
Scott Wakelingfe885462016-09-22 10:24:38 +01003625 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003626 if (second.IsConstant()) {
3627 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3628 GenerateAddLongConst(out, first, value);
3629 } else {
3630 DCHECK(second.IsRegisterPair());
3631 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3632 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3633 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003634 break;
3635 }
3636
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003637 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003638 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003639 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003640 break;
3641
3642 default:
3643 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3644 }
3645}
3646
3647void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3650 switch (sub->GetResultType()) {
3651 case Primitive::kPrimInt: {
3652 locations->SetInAt(0, Location::RequiresRegister());
3653 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3655 break;
3656 }
3657
Scott Wakelingfe885462016-09-22 10:24:38 +01003658 case Primitive::kPrimLong: {
3659 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003660 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3662 break;
3663 }
3664 case Primitive::kPrimFloat:
3665 case Primitive::kPrimDouble: {
3666 locations->SetInAt(0, Location::RequiresFpuRegister());
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
3668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3669 break;
3670 }
3671 default:
3672 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3673 }
3674}
3675
3676void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3677 LocationSummary* locations = sub->GetLocations();
3678 Location out = locations->Out();
3679 Location first = locations->InAt(0);
3680 Location second = locations->InAt(1);
3681 switch (sub->GetResultType()) {
3682 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003683 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003684 break;
3685 }
3686
Scott Wakelingfe885462016-09-22 10:24:38 +01003687 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003688 if (second.IsConstant()) {
3689 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3690 GenerateAddLongConst(out, first, -value);
3691 } else {
3692 DCHECK(second.IsRegisterPair());
3693 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3694 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3695 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003696 break;
3697 }
3698
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003699 case Primitive::kPrimFloat:
3700 case Primitive::kPrimDouble:
3701 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003702 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003703
3704 default:
3705 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3706 }
3707}
3708
3709void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3710 LocationSummary* locations =
3711 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3712 switch (mul->GetResultType()) {
3713 case Primitive::kPrimInt:
3714 case Primitive::kPrimLong: {
3715 locations->SetInAt(0, Location::RequiresRegister());
3716 locations->SetInAt(1, Location::RequiresRegister());
3717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3718 break;
3719 }
3720
3721 case Primitive::kPrimFloat:
3722 case Primitive::kPrimDouble: {
3723 locations->SetInAt(0, Location::RequiresFpuRegister());
3724 locations->SetInAt(1, Location::RequiresFpuRegister());
3725 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3726 break;
3727 }
3728
3729 default:
3730 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3731 }
3732}
3733
3734void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3735 LocationSummary* locations = mul->GetLocations();
3736 Location out = locations->Out();
3737 Location first = locations->InAt(0);
3738 Location second = locations->InAt(1);
3739 switch (mul->GetResultType()) {
3740 case Primitive::kPrimInt: {
3741 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3742 break;
3743 }
3744 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003745 vixl32::Register out_hi = HighRegisterFrom(out);
3746 vixl32::Register out_lo = LowRegisterFrom(out);
3747 vixl32::Register in1_hi = HighRegisterFrom(first);
3748 vixl32::Register in1_lo = LowRegisterFrom(first);
3749 vixl32::Register in2_hi = HighRegisterFrom(second);
3750 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003751
3752 // Extra checks to protect caused by the existence of R1_R2.
3753 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3754 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003755 DCHECK(!out_hi.Is(in1_lo));
3756 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003757
3758 // input: in1 - 64 bits, in2 - 64 bits
3759 // output: out
3760 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3761 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3762 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3763
3764 UseScratchRegisterScope temps(GetVIXLAssembler());
3765 vixl32::Register temp = temps.Acquire();
3766 // temp <- in1.lo * in2.hi
3767 __ Mul(temp, in1_lo, in2_hi);
3768 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3769 __ Mla(out_hi, in1_hi, in2_lo, temp);
3770 // out.lo <- (in1.lo * in2.lo)[31:0];
3771 __ Umull(out_lo, temp, in1_lo, in2_lo);
3772 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003773 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003774 break;
3775 }
3776
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003777 case Primitive::kPrimFloat:
3778 case Primitive::kPrimDouble:
3779 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003780 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003781
3782 default:
3783 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3784 }
3785}
3786
Scott Wakelingfe885462016-09-22 10:24:38 +01003787void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3788 DCHECK(instruction->IsDiv() || instruction->IsRem());
3789 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3790
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003791 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003792 DCHECK(second.IsConstant());
3793
3794 vixl32::Register out = OutputRegister(instruction);
3795 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003796 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003797 DCHECK(imm == 1 || imm == -1);
3798
3799 if (instruction->IsRem()) {
3800 __ Mov(out, 0);
3801 } else {
3802 if (imm == 1) {
3803 __ Mov(out, dividend);
3804 } else {
3805 __ Rsb(out, dividend, 0);
3806 }
3807 }
3808}
3809
3810void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3811 DCHECK(instruction->IsDiv() || instruction->IsRem());
3812 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3813
3814 LocationSummary* locations = instruction->GetLocations();
3815 Location second = locations->InAt(1);
3816 DCHECK(second.IsConstant());
3817
3818 vixl32::Register out = OutputRegister(instruction);
3819 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003820 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003821 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3823 int ctz_imm = CTZ(abs_imm);
3824
3825 if (ctz_imm == 1) {
3826 __ Lsr(temp, dividend, 32 - ctz_imm);
3827 } else {
3828 __ Asr(temp, dividend, 31);
3829 __ Lsr(temp, temp, 32 - ctz_imm);
3830 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003831 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003832
3833 if (instruction->IsDiv()) {
3834 __ Asr(out, out, ctz_imm);
3835 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003836 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003837 }
3838 } else {
3839 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003840 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003841 }
3842}
3843
3844void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3845 DCHECK(instruction->IsDiv() || instruction->IsRem());
3846 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3847
3848 LocationSummary* locations = instruction->GetLocations();
3849 Location second = locations->InAt(1);
3850 DCHECK(second.IsConstant());
3851
3852 vixl32::Register out = OutputRegister(instruction);
3853 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003854 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3855 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003856 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003857
3858 int64_t magic;
3859 int shift;
3860 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3861
Anton Kirilovdda43962016-11-21 19:55:20 +00003862 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3863 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003864 __ Smull(temp2, temp1, dividend, temp1);
3865
3866 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003867 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003869 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003870 }
3871
3872 if (shift != 0) {
3873 __ Asr(temp1, temp1, shift);
3874 }
3875
3876 if (instruction->IsDiv()) {
3877 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3878 } else {
3879 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3880 // TODO: Strength reduction for mls.
3881 __ Mov(temp2, imm);
3882 __ Mls(out, temp1, temp2, dividend);
3883 }
3884}
3885
3886void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3887 HBinaryOperation* instruction) {
3888 DCHECK(instruction->IsDiv() || instruction->IsRem());
3889 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3890
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003891 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003892 DCHECK(second.IsConstant());
3893
Anton Kirilov644032c2016-12-06 17:51:43 +00003894 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003895 if (imm == 0) {
3896 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3897 } else if (imm == 1 || imm == -1) {
3898 DivRemOneOrMinusOne(instruction);
3899 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3900 DivRemByPowerOfTwo(instruction);
3901 } else {
3902 DCHECK(imm <= -2 || imm >= 2);
3903 GenerateDivRemWithAnyConstant(instruction);
3904 }
3905}
3906
3907void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3908 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3909 if (div->GetResultType() == Primitive::kPrimLong) {
3910 // pLdiv runtime call.
3911 call_kind = LocationSummary::kCallOnMainOnly;
3912 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3913 // sdiv will be replaced by other instruction sequence.
3914 } else if (div->GetResultType() == Primitive::kPrimInt &&
3915 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3916 // pIdivmod runtime call.
3917 call_kind = LocationSummary::kCallOnMainOnly;
3918 }
3919
3920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3921
3922 switch (div->GetResultType()) {
3923 case Primitive::kPrimInt: {
3924 if (div->InputAt(1)->IsConstant()) {
3925 locations->SetInAt(0, Location::RequiresRegister());
3926 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003928 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003929 if (value == 1 || value == 0 || value == -1) {
3930 // No temp register required.
3931 } else {
3932 locations->AddTemp(Location::RequiresRegister());
3933 if (!IsPowerOfTwo(AbsOrMin(value))) {
3934 locations->AddTemp(Location::RequiresRegister());
3935 }
3936 }
3937 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3938 locations->SetInAt(0, Location::RequiresRegister());
3939 locations->SetInAt(1, Location::RequiresRegister());
3940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3941 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003942 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3943 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3944 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003945 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003946 // we only need the former.
3947 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 }
3949 break;
3950 }
3951 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003952 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3953 locations->SetInAt(0, LocationFrom(
3954 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3955 locations->SetInAt(1, LocationFrom(
3956 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3957 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003958 break;
3959 }
3960 case Primitive::kPrimFloat:
3961 case Primitive::kPrimDouble: {
3962 locations->SetInAt(0, Location::RequiresFpuRegister());
3963 locations->SetInAt(1, Location::RequiresFpuRegister());
3964 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3965 break;
3966 }
3967
3968 default:
3969 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3970 }
3971}
3972
3973void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003974 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003975 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003976
3977 switch (div->GetResultType()) {
3978 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003979 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003980 GenerateDivRemConstantIntegral(div);
3981 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3982 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3983 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003984 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3985 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3986 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3987 DCHECK(r0.Is(OutputRegister(div)));
3988
3989 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3990 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 }
3992 break;
3993 }
3994
3995 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003996 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3997 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3998 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3999 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4000 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4001 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4002 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4003
4004 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4005 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004006 break;
4007 }
4008
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004009 case Primitive::kPrimFloat:
4010 case Primitive::kPrimDouble:
4011 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004012 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004013
4014 default:
4015 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4016 }
4017}
4018
Artem Serov551b28f2016-10-18 19:11:30 +01004019void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4020 Primitive::Type type = rem->GetResultType();
4021
4022 // Most remainders are implemented in the runtime.
4023 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4024 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4025 // sdiv will be replaced by other instruction sequence.
4026 call_kind = LocationSummary::kNoCall;
4027 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4028 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4029 // Have hardware divide instruction for int, do it with three instructions.
4030 call_kind = LocationSummary::kNoCall;
4031 }
4032
4033 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4034
4035 switch (type) {
4036 case Primitive::kPrimInt: {
4037 if (rem->InputAt(1)->IsConstant()) {
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004041 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004042 if (value == 1 || value == 0 || value == -1) {
4043 // No temp register required.
4044 } else {
4045 locations->AddTemp(Location::RequiresRegister());
4046 if (!IsPowerOfTwo(AbsOrMin(value))) {
4047 locations->AddTemp(Location::RequiresRegister());
4048 }
4049 }
4050 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4051 locations->SetInAt(0, Location::RequiresRegister());
4052 locations->SetInAt(1, Location::RequiresRegister());
4053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4054 locations->AddTemp(Location::RequiresRegister());
4055 } else {
4056 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4057 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4058 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004059 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004060 // we only need the latter.
4061 locations->SetOut(LocationFrom(r1));
4062 }
4063 break;
4064 }
4065 case Primitive::kPrimLong: {
4066 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4067 locations->SetInAt(0, LocationFrom(
4068 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4069 locations->SetInAt(1, LocationFrom(
4070 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4071 // The runtime helper puts the output in R2,R3.
4072 locations->SetOut(LocationFrom(r2, r3));
4073 break;
4074 }
4075 case Primitive::kPrimFloat: {
4076 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4077 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4078 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4079 locations->SetOut(LocationFrom(s0));
4080 break;
4081 }
4082
4083 case Primitive::kPrimDouble: {
4084 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4085 locations->SetInAt(0, LocationFrom(
4086 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4087 locations->SetInAt(1, LocationFrom(
4088 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4089 locations->SetOut(LocationFrom(s0, s1));
4090 break;
4091 }
4092
4093 default:
4094 LOG(FATAL) << "Unexpected rem type " << type;
4095 }
4096}
4097
4098void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4099 LocationSummary* locations = rem->GetLocations();
4100 Location second = locations->InAt(1);
4101
4102 Primitive::Type type = rem->GetResultType();
4103 switch (type) {
4104 case Primitive::kPrimInt: {
4105 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4106 vixl32::Register out_reg = OutputRegister(rem);
4107 if (second.IsConstant()) {
4108 GenerateDivRemConstantIntegral(rem);
4109 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4110 vixl32::Register reg2 = RegisterFrom(second);
4111 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4112
4113 // temp = reg1 / reg2 (integer division)
4114 // dest = reg1 - temp * reg2
4115 __ Sdiv(temp, reg1, reg2);
4116 __ Mls(out_reg, temp, reg2, reg1);
4117 } else {
4118 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4119 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4120 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4121 DCHECK(out_reg.Is(r1));
4122
4123 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4124 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4125 }
4126 break;
4127 }
4128
4129 case Primitive::kPrimLong: {
4130 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4131 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4132 break;
4133 }
4134
4135 case Primitive::kPrimFloat: {
4136 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4137 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4138 break;
4139 }
4140
4141 case Primitive::kPrimDouble: {
4142 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4143 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4144 break;
4145 }
4146
4147 default:
4148 LOG(FATAL) << "Unexpected rem type " << type;
4149 }
4150}
4151
4152
Scott Wakelingfe885462016-09-22 10:24:38 +01004153void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004154 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004155 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004156}
4157
4158void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4159 DivZeroCheckSlowPathARMVIXL* slow_path =
4160 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4161 codegen_->AddSlowPath(slow_path);
4162
4163 LocationSummary* locations = instruction->GetLocations();
4164 Location value = locations->InAt(0);
4165
4166 switch (instruction->GetType()) {
4167 case Primitive::kPrimBoolean:
4168 case Primitive::kPrimByte:
4169 case Primitive::kPrimChar:
4170 case Primitive::kPrimShort:
4171 case Primitive::kPrimInt: {
4172 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004173 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004174 } else {
4175 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004176 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004177 __ B(slow_path->GetEntryLabel());
4178 }
4179 }
4180 break;
4181 }
4182 case Primitive::kPrimLong: {
4183 if (value.IsRegisterPair()) {
4184 UseScratchRegisterScope temps(GetVIXLAssembler());
4185 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004186 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004187 __ B(eq, slow_path->GetEntryLabel());
4188 } else {
4189 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004190 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 __ B(slow_path->GetEntryLabel());
4192 }
4193 }
4194 break;
4195 }
4196 default:
4197 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4198 }
4199}
4200
Artem Serov02109dd2016-09-23 17:17:54 +01004201void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4202 LocationSummary* locations = ror->GetLocations();
4203 vixl32::Register in = InputRegisterAt(ror, 0);
4204 Location rhs = locations->InAt(1);
4205 vixl32::Register out = OutputRegister(ror);
4206
4207 if (rhs.IsConstant()) {
4208 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4209 // so map all rotations to a +ve. equivalent in that range.
4210 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4211 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4212 if (rot) {
4213 // Rotate, mapping left rotations to right equivalents if necessary.
4214 // (e.g. left by 2 bits == right by 30.)
4215 __ Ror(out, in, rot);
4216 } else if (!out.Is(in)) {
4217 __ Mov(out, in);
4218 }
4219 } else {
4220 __ Ror(out, in, RegisterFrom(rhs));
4221 }
4222}
4223
4224// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4225// rotates by swapping input regs (effectively rotating by the first 32-bits of
4226// a larger rotation) or flipping direction (thus treating larger right/left
4227// rotations as sub-word sized rotations in the other direction) as appropriate.
4228void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4229 LocationSummary* locations = ror->GetLocations();
4230 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4231 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4232 Location rhs = locations->InAt(1);
4233 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4234 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4235
4236 if (rhs.IsConstant()) {
4237 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4238 // Map all rotations to +ve. equivalents on the interval [0,63].
4239 rot &= kMaxLongShiftDistance;
4240 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4241 // logic below to a simple pair of binary orr.
4242 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4243 if (rot >= kArmBitsPerWord) {
4244 rot -= kArmBitsPerWord;
4245 std::swap(in_reg_hi, in_reg_lo);
4246 }
4247 // Rotate, or mov to out for zero or word size rotations.
4248 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004249 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004250 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004251 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004252 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4253 } else {
4254 __ Mov(out_reg_lo, in_reg_lo);
4255 __ Mov(out_reg_hi, in_reg_hi);
4256 }
4257 } else {
4258 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4259 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4260 vixl32::Label end;
4261 vixl32::Label shift_by_32_plus_shift_right;
4262
4263 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4264 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004265 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004266 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004267
4268 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4269 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4270 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4271 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4272 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4273 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4274 __ Lsr(shift_left, in_reg_hi, shift_right);
4275 __ Add(out_reg_lo, out_reg_lo, shift_left);
4276 __ B(&end);
4277
4278 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4279 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4280 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4281 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4282 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4283 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4284 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4285 __ Lsl(shift_right, in_reg_hi, shift_left);
4286 __ Add(out_reg_lo, out_reg_lo, shift_right);
4287
4288 __ Bind(&end);
4289 }
4290}
4291
4292void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4293 LocationSummary* locations =
4294 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4295 switch (ror->GetResultType()) {
4296 case Primitive::kPrimInt: {
4297 locations->SetInAt(0, Location::RequiresRegister());
4298 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4300 break;
4301 }
4302 case Primitive::kPrimLong: {
4303 locations->SetInAt(0, Location::RequiresRegister());
4304 if (ror->InputAt(1)->IsConstant()) {
4305 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4306 } else {
4307 locations->SetInAt(1, Location::RequiresRegister());
4308 locations->AddTemp(Location::RequiresRegister());
4309 locations->AddTemp(Location::RequiresRegister());
4310 }
4311 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4312 break;
4313 }
4314 default:
4315 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4316 }
4317}
4318
4319void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4320 Primitive::Type type = ror->GetResultType();
4321 switch (type) {
4322 case Primitive::kPrimInt: {
4323 HandleIntegerRotate(ror);
4324 break;
4325 }
4326 case Primitive::kPrimLong: {
4327 HandleLongRotate(ror);
4328 break;
4329 }
4330 default:
4331 LOG(FATAL) << "Unexpected operation type " << type;
4332 UNREACHABLE();
4333 }
4334}
4335
Artem Serov02d37832016-10-25 15:25:33 +01004336void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4337 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4338
4339 LocationSummary* locations =
4340 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4341
4342 switch (op->GetResultType()) {
4343 case Primitive::kPrimInt: {
4344 locations->SetInAt(0, Location::RequiresRegister());
4345 if (op->InputAt(1)->IsConstant()) {
4346 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4348 } else {
4349 locations->SetInAt(1, Location::RequiresRegister());
4350 // Make the output overlap, as it will be used to hold the masked
4351 // second input.
4352 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4353 }
4354 break;
4355 }
4356 case Primitive::kPrimLong: {
4357 locations->SetInAt(0, Location::RequiresRegister());
4358 if (op->InputAt(1)->IsConstant()) {
4359 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4360 // For simplicity, use kOutputOverlap even though we only require that low registers
4361 // don't clash with high registers which the register allocator currently guarantees.
4362 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4363 } else {
4364 locations->SetInAt(1, Location::RequiresRegister());
4365 locations->AddTemp(Location::RequiresRegister());
4366 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4367 }
4368 break;
4369 }
4370 default:
4371 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4372 }
4373}
4374
4375void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4376 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4377
4378 LocationSummary* locations = op->GetLocations();
4379 Location out = locations->Out();
4380 Location first = locations->InAt(0);
4381 Location second = locations->InAt(1);
4382
4383 Primitive::Type type = op->GetResultType();
4384 switch (type) {
4385 case Primitive::kPrimInt: {
4386 vixl32::Register out_reg = OutputRegister(op);
4387 vixl32::Register first_reg = InputRegisterAt(op, 0);
4388 if (second.IsRegister()) {
4389 vixl32::Register second_reg = RegisterFrom(second);
4390 // ARM doesn't mask the shift count so we need to do it ourselves.
4391 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4392 if (op->IsShl()) {
4393 __ Lsl(out_reg, first_reg, out_reg);
4394 } else if (op->IsShr()) {
4395 __ Asr(out_reg, first_reg, out_reg);
4396 } else {
4397 __ Lsr(out_reg, first_reg, out_reg);
4398 }
4399 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004400 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004401 uint32_t shift_value = cst & kMaxIntShiftDistance;
4402 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4403 __ Mov(out_reg, first_reg);
4404 } else if (op->IsShl()) {
4405 __ Lsl(out_reg, first_reg, shift_value);
4406 } else if (op->IsShr()) {
4407 __ Asr(out_reg, first_reg, shift_value);
4408 } else {
4409 __ Lsr(out_reg, first_reg, shift_value);
4410 }
4411 }
4412 break;
4413 }
4414 case Primitive::kPrimLong: {
4415 vixl32::Register o_h = HighRegisterFrom(out);
4416 vixl32::Register o_l = LowRegisterFrom(out);
4417
4418 vixl32::Register high = HighRegisterFrom(first);
4419 vixl32::Register low = LowRegisterFrom(first);
4420
4421 if (second.IsRegister()) {
4422 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4423
4424 vixl32::Register second_reg = RegisterFrom(second);
4425
4426 if (op->IsShl()) {
4427 __ And(o_l, second_reg, kMaxLongShiftDistance);
4428 // Shift the high part
4429 __ Lsl(o_h, high, o_l);
4430 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004431 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004432 __ Lsr(temp, low, temp);
4433 __ Orr(o_h, o_h, temp);
4434 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004435 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004436 {
Artem Serov0fb37192016-12-06 18:13:40 +00004437 ExactAssemblyScope guard(GetVIXLAssembler(),
4438 2 * vixl32::kMaxInstructionSizeInBytes,
4439 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004440 __ it(pl);
4441 __ lsl(pl, o_h, low, temp);
4442 }
4443 // Shift the low part
4444 __ Lsl(o_l, low, o_l);
4445 } else if (op->IsShr()) {
4446 __ And(o_h, second_reg, kMaxLongShiftDistance);
4447 // Shift the low part
4448 __ Lsr(o_l, low, o_h);
4449 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004450 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004451 __ Lsl(temp, high, temp);
4452 __ Orr(o_l, o_l, temp);
4453 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004454 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004455 {
Artem Serov0fb37192016-12-06 18:13:40 +00004456 ExactAssemblyScope guard(GetVIXLAssembler(),
4457 2 * vixl32::kMaxInstructionSizeInBytes,
4458 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004459 __ it(pl);
4460 __ asr(pl, o_l, high, temp);
4461 }
4462 // Shift the high part
4463 __ Asr(o_h, high, o_h);
4464 } else {
4465 __ And(o_h, second_reg, kMaxLongShiftDistance);
4466 // same as Shr except we use `Lsr`s and not `Asr`s
4467 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004468 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004469 __ Lsl(temp, high, temp);
4470 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004471 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004472 {
Artem Serov0fb37192016-12-06 18:13:40 +00004473 ExactAssemblyScope guard(GetVIXLAssembler(),
4474 2 * vixl32::kMaxInstructionSizeInBytes,
4475 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004476 __ it(pl);
4477 __ lsr(pl, o_l, high, temp);
4478 }
4479 __ Lsr(o_h, high, o_h);
4480 }
4481 } else {
4482 // Register allocator doesn't create partial overlap.
4483 DCHECK(!o_l.Is(high));
4484 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004485 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004486 uint32_t shift_value = cst & kMaxLongShiftDistance;
4487 if (shift_value > 32) {
4488 if (op->IsShl()) {
4489 __ Lsl(o_h, low, shift_value - 32);
4490 __ Mov(o_l, 0);
4491 } else if (op->IsShr()) {
4492 __ Asr(o_l, high, shift_value - 32);
4493 __ Asr(o_h, high, 31);
4494 } else {
4495 __ Lsr(o_l, high, shift_value - 32);
4496 __ Mov(o_h, 0);
4497 }
4498 } else if (shift_value == 32) {
4499 if (op->IsShl()) {
4500 __ Mov(o_h, low);
4501 __ Mov(o_l, 0);
4502 } else if (op->IsShr()) {
4503 __ Mov(o_l, high);
4504 __ Asr(o_h, high, 31);
4505 } else {
4506 __ Mov(o_l, high);
4507 __ Mov(o_h, 0);
4508 }
4509 } else if (shift_value == 1) {
4510 if (op->IsShl()) {
4511 __ Lsls(o_l, low, 1);
4512 __ Adc(o_h, high, high);
4513 } else if (op->IsShr()) {
4514 __ Asrs(o_h, high, 1);
4515 __ Rrx(o_l, low);
4516 } else {
4517 __ Lsrs(o_h, high, 1);
4518 __ Rrx(o_l, low);
4519 }
4520 } else {
4521 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4522 if (op->IsShl()) {
4523 __ Lsl(o_h, high, shift_value);
4524 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4525 __ Lsl(o_l, low, shift_value);
4526 } else if (op->IsShr()) {
4527 __ Lsr(o_l, low, shift_value);
4528 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4529 __ Asr(o_h, high, shift_value);
4530 } else {
4531 __ Lsr(o_l, low, shift_value);
4532 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4533 __ Lsr(o_h, high, shift_value);
4534 }
4535 }
4536 }
4537 break;
4538 }
4539 default:
4540 LOG(FATAL) << "Unexpected operation type " << type;
4541 UNREACHABLE();
4542 }
4543}
4544
4545void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4546 HandleShift(shl);
4547}
4548
4549void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4550 HandleShift(shl);
4551}
4552
4553void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4554 HandleShift(shr);
4555}
4556
4557void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4558 HandleShift(shr);
4559}
4560
4561void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4562 HandleShift(ushr);
4563}
4564
4565void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4566 HandleShift(ushr);
4567}
4568
4569void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4570 LocationSummary* locations =
4571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4572 if (instruction->IsStringAlloc()) {
4573 locations->AddTemp(LocationFrom(kMethodRegister));
4574 } else {
4575 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4576 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004577 }
4578 locations->SetOut(LocationFrom(r0));
4579}
4580
4581void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4582 // Note: if heap poisoning is enabled, the entry point takes cares
4583 // of poisoning the reference.
4584 if (instruction->IsStringAlloc()) {
4585 // String is allocated through StringFactory. Call NewEmptyString entry point.
4586 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4587 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4588 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4589 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004590 // 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 +00004591 ExactAssemblyScope aas(GetVIXLAssembler(),
4592 vixl32::k16BitT32InstructionSizeInBytes,
4593 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004594 __ blx(lr);
4595 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4596 } else {
4597 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004598 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004599 }
4600}
4601
4602void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4603 LocationSummary* locations =
4604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004606 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004607 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4608 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004609}
4610
4611void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004612 // Note: if heap poisoning is enabled, the entry point takes cares
4613 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004614 QuickEntrypointEnum entrypoint =
4615 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4616 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004617 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004618 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004619}
4620
4621void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4622 LocationSummary* locations =
4623 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4624 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4625 if (location.IsStackSlot()) {
4626 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4627 } else if (location.IsDoubleStackSlot()) {
4628 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4629 }
4630 locations->SetOut(location);
4631}
4632
4633void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4634 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4635 // Nothing to do, the parameter is already at its location.
4636}
4637
4638void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4639 LocationSummary* locations =
4640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4641 locations->SetOut(LocationFrom(kMethodRegister));
4642}
4643
4644void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4645 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4646 // Nothing to do, the method is already at its location.
4647}
4648
4649void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4650 LocationSummary* locations =
4651 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4652 locations->SetInAt(0, Location::RequiresRegister());
4653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4654}
4655
4656void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4657 LocationSummary* locations = not_->GetLocations();
4658 Location out = locations->Out();
4659 Location in = locations->InAt(0);
4660 switch (not_->GetResultType()) {
4661 case Primitive::kPrimInt:
4662 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4663 break;
4664
4665 case Primitive::kPrimLong:
4666 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4667 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4668 break;
4669
4670 default:
4671 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4672 }
4673}
4674
Scott Wakelingc34dba72016-10-03 10:14:44 +01004675void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4676 LocationSummary* locations =
4677 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4678 locations->SetInAt(0, Location::RequiresRegister());
4679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4680}
4681
4682void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4683 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4684}
4685
Artem Serov02d37832016-10-25 15:25:33 +01004686void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4687 LocationSummary* locations =
4688 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4689 switch (compare->InputAt(0)->GetType()) {
4690 case Primitive::kPrimBoolean:
4691 case Primitive::kPrimByte:
4692 case Primitive::kPrimShort:
4693 case Primitive::kPrimChar:
4694 case Primitive::kPrimInt:
4695 case Primitive::kPrimLong: {
4696 locations->SetInAt(0, Location::RequiresRegister());
4697 locations->SetInAt(1, Location::RequiresRegister());
4698 // Output overlaps because it is written before doing the low comparison.
4699 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4700 break;
4701 }
4702 case Primitive::kPrimFloat:
4703 case Primitive::kPrimDouble: {
4704 locations->SetInAt(0, Location::RequiresFpuRegister());
4705 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4706 locations->SetOut(Location::RequiresRegister());
4707 break;
4708 }
4709 default:
4710 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4711 }
4712}
4713
4714void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4715 LocationSummary* locations = compare->GetLocations();
4716 vixl32::Register out = OutputRegister(compare);
4717 Location left = locations->InAt(0);
4718 Location right = locations->InAt(1);
4719
4720 vixl32::Label less, greater, done;
4721 Primitive::Type type = compare->InputAt(0)->GetType();
4722 vixl32::Condition less_cond = vixl32::Condition(kNone);
4723 switch (type) {
4724 case Primitive::kPrimBoolean:
4725 case Primitive::kPrimByte:
4726 case Primitive::kPrimShort:
4727 case Primitive::kPrimChar:
4728 case Primitive::kPrimInt: {
4729 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4730 __ Mov(out, 0);
4731 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4732 less_cond = lt;
4733 break;
4734 }
4735 case Primitive::kPrimLong: {
4736 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004737 __ B(lt, &less, /* far_target */ false);
4738 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004739 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4740 __ Mov(out, 0);
4741 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4742 less_cond = lo;
4743 break;
4744 }
4745 case Primitive::kPrimFloat:
4746 case Primitive::kPrimDouble: {
4747 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004748 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01004749 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4750 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4751 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4752 break;
4753 }
4754 default:
4755 LOG(FATAL) << "Unexpected compare type " << type;
4756 UNREACHABLE();
4757 }
4758
Artem Serov517d9f62016-12-12 15:51:15 +00004759 __ B(eq, &done, /* far_target */ false);
4760 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004761
4762 __ Bind(&greater);
4763 __ Mov(out, 1);
4764 __ B(&done);
4765
4766 __ Bind(&less);
4767 __ Mov(out, -1);
4768
4769 __ Bind(&done);
4770}
4771
4772void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4773 LocationSummary* locations =
4774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4775 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4776 locations->SetInAt(i, Location::Any());
4777 }
4778 locations->SetOut(Location::Any());
4779}
4780
4781void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4782 LOG(FATAL) << "Unreachable";
4783}
4784
4785void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4786 // TODO (ported from quick): revisit ARM barrier kinds.
4787 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4788 switch (kind) {
4789 case MemBarrierKind::kAnyStore:
4790 case MemBarrierKind::kLoadAny:
4791 case MemBarrierKind::kAnyAny: {
4792 flavor = DmbOptions::ISH;
4793 break;
4794 }
4795 case MemBarrierKind::kStoreStore: {
4796 flavor = DmbOptions::ISHST;
4797 break;
4798 }
4799 default:
4800 LOG(FATAL) << "Unexpected memory barrier " << kind;
4801 }
4802 __ Dmb(flavor);
4803}
4804
4805void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4806 uint32_t offset,
4807 vixl32::Register out_lo,
4808 vixl32::Register out_hi) {
4809 UseScratchRegisterScope temps(GetVIXLAssembler());
4810 if (offset != 0) {
4811 vixl32::Register temp = temps.Acquire();
4812 __ Add(temp, addr, offset);
4813 addr = temp;
4814 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004815 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004816}
4817
4818void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4819 uint32_t offset,
4820 vixl32::Register value_lo,
4821 vixl32::Register value_hi,
4822 vixl32::Register temp1,
4823 vixl32::Register temp2,
4824 HInstruction* instruction) {
4825 UseScratchRegisterScope temps(GetVIXLAssembler());
4826 vixl32::Label fail;
4827 if (offset != 0) {
4828 vixl32::Register temp = temps.Acquire();
4829 __ Add(temp, addr, offset);
4830 addr = temp;
4831 }
4832 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004833 {
4834 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004835 ExactAssemblyScope aas(GetVIXLAssembler(),
4836 vixl32::kMaxInstructionSizeInBytes,
4837 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004838 // We need a load followed by store. (The address used in a STREX instruction must
4839 // be the same as the address in the most recently executed LDREX instruction.)
4840 __ ldrexd(temp1, temp2, MemOperand(addr));
4841 codegen_->MaybeRecordImplicitNullCheck(instruction);
4842 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004843 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004844 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004845}
Artem Serov02109dd2016-09-23 17:17:54 +01004846
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004847void LocationsBuilderARMVIXL::HandleFieldSet(
4848 HInstruction* instruction, const FieldInfo& field_info) {
4849 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4850
4851 LocationSummary* locations =
4852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4853 locations->SetInAt(0, Location::RequiresRegister());
4854
4855 Primitive::Type field_type = field_info.GetFieldType();
4856 if (Primitive::IsFloatingPointType(field_type)) {
4857 locations->SetInAt(1, Location::RequiresFpuRegister());
4858 } else {
4859 locations->SetInAt(1, Location::RequiresRegister());
4860 }
4861
4862 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4863 bool generate_volatile = field_info.IsVolatile()
4864 && is_wide
4865 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4866 bool needs_write_barrier =
4867 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4868 // Temporary registers for the write barrier.
4869 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4870 if (needs_write_barrier) {
4871 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4872 locations->AddTemp(Location::RequiresRegister());
4873 } else if (generate_volatile) {
4874 // ARM encoding have some additional constraints for ldrexd/strexd:
4875 // - registers need to be consecutive
4876 // - the first register should be even but not R14.
4877 // We don't test for ARM yet, and the assertion makes sure that we
4878 // revisit this if we ever enable ARM encoding.
4879 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4880
4881 locations->AddTemp(Location::RequiresRegister());
4882 locations->AddTemp(Location::RequiresRegister());
4883 if (field_type == Primitive::kPrimDouble) {
4884 // For doubles we need two more registers to copy the value.
4885 locations->AddTemp(LocationFrom(r2));
4886 locations->AddTemp(LocationFrom(r3));
4887 }
4888 }
4889}
4890
4891void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4892 const FieldInfo& field_info,
4893 bool value_can_be_null) {
4894 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4895
4896 LocationSummary* locations = instruction->GetLocations();
4897 vixl32::Register base = InputRegisterAt(instruction, 0);
4898 Location value = locations->InAt(1);
4899
4900 bool is_volatile = field_info.IsVolatile();
4901 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4902 Primitive::Type field_type = field_info.GetFieldType();
4903 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4904 bool needs_write_barrier =
4905 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4906
4907 if (is_volatile) {
4908 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4909 }
4910
4911 switch (field_type) {
4912 case Primitive::kPrimBoolean:
4913 case Primitive::kPrimByte: {
4914 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4915 break;
4916 }
4917
4918 case Primitive::kPrimShort:
4919 case Primitive::kPrimChar: {
4920 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4921 break;
4922 }
4923
4924 case Primitive::kPrimInt:
4925 case Primitive::kPrimNot: {
4926 if (kPoisonHeapReferences && needs_write_barrier) {
4927 // Note that in the case where `value` is a null reference,
4928 // we do not enter this block, as a null reference does not
4929 // need poisoning.
4930 DCHECK_EQ(field_type, Primitive::kPrimNot);
4931 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4932 __ Mov(temp, RegisterFrom(value));
4933 GetAssembler()->PoisonHeapReference(temp);
4934 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4935 } else {
4936 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4937 }
4938 break;
4939 }
4940
4941 case Primitive::kPrimLong: {
4942 if (is_volatile && !atomic_ldrd_strd) {
4943 GenerateWideAtomicStore(base,
4944 offset,
4945 LowRegisterFrom(value),
4946 HighRegisterFrom(value),
4947 RegisterFrom(locations->GetTemp(0)),
4948 RegisterFrom(locations->GetTemp(1)),
4949 instruction);
4950 } else {
4951 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4952 codegen_->MaybeRecordImplicitNullCheck(instruction);
4953 }
4954 break;
4955 }
4956
4957 case Primitive::kPrimFloat: {
4958 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4959 break;
4960 }
4961
4962 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004963 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004964 if (is_volatile && !atomic_ldrd_strd) {
4965 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4966 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4967
4968 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4969
4970 GenerateWideAtomicStore(base,
4971 offset,
4972 value_reg_lo,
4973 value_reg_hi,
4974 RegisterFrom(locations->GetTemp(2)),
4975 RegisterFrom(locations->GetTemp(3)),
4976 instruction);
4977 } else {
4978 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4979 codegen_->MaybeRecordImplicitNullCheck(instruction);
4980 }
4981 break;
4982 }
4983
4984 case Primitive::kPrimVoid:
4985 LOG(FATAL) << "Unreachable type " << field_type;
4986 UNREACHABLE();
4987 }
4988
4989 // Longs and doubles are handled in the switch.
4990 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004991 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4992 // should use a scope and the assembler to emit the store instruction to guarantee that we
4993 // record the pc at the correct position. But the `Assembler` does not automatically handle
4994 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4995 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004996 codegen_->MaybeRecordImplicitNullCheck(instruction);
4997 }
4998
4999 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5000 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5001 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5002 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5003 }
5004
5005 if (is_volatile) {
5006 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5007 }
5008}
5009
Artem Serov02d37832016-10-25 15:25:33 +01005010void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5011 const FieldInfo& field_info) {
5012 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5013
5014 bool object_field_get_with_read_barrier =
5015 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(instruction,
5018 object_field_get_with_read_barrier ?
5019 LocationSummary::kCallOnSlowPath :
5020 LocationSummary::kNoCall);
5021 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5022 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5023 }
5024 locations->SetInAt(0, Location::RequiresRegister());
5025
5026 bool volatile_for_double = field_info.IsVolatile()
5027 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5028 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5029 // The output overlaps in case of volatile long: we don't want the
5030 // code generated by GenerateWideAtomicLoad to overwrite the
5031 // object's location. Likewise, in the case of an object field get
5032 // with read barriers enabled, we do not want the load to overwrite
5033 // the object's location, as we need it to emit the read barrier.
5034 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5035 object_field_get_with_read_barrier;
5036
5037 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5038 locations->SetOut(Location::RequiresFpuRegister());
5039 } else {
5040 locations->SetOut(Location::RequiresRegister(),
5041 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5042 }
5043 if (volatile_for_double) {
5044 // ARM encoding have some additional constraints for ldrexd/strexd:
5045 // - registers need to be consecutive
5046 // - the first register should be even but not R14.
5047 // We don't test for ARM yet, and the assertion makes sure that we
5048 // revisit this if we ever enable ARM encoding.
5049 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5050 locations->AddTemp(Location::RequiresRegister());
5051 locations->AddTemp(Location::RequiresRegister());
5052 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5053 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005054 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01005055 locations->AddTemp(Location::RequiresRegister());
5056 }
5057}
5058
5059Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5060 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5061 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5062 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5063 return Location::ConstantLocation(input->AsConstant());
5064 } else {
5065 return Location::RequiresFpuRegister();
5066 }
5067}
5068
Artem Serov02109dd2016-09-23 17:17:54 +01005069Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5070 Opcode opcode) {
5071 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5072 if (constant->IsConstant() &&
5073 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5074 return Location::ConstantLocation(constant->AsConstant());
5075 }
5076 return Location::RequiresRegister();
5077}
5078
5079bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5080 Opcode opcode) {
5081 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5082 if (Primitive::Is64BitType(input_cst->GetType())) {
5083 Opcode high_opcode = opcode;
5084 SetCc low_set_cc = kCcDontCare;
5085 switch (opcode) {
5086 case SUB:
5087 // Flip the operation to an ADD.
5088 value = -value;
5089 opcode = ADD;
5090 FALLTHROUGH_INTENDED;
5091 case ADD:
5092 if (Low32Bits(value) == 0u) {
5093 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5094 }
5095 high_opcode = ADC;
5096 low_set_cc = kCcSet;
5097 break;
5098 default:
5099 break;
5100 }
5101 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5102 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5103 } else {
5104 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5105 }
5106}
5107
5108// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5109// enabled.
5110bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5111 Opcode opcode,
5112 SetCc set_cc) {
5113 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5114 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5115 return true;
5116 }
5117 Opcode neg_opcode = kNoOperand;
5118 switch (opcode) {
5119 case AND: neg_opcode = BIC; value = ~value; break;
5120 case ORR: neg_opcode = ORN; value = ~value; break;
5121 case ADD: neg_opcode = SUB; value = -value; break;
5122 case ADC: neg_opcode = SBC; value = ~value; break;
5123 case SUB: neg_opcode = ADD; value = -value; break;
5124 case SBC: neg_opcode = ADC; value = ~value; break;
Donghui Bai426b49c2016-11-08 14:55:38 +08005125 case MOV: neg_opcode = MVN; value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005126 default:
5127 return false;
5128 }
5129 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
5130}
5131
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005132void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5133 const FieldInfo& field_info) {
5134 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5135
5136 LocationSummary* locations = instruction->GetLocations();
5137 vixl32::Register base = InputRegisterAt(instruction, 0);
5138 Location out = locations->Out();
5139 bool is_volatile = field_info.IsVolatile();
5140 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5141 Primitive::Type field_type = field_info.GetFieldType();
5142 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5143
5144 switch (field_type) {
5145 case Primitive::kPrimBoolean:
5146 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5147 break;
5148
5149 case Primitive::kPrimByte:
5150 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5151 break;
5152
5153 case Primitive::kPrimShort:
5154 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5155 break;
5156
5157 case Primitive::kPrimChar:
5158 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5159 break;
5160
5161 case Primitive::kPrimInt:
5162 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5163 break;
5164
5165 case Primitive::kPrimNot: {
5166 // /* HeapReference<Object> */ out = *(base + offset)
5167 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005168 Location temp_loc = locations->GetTemp(0);
5169 // Note that a potential implicit null check is handled in this
5170 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5171 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5172 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5173 if (is_volatile) {
5174 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5175 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005176 } else {
5177 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005178 codegen_->MaybeRecordImplicitNullCheck(instruction);
5179 if (is_volatile) {
5180 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5181 }
5182 // If read barriers are enabled, emit read barriers other than
5183 // Baker's using a slow path (and also unpoison the loaded
5184 // reference, if heap poisoning is enabled).
5185 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5186 }
5187 break;
5188 }
5189
5190 case Primitive::kPrimLong:
5191 if (is_volatile && !atomic_ldrd_strd) {
5192 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5193 } else {
5194 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5195 }
5196 break;
5197
5198 case Primitive::kPrimFloat:
5199 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5200 break;
5201
5202 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005203 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005204 if (is_volatile && !atomic_ldrd_strd) {
5205 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5206 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5207 GenerateWideAtomicLoad(base, offset, lo, hi);
5208 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5209 // scope.
5210 codegen_->MaybeRecordImplicitNullCheck(instruction);
5211 __ Vmov(out_dreg, lo, hi);
5212 } else {
5213 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 }
5216 break;
5217 }
5218
5219 case Primitive::kPrimVoid:
5220 LOG(FATAL) << "Unreachable type " << field_type;
5221 UNREACHABLE();
5222 }
5223
5224 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5225 // Potential implicit null checks, in the case of reference or
5226 // double fields, are handled in the previous switch statement.
5227 } else {
5228 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005229 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5230 // should use a scope and the assembler to emit the load instruction to guarantee that we
5231 // record the pc at the correct position. But the `Assembler` does not automatically handle
5232 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5233 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005234 codegen_->MaybeRecordImplicitNullCheck(instruction);
5235 }
5236
5237 if (is_volatile) {
5238 if (field_type == Primitive::kPrimNot) {
5239 // Memory barriers, in the case of references, are also handled
5240 // in the previous switch statement.
5241 } else {
5242 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5243 }
5244 }
5245}
5246
5247void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5248 HandleFieldSet(instruction, instruction->GetFieldInfo());
5249}
5250
5251void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5252 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5253}
5254
5255void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5256 HandleFieldGet(instruction, instruction->GetFieldInfo());
5257}
5258
5259void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5260 HandleFieldGet(instruction, instruction->GetFieldInfo());
5261}
5262
5263void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5264 HandleFieldGet(instruction, instruction->GetFieldInfo());
5265}
5266
5267void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5268 HandleFieldGet(instruction, instruction->GetFieldInfo());
5269}
5270
Scott Wakelingc34dba72016-10-03 10:14:44 +01005271void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5272 HandleFieldSet(instruction, instruction->GetFieldInfo());
5273}
5274
5275void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5276 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5277}
5278
Artem Serovcfbe9132016-10-14 15:58:56 +01005279void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5280 HUnresolvedInstanceFieldGet* instruction) {
5281 FieldAccessCallingConventionARMVIXL calling_convention;
5282 codegen_->CreateUnresolvedFieldLocationSummary(
5283 instruction, instruction->GetFieldType(), calling_convention);
5284}
5285
5286void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5287 HUnresolvedInstanceFieldGet* instruction) {
5288 FieldAccessCallingConventionARMVIXL calling_convention;
5289 codegen_->GenerateUnresolvedFieldAccess(instruction,
5290 instruction->GetFieldType(),
5291 instruction->GetFieldIndex(),
5292 instruction->GetDexPc(),
5293 calling_convention);
5294}
5295
5296void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5297 HUnresolvedInstanceFieldSet* instruction) {
5298 FieldAccessCallingConventionARMVIXL calling_convention;
5299 codegen_->CreateUnresolvedFieldLocationSummary(
5300 instruction, instruction->GetFieldType(), calling_convention);
5301}
5302
5303void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5304 HUnresolvedInstanceFieldSet* instruction) {
5305 FieldAccessCallingConventionARMVIXL calling_convention;
5306 codegen_->GenerateUnresolvedFieldAccess(instruction,
5307 instruction->GetFieldType(),
5308 instruction->GetFieldIndex(),
5309 instruction->GetDexPc(),
5310 calling_convention);
5311}
5312
5313void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5314 HUnresolvedStaticFieldGet* instruction) {
5315 FieldAccessCallingConventionARMVIXL calling_convention;
5316 codegen_->CreateUnresolvedFieldLocationSummary(
5317 instruction, instruction->GetFieldType(), calling_convention);
5318}
5319
5320void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5321 HUnresolvedStaticFieldGet* instruction) {
5322 FieldAccessCallingConventionARMVIXL calling_convention;
5323 codegen_->GenerateUnresolvedFieldAccess(instruction,
5324 instruction->GetFieldType(),
5325 instruction->GetFieldIndex(),
5326 instruction->GetDexPc(),
5327 calling_convention);
5328}
5329
5330void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5331 HUnresolvedStaticFieldSet* instruction) {
5332 FieldAccessCallingConventionARMVIXL calling_convention;
5333 codegen_->CreateUnresolvedFieldLocationSummary(
5334 instruction, instruction->GetFieldType(), calling_convention);
5335}
5336
5337void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5338 HUnresolvedStaticFieldSet* instruction) {
5339 FieldAccessCallingConventionARMVIXL calling_convention;
5340 codegen_->GenerateUnresolvedFieldAccess(instruction,
5341 instruction->GetFieldType(),
5342 instruction->GetFieldIndex(),
5343 instruction->GetDexPc(),
5344 calling_convention);
5345}
5346
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005347void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005348 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005349 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005350}
5351
5352void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5353 if (CanMoveNullCheckToUser(instruction)) {
5354 return;
5355 }
5356
5357 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005358 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005359 ExactAssemblyScope aas(GetVIXLAssembler(),
5360 vixl32::kMaxInstructionSizeInBytes,
5361 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005362 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5363 RecordPcInfo(instruction, instruction->GetDexPc());
5364}
5365
5366void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5367 NullCheckSlowPathARMVIXL* slow_path =
5368 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5369 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005370 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005371}
5372
5373void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5374 codegen_->GenerateNullCheck(instruction);
5375}
5376
Scott Wakelingc34dba72016-10-03 10:14:44 +01005377static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5378 switch (type) {
5379 case Primitive::kPrimNot:
5380 return kLoadWord;
5381 case Primitive::kPrimBoolean:
5382 return kLoadUnsignedByte;
5383 case Primitive::kPrimByte:
5384 return kLoadSignedByte;
5385 case Primitive::kPrimChar:
5386 return kLoadUnsignedHalfword;
5387 case Primitive::kPrimShort:
5388 return kLoadSignedHalfword;
5389 case Primitive::kPrimInt:
5390 return kLoadWord;
5391 case Primitive::kPrimLong:
5392 return kLoadWordPair;
5393 case Primitive::kPrimFloat:
5394 return kLoadSWord;
5395 case Primitive::kPrimDouble:
5396 return kLoadDWord;
5397 default:
5398 LOG(FATAL) << "Unreachable type " << type;
5399 UNREACHABLE();
5400 }
5401}
5402
5403static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5404 switch (type) {
5405 case Primitive::kPrimNot:
5406 return kStoreWord;
5407 case Primitive::kPrimBoolean:
5408 case Primitive::kPrimByte:
5409 return kStoreByte;
5410 case Primitive::kPrimChar:
5411 case Primitive::kPrimShort:
5412 return kStoreHalfword;
5413 case Primitive::kPrimInt:
5414 return kStoreWord;
5415 case Primitive::kPrimLong:
5416 return kStoreWordPair;
5417 case Primitive::kPrimFloat:
5418 return kStoreSWord;
5419 case Primitive::kPrimDouble:
5420 return kStoreDWord;
5421 default:
5422 LOG(FATAL) << "Unreachable type " << type;
5423 UNREACHABLE();
5424 }
5425}
5426
5427void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5428 Location out_loc,
5429 vixl32::Register base,
5430 vixl32::Register reg_index,
5431 vixl32::Condition cond) {
5432 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5433 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5434
5435 switch (type) {
5436 case Primitive::kPrimByte:
5437 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5438 break;
5439 case Primitive::kPrimBoolean:
5440 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5441 break;
5442 case Primitive::kPrimShort:
5443 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5444 break;
5445 case Primitive::kPrimChar:
5446 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5447 break;
5448 case Primitive::kPrimNot:
5449 case Primitive::kPrimInt:
5450 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5451 break;
5452 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5453 case Primitive::kPrimLong:
5454 case Primitive::kPrimFloat:
5455 case Primitive::kPrimDouble:
5456 default:
5457 LOG(FATAL) << "Unreachable type " << type;
5458 UNREACHABLE();
5459 }
5460}
5461
5462void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5463 Location loc,
5464 vixl32::Register base,
5465 vixl32::Register reg_index,
5466 vixl32::Condition cond) {
5467 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5468 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5469
5470 switch (type) {
5471 case Primitive::kPrimByte:
5472 case Primitive::kPrimBoolean:
5473 __ Strb(cond, RegisterFrom(loc), mem_address);
5474 break;
5475 case Primitive::kPrimShort:
5476 case Primitive::kPrimChar:
5477 __ Strh(cond, RegisterFrom(loc), mem_address);
5478 break;
5479 case Primitive::kPrimNot:
5480 case Primitive::kPrimInt:
5481 __ Str(cond, RegisterFrom(loc), mem_address);
5482 break;
5483 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5484 case Primitive::kPrimLong:
5485 case Primitive::kPrimFloat:
5486 case Primitive::kPrimDouble:
5487 default:
5488 LOG(FATAL) << "Unreachable type " << type;
5489 UNREACHABLE();
5490 }
5491}
5492
5493void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5494 bool object_array_get_with_read_barrier =
5495 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5496 LocationSummary* locations =
5497 new (GetGraph()->GetArena()) LocationSummary(instruction,
5498 object_array_get_with_read_barrier ?
5499 LocationSummary::kCallOnSlowPath :
5500 LocationSummary::kNoCall);
5501 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005503 }
5504 locations->SetInAt(0, Location::RequiresRegister());
5505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5506 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5508 } else {
5509 // The output overlaps in the case of an object array get with
5510 // read barriers enabled: we do not want the move to overwrite the
5511 // array's location, as we need it to emit the read barrier.
5512 locations->SetOut(
5513 Location::RequiresRegister(),
5514 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5515 }
5516 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005517 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005518 // Also need for String compression feature.
5519 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5520 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005521 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005522 }
5523}
5524
5525void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005526 LocationSummary* locations = instruction->GetLocations();
5527 Location obj_loc = locations->InAt(0);
5528 vixl32::Register obj = InputRegisterAt(instruction, 0);
5529 Location index = locations->InAt(1);
5530 Location out_loc = locations->Out();
5531 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5532 Primitive::Type type = instruction->GetType();
5533 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5534 instruction->IsStringCharAt();
5535 HInstruction* array_instr = instruction->GetArray();
5536 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005537
5538 switch (type) {
5539 case Primitive::kPrimBoolean:
5540 case Primitive::kPrimByte:
5541 case Primitive::kPrimShort:
5542 case Primitive::kPrimChar:
5543 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005544 vixl32::Register length;
5545 if (maybe_compressed_char_at) {
5546 length = RegisterFrom(locations->GetTemp(0));
5547 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5548 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5549 codegen_->MaybeRecordImplicitNullCheck(instruction);
5550 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005551 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005552 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005553 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005554 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005555 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5556 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5557 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005558 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005559 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5560 RegisterFrom(out_loc),
5561 obj,
5562 data_offset + const_index);
5563 __ B(&done);
5564 __ Bind(&uncompressed_load);
5565 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5566 RegisterFrom(out_loc),
5567 obj,
5568 data_offset + (const_index << 1));
5569 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005570 } else {
5571 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5572
5573 LoadOperandType load_type = GetLoadOperandType(type);
5574 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5575 }
5576 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005577 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005578 vixl32::Register temp = temps.Acquire();
5579
5580 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005581 // We do not need to compute the intermediate address from the array: the
5582 // input instruction has done it already. See the comment in
5583 // `TryExtractArrayAccessAddress()`.
5584 if (kIsDebugBuild) {
5585 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005586 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005587 }
5588 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005589 } else {
5590 __ Add(temp, obj, data_offset);
5591 }
5592 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005593 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005594 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5595 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5596 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005597 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005598 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
5599 __ B(&done);
5600 __ Bind(&uncompressed_load);
5601 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
5602 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005603 } else {
5604 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5605 }
5606 }
5607 break;
5608 }
5609
5610 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005611 // The read barrier instrumentation of object ArrayGet
5612 // instructions does not support the HIntermediateAddress
5613 // instruction.
5614 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5615
Scott Wakelingc34dba72016-10-03 10:14:44 +01005616 static_assert(
5617 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5618 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5619 // /* HeapReference<Object> */ out =
5620 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5621 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005622 Location temp = locations->GetTemp(0);
5623 // Note that a potential implicit null check is handled in this
5624 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5625 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5626 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005627 } else {
5628 vixl32::Register out = OutputRegister(instruction);
5629 if (index.IsConstant()) {
5630 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005631 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005632 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005633 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5634 // we should use a scope and the assembler to emit the load instruction to guarantee that
5635 // we record the pc at the correct position. But the `Assembler` does not automatically
5636 // handle unencodable offsets. Practically, everything is fine because the helper and
5637 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005638 codegen_->MaybeRecordImplicitNullCheck(instruction);
5639 // If read barriers are enabled, emit read barriers other than
5640 // Baker's using a slow path (and also unpoison the loaded
5641 // reference, if heap poisoning is enabled).
5642 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5643 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005644 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005645 vixl32::Register temp = temps.Acquire();
5646
5647 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005648 // We do not need to compute the intermediate address from the array: the
5649 // input instruction has done it already. See the comment in
5650 // `TryExtractArrayAccessAddress()`.
5651 if (kIsDebugBuild) {
5652 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005653 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005654 }
5655 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005656 } else {
5657 __ Add(temp, obj, data_offset);
5658 }
5659 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005660 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005661 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5662 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5663 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005664 codegen_->MaybeRecordImplicitNullCheck(instruction);
5665 // If read barriers are enabled, emit read barriers other than
5666 // Baker's using a slow path (and also unpoison the loaded
5667 // reference, if heap poisoning is enabled).
5668 codegen_->MaybeGenerateReadBarrierSlow(
5669 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5670 }
5671 }
5672 break;
5673 }
5674
5675 case Primitive::kPrimLong: {
5676 if (index.IsConstant()) {
5677 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005678 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005679 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5680 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005681 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005682 vixl32::Register temp = temps.Acquire();
5683 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5684 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5685 }
5686 break;
5687 }
5688
5689 case Primitive::kPrimFloat: {
5690 vixl32::SRegister out = SRegisterFrom(out_loc);
5691 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005692 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005693 GetAssembler()->LoadSFromOffset(out, obj, offset);
5694 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005695 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005696 vixl32::Register temp = temps.Acquire();
5697 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5698 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5699 }
5700 break;
5701 }
5702
5703 case Primitive::kPrimDouble: {
5704 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005705 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005706 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5707 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005708 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005709 vixl32::Register temp = temps.Acquire();
5710 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5711 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5712 }
5713 break;
5714 }
5715
5716 case Primitive::kPrimVoid:
5717 LOG(FATAL) << "Unreachable type " << type;
5718 UNREACHABLE();
5719 }
5720
5721 if (type == Primitive::kPrimNot) {
5722 // Potential implicit null checks, in the case of reference
5723 // arrays, are handled in the previous switch statement.
5724 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005725 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5726 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005727 codegen_->MaybeRecordImplicitNullCheck(instruction);
5728 }
5729}
5730
5731void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5732 Primitive::Type value_type = instruction->GetComponentType();
5733
5734 bool needs_write_barrier =
5735 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5736 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5737
5738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5739 instruction,
5740 may_need_runtime_call_for_type_check ?
5741 LocationSummary::kCallOnSlowPath :
5742 LocationSummary::kNoCall);
5743
5744 locations->SetInAt(0, Location::RequiresRegister());
5745 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5746 if (Primitive::IsFloatingPointType(value_type)) {
5747 locations->SetInAt(2, Location::RequiresFpuRegister());
5748 } else {
5749 locations->SetInAt(2, Location::RequiresRegister());
5750 }
5751 if (needs_write_barrier) {
5752 // Temporary registers for the write barrier.
5753 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5754 locations->AddTemp(Location::RequiresRegister());
5755 }
5756}
5757
5758void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005759 LocationSummary* locations = instruction->GetLocations();
5760 vixl32::Register array = InputRegisterAt(instruction, 0);
5761 Location index = locations->InAt(1);
5762 Primitive::Type value_type = instruction->GetComponentType();
5763 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5764 bool needs_write_barrier =
5765 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5766 uint32_t data_offset =
5767 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5768 Location value_loc = locations->InAt(2);
5769 HInstruction* array_instr = instruction->GetArray();
5770 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005771
5772 switch (value_type) {
5773 case Primitive::kPrimBoolean:
5774 case Primitive::kPrimByte:
5775 case Primitive::kPrimShort:
5776 case Primitive::kPrimChar:
5777 case Primitive::kPrimInt: {
5778 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005779 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005780 uint32_t full_offset =
5781 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5782 StoreOperandType store_type = GetStoreOperandType(value_type);
5783 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5784 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005785 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005786 vixl32::Register temp = temps.Acquire();
5787
5788 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005789 // We do not need to compute the intermediate address from the array: the
5790 // input instruction has done it already. See the comment in
5791 // `TryExtractArrayAccessAddress()`.
5792 if (kIsDebugBuild) {
5793 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005794 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005795 }
5796 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005797 } else {
5798 __ Add(temp, array, data_offset);
5799 }
5800 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5801 }
5802 break;
5803 }
5804
5805 case Primitive::kPrimNot: {
5806 vixl32::Register value = RegisterFrom(value_loc);
5807 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5808 // See the comment in instruction_simplifier_shared.cc.
5809 DCHECK(!has_intermediate_address);
5810
5811 if (instruction->InputAt(2)->IsNullConstant()) {
5812 // Just setting null.
5813 if (index.IsConstant()) {
5814 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005815 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005816 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5817 } else {
5818 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005819 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005820 vixl32::Register temp = temps.Acquire();
5821 __ Add(temp, array, data_offset);
5822 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5823 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005824 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5825 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005826 codegen_->MaybeRecordImplicitNullCheck(instruction);
5827 DCHECK(!needs_write_barrier);
5828 DCHECK(!may_need_runtime_call_for_type_check);
5829 break;
5830 }
5831
5832 DCHECK(needs_write_barrier);
5833 Location temp1_loc = locations->GetTemp(0);
5834 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5835 Location temp2_loc = locations->GetTemp(1);
5836 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5837 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5838 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5839 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5840 vixl32::Label done;
5841 SlowPathCodeARMVIXL* slow_path = nullptr;
5842
5843 if (may_need_runtime_call_for_type_check) {
5844 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5845 codegen_->AddSlowPath(slow_path);
5846 if (instruction->GetValueCanBeNull()) {
5847 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005848 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005849 if (index.IsConstant()) {
5850 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005851 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005852 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5853 } else {
5854 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005855 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005856 vixl32::Register temp = temps.Acquire();
5857 __ Add(temp, array, data_offset);
5858 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5859 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005860 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5861 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005862 codegen_->MaybeRecordImplicitNullCheck(instruction);
5863 __ B(&done);
5864 __ Bind(&non_zero);
5865 }
5866
5867 // Note that when read barriers are enabled, the type checks
5868 // are performed without read barriers. This is fine, even in
5869 // the case where a class object is in the from-space after
5870 // the flip, as a comparison involving such a type would not
5871 // produce a false positive; it may of course produce a false
5872 // negative, in which case we would take the ArraySet slow
5873 // path.
5874
Alexandre Rames374ddf32016-11-04 10:40:49 +00005875 {
5876 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005877 ExactAssemblyScope aas(GetVIXLAssembler(),
5878 vixl32::kMaxInstructionSizeInBytes,
5879 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005880 // /* HeapReference<Class> */ temp1 = array->klass_
5881 __ ldr(temp1, MemOperand(array, class_offset));
5882 codegen_->MaybeRecordImplicitNullCheck(instruction);
5883 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005884 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5885
5886 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5887 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5888 // /* HeapReference<Class> */ temp2 = value->klass_
5889 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5890 // If heap poisoning is enabled, no need to unpoison `temp1`
5891 // nor `temp2`, as we are comparing two poisoned references.
5892 __ Cmp(temp1, temp2);
5893
5894 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5895 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00005896 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005897 // If heap poisoning is enabled, the `temp1` reference has
5898 // not been unpoisoned yet; unpoison it now.
5899 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5900
5901 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5902 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5903 // If heap poisoning is enabled, no need to unpoison
5904 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005905 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005906 __ Bind(&do_put);
5907 } else {
5908 __ B(ne, slow_path->GetEntryLabel());
5909 }
5910 }
5911
5912 vixl32::Register source = value;
5913 if (kPoisonHeapReferences) {
5914 // Note that in the case where `value` is a null reference,
5915 // we do not enter this block, as a null reference does not
5916 // need poisoning.
5917 DCHECK_EQ(value_type, Primitive::kPrimNot);
5918 __ Mov(temp1, value);
5919 GetAssembler()->PoisonHeapReference(temp1);
5920 source = temp1;
5921 }
5922
5923 if (index.IsConstant()) {
5924 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005925 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005926 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5927 } else {
5928 DCHECK(index.IsRegister()) << index;
5929
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005930 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005931 vixl32::Register temp = temps.Acquire();
5932 __ Add(temp, array, data_offset);
5933 codegen_->StoreToShiftedRegOffset(value_type,
5934 LocationFrom(source),
5935 temp,
5936 RegisterFrom(index));
5937 }
5938
5939 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005940 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5941 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005942 codegen_->MaybeRecordImplicitNullCheck(instruction);
5943 }
5944
5945 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5946
5947 if (done.IsReferenced()) {
5948 __ Bind(&done);
5949 }
5950
5951 if (slow_path != nullptr) {
5952 __ Bind(slow_path->GetExitLabel());
5953 }
5954
5955 break;
5956 }
5957
5958 case Primitive::kPrimLong: {
5959 Location value = locations->InAt(2);
5960 if (index.IsConstant()) {
5961 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005962 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005963 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5964 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005965 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005966 vixl32::Register temp = temps.Acquire();
5967 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5968 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5969 }
5970 break;
5971 }
5972
5973 case Primitive::kPrimFloat: {
5974 Location value = locations->InAt(2);
5975 DCHECK(value.IsFpuRegister());
5976 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005977 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005978 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5979 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005980 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005981 vixl32::Register temp = temps.Acquire();
5982 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5983 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5984 }
5985 break;
5986 }
5987
5988 case Primitive::kPrimDouble: {
5989 Location value = locations->InAt(2);
5990 DCHECK(value.IsFpuRegisterPair());
5991 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005992 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005993 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5994 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005995 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005996 vixl32::Register temp = temps.Acquire();
5997 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5998 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5999 }
6000 break;
6001 }
6002
6003 case Primitive::kPrimVoid:
6004 LOG(FATAL) << "Unreachable type " << value_type;
6005 UNREACHABLE();
6006 }
6007
6008 // Objects are handled in the switch.
6009 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006010 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6011 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006012 codegen_->MaybeRecordImplicitNullCheck(instruction);
6013 }
6014}
6015
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006016void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6017 LocationSummary* locations =
6018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6019 locations->SetInAt(0, Location::RequiresRegister());
6020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6021}
6022
6023void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6024 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6025 vixl32::Register obj = InputRegisterAt(instruction, 0);
6026 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006027 {
Artem Serov0fb37192016-12-06 18:13:40 +00006028 ExactAssemblyScope aas(GetVIXLAssembler(),
6029 vixl32::kMaxInstructionSizeInBytes,
6030 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006031 __ ldr(out, MemOperand(obj, offset));
6032 codegen_->MaybeRecordImplicitNullCheck(instruction);
6033 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006034 // Mask out compression flag from String's array length.
6035 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006036 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006037 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006038}
6039
Artem Serov2bbc9532016-10-21 11:51:50 +01006040void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006041 LocationSummary* locations =
6042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6043
6044 locations->SetInAt(0, Location::RequiresRegister());
6045 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6047}
6048
6049void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6050 vixl32::Register out = OutputRegister(instruction);
6051 vixl32::Register first = InputRegisterAt(instruction, 0);
6052 Location second = instruction->GetLocations()->InAt(1);
6053
Artem Serov2bbc9532016-10-21 11:51:50 +01006054 if (second.IsRegister()) {
6055 __ Add(out, first, RegisterFrom(second));
6056 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006057 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006058 }
6059}
6060
Scott Wakelingc34dba72016-10-03 10:14:44 +01006061void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6062 RegisterSet caller_saves = RegisterSet::Empty();
6063 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6064 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6065 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6066 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
6067 locations->SetInAt(0, Location::RequiresRegister());
6068 locations->SetInAt(1, Location::RequiresRegister());
6069}
6070
6071void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6072 SlowPathCodeARMVIXL* slow_path =
6073 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6074 codegen_->AddSlowPath(slow_path);
6075
6076 vixl32::Register index = InputRegisterAt(instruction, 0);
6077 vixl32::Register length = InputRegisterAt(instruction, 1);
6078
6079 __ Cmp(index, length);
6080 __ B(hs, slow_path->GetEntryLabel());
6081}
6082
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006083void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6084 vixl32::Register card,
6085 vixl32::Register object,
6086 vixl32::Register value,
6087 bool can_be_null) {
6088 vixl32::Label is_null;
6089 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006090 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006091 }
6092 GetAssembler()->LoadFromOffset(
6093 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006094 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006095 __ Strb(card, MemOperand(card, temp));
6096 if (can_be_null) {
6097 __ Bind(&is_null);
6098 }
6099}
6100
Scott Wakelingfe885462016-09-22 10:24:38 +01006101void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6102 LOG(FATAL) << "Unreachable";
6103}
6104
6105void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6106 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6107}
6108
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006109void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006110 LocationSummary* locations =
6111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6112 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006113}
6114
6115void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6116 HBasicBlock* block = instruction->GetBlock();
6117 if (block->GetLoopInformation() != nullptr) {
6118 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6119 // The back edge will generate the suspend check.
6120 return;
6121 }
6122 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6123 // The goto will generate the suspend check.
6124 return;
6125 }
6126 GenerateSuspendCheck(instruction, nullptr);
6127}
6128
6129void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6130 HBasicBlock* successor) {
6131 SuspendCheckSlowPathARMVIXL* slow_path =
6132 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6133 if (slow_path == nullptr) {
6134 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6135 instruction->SetSlowPath(slow_path);
6136 codegen_->AddSlowPath(slow_path);
6137 if (successor != nullptr) {
6138 DCHECK(successor->IsLoopHeader());
6139 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6140 }
6141 } else {
6142 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6143 }
6144
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006145 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006146 vixl32::Register temp = temps.Acquire();
6147 GetAssembler()->LoadFromOffset(
6148 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6149 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006150 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006151 __ Bind(slow_path->GetReturnLabel());
6152 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006153 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006154 __ B(slow_path->GetEntryLabel());
6155 }
6156}
6157
Scott Wakelingfe885462016-09-22 10:24:38 +01006158ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6159 return codegen_->GetAssembler();
6160}
6161
6162void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006163 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006164 MoveOperands* move = moves_[index];
6165 Location source = move->GetSource();
6166 Location destination = move->GetDestination();
6167
6168 if (source.IsRegister()) {
6169 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006170 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006171 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006172 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006173 } else {
6174 DCHECK(destination.IsStackSlot());
6175 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006176 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006177 sp,
6178 destination.GetStackIndex());
6179 }
6180 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006181 if (destination.IsRegister()) {
6182 GetAssembler()->LoadFromOffset(kLoadWord,
6183 RegisterFrom(destination),
6184 sp,
6185 source.GetStackIndex());
6186 } else if (destination.IsFpuRegister()) {
6187 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6188 } else {
6189 DCHECK(destination.IsStackSlot());
6190 vixl32::Register temp = temps.Acquire();
6191 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6192 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6193 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006194 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006195 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006196 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006197 } else if (destination.IsFpuRegister()) {
6198 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6199 } else {
6200 DCHECK(destination.IsStackSlot());
6201 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6202 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006203 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006204 if (destination.IsDoubleStackSlot()) {
6205 vixl32::DRegister temp = temps.AcquireD();
6206 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6207 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6208 } else if (destination.IsRegisterPair()) {
6209 DCHECK(ExpectedPairLayout(destination));
6210 GetAssembler()->LoadFromOffset(
6211 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6212 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006213 DCHECK(destination.IsFpuRegisterPair()) << destination;
6214 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006215 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006216 } else if (source.IsRegisterPair()) {
6217 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006218 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6219 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006220 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006221 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006222 } else {
6223 DCHECK(destination.IsDoubleStackSlot()) << destination;
6224 DCHECK(ExpectedPairLayout(source));
6225 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006226 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006227 sp,
6228 destination.GetStackIndex());
6229 }
6230 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006231 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006232 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006233 } else if (destination.IsFpuRegisterPair()) {
6234 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6235 } else {
6236 DCHECK(destination.IsDoubleStackSlot()) << destination;
6237 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6238 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006239 } else {
6240 DCHECK(source.IsConstant()) << source;
6241 HConstant* constant = source.GetConstant();
6242 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6243 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6244 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006245 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006246 } else {
6247 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006248 vixl32::Register temp = temps.Acquire();
6249 __ Mov(temp, value);
6250 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6251 }
6252 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006253 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006254 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006255 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6256 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006257 } else {
6258 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006259 vixl32::Register temp = temps.Acquire();
6260 __ Mov(temp, Low32Bits(value));
6261 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6262 __ Mov(temp, High32Bits(value));
6263 GetAssembler()->StoreToOffset(kStoreWord,
6264 temp,
6265 sp,
6266 destination.GetHighStackIndex(kArmWordSize));
6267 }
6268 } else if (constant->IsDoubleConstant()) {
6269 double value = constant->AsDoubleConstant()->GetValue();
6270 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006271 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006272 } else {
6273 DCHECK(destination.IsDoubleStackSlot()) << destination;
6274 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006275 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006276 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006277 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006278 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006279 GetAssembler()->StoreToOffset(kStoreWord,
6280 temp,
6281 sp,
6282 destination.GetHighStackIndex(kArmWordSize));
6283 }
6284 } else {
6285 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6286 float value = constant->AsFloatConstant()->GetValue();
6287 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006288 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006289 } else {
6290 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006291 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006292 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006293 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6294 }
6295 }
6296 }
6297}
6298
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006299void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6300 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6301 vixl32::Register temp = temps.Acquire();
6302 __ Mov(temp, reg);
6303 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6304 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006305}
6306
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006307void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6308 // TODO(VIXL32): Double check the performance of this implementation.
6309 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Artem Serov4593f7d2016-12-29 16:21:49 +00006310 vixl32::SRegister temp_1 = temps.AcquireS();
6311 vixl32::SRegister temp_2 = temps.AcquireS();
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006312
Artem Serov4593f7d2016-12-29 16:21:49 +00006313 __ Vldr(temp_1, MemOperand(sp, mem1));
6314 __ Vldr(temp_2, MemOperand(sp, mem2));
6315 __ Vstr(temp_1, MemOperand(sp, mem2));
6316 __ Vstr(temp_2, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01006317}
6318
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006319void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6320 MoveOperands* move = moves_[index];
6321 Location source = move->GetSource();
6322 Location destination = move->GetDestination();
6323 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6324
6325 if (source.IsRegister() && destination.IsRegister()) {
6326 vixl32::Register temp = temps.Acquire();
6327 DCHECK(!RegisterFrom(source).Is(temp));
6328 DCHECK(!RegisterFrom(destination).Is(temp));
6329 __ Mov(temp, RegisterFrom(destination));
6330 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6331 __ Mov(RegisterFrom(source), temp);
6332 } else if (source.IsRegister() && destination.IsStackSlot()) {
6333 Exchange(RegisterFrom(source), destination.GetStackIndex());
6334 } else if (source.IsStackSlot() && destination.IsRegister()) {
6335 Exchange(RegisterFrom(destination), source.GetStackIndex());
6336 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006337 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006338 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006339 vixl32::SRegister temp = temps.AcquireS();
6340 __ Vmov(temp, SRegisterFrom(source));
6341 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6342 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006343 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6344 vixl32::DRegister temp = temps.AcquireD();
6345 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6346 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6347 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6348 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6349 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6350 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6351 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6352 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6353 vixl32::DRegister temp = temps.AcquireD();
6354 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6355 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6356 GetAssembler()->StoreDToOffset(temp, sp, mem);
6357 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006358 vixl32::DRegister first = DRegisterFrom(source);
6359 vixl32::DRegister second = DRegisterFrom(destination);
6360 vixl32::DRegister temp = temps.AcquireD();
6361 __ Vmov(temp, first);
6362 __ Vmov(first, second);
6363 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006364 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006365 vixl32::DRegister reg = source.IsFpuRegisterPair()
6366 ? DRegisterFrom(source)
6367 : DRegisterFrom(destination);
6368 int mem = source.IsFpuRegisterPair()
6369 ? destination.GetStackIndex()
6370 : source.GetStackIndex();
6371 vixl32::DRegister temp = temps.AcquireD();
6372 __ Vmov(temp, reg);
6373 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6374 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006375 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006376 vixl32::SRegister reg = source.IsFpuRegister()
6377 ? SRegisterFrom(source)
6378 : SRegisterFrom(destination);
6379 int mem = source.IsFpuRegister()
6380 ? destination.GetStackIndex()
6381 : source.GetStackIndex();
6382 vixl32::Register temp = temps.Acquire();
6383 __ Vmov(temp, reg);
6384 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6385 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006386 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6387 vixl32::DRegister temp1 = temps.AcquireD();
6388 vixl32::DRegister temp2 = temps.AcquireD();
6389 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6390 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6391 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6392 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6393 } else {
6394 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6395 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006396}
6397
6398void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
6399 TODO_VIXL32(FATAL);
6400}
6401
6402void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
6403 TODO_VIXL32(FATAL);
6404}
6405
Artem Serov02d37832016-10-25 15:25:33 +01006406HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006407 HLoadClass::LoadKind desired_class_load_kind) {
6408 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006409 case HLoadClass::LoadKind::kInvalid:
6410 LOG(FATAL) << "UNREACHABLE";
6411 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006412 case HLoadClass::LoadKind::kReferrersClass:
6413 break;
6414 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006415 DCHECK(!GetCompilerOptions().GetCompilePic());
6416 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006417 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6418 DCHECK(GetCompilerOptions().GetCompilePic());
6419 break;
6420 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006421 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006422 case HLoadClass::LoadKind::kBssEntry:
6423 DCHECK(!Runtime::Current()->UseJitCompilation());
6424 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006425 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006426 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006427 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006428 case HLoadClass::LoadKind::kDexCacheViaMethod:
6429 break;
6430 }
6431 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006432}
6433
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006434void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006435 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6436 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006437 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006438 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006439 cls,
6440 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006441 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006442 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006443 return;
6444 }
Vladimir Marko41559982017-01-06 14:04:23 +00006445 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006446
Artem Serovd4cc5b22016-11-04 11:19:09 +00006447 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6448 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006449 ? LocationSummary::kCallOnSlowPath
6450 : LocationSummary::kNoCall;
6451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006452 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006453 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006454 }
6455
Vladimir Marko41559982017-01-06 14:04:23 +00006456 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006457 locations->SetInAt(0, Location::RequiresRegister());
6458 }
6459 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006460 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6461 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6462 // Rely on the type resolution or initialization and marking to save everything we need.
6463 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6464 // to the custom calling convention) or by marking, so we request a different temp.
6465 locations->AddTemp(Location::RequiresRegister());
6466 RegisterSet caller_saves = RegisterSet::Empty();
6467 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6468 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6469 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6470 // that the the kPrimNot result register is the same as the first argument register.
6471 locations->SetCustomSlowPathCallerSaves(caller_saves);
6472 } else {
6473 // For non-Baker read barrier we have a temp-clobbering call.
6474 }
6475 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006476}
6477
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006478// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6479// move.
6480void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006481 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6482 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6483 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006484 return;
6485 }
Vladimir Marko41559982017-01-06 14:04:23 +00006486 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006487
Vladimir Marko41559982017-01-06 14:04:23 +00006488 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006489 Location out_loc = locations->Out();
6490 vixl32::Register out = OutputRegister(cls);
6491
Artem Serovd4cc5b22016-11-04 11:19:09 +00006492 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6493 ? kWithoutReadBarrier
6494 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006495 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006496 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006497 case HLoadClass::LoadKind::kReferrersClass: {
6498 DCHECK(!cls->CanCallRuntime());
6499 DCHECK(!cls->MustGenerateClinitCheck());
6500 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6501 vixl32::Register current_method = InputRegisterAt(cls, 0);
6502 GenerateGcRootFieldLoad(cls,
6503 out_loc,
6504 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006505 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006506 read_barrier_option);
6507 break;
6508 }
6509 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006510 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006511 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6512 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6513 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006514 break;
6515 }
6516 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006517 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006518 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6519 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6520 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6521 codegen_->EmitMovwMovtPlaceholder(labels, out);
6522 break;
6523 }
6524 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006525 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006526 uint32_t address = dchecked_integral_cast<uint32_t>(
6527 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6528 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006529 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006530 break;
6531 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006532 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006533 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6534 ? RegisterFrom(locations->GetTemp(0))
6535 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006536 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006537 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006538 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6539 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006540 generate_null_check = true;
6541 break;
6542 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006543 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006544 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6545 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006546 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006547 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006548 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006549 break;
6550 }
Vladimir Marko41559982017-01-06 14:04:23 +00006551 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006552 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006553 LOG(FATAL) << "UNREACHABLE";
6554 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006555 }
6556
6557 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6558 DCHECK(cls->CanCallRuntime());
6559 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6560 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6561 codegen_->AddSlowPath(slow_path);
6562 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006563 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006564 }
6565 if (cls->MustGenerateClinitCheck()) {
6566 GenerateClassInitializationCheck(slow_path, out);
6567 } else {
6568 __ Bind(slow_path->GetExitLabel());
6569 }
6570 }
6571}
6572
Artem Serov02d37832016-10-25 15:25:33 +01006573void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6574 LocationSummary* locations =
6575 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6576 locations->SetInAt(0, Location::RequiresRegister());
6577 if (check->HasUses()) {
6578 locations->SetOut(Location::SameAsFirstInput());
6579 }
6580}
6581
6582void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
6583 // We assume the class is not null.
6584 LoadClassSlowPathARMVIXL* slow_path =
6585 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
6586 check,
6587 check->GetDexPc(),
6588 /* do_clinit */ true);
6589 codegen_->AddSlowPath(slow_path);
6590 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
6591}
6592
6593void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
6594 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
6595 UseScratchRegisterScope temps(GetVIXLAssembler());
6596 vixl32::Register temp = temps.Acquire();
6597 GetAssembler()->LoadFromOffset(kLoadWord,
6598 temp,
6599 class_reg,
6600 mirror::Class::StatusOffset().Int32Value());
6601 __ Cmp(temp, mirror::Class::kStatusInitialized);
6602 __ B(lt, slow_path->GetEntryLabel());
6603 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6604 // properly. Therefore, we do a memory fence.
6605 __ Dmb(ISH);
6606 __ Bind(slow_path->GetExitLabel());
6607}
6608
Artem Serov02d37832016-10-25 15:25:33 +01006609HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006610 HLoadString::LoadKind desired_string_load_kind) {
6611 switch (desired_string_load_kind) {
6612 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006613 DCHECK(!GetCompilerOptions().GetCompilePic());
6614 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006615 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6616 DCHECK(GetCompilerOptions().GetCompilePic());
6617 break;
6618 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006619 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006620 case HLoadString::LoadKind::kBssEntry:
6621 DCHECK(!Runtime::Current()->UseJitCompilation());
6622 break;
6623 case HLoadString::LoadKind::kJitTableAddress:
6624 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006625 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006626 case HLoadString::LoadKind::kDexCacheViaMethod:
6627 break;
6628 }
6629 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006630}
6631
6632void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006633 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006635 HLoadString::LoadKind load_kind = load->GetLoadKind();
6636 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006637 locations->SetOut(LocationFrom(r0));
6638 } else {
6639 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006640 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6641 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006642 // Rely on the pResolveString and marking to save everything we need, including temps.
6643 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6644 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006645 locations->AddTemp(Location::RequiresRegister());
6646 RegisterSet caller_saves = RegisterSet::Empty();
6647 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6648 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6649 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6650 // that the the kPrimNot result register is the same as the first argument register.
6651 locations->SetCustomSlowPathCallerSaves(caller_saves);
6652 } else {
6653 // For non-Baker read barrier we have a temp-clobbering call.
6654 }
6655 }
Artem Serov02d37832016-10-25 15:25:33 +01006656 }
6657}
6658
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006659// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6660// move.
6661void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006662 LocationSummary* locations = load->GetLocations();
6663 Location out_loc = locations->Out();
6664 vixl32::Register out = OutputRegister(load);
6665 HLoadString::LoadKind load_kind = load->GetLoadKind();
6666
6667 switch (load_kind) {
6668 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006669 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6670 load->GetStringIndex()));
6671 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006672 }
6673 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6674 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6675 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006676 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006677 codegen_->EmitMovwMovtPlaceholder(labels, out);
6678 return; // No dex cache slow path.
6679 }
6680 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006681 uint32_t address = dchecked_integral_cast<uint32_t>(
6682 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6683 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006684 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6685 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006686 }
6687 case HLoadString::LoadKind::kBssEntry: {
6688 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006689 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6690 ? RegisterFrom(locations->GetTemp(0))
6691 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006692 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006693 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006694 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6695 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6696 LoadStringSlowPathARMVIXL* slow_path =
6697 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6698 codegen_->AddSlowPath(slow_path);
6699 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6700 __ Bind(slow_path->GetExitLabel());
6701 return;
6702 }
6703 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006704 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006705 load->GetStringIndex(),
6706 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006707 // /* GcRoot<mirror::String> */ out = *out
6708 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6709 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006710 }
6711 default:
6712 break;
6713 }
Artem Serov02d37832016-10-25 15:25:33 +01006714
6715 // TODO: Re-add the compiler code to do string dex cache lookup again.
6716 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6717 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006718 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006719 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6720 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6721}
6722
6723static int32_t GetExceptionTlsOffset() {
6724 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6725}
6726
6727void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6728 LocationSummary* locations =
6729 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6730 locations->SetOut(Location::RequiresRegister());
6731}
6732
6733void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6734 vixl32::Register out = OutputRegister(load);
6735 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6736}
6737
6738
6739void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6740 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6741}
6742
6743void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6744 UseScratchRegisterScope temps(GetVIXLAssembler());
6745 vixl32::Register temp = temps.Acquire();
6746 __ Mov(temp, 0);
6747 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6748}
6749
6750void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6751 LocationSummary* locations =
6752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6753 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6754 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6755}
6756
6757void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6758 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6759 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6760}
6761
Artem Serov657022c2016-11-23 14:19:38 +00006762// Temp is used for read barrier.
6763static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6764 if (kEmitCompilerReadBarrier &&
6765 (kUseBakerReadBarrier ||
6766 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6767 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6768 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6769 return 1;
6770 }
6771 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006772}
6773
Artem Serov657022c2016-11-23 14:19:38 +00006774// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6775// interface pointer, one for loading the current interface.
6776// The other checks have one temp for loading the object's class.
6777static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6778 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6779 return 3;
6780 }
6781 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6782}
Artem Serovcfbe9132016-10-14 15:58:56 +01006783
6784void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6785 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6786 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6787 bool baker_read_barrier_slow_path = false;
6788 switch (type_check_kind) {
6789 case TypeCheckKind::kExactCheck:
6790 case TypeCheckKind::kAbstractClassCheck:
6791 case TypeCheckKind::kClassHierarchyCheck:
6792 case TypeCheckKind::kArrayObjectCheck:
6793 call_kind =
6794 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6795 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6796 break;
6797 case TypeCheckKind::kArrayCheck:
6798 case TypeCheckKind::kUnresolvedCheck:
6799 case TypeCheckKind::kInterfaceCheck:
6800 call_kind = LocationSummary::kCallOnSlowPath;
6801 break;
6802 }
6803
6804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6805 if (baker_read_barrier_slow_path) {
6806 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6807 }
6808 locations->SetInAt(0, Location::RequiresRegister());
6809 locations->SetInAt(1, Location::RequiresRegister());
6810 // The "out" register is used as a temporary, so it overlaps with the inputs.
6811 // Note that TypeCheckSlowPathARM uses this register too.
6812 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006813 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006814}
6815
6816void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6817 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6818 LocationSummary* locations = instruction->GetLocations();
6819 Location obj_loc = locations->InAt(0);
6820 vixl32::Register obj = InputRegisterAt(instruction, 0);
6821 vixl32::Register cls = InputRegisterAt(instruction, 1);
6822 Location out_loc = locations->Out();
6823 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006824 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6825 DCHECK_LE(num_temps, 1u);
6826 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6830 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6831 vixl32::Label done, zero;
6832 SlowPathCodeARMVIXL* slow_path = nullptr;
6833
6834 // Return 0 if `obj` is null.
6835 // avoid null check if we know obj is not null.
6836 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006837 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006838 }
6839
Artem Serovcfbe9132016-10-14 15:58:56 +01006840 switch (type_check_kind) {
6841 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006842 // /* HeapReference<Class> */ out = obj->klass_
6843 GenerateReferenceLoadTwoRegisters(instruction,
6844 out_loc,
6845 obj_loc,
6846 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006847 maybe_temp_loc,
6848 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006849 __ Cmp(out, cls);
6850 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00006851 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006852 __ Mov(out, 1);
6853 __ B(&done);
6854 break;
6855 }
6856
6857 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006858 // /* HeapReference<Class> */ out = obj->klass_
6859 GenerateReferenceLoadTwoRegisters(instruction,
6860 out_loc,
6861 obj_loc,
6862 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006863 maybe_temp_loc,
6864 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006865 // If the class is abstract, we eagerly fetch the super class of the
6866 // object to avoid doing a comparison we know will fail.
6867 vixl32::Label loop;
6868 __ Bind(&loop);
6869 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006870 GenerateReferenceLoadOneRegister(instruction,
6871 out_loc,
6872 super_offset,
6873 maybe_temp_loc,
6874 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006875 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006876 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006877 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006878 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006879 __ Mov(out, 1);
6880 if (zero.IsReferenced()) {
6881 __ B(&done);
6882 }
6883 break;
6884 }
6885
6886 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006887 // /* HeapReference<Class> */ out = obj->klass_
6888 GenerateReferenceLoadTwoRegisters(instruction,
6889 out_loc,
6890 obj_loc,
6891 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006892 maybe_temp_loc,
6893 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006894 // Walk over the class hierarchy to find a match.
6895 vixl32::Label loop, success;
6896 __ Bind(&loop);
6897 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006898 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006899 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006900 GenerateReferenceLoadOneRegister(instruction,
6901 out_loc,
6902 super_offset,
6903 maybe_temp_loc,
6904 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006905 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006906 // If `out` is null, we use it for the result, and jump to `done`.
6907 __ B(&done);
6908 __ Bind(&success);
6909 __ Mov(out, 1);
6910 if (zero.IsReferenced()) {
6911 __ B(&done);
6912 }
6913 break;
6914 }
6915
6916 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006917 // /* HeapReference<Class> */ out = obj->klass_
6918 GenerateReferenceLoadTwoRegisters(instruction,
6919 out_loc,
6920 obj_loc,
6921 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006922 maybe_temp_loc,
6923 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006924 // Do an exact check.
6925 vixl32::Label exact_check;
6926 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006927 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006928 // Otherwise, we need to check that the object's class is a non-primitive array.
6929 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006930 GenerateReferenceLoadOneRegister(instruction,
6931 out_loc,
6932 component_offset,
6933 maybe_temp_loc,
6934 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006935 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006936 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006937 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6938 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006939 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006940 __ Bind(&exact_check);
6941 __ Mov(out, 1);
6942 __ B(&done);
6943 break;
6944 }
6945
6946 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006947 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006948 // /* HeapReference<Class> */ out = obj->klass_
6949 GenerateReferenceLoadTwoRegisters(instruction,
6950 out_loc,
6951 obj_loc,
6952 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006953 maybe_temp_loc,
6954 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006955 __ Cmp(out, cls);
6956 DCHECK(locations->OnlyCallsOnSlowPath());
6957 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6958 /* is_fatal */ false);
6959 codegen_->AddSlowPath(slow_path);
6960 __ B(ne, slow_path->GetEntryLabel());
6961 __ Mov(out, 1);
6962 if (zero.IsReferenced()) {
6963 __ B(&done);
6964 }
6965 break;
6966 }
6967
6968 case TypeCheckKind::kUnresolvedCheck:
6969 case TypeCheckKind::kInterfaceCheck: {
6970 // Note that we indeed only call on slow path, but we always go
6971 // into the slow path for the unresolved and interface check
6972 // cases.
6973 //
6974 // We cannot directly call the InstanceofNonTrivial runtime
6975 // entry point without resorting to a type checking slow path
6976 // here (i.e. by calling InvokeRuntime directly), as it would
6977 // require to assign fixed registers for the inputs of this
6978 // HInstanceOf instruction (following the runtime calling
6979 // convention), which might be cluttered by the potential first
6980 // read barrier emission at the beginning of this method.
6981 //
6982 // TODO: Introduce a new runtime entry point taking the object
6983 // to test (instead of its class) as argument, and let it deal
6984 // with the read barrier issues. This will let us refactor this
6985 // case of the `switch` code as it was previously (with a direct
6986 // call to the runtime not using a type checking slow path).
6987 // This should also be beneficial for the other cases above.
6988 DCHECK(locations->OnlyCallsOnSlowPath());
6989 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6990 /* is_fatal */ false);
6991 codegen_->AddSlowPath(slow_path);
6992 __ B(slow_path->GetEntryLabel());
6993 if (zero.IsReferenced()) {
6994 __ B(&done);
6995 }
6996 break;
6997 }
6998 }
6999
7000 if (zero.IsReferenced()) {
7001 __ Bind(&zero);
7002 __ Mov(out, 0);
7003 }
7004
7005 if (done.IsReferenced()) {
7006 __ Bind(&done);
7007 }
7008
7009 if (slow_path != nullptr) {
7010 __ Bind(slow_path->GetExitLabel());
7011 }
7012}
7013
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007014void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7015 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7016 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7017
7018 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7019 switch (type_check_kind) {
7020 case TypeCheckKind::kExactCheck:
7021 case TypeCheckKind::kAbstractClassCheck:
7022 case TypeCheckKind::kClassHierarchyCheck:
7023 case TypeCheckKind::kArrayObjectCheck:
7024 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7025 LocationSummary::kCallOnSlowPath :
7026 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7027 break;
7028 case TypeCheckKind::kArrayCheck:
7029 case TypeCheckKind::kUnresolvedCheck:
7030 case TypeCheckKind::kInterfaceCheck:
7031 call_kind = LocationSummary::kCallOnSlowPath;
7032 break;
7033 }
7034
7035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7036 locations->SetInAt(0, Location::RequiresRegister());
7037 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007038 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007039}
7040
7041void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7042 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7043 LocationSummary* locations = instruction->GetLocations();
7044 Location obj_loc = locations->InAt(0);
7045 vixl32::Register obj = InputRegisterAt(instruction, 0);
7046 vixl32::Register cls = InputRegisterAt(instruction, 1);
7047 Location temp_loc = locations->GetTemp(0);
7048 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007049 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7050 DCHECK_LE(num_temps, 3u);
7051 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7052 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7053 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7054 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7055 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7056 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7057 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7058 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7059 const uint32_t object_array_data_offset =
7060 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007061
Artem Serov657022c2016-11-23 14:19:38 +00007062 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7063 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7064 // read barriers is done for performance and code size reasons.
7065 bool is_type_check_slow_path_fatal = false;
7066 if (!kEmitCompilerReadBarrier) {
7067 is_type_check_slow_path_fatal =
7068 (type_check_kind == TypeCheckKind::kExactCheck ||
7069 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7070 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7071 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7072 !instruction->CanThrowIntoCatchBlock();
7073 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007074 SlowPathCodeARMVIXL* type_check_slow_path =
7075 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7076 is_type_check_slow_path_fatal);
7077 codegen_->AddSlowPath(type_check_slow_path);
7078
7079 vixl32::Label done;
7080 // Avoid null check if we know obj is not null.
7081 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007082 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007083 }
7084
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007085 switch (type_check_kind) {
7086 case TypeCheckKind::kExactCheck:
7087 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007088 // /* HeapReference<Class> */ temp = obj->klass_
7089 GenerateReferenceLoadTwoRegisters(instruction,
7090 temp_loc,
7091 obj_loc,
7092 class_offset,
7093 maybe_temp2_loc,
7094 kWithoutReadBarrier);
7095
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007096 __ Cmp(temp, cls);
7097 // Jump to slow path for throwing the exception or doing a
7098 // more involved array check.
7099 __ B(ne, type_check_slow_path->GetEntryLabel());
7100 break;
7101 }
7102
7103 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007104 // /* HeapReference<Class> */ temp = obj->klass_
7105 GenerateReferenceLoadTwoRegisters(instruction,
7106 temp_loc,
7107 obj_loc,
7108 class_offset,
7109 maybe_temp2_loc,
7110 kWithoutReadBarrier);
7111
Artem Serovcfbe9132016-10-14 15:58:56 +01007112 // If the class is abstract, we eagerly fetch the super class of the
7113 // object to avoid doing a comparison we know will fail.
7114 vixl32::Label loop;
7115 __ Bind(&loop);
7116 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007117 GenerateReferenceLoadOneRegister(instruction,
7118 temp_loc,
7119 super_offset,
7120 maybe_temp2_loc,
7121 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007122
7123 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7124 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007125 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007126
7127 // Otherwise, compare the classes.
7128 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007129 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007130 break;
7131 }
7132
7133 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007134 // /* HeapReference<Class> */ temp = obj->klass_
7135 GenerateReferenceLoadTwoRegisters(instruction,
7136 temp_loc,
7137 obj_loc,
7138 class_offset,
7139 maybe_temp2_loc,
7140 kWithoutReadBarrier);
7141
Artem Serovcfbe9132016-10-14 15:58:56 +01007142 // Walk over the class hierarchy to find a match.
7143 vixl32::Label loop;
7144 __ Bind(&loop);
7145 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007146 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007147
7148 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007149 GenerateReferenceLoadOneRegister(instruction,
7150 temp_loc,
7151 super_offset,
7152 maybe_temp2_loc,
7153 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007154
7155 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7156 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007157 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007158 // Otherwise, jump to the beginning of the loop.
7159 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007160 break;
7161 }
7162
Artem Serovcfbe9132016-10-14 15:58:56 +01007163 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007164 // /* HeapReference<Class> */ temp = obj->klass_
7165 GenerateReferenceLoadTwoRegisters(instruction,
7166 temp_loc,
7167 obj_loc,
7168 class_offset,
7169 maybe_temp2_loc,
7170 kWithoutReadBarrier);
7171
Artem Serovcfbe9132016-10-14 15:58:56 +01007172 // Do an exact check.
7173 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007174 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007175
7176 // Otherwise, we need to check that the object's class is a non-primitive array.
7177 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007178 GenerateReferenceLoadOneRegister(instruction,
7179 temp_loc,
7180 component_offset,
7181 maybe_temp2_loc,
7182 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007183 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007184 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007185 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7186 // to further check that this component type is not a primitive type.
7187 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7188 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007189 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007190 break;
7191 }
7192
7193 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007194 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007195 // We cannot directly call the CheckCast runtime entry point
7196 // without resorting to a type checking slow path here (i.e. by
7197 // calling InvokeRuntime directly), as it would require to
7198 // assign fixed registers for the inputs of this HInstanceOf
7199 // instruction (following the runtime calling convention), which
7200 // might be cluttered by the potential first read barrier
7201 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007202
Artem Serovcfbe9132016-10-14 15:58:56 +01007203 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007204 break;
Artem Serov657022c2016-11-23 14:19:38 +00007205
7206 case TypeCheckKind::kInterfaceCheck: {
7207 // Avoid read barriers to improve performance of the fast path. We can not get false
7208 // positives by doing this.
7209 // /* HeapReference<Class> */ temp = obj->klass_
7210 GenerateReferenceLoadTwoRegisters(instruction,
7211 temp_loc,
7212 obj_loc,
7213 class_offset,
7214 maybe_temp2_loc,
7215 kWithoutReadBarrier);
7216
7217 // /* HeapReference<Class> */ temp = temp->iftable_
7218 GenerateReferenceLoadTwoRegisters(instruction,
7219 temp_loc,
7220 temp_loc,
7221 iftable_offset,
7222 maybe_temp2_loc,
7223 kWithoutReadBarrier);
7224 // Iftable is never null.
7225 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7226 // Loop through the iftable and check if any class matches.
7227 vixl32::Label start_loop;
7228 __ Bind(&start_loop);
7229 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7230 type_check_slow_path->GetEntryLabel());
7231 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7232 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7233 // Go to next interface.
7234 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7235 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7236 // Compare the classes and continue the loop if they do not match.
7237 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007238 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007239 break;
7240 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007241 }
7242 __ Bind(&done);
7243
7244 __ Bind(type_check_slow_path->GetExitLabel());
7245}
7246
Artem Serov551b28f2016-10-18 19:11:30 +01007247void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7248 LocationSummary* locations =
7249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7250 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7251 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7252}
7253
7254void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7255 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7256 instruction,
7257 instruction->GetDexPc());
7258 if (instruction->IsEnter()) {
7259 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7260 } else {
7261 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7262 }
7263}
7264
Artem Serov02109dd2016-09-23 17:17:54 +01007265void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7266 HandleBitwiseOperation(instruction, AND);
7267}
7268
7269void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7270 HandleBitwiseOperation(instruction, ORR);
7271}
7272
7273void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7274 HandleBitwiseOperation(instruction, EOR);
7275}
7276
7277void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7278 LocationSummary* locations =
7279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7280 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7281 || instruction->GetResultType() == Primitive::kPrimLong);
7282 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7283 locations->SetInAt(0, Location::RequiresRegister());
7284 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7286}
7287
7288void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7289 HandleBitwiseOperation(instruction);
7290}
7291
7292void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7293 HandleBitwiseOperation(instruction);
7294}
7295
7296void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7297 HandleBitwiseOperation(instruction);
7298}
7299
Artem Serov2bbc9532016-10-21 11:51:50 +01007300void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7301 LocationSummary* locations =
7302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7303 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7304 || instruction->GetResultType() == Primitive::kPrimLong);
7305
7306 locations->SetInAt(0, Location::RequiresRegister());
7307 locations->SetInAt(1, Location::RequiresRegister());
7308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7309}
7310
7311void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7312 LocationSummary* locations = instruction->GetLocations();
7313 Location first = locations->InAt(0);
7314 Location second = locations->InAt(1);
7315 Location out = locations->Out();
7316
7317 if (instruction->GetResultType() == Primitive::kPrimInt) {
7318 vixl32::Register first_reg = RegisterFrom(first);
7319 vixl32::Register second_reg = RegisterFrom(second);
7320 vixl32::Register out_reg = RegisterFrom(out);
7321
7322 switch (instruction->GetOpKind()) {
7323 case HInstruction::kAnd:
7324 __ Bic(out_reg, first_reg, second_reg);
7325 break;
7326 case HInstruction::kOr:
7327 __ Orn(out_reg, first_reg, second_reg);
7328 break;
7329 // There is no EON on arm.
7330 case HInstruction::kXor:
7331 default:
7332 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7333 UNREACHABLE();
7334 }
7335 return;
7336
7337 } else {
7338 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7339 vixl32::Register first_low = LowRegisterFrom(first);
7340 vixl32::Register first_high = HighRegisterFrom(first);
7341 vixl32::Register second_low = LowRegisterFrom(second);
7342 vixl32::Register second_high = HighRegisterFrom(second);
7343 vixl32::Register out_low = LowRegisterFrom(out);
7344 vixl32::Register out_high = HighRegisterFrom(out);
7345
7346 switch (instruction->GetOpKind()) {
7347 case HInstruction::kAnd:
7348 __ Bic(out_low, first_low, second_low);
7349 __ Bic(out_high, first_high, second_high);
7350 break;
7351 case HInstruction::kOr:
7352 __ Orn(out_low, first_low, second_low);
7353 __ Orn(out_high, first_high, second_high);
7354 break;
7355 // There is no EON on arm.
7356 case HInstruction::kXor:
7357 default:
7358 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7359 UNREACHABLE();
7360 }
7361 }
7362}
7363
Anton Kirilov74234da2017-01-13 14:42:47 +00007364void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7365 HDataProcWithShifterOp* instruction) {
7366 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7367 instruction->GetType() == Primitive::kPrimLong);
7368 LocationSummary* locations =
7369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7370 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7371 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7372
7373 locations->SetInAt(0, Location::RequiresRegister());
7374 locations->SetInAt(1, Location::RequiresRegister());
7375 locations->SetOut(Location::RequiresRegister(),
7376 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7377}
7378
7379void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7380 HDataProcWithShifterOp* instruction) {
7381 const LocationSummary* const locations = instruction->GetLocations();
7382 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7383 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7384
7385 if (instruction->GetType() == Primitive::kPrimInt) {
7386 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7387
7388 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7389 ? LowRegisterFrom(locations->InAt(1))
7390 : InputRegisterAt(instruction, 1);
7391
7392 GenerateDataProcInstruction(kind,
7393 OutputRegister(instruction),
7394 InputRegisterAt(instruction, 0),
7395 Operand(second,
7396 ShiftFromOpKind(op_kind),
7397 instruction->GetShiftAmount()),
7398 codegen_);
7399 } else {
7400 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7401
7402 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7403 const vixl32::Register second = InputRegisterAt(instruction, 1);
7404
7405 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7406 GenerateDataProc(kind,
7407 locations->Out(),
7408 locations->InAt(0),
7409 second,
7410 Operand(second, ShiftType::ASR, 31),
7411 codegen_);
7412 } else {
7413 GenerateLongDataProc(instruction, codegen_);
7414 }
7415 }
7416}
7417
Artem Serov02109dd2016-09-23 17:17:54 +01007418// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7419void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7420 vixl32::Register first,
7421 uint32_t value) {
7422 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7423 if (value == 0xffffffffu) {
7424 if (!out.Is(first)) {
7425 __ Mov(out, first);
7426 }
7427 return;
7428 }
7429 if (value == 0u) {
7430 __ Mov(out, 0);
7431 return;
7432 }
7433 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
7434 __ And(out, first, value);
7435 } else {
7436 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
7437 __ Bic(out, first, ~value);
7438 }
7439}
7440
7441// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7442void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7443 vixl32::Register first,
7444 uint32_t value) {
7445 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7446 if (value == 0u) {
7447 if (!out.Is(first)) {
7448 __ Mov(out, first);
7449 }
7450 return;
7451 }
7452 if (value == 0xffffffffu) {
7453 __ Mvn(out, 0);
7454 return;
7455 }
7456 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7457 __ Orr(out, first, value);
7458 } else {
7459 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7460 __ Orn(out, first, ~value);
7461 }
7462}
7463
7464// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7465void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7466 vixl32::Register first,
7467 uint32_t value) {
7468 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7469 if (value == 0u) {
7470 if (!out.Is(first)) {
7471 __ Mov(out, first);
7472 }
7473 return;
7474 }
7475 __ Eor(out, first, value);
7476}
7477
Anton Kirilovdda43962016-11-21 19:55:20 +00007478void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7479 Location first,
7480 uint64_t value) {
7481 vixl32::Register out_low = LowRegisterFrom(out);
7482 vixl32::Register out_high = HighRegisterFrom(out);
7483 vixl32::Register first_low = LowRegisterFrom(first);
7484 vixl32::Register first_high = HighRegisterFrom(first);
7485 uint32_t value_low = Low32Bits(value);
7486 uint32_t value_high = High32Bits(value);
7487 if (value_low == 0u) {
7488 if (!out_low.Is(first_low)) {
7489 __ Mov(out_low, first_low);
7490 }
7491 __ Add(out_high, first_high, value_high);
7492 return;
7493 }
7494 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007495 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007496 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007497 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007498 __ Sbc(out_high, first_high, ~value_high);
7499 } else {
7500 LOG(FATAL) << "Unexpected constant " << value_high;
7501 UNREACHABLE();
7502 }
7503}
7504
Artem Serov02109dd2016-09-23 17:17:54 +01007505void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7506 LocationSummary* locations = instruction->GetLocations();
7507 Location first = locations->InAt(0);
7508 Location second = locations->InAt(1);
7509 Location out = locations->Out();
7510
7511 if (second.IsConstant()) {
7512 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7513 uint32_t value_low = Low32Bits(value);
7514 if (instruction->GetResultType() == Primitive::kPrimInt) {
7515 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7516 vixl32::Register out_reg = OutputRegister(instruction);
7517 if (instruction->IsAnd()) {
7518 GenerateAndConst(out_reg, first_reg, value_low);
7519 } else if (instruction->IsOr()) {
7520 GenerateOrrConst(out_reg, first_reg, value_low);
7521 } else {
7522 DCHECK(instruction->IsXor());
7523 GenerateEorConst(out_reg, first_reg, value_low);
7524 }
7525 } else {
7526 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7527 uint32_t value_high = High32Bits(value);
7528 vixl32::Register first_low = LowRegisterFrom(first);
7529 vixl32::Register first_high = HighRegisterFrom(first);
7530 vixl32::Register out_low = LowRegisterFrom(out);
7531 vixl32::Register out_high = HighRegisterFrom(out);
7532 if (instruction->IsAnd()) {
7533 GenerateAndConst(out_low, first_low, value_low);
7534 GenerateAndConst(out_high, first_high, value_high);
7535 } else if (instruction->IsOr()) {
7536 GenerateOrrConst(out_low, first_low, value_low);
7537 GenerateOrrConst(out_high, first_high, value_high);
7538 } else {
7539 DCHECK(instruction->IsXor());
7540 GenerateEorConst(out_low, first_low, value_low);
7541 GenerateEorConst(out_high, first_high, value_high);
7542 }
7543 }
7544 return;
7545 }
7546
7547 if (instruction->GetResultType() == Primitive::kPrimInt) {
7548 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7549 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
7550 vixl32::Register out_reg = OutputRegister(instruction);
7551 if (instruction->IsAnd()) {
7552 __ And(out_reg, first_reg, second_reg);
7553 } else if (instruction->IsOr()) {
7554 __ Orr(out_reg, first_reg, second_reg);
7555 } else {
7556 DCHECK(instruction->IsXor());
7557 __ Eor(out_reg, first_reg, second_reg);
7558 }
7559 } else {
7560 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7561 vixl32::Register first_low = LowRegisterFrom(first);
7562 vixl32::Register first_high = HighRegisterFrom(first);
7563 vixl32::Register second_low = LowRegisterFrom(second);
7564 vixl32::Register second_high = HighRegisterFrom(second);
7565 vixl32::Register out_low = LowRegisterFrom(out);
7566 vixl32::Register out_high = HighRegisterFrom(out);
7567 if (instruction->IsAnd()) {
7568 __ And(out_low, first_low, second_low);
7569 __ And(out_high, first_high, second_high);
7570 } else if (instruction->IsOr()) {
7571 __ Orr(out_low, first_low, second_low);
7572 __ Orr(out_high, first_high, second_high);
7573 } else {
7574 DCHECK(instruction->IsXor());
7575 __ Eor(out_low, first_low, second_low);
7576 __ Eor(out_high, first_high, second_high);
7577 }
7578 }
7579}
7580
Artem Serovcfbe9132016-10-14 15:58:56 +01007581void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007582 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01007583 Location out,
7584 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007585 Location maybe_temp,
7586 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01007587 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007588 if (read_barrier_option == kWithReadBarrier) {
7589 CHECK(kEmitCompilerReadBarrier);
7590 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7591 if (kUseBakerReadBarrier) {
7592 // Load with fast path based Baker's read barrier.
7593 // /* HeapReference<Object> */ out = *(out + offset)
7594 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7595 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
7596 } else {
7597 // Load with slow path based read barrier.
7598 // Save the value of `out` into `maybe_temp` before overwriting it
7599 // in the following move operation, as we will need it for the
7600 // read barrier below.
7601 __ Mov(RegisterFrom(maybe_temp), out_reg);
7602 // /* HeapReference<Object> */ out = *(out + offset)
7603 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7604 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
7605 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007606 } else {
7607 // Plain load with no read barrier.
7608 // /* HeapReference<Object> */ out = *(out + offset)
7609 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7610 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7611 }
7612}
7613
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007614void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007615 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007616 Location out,
7617 Location obj,
7618 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007619 Location maybe_temp,
7620 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007621 vixl32::Register out_reg = RegisterFrom(out);
7622 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007623 if (read_barrier_option == kWithReadBarrier) {
7624 CHECK(kEmitCompilerReadBarrier);
7625 if (kUseBakerReadBarrier) {
7626 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
7627 // Load with fast path based Baker's read barrier.
7628 // /* HeapReference<Object> */ out = *(obj + offset)
7629 codegen_->GenerateFieldLoadWithBakerReadBarrier(
7630 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
7631 } else {
7632 // Load with slow path based read barrier.
7633 // /* HeapReference<Object> */ out = *(obj + offset)
7634 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7635 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7636 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007637 } else {
7638 // Plain load with no read barrier.
7639 // /* HeapReference<Object> */ out = *(obj + offset)
7640 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7641 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
7642 }
7643}
7644
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007645void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007646 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007647 Location root,
7648 vixl32::Register obj,
7649 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007650 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007651 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007652 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007653 DCHECK(kEmitCompilerReadBarrier);
7654 if (kUseBakerReadBarrier) {
7655 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillain35345a52017-02-27 14:32:08 +00007656 // Baker's read barrier are used:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007657 //
Roland Levillain35345a52017-02-27 14:32:08 +00007658 // root = obj.field;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007659 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain35345a52017-02-27 14:32:08 +00007660 // if (temp != null) {
7661 // root = temp(root)
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007662 // }
7663
7664 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7665 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7666 static_assert(
7667 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7668 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7669 "have different sizes.");
7670 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7671 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7672 "have different sizes.");
7673
Roland Levillain35345a52017-02-27 14:32:08 +00007674 // Slow path marking the GC root `root`.
7675 Location temp = LocationFrom(lr);
7676 SlowPathCodeARMVIXL* slow_path =
7677 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
7678 instruction,
7679 root,
7680 /*entrypoint*/ temp);
7681 codegen_->AddSlowPath(slow_path);
7682
7683 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7684 const int32_t entry_point_offset =
7685 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7686 // Loading the entrypoint does not require a load acquire since it is only changed when
7687 // threads are suspended or running a checkpoint.
7688 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007689 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7690 // checking GetIsGcMarking.
7691 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7692 __ Bind(slow_path->GetExitLabel());
7693 } else {
7694 // GC root loaded through a slow path for read barriers other
7695 // than Baker's.
7696 // /* GcRoot<mirror::Object>* */ root = obj + offset
7697 __ Add(root_reg, obj, offset);
7698 // /* mirror::Object* */ root = root->Read()
7699 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7700 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007701 } else {
7702 // Plain GC root load with no read barrier.
7703 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7704 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7705 // Note that GC roots are not affected by heap poisoning, thus we
7706 // do not have to unpoison `root_reg` here.
7707 }
7708}
7709
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007710void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7711 Location ref,
7712 vixl32::Register obj,
7713 uint32_t offset,
7714 Location temp,
7715 bool needs_null_check) {
7716 DCHECK(kEmitCompilerReadBarrier);
7717 DCHECK(kUseBakerReadBarrier);
7718
7719 // /* HeapReference<Object> */ ref = *(obj + offset)
7720 Location no_index = Location::NoLocation();
7721 ScaleFactor no_scale_factor = TIMES_1;
7722 GenerateReferenceLoadWithBakerReadBarrier(
7723 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007724}
7725
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007726void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7727 Location ref,
7728 vixl32::Register obj,
7729 uint32_t data_offset,
7730 Location index,
7731 Location temp,
7732 bool needs_null_check) {
7733 DCHECK(kEmitCompilerReadBarrier);
7734 DCHECK(kUseBakerReadBarrier);
7735
7736 static_assert(
7737 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7738 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7739 // /* HeapReference<Object> */ ref =
7740 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7741 ScaleFactor scale_factor = TIMES_4;
7742 GenerateReferenceLoadWithBakerReadBarrier(
7743 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007744}
7745
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007746void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7747 Location ref,
7748 vixl32::Register obj,
7749 uint32_t offset,
7750 Location index,
7751 ScaleFactor scale_factor,
7752 Location temp,
7753 bool needs_null_check,
7754 bool always_update_field,
7755 vixl32::Register* temp2) {
7756 DCHECK(kEmitCompilerReadBarrier);
7757 DCHECK(kUseBakerReadBarrier);
7758
Roland Levillain35345a52017-02-27 14:32:08 +00007759 // In slow path based read barriers, the read barrier call is
7760 // inserted after the original load. However, in fast path based
7761 // Baker's read barriers, we need to perform the load of
7762 // mirror::Object::monitor_ *before* the original reference load.
7763 // This load-load ordering is required by the read barrier.
7764 // The fast path/slow path (for Baker's algorithm) should look like:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007765 //
Roland Levillain35345a52017-02-27 14:32:08 +00007766 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7767 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7768 // HeapReference<Object> ref = *src; // Original reference load.
7769 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7770 // if (is_gray) {
7771 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007772 // }
Roland Levillain35345a52017-02-27 14:32:08 +00007773 //
7774 // Note: the original implementation in ReadBarrier::Barrier is
7775 // slightly more complex as it performs additional checks that we do
7776 // not do here for performance reasons.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007777
Roland Levillain35345a52017-02-27 14:32:08 +00007778 vixl32::Register ref_reg = RegisterFrom(ref);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007779 vixl32::Register temp_reg = RegisterFrom(temp);
Roland Levillain35345a52017-02-27 14:32:08 +00007780 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007781
Roland Levillain35345a52017-02-27 14:32:08 +00007782 // /* int32_t */ monitor = obj->monitor_
7783 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7784 if (needs_null_check) {
7785 MaybeRecordImplicitNullCheck(instruction);
7786 }
7787 // /* LockWord */ lock_word = LockWord(monitor)
7788 static_assert(sizeof(LockWord) == sizeof(int32_t),
7789 "art::LockWord and int32_t have different sizes.");
7790
7791 // Introduce a dependency on the lock_word including the rb_state,
7792 // which shall prevent load-load reordering without using
7793 // a memory barrier (which would be more expensive).
7794 // `obj` is unchanged by this operation, but its value now depends
7795 // on `temp_reg`.
7796 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7797
7798 // The actual reference load.
7799 if (index.IsValid()) {
7800 // Load types involving an "index": ArrayGet,
7801 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7802 // intrinsics.
7803 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7804 if (index.IsConstant()) {
7805 size_t computed_offset =
7806 (Int32ConstantFrom(index) << scale_factor) + offset;
7807 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7808 } else {
7809 // Handle the special case of the
7810 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7811 // intrinsics, which use a register pair as index ("long
7812 // offset"), of which only the low part contains data.
7813 vixl32::Register index_reg = index.IsRegisterPair()
7814 ? LowRegisterFrom(index)
7815 : RegisterFrom(index);
7816 UseScratchRegisterScope temps(GetVIXLAssembler());
7817 const vixl32::Register temp3 = temps.Acquire();
7818 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7819 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7820 }
7821 } else {
7822 // /* HeapReference<Object> */ ref = *(obj + offset)
7823 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7824 }
7825
7826 // Object* ref = ref_addr->AsMirrorPtr()
7827 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7828
7829 // Slow path marking the object `ref` when it is gray.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007830 SlowPathCodeARMVIXL* slow_path;
7831 if (always_update_field) {
7832 DCHECK(temp2 != nullptr);
Roland Levillain47b3ab22017-02-27 14:31:35 +00007833 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7834 // of the form `obj + field_offset`, where `obj` is a register and
7835 // `field_offset` is a register pair (of which only the lower half
7836 // is used). Thus `offset` and `scale_factor` above are expected
7837 // to be null in this code path.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007838 DCHECK_EQ(offset, 0u);
7839 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain47b3ab22017-02-27 14:31:35 +00007840 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
Roland Levillain35345a52017-02-27 14:32:08 +00007841 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007842 } else {
Roland Levillain35345a52017-02-27 14:32:08 +00007843 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007844 }
7845 AddSlowPath(slow_path);
7846
Roland Levillain35345a52017-02-27 14:32:08 +00007847 // if (rb_state == ReadBarrier::GrayState())
7848 // ref = ReadBarrier::Mark(ref);
7849 // Given the numeric representation, it's enough to check the low bit of the
7850 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7851 // which can be a 16-bit instruction unlike the TST immediate.
7852 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7853 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7854 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7855 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007856 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007857}
7858
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007859void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7860 Location out,
7861 Location ref,
7862 Location obj,
7863 uint32_t offset,
7864 Location index) {
7865 DCHECK(kEmitCompilerReadBarrier);
7866
7867 // Insert a slow path based read barrier *after* the reference load.
7868 //
7869 // If heap poisoning is enabled, the unpoisoning of the loaded
7870 // reference will be carried out by the runtime within the slow
7871 // path.
7872 //
7873 // Note that `ref` currently does not get unpoisoned (when heap
7874 // poisoning is enabled), which is alright as the `ref` argument is
7875 // not used by the artReadBarrierSlow entry point.
7876 //
7877 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7878 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7879 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7880 AddSlowPath(slow_path);
7881
7882 __ B(slow_path->GetEntryLabel());
7883 __ Bind(slow_path->GetExitLabel());
7884}
7885
7886void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007887 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007888 Location ref,
7889 Location obj,
7890 uint32_t offset,
7891 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007892 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007893 // Baker's read barriers shall be handled by the fast path
7894 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007895 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007896 // If heap poisoning is enabled, unpoisoning will be taken care of
7897 // by the runtime within the slow path.
7898 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007899 } else if (kPoisonHeapReferences) {
7900 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7901 }
7902}
7903
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007904void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7905 Location out,
7906 Location root) {
7907 DCHECK(kEmitCompilerReadBarrier);
7908
7909 // Insert a slow path based read barrier *after* the GC root load.
7910 //
7911 // Note that GC roots are not affected by heap poisoning, so we do
7912 // not need to do anything special for this here.
7913 SlowPathCodeARMVIXL* slow_path =
7914 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7915 AddSlowPath(slow_path);
7916
7917 __ B(slow_path->GetEntryLabel());
7918 __ Bind(slow_path->GetExitLabel());
7919}
7920
Artem Serov02d37832016-10-25 15:25:33 +01007921// Check if the desired_dispatch_info is supported. If it is, return it,
7922// otherwise return a fall-back info that should be used instead.
7923HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007924 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007925 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007926 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007927}
7928
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007929vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7930 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7931 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7932 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7933 if (!invoke->GetLocations()->Intrinsified()) {
7934 return RegisterFrom(location);
7935 }
7936 // For intrinsics we allow any location, so it may be on the stack.
7937 if (!location.IsRegister()) {
7938 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7939 return temp;
7940 }
7941 // For register locations, check if the register was saved. If so, get it from the stack.
7942 // Note: There is a chance that the register was saved but not overwritten, so we could
7943 // save one load. However, since this is just an intrinsic slow path we prefer this
7944 // simple and more robust approach rather that trying to determine if that's the case.
7945 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00007946 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007947 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7948 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7949 return temp;
7950 }
7951 return RegisterFrom(location);
7952}
7953
TatWai Chongd8c052a2016-11-02 16:12:48 +08007954Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007955 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007956 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007957 switch (invoke->GetMethodLoadKind()) {
7958 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7959 uint32_t offset =
7960 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7961 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007962 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7963 break;
7964 }
7965 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7966 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7967 break;
7968 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7969 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7970 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007971 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7972 HArmDexCacheArraysBase* base =
7973 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7974 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7975 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7976 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007977 break;
7978 }
7979 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7980 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7981 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007982 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007983 if (current_method.IsRegister()) {
7984 method_reg = RegisterFrom(current_method);
7985 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007986 DCHECK(invoke->GetLocations()->Intrinsified());
7987 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007988 method_reg = reg;
7989 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007990 }
7991 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7992 GetAssembler()->LoadFromOffset(
7993 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007994 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007995 method_reg,
7996 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7997 // temp = temp[index_in_cache];
7998 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7999 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8000 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008001 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008002 break;
8003 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008004 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008005 return callee_method;
8006}
8007
8008void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8009 Location temp) {
8010 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008011
Artem Serovd4cc5b22016-11-04 11:19:09 +00008012 switch (invoke->GetCodePtrLocation()) {
8013 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8014 __ Bl(GetFrameEntryLabel());
8015 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008016 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8017 // LR = callee_method->entry_point_from_quick_compiled_code_
8018 GetAssembler()->LoadFromOffset(
8019 kLoadWord,
8020 lr,
8021 RegisterFrom(callee_method),
8022 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008023 {
8024 // 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 +00008025 ExactAssemblyScope aas(GetVIXLAssembler(),
8026 vixl32::k16BitT32InstructionSizeInBytes,
8027 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008028 // LR()
8029 __ blx(lr);
8030 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008031 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008032 }
8033
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008034 DCHECK(!IsLeafMethod());
8035}
8036
8037void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8038 vixl32::Register temp = RegisterFrom(temp_location);
8039 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8040 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8041
8042 // Use the calling convention instead of the location of the receiver, as
8043 // intrinsics may have put the receiver in a different register. In the intrinsics
8044 // slow path, the arguments have been moved to the right place, so here we are
8045 // guaranteed that the receiver is the first register of the calling convention.
8046 InvokeDexCallingConventionARMVIXL calling_convention;
8047 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8048 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008049 {
8050 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008051 ExactAssemblyScope aas(GetVIXLAssembler(),
8052 vixl32::kMaxInstructionSizeInBytes,
8053 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008054 // /* HeapReference<Class> */ temp = receiver->klass_
8055 __ ldr(temp, MemOperand(receiver, class_offset));
8056 MaybeRecordImplicitNullCheck(invoke);
8057 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008058 // Instead of simply (possibly) unpoisoning `temp` here, we should
8059 // emit a read barrier for the previous class reference load.
8060 // However this is not required in practice, as this is an
8061 // intermediate/temporary reference and because the current
8062 // concurrent copying collector keeps the from-space memory
8063 // intact/accessible until the end of the marking phase (the
8064 // concurrent copying collector may not in the future).
8065 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8066
8067 // temp = temp->GetMethodAt(method_offset);
8068 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8069 kArmPointerSize).Int32Value();
8070 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8071 // LR = temp->GetEntryPoint();
8072 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8073 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008074 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8075 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8076 // that.
8077 // 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 +00008078 ExactAssemblyScope aas(GetVIXLAssembler(),
8079 vixl32::k16BitT32InstructionSizeInBytes,
8080 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008081 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008082}
8083
Artem Serovd4cc5b22016-11-04 11:19:09 +00008084CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008085 const DexFile& dex_file, dex::StringIndex string_index) {
8086 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008087}
8088
8089CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8090 const DexFile& dex_file, dex::TypeIndex type_index) {
8091 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8092}
8093
Vladimir Marko1998cd02017-01-13 13:02:58 +00008094CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8095 const DexFile& dex_file, dex::TypeIndex type_index) {
8096 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8097}
8098
Artem Serovd4cc5b22016-11-04 11:19:09 +00008099CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8100 const DexFile& dex_file, uint32_t element_offset) {
8101 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8102}
8103
8104CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8105 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8106 patches->emplace_back(dex_file, offset_or_index);
8107 return &patches->back();
8108}
8109
Artem Serovc5fcb442016-12-02 19:19:58 +00008110VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
8111 const DexFile& dex_file,
8112 dex::StringIndex string_index) {
8113 return boot_image_string_patches_.GetOrCreate(
8114 StringReference(&dex_file, string_index),
8115 [this]() {
8116 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8117 });
8118}
8119
8120VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
8121 const DexFile& dex_file,
8122 dex::TypeIndex type_index) {
8123 return boot_image_type_patches_.GetOrCreate(
8124 TypeReference(&dex_file, type_index),
8125 [this]() {
8126 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8127 });
8128}
8129
8130VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
8131 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
8132 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
8133 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
8134}
8135
8136VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
8137 return DeduplicateUint32Literal(address, &uint32_literals_);
8138}
8139
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008140VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8141 const DexFile& dex_file,
8142 dex::StringIndex string_index,
8143 Handle<mirror::String> handle) {
8144 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8145 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008146 return jit_string_patches_.GetOrCreate(
8147 StringReference(&dex_file, string_index),
8148 [this]() {
8149 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8150 });
8151}
8152
8153VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8154 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008155 Handle<mirror::Class> handle) {
8156 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8157 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008158 return jit_class_patches_.GetOrCreate(
8159 TypeReference(&dex_file, type_index),
8160 [this]() {
8161 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8162 });
8163}
8164
Artem Serovd4cc5b22016-11-04 11:19:09 +00008165template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8166inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8167 const ArenaDeque<PcRelativePatchInfo>& infos,
8168 ArenaVector<LinkerPatch>* linker_patches) {
8169 for (const PcRelativePatchInfo& info : infos) {
8170 const DexFile& dex_file = info.target_dex_file;
8171 size_t offset_or_index = info.offset_or_index;
8172 DCHECK(info.add_pc_label.IsBound());
8173 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8174 // Add MOVW patch.
8175 DCHECK(info.movw_label.IsBound());
8176 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8177 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8178 // Add MOVT patch.
8179 DCHECK(info.movt_label.IsBound());
8180 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8181 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8182 }
8183}
8184
8185void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8186 DCHECK(linker_patches->empty());
8187 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008188 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008189 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00008190 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008191 boot_image_type_patches_.size() +
8192 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00008193 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008194 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008195 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008196 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8197 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008198 for (const auto& entry : boot_image_string_patches_) {
8199 const StringReference& target_string = entry.first;
8200 VIXLUInt32Literal* literal = entry.second;
8201 DCHECK(literal->IsBound());
8202 uint32_t literal_offset = literal->GetLocation();
8203 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8204 target_string.dex_file,
8205 target_string.string_index.index_));
8206 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008207 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008208 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008209 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8210 linker_patches);
8211 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008212 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8213 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008214 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8215 linker_patches);
8216 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008217 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8218 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008219 for (const auto& entry : boot_image_type_patches_) {
8220 const TypeReference& target_type = entry.first;
8221 VIXLUInt32Literal* literal = entry.second;
8222 DCHECK(literal->IsBound());
8223 uint32_t literal_offset = literal->GetLocation();
8224 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8225 target_type.dex_file,
8226 target_type.type_index.index_));
8227 }
Artem Serovc5fcb442016-12-02 19:19:58 +00008228 for (const auto& entry : boot_image_address_patches_) {
8229 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
8230 VIXLUInt32Literal* literal = entry.second;
8231 DCHECK(literal->IsBound());
8232 uint32_t literal_offset = literal->GetLocation();
8233 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
8234 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008235 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00008236}
8237
8238VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
8239 uint32_t value,
8240 Uint32ToLiteralMap* map) {
8241 return map->GetOrCreate(
8242 value,
8243 [this, value]() {
8244 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
8245 });
8246}
8247
8248VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
8249 MethodReference target_method,
8250 MethodToLiteralMap* map) {
8251 return map->GetOrCreate(
8252 target_method,
8253 [this]() {
8254 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8255 });
8256}
8257
Artem Serov2bbc9532016-10-21 11:51:50 +01008258void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8259 LocationSummary* locations =
8260 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8261 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8262 Location::RequiresRegister());
8263 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8264 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8266}
8267
8268void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8269 vixl32::Register res = OutputRegister(instr);
8270 vixl32::Register accumulator =
8271 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
8272 vixl32::Register mul_left =
8273 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
8274 vixl32::Register mul_right =
8275 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
8276
8277 if (instr->GetOpKind() == HInstruction::kAdd) {
8278 __ Mla(res, mul_left, mul_right, accumulator);
8279 } else {
8280 __ Mls(res, mul_left, mul_right, accumulator);
8281 }
8282}
8283
Artem Serov551b28f2016-10-18 19:11:30 +01008284void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8285 // Nothing to do, this should be removed during prepare for register allocator.
8286 LOG(FATAL) << "Unreachable";
8287}
8288
8289void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8290 // Nothing to do, this should be removed during prepare for register allocator.
8291 LOG(FATAL) << "Unreachable";
8292}
8293
8294// Simple implementation of packed switch - generate cascaded compare/jumps.
8295void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8296 LocationSummary* locations =
8297 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8298 locations->SetInAt(0, Location::RequiresRegister());
8299 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
8300 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8301 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8302 if (switch_instr->GetStartValue() != 0) {
8303 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8304 }
8305 }
8306}
8307
8308// TODO(VIXL): Investigate and reach the parity with old arm codegen.
8309void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8310 int32_t lower_bound = switch_instr->GetStartValue();
8311 uint32_t num_entries = switch_instr->GetNumEntries();
8312 LocationSummary* locations = switch_instr->GetLocations();
8313 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
8314 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8315
8316 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
8317 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
8318 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008319 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008320 vixl32::Register temp_reg = temps.Acquire();
8321 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8322 // the immediate, because IP is used as the destination register. For the other
8323 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8324 // and they can be encoded in the instruction without making use of IP register.
8325 __ Adds(temp_reg, value_reg, -lower_bound);
8326
8327 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8328 // Jump to successors[0] if value == lower_bound.
8329 __ B(eq, codegen_->GetLabelOf(successors[0]));
8330 int32_t last_index = 0;
8331 for (; num_entries - last_index > 2; last_index += 2) {
8332 __ Adds(temp_reg, temp_reg, -2);
8333 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8334 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
8335 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8336 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
8337 }
8338 if (num_entries - last_index == 2) {
8339 // The last missing case_value.
8340 __ Cmp(temp_reg, 1);
8341 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
8342 }
8343
8344 // And the default for any other value.
8345 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8346 __ B(codegen_->GetLabelOf(default_block));
8347 }
8348 } else {
8349 // Create a table lookup.
8350 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
8351
8352 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
8353
8354 // Remove the bias.
8355 vixl32::Register key_reg;
8356 if (lower_bound != 0) {
8357 key_reg = RegisterFrom(locations->GetTemp(1));
8358 __ Sub(key_reg, value_reg, lower_bound);
8359 } else {
8360 key_reg = value_reg;
8361 }
8362
8363 // Check whether the value is in the table, jump to default block if not.
8364 __ Cmp(key_reg, num_entries - 1);
8365 __ B(hi, codegen_->GetLabelOf(default_block));
8366
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008367 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01008368 vixl32::Register jump_offset = temps.Acquire();
8369
8370 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00008371 {
8372 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
8373 ExactAssemblyScope aas(GetVIXLAssembler(),
8374 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
8375 CodeBufferCheckScope::kMaximumSize);
8376 __ adr(table_base, jump_table->GetTableStartLabel());
8377 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01008378
Scott Wakeling86e9d262017-01-18 15:59:24 +00008379 // Jump to target block by branching to table_base(pc related) + offset.
8380 vixl32::Register target_address = table_base;
8381 __ add(target_address, table_base, jump_offset);
8382 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00008383
Scott Wakeling86e9d262017-01-18 15:59:24 +00008384 jump_table->EmitTable(codegen_);
8385 }
Artem Serov551b28f2016-10-18 19:11:30 +01008386 }
8387}
Artem Serovd4cc5b22016-11-04 11:19:09 +00008388void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8390 locations->SetOut(Location::RequiresRegister());
8391}
8392
8393void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8394 vixl32::Register base_reg = OutputRegister(base);
8395 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8396 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
8397 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
8398}
Artem Serov551b28f2016-10-18 19:11:30 +01008399
Artem Serov02d37832016-10-25 15:25:33 +01008400// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008401void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8402 if (!trg.IsValid()) {
8403 DCHECK_EQ(type, Primitive::kPrimVoid);
8404 return;
8405 }
8406
8407 DCHECK_NE(type, Primitive::kPrimVoid);
8408
Artem Serovd4cc5b22016-11-04 11:19:09 +00008409 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008410 if (return_loc.Equals(trg)) {
8411 return;
8412 }
8413
8414 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8415 // with the last branch.
8416 if (type == Primitive::kPrimLong) {
8417 TODO_VIXL32(FATAL);
8418 } else if (type == Primitive::kPrimDouble) {
8419 TODO_VIXL32(FATAL);
8420 } else {
8421 // Let the parallel move resolver take care of all of this.
8422 HParallelMove parallel_move(GetGraph()->GetArena());
8423 parallel_move.AddMove(return_loc, trg, type, nullptr);
8424 GetMoveResolver()->EmitNativeCode(&parallel_move);
8425 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008426}
Scott Wakelingfe885462016-09-22 10:24:38 +01008427
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008428void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8429 LocationSummary* locations =
8430 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8431 locations->SetInAt(0, Location::RequiresRegister());
8432 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01008433}
8434
xueliang.zhong8d2c4592016-11-23 17:05:25 +00008435void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
8436 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
8437 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8438 instruction->GetIndex(), kArmPointerSize).SizeValue();
8439 GetAssembler()->LoadFromOffset(kLoadWord,
8440 OutputRegister(instruction),
8441 InputRegisterAt(instruction, 0),
8442 method_offset);
8443 } else {
8444 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
8445 instruction->GetIndex(), kArmPointerSize));
8446 GetAssembler()->LoadFromOffset(kLoadWord,
8447 OutputRegister(instruction),
8448 InputRegisterAt(instruction, 0),
8449 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8450 GetAssembler()->LoadFromOffset(kLoadWord,
8451 OutputRegister(instruction),
8452 OutputRegister(instruction),
8453 method_offset);
8454 }
Artem Serov551b28f2016-10-18 19:11:30 +01008455}
8456
Artem Serovc5fcb442016-12-02 19:19:58 +00008457static void PatchJitRootUse(uint8_t* code,
8458 const uint8_t* roots_data,
8459 VIXLUInt32Literal* literal,
8460 uint64_t index_in_table) {
8461 DCHECK(literal->IsBound());
8462 uint32_t literal_offset = literal->GetLocation();
8463 uintptr_t address =
8464 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8465 uint8_t* data = code + literal_offset;
8466 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8467}
8468
8469void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8470 for (const auto& entry : jit_string_patches_) {
8471 const auto& it = jit_string_roots_.find(entry.first);
8472 DCHECK(it != jit_string_roots_.end());
8473 PatchJitRootUse(code, roots_data, entry.second, it->second);
8474 }
8475 for (const auto& entry : jit_class_patches_) {
8476 const auto& it = jit_class_roots_.find(entry.first);
8477 DCHECK(it != jit_class_roots_.end());
8478 PatchJitRootUse(code, roots_data, entry.second, it->second);
8479 }
8480}
8481
Artem Serovd4cc5b22016-11-04 11:19:09 +00008482void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
8483 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
8484 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00008485 ExactAssemblyScope aas(GetVIXLAssembler(),
8486 3 * vixl32::kMaxInstructionSizeInBytes,
8487 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008488 // TODO(VIXL): Think about using mov instead of movw.
8489 __ bind(&labels->movw_label);
8490 __ movw(out, /* placeholder */ 0u);
8491 __ bind(&labels->movt_label);
8492 __ movt(out, /* placeholder */ 0u);
8493 __ bind(&labels->add_pc_label);
8494 __ add(out, out, pc);
8495}
8496
Scott Wakelingfe885462016-09-22 10:24:38 +01008497#undef __
8498#undef QUICK_ENTRY_POINT
8499#undef TODO_VIXL32
8500
8501} // namespace arm
8502} // namespace art