blob: f4d3ec54ee3a3d3c50c4ada074844e3933e245e7 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "common_arm.h"
23#include "compiled_method.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010026#include "intrinsics_arm_vixl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "thread.h"
30#include "utils/arm/assembler_arm_vixl.h"
31#include "utils/arm/managed_register_arm.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35namespace art {
36namespace arm {
37
38namespace vixl32 = vixl::aarch32;
39using namespace vixl32; // NOLINT(build/namespaces)
40
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010041using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010042using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010043using helpers::HighDRegisterFrom;
44using helpers::HighRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010045using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010046using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010047using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000049using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000051using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000052using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::LocationFrom;
54using helpers::LowRegisterFrom;
55using helpers::LowSRegisterFrom;
56using helpers::OutputRegister;
57using helpers::OutputSRegister;
58using helpers::OutputVRegister;
59using helpers::RegisterFrom;
60using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000061using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010062
Artem Serov0fb37192016-12-06 18:13:40 +000063using vixl::ExactAssemblyScope;
64using vixl::CodeBufferCheckScope;
65
Scott Wakelingfe885462016-09-22 10:24:38 +010066using RegisterList = vixl32::RegisterList;
67
68static bool ExpectedPairLayout(Location location) {
69 // We expected this for both core and fpu register pairs.
70 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
71}
Artem Serovd4cc5b22016-11-04 11:19:09 +000072// Use a local definition to prevent copying mistakes.
73static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
74static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010075static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010077
78#ifdef __
79#error "ARM Codegen VIXL macro-assembler macro already defined."
80#endif
81
Scott Wakelingfe885462016-09-22 10:24:38 +010082// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
83#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
84#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
85
86// Marker that code is yet to be, and must, be implemented.
87#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
88
Scott Wakelinga7812ae2016-10-17 10:03:36 +010089// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
90// for each live D registers they treat two corresponding S registers as live ones.
91//
92// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
93// from a list of contiguous S registers a list of contiguous D registers (processing first/last
94// S registers corner cases) and save/restore this new list treating them as D registers.
95// - decreasing code size
96// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
97// restored and then used in regular non SlowPath code as D register.
98//
99// For the following example (v means the S register is live):
100// D names: | D0 | D1 | D2 | D4 | ...
101// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
102// Live? | | v | v | v | v | v | v | | ...
103//
104// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
105// as D registers.
106//
107// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
108// for lists of floating-point registers.
109static size_t SaveContiguousSRegisterList(size_t first,
110 size_t last,
111 CodeGenerator* codegen,
112 size_t stack_offset) {
113 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
114 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
115 DCHECK_LE(first, last);
116 if ((first == last) && (first == 0)) {
117 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
118 return stack_offset + kSRegSizeInBytes;
119 }
120 if (first % 2 == 1) {
121 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
122 stack_offset += kSRegSizeInBytes;
123 }
124
125 bool save_last = false;
126 if (last % 2 == 0) {
127 save_last = true;
128 --last;
129 }
130
131 if (first < last) {
132 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
133 DCHECK_EQ((last - first + 1) % 2, 0u);
134 size_t number_of_d_regs = (last - first + 1) / 2;
135
136 if (number_of_d_regs == 1) {
137 __ Vstr(d_reg, MemOperand(sp, stack_offset));
138 } else if (number_of_d_regs > 1) {
139 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
140 vixl32::Register base = sp;
141 if (stack_offset != 0) {
142 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000143 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100144 }
145 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
146 }
147 stack_offset += number_of_d_regs * kDRegSizeInBytes;
148 }
149
150 if (save_last) {
151 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
152 stack_offset += kSRegSizeInBytes;
153 }
154
155 return stack_offset;
156}
157
158static size_t RestoreContiguousSRegisterList(size_t first,
159 size_t last,
160 CodeGenerator* codegen,
161 size_t stack_offset) {
162 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
163 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
164 DCHECK_LE(first, last);
165 if ((first == last) && (first == 0)) {
166 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
167 return stack_offset + kSRegSizeInBytes;
168 }
169 if (first % 2 == 1) {
170 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
171 stack_offset += kSRegSizeInBytes;
172 }
173
174 bool restore_last = false;
175 if (last % 2 == 0) {
176 restore_last = true;
177 --last;
178 }
179
180 if (first < last) {
181 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
182 DCHECK_EQ((last - first + 1) % 2, 0u);
183 size_t number_of_d_regs = (last - first + 1) / 2;
184 if (number_of_d_regs == 1) {
185 __ Vldr(d_reg, MemOperand(sp, stack_offset));
186 } else if (number_of_d_regs > 1) {
187 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
188 vixl32::Register base = sp;
189 if (stack_offset != 0) {
190 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000191 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100192 }
193 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
194 }
195 stack_offset += number_of_d_regs * kDRegSizeInBytes;
196 }
197
198 if (restore_last) {
199 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
200 stack_offset += kSRegSizeInBytes;
201 }
202
203 return stack_offset;
204}
205
206void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
207 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
208 size_t orig_offset = stack_offset;
209
210 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
211 for (uint32_t i : LowToHighBits(core_spills)) {
212 // If the register holds an object, update the stack mask.
213 if (locations->RegisterContainsObject(i)) {
214 locations->SetStackBit(stack_offset / kVRegSize);
215 }
216 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
217 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
218 saved_core_stack_offsets_[i] = stack_offset;
219 stack_offset += kArmWordSize;
220 }
221
222 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
223 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
224
225 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
226 orig_offset = stack_offset;
227 for (uint32_t i : LowToHighBits(fp_spills)) {
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_fpu_stack_offsets_[i] = stack_offset;
230 stack_offset += kArmWordSize;
231 }
232
233 stack_offset = orig_offset;
234 while (fp_spills != 0u) {
235 uint32_t begin = CTZ(fp_spills);
236 uint32_t tmp = fp_spills + (1u << begin);
237 fp_spills &= tmp; // Clear the contiguous range of 1s.
238 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
239 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
240 }
241 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
242}
243
244void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
245 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
246 size_t orig_offset = stack_offset;
247
248 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
249 for (uint32_t i : LowToHighBits(core_spills)) {
250 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
251 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
252 stack_offset += kArmWordSize;
253 }
254
255 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
256 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
257 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
258
259 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
260 while (fp_spills != 0u) {
261 uint32_t begin = CTZ(fp_spills);
262 uint32_t tmp = fp_spills + (1u << begin);
263 fp_spills &= tmp; // Clear the contiguous range of 1s.
264 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
265 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
266 }
267 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
268}
269
270class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
271 public:
272 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
273
274 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
275 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
276 __ Bind(GetEntryLabel());
277 if (instruction_->CanThrowIntoCatchBlock()) {
278 // Live registers will be restored in the catch block if caught.
279 SaveLiveRegisters(codegen, instruction_->GetLocations());
280 }
281 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
282 instruction_,
283 instruction_->GetDexPc(),
284 this);
285 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
286 }
287
288 bool IsFatal() const OVERRIDE { return true; }
289
290 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
291
292 private:
293 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
294};
295
Scott Wakelingfe885462016-09-22 10:24:38 +0100296class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
297 public:
298 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
299 : SlowPathCodeARMVIXL(instruction) {}
300
301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100302 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100303 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100304 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100305 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
306 }
307
308 bool IsFatal() const OVERRIDE { return true; }
309
310 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
311
312 private:
313 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
314};
315
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100316class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
317 public:
318 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
319 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
323 __ Bind(GetEntryLabel());
324 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
325 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
326 if (successor_ == nullptr) {
327 __ B(GetReturnLabel());
328 } else {
329 __ B(arm_codegen->GetLabelOf(successor_));
330 }
331 }
332
333 vixl32::Label* GetReturnLabel() {
334 DCHECK(successor_ == nullptr);
335 return &return_label_;
336 }
337
338 HBasicBlock* GetSuccessor() const {
339 return successor_;
340 }
341
342 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
343
344 private:
345 // If not null, the block to branch to after the suspend check.
346 HBasicBlock* const successor_;
347
348 // If `successor_` is null, the label to branch to after the suspend check.
349 vixl32::Label return_label_;
350
351 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
352};
353
Scott Wakelingc34dba72016-10-03 10:14:44 +0100354class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
355 public:
356 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
357 : SlowPathCodeARMVIXL(instruction) {}
358
359 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
360 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
361 LocationSummary* locations = instruction_->GetLocations();
362
363 __ Bind(GetEntryLabel());
364 if (instruction_->CanThrowIntoCatchBlock()) {
365 // Live registers will be restored in the catch block if caught.
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
367 }
368 // We're moving two locations to locations that could overlap, so we need a parallel
369 // move resolver.
370 InvokeRuntimeCallingConventionARMVIXL calling_convention;
371 codegen->EmitParallelMoves(
372 locations->InAt(0),
373 LocationFrom(calling_convention.GetRegisterAt(0)),
374 Primitive::kPrimInt,
375 locations->InAt(1),
376 LocationFrom(calling_convention.GetRegisterAt(1)),
377 Primitive::kPrimInt);
378 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
379 ? kQuickThrowStringBounds
380 : kQuickThrowArrayBounds;
381 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
382 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
383 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
384 }
385
386 bool IsFatal() const OVERRIDE { return true; }
387
388 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
389
390 private:
391 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
392};
393
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100394class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
395 public:
396 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000397 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100398 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
399 }
400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 LocationSummary* locations = instruction_->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100403
404 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
405 __ Bind(GetEntryLabel());
406 SaveLiveRegisters(codegen, locations);
407
408 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000409 dex::TypeIndex type_index = cls_->GetTypeIndex();
410 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100411 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
412 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000413 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100414 if (do_clinit_) {
415 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
416 } else {
417 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
418 }
419
420 // Move the class to the desired location.
421 Location out = locations->Out();
422 if (out.IsValid()) {
423 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
424 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
425 }
426 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000427 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
428 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
429 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
430 DCHECK(out.IsValid());
431 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
432 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
433 // so that we can avoid another calculation here.
Vladimir Markoaf35a252017-01-18 12:54:44 +0000434 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
435 vixl32::Register temp = temps.Acquire();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000436 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000437 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Markoaf35a252017-01-18 12:54:44 +0000438 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
439 __ Str(OutputRegister(cls_), MemOperand(temp));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000440 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100441 __ B(GetExitLabel());
442 }
443
444 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
445
446 private:
447 // The class this slow path will load.
448 HLoadClass* const cls_;
449
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100450 // The dex PC of `at_`.
451 const uint32_t dex_pc_;
452
453 // Whether to initialize the class.
454 const bool do_clinit_;
455
456 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
457};
458
Artem Serovd4cc5b22016-11-04 11:19:09 +0000459class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
460 public:
461 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
462 : SlowPathCodeARMVIXL(instruction) {}
463
464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 LocationSummary* locations = instruction_->GetLocations();
466 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
467 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000468 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000469 vixl32::Register out = OutputRegister(load);
470 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
471 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
472
473 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
474 __ Bind(GetEntryLabel());
475 SaveLiveRegisters(codegen, locations);
476
477 InvokeRuntimeCallingConventionARMVIXL calling_convention;
478 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
479 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
480 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
481 vixl32::Register entry_address = temp_is_r0 ? out : temp;
482 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
483 if (call_saves_everything_except_r0 && temp_is_r0) {
484 __ Mov(entry_address, temp);
485 }
486
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000487 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000488 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
489 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
490
491 // Store the resolved String to the .bss entry.
492 if (call_saves_everything_except_r0) {
493 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
494 __ Str(r0, MemOperand(entry_address));
495 } else {
496 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
497 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
498 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
499 arm_codegen->EmitMovwMovtPlaceholder(labels, out);
500 __ Str(r0, MemOperand(entry_address));
501 }
502
503 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
504 RestoreLiveRegisters(codegen, locations);
505
506 __ B(GetExitLabel());
507 }
508
509 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
510
511 private:
512 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
513};
514
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100515class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
516 public:
517 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
518 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
519
520 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
521 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100522 DCHECK(instruction_->IsCheckCast()
523 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
524
525 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
526 __ Bind(GetEntryLabel());
527
528 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100529 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100530 }
531
532 // We're moving two locations to locations that could overlap, so we need a parallel
533 // move resolver.
534 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100535
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800536 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800537 LocationFrom(calling_convention.GetRegisterAt(0)),
538 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800539 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800540 LocationFrom(calling_convention.GetRegisterAt(1)),
541 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100542 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100543 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
544 instruction_,
545 instruction_->GetDexPc(),
546 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800547 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100548 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100549 } else {
550 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800551 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
552 instruction_,
553 instruction_->GetDexPc(),
554 this);
555 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100556 }
557
558 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100559 RestoreLiveRegisters(codegen, locations);
560 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100561 }
562 }
563
564 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
565
566 bool IsFatal() const OVERRIDE { return is_fatal_; }
567
568 private:
569 const bool is_fatal_;
570
571 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
572};
573
Scott Wakelingc34dba72016-10-03 10:14:44 +0100574class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
575 public:
576 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
577 : SlowPathCodeARMVIXL(instruction) {}
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
581 __ Bind(GetEntryLabel());
582 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
583 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
584 }
585
586 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
587
588 private:
589 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
590};
591
592class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
593 public:
594 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
595
596 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
597 LocationSummary* locations = instruction_->GetLocations();
598 __ Bind(GetEntryLabel());
599 SaveLiveRegisters(codegen, locations);
600
601 InvokeRuntimeCallingConventionARMVIXL calling_convention;
602 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
603 parallel_move.AddMove(
604 locations->InAt(0),
605 LocationFrom(calling_convention.GetRegisterAt(0)),
606 Primitive::kPrimNot,
607 nullptr);
608 parallel_move.AddMove(
609 locations->InAt(1),
610 LocationFrom(calling_convention.GetRegisterAt(1)),
611 Primitive::kPrimInt,
612 nullptr);
613 parallel_move.AddMove(
614 locations->InAt(2),
615 LocationFrom(calling_convention.GetRegisterAt(2)),
616 Primitive::kPrimNot,
617 nullptr);
618 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
619
620 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
621 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
622 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
623 RestoreLiveRegisters(codegen, locations);
624 __ B(GetExitLabel());
625 }
626
627 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
628
629 private:
630 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
631};
632
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000633// Slow path marking an object reference `ref` during a read
634// barrier. The field `obj.field` in the object `obj` holding this
635// reference does not get updated by this slow path after marking (see
636// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
637//
638// This means that after the execution of this slow path, `ref` will
639// always be up-to-date, but `obj.field` may not; i.e., after the
640// flip, `ref` will be a to-space reference, but `obj.field` will
641// probably still be a from-space reference (unless it gets updated by
642// another thread, or if another thread installed another object
643// reference (different from `ref`) in `obj.field`).
644class ReadBarrierMarkSlowPathARMVIXL : public SlowPathCodeARMVIXL {
645 public:
646 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
647 Location ref,
648 Location entrypoint = Location::NoLocation())
649 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
650 DCHECK(kEmitCompilerReadBarrier);
651 }
652
653 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
654
655 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
656 LocationSummary* locations = instruction_->GetLocations();
657 vixl32::Register ref_reg = RegisterFrom(ref_);
658 DCHECK(locations->CanCall());
659 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
660 DCHECK(instruction_->IsInstanceFieldGet() ||
661 instruction_->IsStaticFieldGet() ||
662 instruction_->IsArrayGet() ||
663 instruction_->IsArraySet() ||
664 instruction_->IsLoadClass() ||
665 instruction_->IsLoadString() ||
666 instruction_->IsInstanceOf() ||
667 instruction_->IsCheckCast() ||
668 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
669 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
670 << "Unexpected instruction in read barrier marking slow path: "
671 << instruction_->DebugName();
672 // The read barrier instrumentation of object ArrayGet
673 // instructions does not support the HIntermediateAddress
674 // instruction.
675 DCHECK(!(instruction_->IsArrayGet() &&
676 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
677
678 __ Bind(GetEntryLabel());
679 // No need to save live registers; it's taken care of by the
680 // entrypoint. Also, there is no need to update the stack mask,
681 // as this runtime call will not trigger a garbage collection.
682 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
683 DCHECK(!ref_reg.Is(sp));
684 DCHECK(!ref_reg.Is(lr));
685 DCHECK(!ref_reg.Is(pc));
686 // IP is used internally by the ReadBarrierMarkRegX entry point
687 // as a temporary, it cannot be the entry point's input/output.
688 DCHECK(!ref_reg.Is(ip));
689 DCHECK(ref_reg.IsRegister()) << ref_reg;
690 // "Compact" slow path, saving two moves.
691 //
692 // Instead of using the standard runtime calling convention (input
693 // and output in R0):
694 //
695 // R0 <- ref
696 // R0 <- ReadBarrierMark(R0)
697 // ref <- R0
698 //
699 // we just use rX (the register containing `ref`) as input and output
700 // of a dedicated entrypoint:
701 //
702 // rX <- ReadBarrierMarkRegX(rX)
703 //
704 if (entrypoint_.IsValid()) {
705 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
706 __ Blx(RegisterFrom(entrypoint_));
707 } else {
708 int32_t entry_point_offset =
709 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
710 // This runtime call does not require a stack map.
711 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
712 }
713 __ B(GetExitLabel());
714 }
715
716 private:
717 // The location (register) of the marked object reference.
718 const Location ref_;
719
720 // The location of the entrypoint if already loaded.
721 const Location entrypoint_;
722
723 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
724};
725
726// Slow path marking an object reference `ref` during a read barrier,
727// and if needed, atomically updating the field `obj.field` in the
728// object `obj` holding this reference after marking (contrary to
729// ReadBarrierMarkSlowPathARM above, which never tries to update
730// `obj.field`).
731//
732// This means that after the execution of this slow path, both `ref`
733// and `obj.field` will be up-to-date; i.e., after the flip, both will
734// hold the same to-space reference (unless another thread installed
735// another object reference (different from `ref`) in `obj.field`).
736class ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL : public SlowPathCodeARMVIXL {
737 public:
738 ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
739 Location ref,
740 vixl32::Register obj,
741 Location field_offset,
742 vixl32::Register temp1,
743 vixl32::Register temp2)
744 : SlowPathCodeARMVIXL(instruction),
745 ref_(ref),
746 obj_(obj),
747 field_offset_(field_offset),
748 temp1_(temp1),
749 temp2_(temp2) {
750 DCHECK(kEmitCompilerReadBarrier);
751 }
752
753 const char* GetDescription() const OVERRIDE {
754 return "ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL";
755 }
756
757 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
758 LocationSummary* locations = instruction_->GetLocations();
759 vixl32::Register ref_reg = RegisterFrom(ref_);
760 DCHECK(locations->CanCall());
761 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
762 // This slow path is only used by the UnsafeCASObject intrinsic.
763 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
764 << "Unexpected instruction in read barrier marking and field updating slow path: "
765 << instruction_->DebugName();
766 DCHECK(instruction_->GetLocations()->Intrinsified());
767 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
768 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
769
770 __ Bind(GetEntryLabel());
771
772 // Save the old reference.
773 // Note that we cannot use IP to save the old reference, as IP is
774 // used internally by the ReadBarrierMarkRegX entry point, and we
775 // need the old reference after the call to that entry point.
776 DCHECK(!temp1_.Is(ip));
777 __ Mov(temp1_, ref_reg);
778
779 // No need to save live registers; it's taken care of by the
780 // entrypoint. Also, there is no need to update the stack mask,
781 // as this runtime call will not trigger a garbage collection.
782 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
783 DCHECK(!ref_reg.Is(sp));
784 DCHECK(!ref_reg.Is(lr));
785 DCHECK(!ref_reg.Is(pc));
786 // IP is used internally by the ReadBarrierMarkRegX entry point
787 // as a temporary, it cannot be the entry point's input/output.
788 DCHECK(!ref_reg.Is(ip));
789 DCHECK(ref_reg.IsRegister()) << ref_reg;
790 // "Compact" slow path, saving two moves.
791 //
792 // Instead of using the standard runtime calling convention (input
793 // and output in R0):
794 //
795 // R0 <- ref
796 // R0 <- ReadBarrierMark(R0)
797 // ref <- R0
798 //
799 // we just use rX (the register containing `ref`) as input and output
800 // of a dedicated entrypoint:
801 //
802 // rX <- ReadBarrierMarkRegX(rX)
803 //
804 int32_t entry_point_offset =
805 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
806 // This runtime call does not require a stack map.
807 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
808
809 // If the new reference is different from the old reference,
810 // update the field in the holder (`*(obj_ + field_offset_)`).
811 //
812 // Note that this field could also hold a different object, if
813 // another thread had concurrently changed it. In that case, the
814 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
815 // (CAS) operation below would abort the CAS, leaving the field
816 // as-is.
817 vixl32::Label done;
818 __ Cmp(temp1_, ref_reg);
Artem Serov517d9f62016-12-12 15:51:15 +0000819 __ B(eq, &done, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000820
821 // Update the the holder's field atomically. This may fail if
822 // mutator updates before us, but it's OK. This is achieved
823 // using a strong compare-and-set (CAS) operation with relaxed
824 // memory synchronization ordering, where the expected value is
825 // the old reference and the desired value is the new reference.
826
827 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
828 // Convenience aliases.
829 vixl32::Register base = obj_;
830 // The UnsafeCASObject intrinsic uses a register pair as field
831 // offset ("long offset"), of which only the low part contains
832 // data.
833 vixl32::Register offset = LowRegisterFrom(field_offset_);
834 vixl32::Register expected = temp1_;
835 vixl32::Register value = ref_reg;
836 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
837 vixl32::Register tmp = temp2_; // Value in memory.
838
839 __ Add(tmp_ptr, base, offset);
840
841 if (kPoisonHeapReferences) {
842 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
843 if (value.Is(expected)) {
844 // Do not poison `value`, as it is the same register as
845 // `expected`, which has just been poisoned.
846 } else {
847 arm_codegen->GetAssembler()->PoisonHeapReference(value);
848 }
849 }
850
851 // do {
852 // tmp = [r_ptr] - expected;
853 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
854
855 vixl32::Label loop_head, exit_loop;
856 __ Bind(&loop_head);
857
858 __ Ldrex(tmp, MemOperand(tmp_ptr));
859
860 __ Subs(tmp, tmp, expected);
861
862 {
Artem Serov0fb37192016-12-06 18:13:40 +0000863 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
864 2 * kMaxInstructionSizeInBytes,
865 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000866
867 __ it(ne);
868 __ clrex(ne);
869 }
870
Artem Serov517d9f62016-12-12 15:51:15 +0000871 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000872
873 __ Strex(tmp, value, MemOperand(tmp_ptr));
874 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +0000875 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000876
877 __ Bind(&exit_loop);
878
879 if (kPoisonHeapReferences) {
880 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
881 if (value.Is(expected)) {
882 // Do not unpoison `value`, as it is the same register as
883 // `expected`, which has just been unpoisoned.
884 } else {
885 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
886 }
887 }
888
889 __ Bind(&done);
890 __ B(GetExitLabel());
891 }
892
893 private:
894 // The location (register) of the marked object reference.
895 const Location ref_;
896 // The register containing the object holding the marked object reference field.
897 const vixl32::Register obj_;
898 // The location of the offset of the marked reference field within `obj_`.
899 Location field_offset_;
900
901 const vixl32::Register temp1_;
902 const vixl32::Register temp2_;
903
904 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL);
905};
906
907// Slow path generating a read barrier for a heap reference.
908class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
909 public:
910 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
911 Location out,
912 Location ref,
913 Location obj,
914 uint32_t offset,
915 Location index)
916 : SlowPathCodeARMVIXL(instruction),
917 out_(out),
918 ref_(ref),
919 obj_(obj),
920 offset_(offset),
921 index_(index) {
922 DCHECK(kEmitCompilerReadBarrier);
923 // If `obj` is equal to `out` or `ref`, it means the initial object
924 // has been overwritten by (or after) the heap object reference load
925 // to be instrumented, e.g.:
926 //
927 // __ LoadFromOffset(kLoadWord, out, out, offset);
928 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
929 //
930 // In that case, we have lost the information about the original
931 // object, and the emitted read barrier cannot work properly.
932 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
933 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
934 }
935
936 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
937 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
938 LocationSummary* locations = instruction_->GetLocations();
939 vixl32::Register reg_out = RegisterFrom(out_);
940 DCHECK(locations->CanCall());
941 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
942 DCHECK(instruction_->IsInstanceFieldGet() ||
943 instruction_->IsStaticFieldGet() ||
944 instruction_->IsArrayGet() ||
945 instruction_->IsInstanceOf() ||
946 instruction_->IsCheckCast() ||
947 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
948 << "Unexpected instruction in read barrier for heap reference slow path: "
949 << instruction_->DebugName();
950 // The read barrier instrumentation of object ArrayGet
951 // instructions does not support the HIntermediateAddress
952 // instruction.
953 DCHECK(!(instruction_->IsArrayGet() &&
954 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
955
956 __ Bind(GetEntryLabel());
957 SaveLiveRegisters(codegen, locations);
958
959 // We may have to change the index's value, but as `index_` is a
960 // constant member (like other "inputs" of this slow path),
961 // introduce a copy of it, `index`.
962 Location index = index_;
963 if (index_.IsValid()) {
964 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
965 if (instruction_->IsArrayGet()) {
966 // Compute the actual memory offset and store it in `index`.
967 vixl32::Register index_reg = RegisterFrom(index_);
968 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
969 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
970 // We are about to change the value of `index_reg` (see the
971 // calls to art::arm::Thumb2Assembler::Lsl and
972 // art::arm::Thumb2Assembler::AddConstant below), but it has
973 // not been saved by the previous call to
974 // art::SlowPathCode::SaveLiveRegisters, as it is a
975 // callee-save register --
976 // art::SlowPathCode::SaveLiveRegisters does not consider
977 // callee-save registers, as it has been designed with the
978 // assumption that callee-save registers are supposed to be
979 // handled by the called function. So, as a callee-save
980 // register, `index_reg` _would_ eventually be saved onto
981 // the stack, but it would be too late: we would have
982 // changed its value earlier. Therefore, we manually save
983 // it here into another freely available register,
984 // `free_reg`, chosen of course among the caller-save
985 // registers (as a callee-save `free_reg` register would
986 // exhibit the same problem).
987 //
988 // Note we could have requested a temporary register from
989 // the register allocator instead; but we prefer not to, as
990 // this is a slow path, and we know we can find a
991 // caller-save register that is available.
992 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
993 __ Mov(free_reg, index_reg);
994 index_reg = free_reg;
995 index = LocationFrom(index_reg);
996 } else {
997 // The initial register stored in `index_` has already been
998 // saved in the call to art::SlowPathCode::SaveLiveRegisters
999 // (as it is not a callee-save register), so we can freely
1000 // use it.
1001 }
1002 // Shifting the index value contained in `index_reg` by the scale
1003 // factor (2) cannot overflow in practice, as the runtime is
1004 // unable to allocate object arrays with a size larger than
1005 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1006 __ Lsl(index_reg, index_reg, TIMES_4);
1007 static_assert(
1008 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1009 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1010 __ Add(index_reg, index_reg, offset_);
1011 } else {
1012 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1013 // intrinsics, `index_` is not shifted by a scale factor of 2
1014 // (as in the case of ArrayGet), as it is actually an offset
1015 // to an object field within an object.
1016 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1017 DCHECK(instruction_->GetLocations()->Intrinsified());
1018 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1019 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1020 << instruction_->AsInvoke()->GetIntrinsic();
1021 DCHECK_EQ(offset_, 0U);
1022 DCHECK(index_.IsRegisterPair());
1023 // UnsafeGet's offset location is a register pair, the low
1024 // part contains the correct offset.
1025 index = index_.ToLow();
1026 }
1027 }
1028
1029 // We're moving two or three locations to locations that could
1030 // overlap, so we need a parallel move resolver.
1031 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1032 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1033 parallel_move.AddMove(ref_,
1034 LocationFrom(calling_convention.GetRegisterAt(0)),
1035 Primitive::kPrimNot,
1036 nullptr);
1037 parallel_move.AddMove(obj_,
1038 LocationFrom(calling_convention.GetRegisterAt(1)),
1039 Primitive::kPrimNot,
1040 nullptr);
1041 if (index.IsValid()) {
1042 parallel_move.AddMove(index,
1043 LocationFrom(calling_convention.GetRegisterAt(2)),
1044 Primitive::kPrimInt,
1045 nullptr);
1046 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1047 } else {
1048 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1049 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1050 }
1051 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1052 CheckEntrypointTypes<
1053 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1054 arm_codegen->Move32(out_, LocationFrom(r0));
1055
1056 RestoreLiveRegisters(codegen, locations);
1057 __ B(GetExitLabel());
1058 }
1059
1060 const char* GetDescription() const OVERRIDE {
1061 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1062 }
1063
1064 private:
1065 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1066 uint32_t ref = RegisterFrom(ref_).GetCode();
1067 uint32_t obj = RegisterFrom(obj_).GetCode();
1068 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1069 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1070 return vixl32::Register(i);
1071 }
1072 }
1073 // We shall never fail to find a free caller-save register, as
1074 // there are more than two core caller-save registers on ARM
1075 // (meaning it is possible to find one which is different from
1076 // `ref` and `obj`).
1077 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1078 LOG(FATAL) << "Could not find a free caller-save register";
1079 UNREACHABLE();
1080 }
1081
1082 const Location out_;
1083 const Location ref_;
1084 const Location obj_;
1085 const uint32_t offset_;
1086 // An additional location containing an index to an array.
1087 // Only used for HArrayGet and the UnsafeGetObject &
1088 // UnsafeGetObjectVolatile intrinsics.
1089 const Location index_;
1090
1091 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1092};
1093
1094// Slow path generating a read barrier for a GC root.
1095class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1096 public:
1097 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1098 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1099 DCHECK(kEmitCompilerReadBarrier);
1100 }
1101
1102 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1103 LocationSummary* locations = instruction_->GetLocations();
1104 vixl32::Register reg_out = RegisterFrom(out_);
1105 DCHECK(locations->CanCall());
1106 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1107 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1108 << "Unexpected instruction in read barrier for GC root slow path: "
1109 << instruction_->DebugName();
1110
1111 __ Bind(GetEntryLabel());
1112 SaveLiveRegisters(codegen, locations);
1113
1114 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1115 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1116 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1117 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1118 instruction_,
1119 instruction_->GetDexPc(),
1120 this);
1121 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1122 arm_codegen->Move32(out_, LocationFrom(r0));
1123
1124 RestoreLiveRegisters(codegen, locations);
1125 __ B(GetExitLabel());
1126 }
1127
1128 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1129
1130 private:
1131 const Location out_;
1132 const Location root_;
1133
1134 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1135};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001136
Scott Wakelingfe885462016-09-22 10:24:38 +01001137inline vixl32::Condition ARMCondition(IfCondition cond) {
1138 switch (cond) {
1139 case kCondEQ: return eq;
1140 case kCondNE: return ne;
1141 case kCondLT: return lt;
1142 case kCondLE: return le;
1143 case kCondGT: return gt;
1144 case kCondGE: return ge;
1145 case kCondB: return lo;
1146 case kCondBE: return ls;
1147 case kCondA: return hi;
1148 case kCondAE: return hs;
1149 }
1150 LOG(FATAL) << "Unreachable";
1151 UNREACHABLE();
1152}
1153
1154// Maps signed condition to unsigned condition.
1155inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1156 switch (cond) {
1157 case kCondEQ: return eq;
1158 case kCondNE: return ne;
1159 // Signed to unsigned.
1160 case kCondLT: return lo;
1161 case kCondLE: return ls;
1162 case kCondGT: return hi;
1163 case kCondGE: return hs;
1164 // Unsigned remain unchanged.
1165 case kCondB: return lo;
1166 case kCondBE: return ls;
1167 case kCondA: return hi;
1168 case kCondAE: return hs;
1169 }
1170 LOG(FATAL) << "Unreachable";
1171 UNREACHABLE();
1172}
1173
1174inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1175 // The ARM condition codes can express all the necessary branches, see the
1176 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1177 // There is no dex instruction or HIR that would need the missing conditions
1178 // "equal or unordered" or "not equal".
1179 switch (cond) {
1180 case kCondEQ: return eq;
1181 case kCondNE: return ne /* unordered */;
1182 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1183 case kCondLE: return gt_bias ? ls : le /* unordered */;
1184 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1185 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1186 default:
1187 LOG(FATAL) << "UNREACHABLE";
1188 UNREACHABLE();
1189 }
1190}
1191
Scott Wakelingfe885462016-09-22 10:24:38 +01001192void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1193 stream << vixl32::Register(reg);
1194}
1195
1196void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1197 stream << vixl32::SRegister(reg);
1198}
1199
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001200static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001201 uint32_t mask = 0;
1202 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1203 i <= regs.GetLastSRegister().GetCode();
1204 ++i) {
1205 mask |= (1 << i);
1206 }
1207 return mask;
1208}
1209
Artem Serovd4cc5b22016-11-04 11:19:09 +00001210// Saves the register in the stack. Returns the size taken on stack.
1211size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1212 uint32_t reg_id ATTRIBUTE_UNUSED) {
1213 TODO_VIXL32(FATAL);
1214 return 0;
1215}
1216
1217// Restores the register from the stack. Returns the size taken on stack.
1218size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1219 uint32_t reg_id ATTRIBUTE_UNUSED) {
1220 TODO_VIXL32(FATAL);
1221 return 0;
1222}
1223
1224size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1225 uint32_t reg_id ATTRIBUTE_UNUSED) {
1226 TODO_VIXL32(FATAL);
1227 return 0;
1228}
1229
1230size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1231 uint32_t reg_id ATTRIBUTE_UNUSED) {
1232 TODO_VIXL32(FATAL);
1233 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001234}
1235
Scott Wakelingfe885462016-09-22 10:24:38 +01001236#undef __
1237
1238CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
1239 const ArmInstructionSetFeatures& isa_features,
1240 const CompilerOptions& compiler_options,
1241 OptimizingCompilerStats* stats)
1242 : CodeGenerator(graph,
1243 kNumberOfCoreRegisters,
1244 kNumberOfSRegisters,
1245 kNumberOfRegisterPairs,
1246 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001247 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001248 compiler_options,
1249 stats),
1250 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01001251 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001252 location_builder_(graph, this),
1253 instruction_visitor_(graph, this),
1254 move_resolver_(graph->GetArena(), this),
1255 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001256 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00001257 uint32_literals_(std::less<uint32_t>(),
1258 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001259 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001260 boot_image_string_patches_(StringReferenceValueComparator(),
1261 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00001262 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001263 boot_image_type_patches_(TypeReferenceValueComparator(),
1264 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1265 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001266 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001267 boot_image_address_patches_(std::less<uint32_t>(),
1268 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1269 jit_string_patches_(StringReferenceValueComparator(),
1270 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1271 jit_class_patches_(TypeReferenceValueComparator(),
1272 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001273 // Always save the LR register to mimic Quick.
1274 AddAllocatedRegister(Location::RegisterLocation(LR));
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001275 // Give d14 and d15 as scratch registers to VIXL.
1276 // They are removed from the register allocator in `SetupBlockedRegisters()`.
1277 // TODO(VIXL): We need two scratch D registers for `EmitSwap` when swapping two double stack
1278 // slots. If that is sufficiently rare, and we have pressure on FP registers, we could instead
1279 // spill in `EmitSwap`. But if we actually are guaranteed to have 32 D registers, we could give
1280 // d30 and d31 to VIXL to avoid removing registers from the allocator. If that is the case, we may
1281 // also want to investigate giving those 14 other D registers to the allocator.
1282 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d14);
1283 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d15);
Scott Wakelingfe885462016-09-22 10:24:38 +01001284}
1285
Artem Serov551b28f2016-10-18 19:11:30 +01001286void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1287 uint32_t num_entries = switch_instr_->GetNumEntries();
1288 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1289
1290 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001291 // underlying code buffer and we have generated a jump table of the right size, using
1292 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001293 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1294 num_entries * sizeof(int32_t),
1295 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001296 // TODO(VIXL): Check that using lower case bind is fine here.
1297 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001298 for (uint32_t i = 0; i < num_entries; i++) {
1299 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1300 }
1301}
1302
1303void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1304 uint32_t num_entries = switch_instr_->GetNumEntries();
1305 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1306
Artem Serov551b28f2016-10-18 19:11:30 +01001307 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1308 for (uint32_t i = 0; i < num_entries; i++) {
1309 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1310 DCHECK(target_label->IsBound());
1311 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1312 // When doing BX to address we need to have lower bit set to 1 in T32.
1313 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1314 jump_offset++;
1315 }
1316 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1317 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001318
Scott Wakelingb77051e2016-11-21 19:46:00 +00001319 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001320 }
1321}
1322
Artem Serov09a940d2016-11-11 16:15:11 +00001323void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001324 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001325 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001326 }
1327}
1328
Andreas Gampeca620d72016-11-08 08:09:33 -08001329#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001330
1331void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001332 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01001333 GetAssembler()->FinalizeCode();
1334 CodeGenerator::Finalize(allocator);
1335}
1336
1337void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01001338 // Stack register, LR and PC are always reserved.
1339 blocked_core_registers_[SP] = true;
1340 blocked_core_registers_[LR] = true;
1341 blocked_core_registers_[PC] = true;
1342
1343 // Reserve thread register.
1344 blocked_core_registers_[TR] = true;
1345
1346 // Reserve temp register.
1347 blocked_core_registers_[IP] = true;
1348
Alexandre Rames9c19bd62016-10-24 11:50:32 +01001349 // Registers s28-s31 (d14-d15) are left to VIXL for scratch registers.
1350 // (They are given to the `MacroAssembler` in `CodeGeneratorARMVIXL::CodeGeneratorARMVIXL`.)
1351 blocked_fpu_registers_[28] = true;
1352 blocked_fpu_registers_[29] = true;
1353 blocked_fpu_registers_[30] = true;
1354 blocked_fpu_registers_[31] = true;
1355
Scott Wakelingfe885462016-09-22 10:24:38 +01001356 if (GetGraph()->IsDebuggable()) {
1357 // Stubs do not save callee-save floating point registers. If the graph
1358 // is debuggable, we need to deal with these registers differently. For
1359 // now, just block them.
1360 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
1361 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
1362 ++i) {
1363 blocked_fpu_registers_[i] = true;
1364 }
1365 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001366}
1367
Scott Wakelingfe885462016-09-22 10:24:38 +01001368InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
1369 CodeGeneratorARMVIXL* codegen)
1370 : InstructionCodeGenerator(graph, codegen),
1371 assembler_(codegen->GetAssembler()),
1372 codegen_(codegen) {}
1373
1374void CodeGeneratorARMVIXL::ComputeSpillMask() {
1375 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1376 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1377 // There is no easy instruction to restore just the PC on thumb2. We spill and
1378 // restore another arbitrary register.
1379 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
1380 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1381 // We use vpush and vpop for saving and restoring floating point registers, which take
1382 // a SRegister and the number of registers to save/restore after that SRegister. We
1383 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1384 // but in the range.
1385 if (fpu_spill_mask_ != 0) {
1386 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1387 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1388 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1389 fpu_spill_mask_ |= (1 << i);
1390 }
1391 }
1392}
1393
1394void CodeGeneratorARMVIXL::GenerateFrameEntry() {
1395 bool skip_overflow_check =
1396 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
1397 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
1398 __ Bind(&frame_entry_label_);
1399
1400 if (HasEmptyFrame()) {
1401 return;
1402 }
1403
Scott Wakelingfe885462016-09-22 10:24:38 +01001404 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001405 UseScratchRegisterScope temps(GetVIXLAssembler());
1406 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00001407 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01001408 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00001409 ExactAssemblyScope aas(GetVIXLAssembler(),
1410 vixl32::kMaxInstructionSizeInBytes,
1411 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001412 __ ldr(temp, MemOperand(temp));
1413 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001414 }
1415
1416 __ Push(RegisterList(core_spill_mask_));
1417 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1418 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
1419 0,
1420 core_spill_mask_,
1421 kArmWordSize);
1422 if (fpu_spill_mask_ != 0) {
1423 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1424
1425 // Check that list is contiguous.
1426 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1427
1428 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1429 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001430 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01001431 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00001432
1433 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1434 UseScratchRegisterScope temps(GetVIXLAssembler());
1435 vixl32::Register temp = temps.Acquire();
1436 // Initialize should_deoptimize flag to 0.
1437 __ Mov(temp, 0);
1438 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
1439 }
1440
Scott Wakelingfe885462016-09-22 10:24:38 +01001441 int adjust = GetFrameSize() - FrameEntrySpillSize();
1442 __ Sub(sp, sp, adjust);
1443 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00001444
1445 // Save the current method if we need it. Note that we do not
1446 // do this in HCurrentMethod, as the instruction might have been removed
1447 // in the SSA graph.
1448 if (RequiresCurrentMethod()) {
1449 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
1450 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001451}
1452
1453void CodeGeneratorARMVIXL::GenerateFrameExit() {
1454 if (HasEmptyFrame()) {
1455 __ Bx(lr);
1456 return;
1457 }
1458 GetAssembler()->cfi().RememberState();
1459 int adjust = GetFrameSize() - FrameEntrySpillSize();
1460 __ Add(sp, sp, adjust);
1461 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
1462 if (fpu_spill_mask_ != 0) {
1463 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
1464
1465 // Check that list is contiguous.
1466 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
1467
1468 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
1469 GetAssembler()->cfi().AdjustCFAOffset(
1470 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001471 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01001472 }
1473 // Pop LR into PC to return.
1474 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
1475 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
1476 __ Pop(RegisterList(pop_mask));
1477 GetAssembler()->cfi().RestoreState();
1478 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
1479}
1480
1481void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
1482 __ Bind(GetLabelOf(block));
1483}
1484
Artem Serovd4cc5b22016-11-04 11:19:09 +00001485Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
1486 switch (type) {
1487 case Primitive::kPrimBoolean:
1488 case Primitive::kPrimByte:
1489 case Primitive::kPrimChar:
1490 case Primitive::kPrimShort:
1491 case Primitive::kPrimInt:
1492 case Primitive::kPrimNot: {
1493 uint32_t index = gp_index_++;
1494 uint32_t stack_index = stack_index_++;
1495 if (index < calling_convention.GetNumberOfRegisters()) {
1496 return LocationFrom(calling_convention.GetRegisterAt(index));
1497 } else {
1498 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1499 }
1500 }
1501
1502 case Primitive::kPrimLong: {
1503 uint32_t index = gp_index_;
1504 uint32_t stack_index = stack_index_;
1505 gp_index_ += 2;
1506 stack_index_ += 2;
1507 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1508 if (calling_convention.GetRegisterAt(index).Is(r1)) {
1509 // Skip R1, and use R2_R3 instead.
1510 gp_index_++;
1511 index++;
1512 }
1513 }
1514 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1515 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
1516 calling_convention.GetRegisterAt(index + 1).GetCode());
1517
1518 return LocationFrom(calling_convention.GetRegisterAt(index),
1519 calling_convention.GetRegisterAt(index + 1));
1520 } else {
1521 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1522 }
1523 }
1524
1525 case Primitive::kPrimFloat: {
1526 uint32_t stack_index = stack_index_++;
1527 if (float_index_ % 2 == 0) {
1528 float_index_ = std::max(double_index_, float_index_);
1529 }
1530 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1531 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1532 } else {
1533 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1534 }
1535 }
1536
1537 case Primitive::kPrimDouble: {
1538 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1539 uint32_t stack_index = stack_index_;
1540 stack_index_ += 2;
1541 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1542 uint32_t index = double_index_;
1543 double_index_ += 2;
1544 Location result = LocationFrom(
1545 calling_convention.GetFpuRegisterAt(index),
1546 calling_convention.GetFpuRegisterAt(index + 1));
1547 DCHECK(ExpectedPairLayout(result));
1548 return result;
1549 } else {
1550 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1551 }
1552 }
1553
1554 case Primitive::kPrimVoid:
1555 LOG(FATAL) << "Unexpected parameter type " << type;
1556 break;
1557 }
1558 return Location::NoLocation();
1559}
1560
1561Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
1562 switch (type) {
1563 case Primitive::kPrimBoolean:
1564 case Primitive::kPrimByte:
1565 case Primitive::kPrimChar:
1566 case Primitive::kPrimShort:
1567 case Primitive::kPrimInt:
1568 case Primitive::kPrimNot: {
1569 return LocationFrom(r0);
1570 }
1571
1572 case Primitive::kPrimFloat: {
1573 return LocationFrom(s0);
1574 }
1575
1576 case Primitive::kPrimLong: {
1577 return LocationFrom(r0, r1);
1578 }
1579
1580 case Primitive::kPrimDouble: {
1581 return LocationFrom(s0, s1);
1582 }
1583
1584 case Primitive::kPrimVoid:
1585 return Location::NoLocation();
1586 }
1587
1588 UNREACHABLE();
1589}
1590
1591Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
1592 return LocationFrom(kMethodRegister);
1593}
1594
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001595void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
1596 if (source.Equals(destination)) {
1597 return;
1598 }
1599 if (destination.IsRegister()) {
1600 if (source.IsRegister()) {
1601 __ Mov(RegisterFrom(destination), RegisterFrom(source));
1602 } else if (source.IsFpuRegister()) {
1603 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
1604 } else {
1605 GetAssembler()->LoadFromOffset(kLoadWord,
1606 RegisterFrom(destination),
1607 sp,
1608 source.GetStackIndex());
1609 }
1610 } else if (destination.IsFpuRegister()) {
1611 if (source.IsRegister()) {
1612 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
1613 } else if (source.IsFpuRegister()) {
1614 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
1615 } else {
1616 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
1617 }
1618 } else {
1619 DCHECK(destination.IsStackSlot()) << destination;
1620 if (source.IsRegister()) {
1621 GetAssembler()->StoreToOffset(kStoreWord,
1622 RegisterFrom(source),
1623 sp,
1624 destination.GetStackIndex());
1625 } else if (source.IsFpuRegister()) {
1626 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
1627 } else {
1628 DCHECK(source.IsStackSlot()) << source;
1629 UseScratchRegisterScope temps(GetVIXLAssembler());
1630 vixl32::Register temp = temps.Acquire();
1631 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
1632 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
1633 }
1634 }
1635}
1636
Artem Serovcfbe9132016-10-14 15:58:56 +01001637void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
1638 DCHECK(location.IsRegister());
1639 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01001640}
1641
1642void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001643 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
1644 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
1645 HParallelMove move(GetGraph()->GetArena());
1646 move.AddMove(src, dst, dst_type, nullptr);
1647 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01001648}
1649
Artem Serovcfbe9132016-10-14 15:58:56 +01001650void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
1651 if (location.IsRegister()) {
1652 locations->AddTemp(location);
1653 } else if (location.IsRegisterPair()) {
1654 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
1655 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
1656 } else {
1657 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1658 }
Scott Wakelingfe885462016-09-22 10:24:38 +01001659}
1660
1661void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
1662 HInstruction* instruction,
1663 uint32_t dex_pc,
1664 SlowPathCode* slow_path) {
1665 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001666 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
1667 // Ensure the pc position is recorded immediately after the `blx` instruction.
1668 // 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 +00001669 ExactAssemblyScope aas(GetVIXLAssembler(),
1670 vixl32::k16BitT32InstructionSizeInBytes,
1671 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001672 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01001673 if (EntrypointRequiresStackMap(entrypoint)) {
1674 RecordPcInfo(instruction, dex_pc, slow_path);
1675 }
1676}
1677
1678void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1679 HInstruction* instruction,
1680 SlowPathCode* slow_path) {
1681 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00001682 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01001683 __ Blx(lr);
1684}
1685
Scott Wakelingfe885462016-09-22 10:24:38 +01001686void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1687 DCHECK(!successor->IsExitBlock());
1688 HBasicBlock* block = got->GetBlock();
1689 HInstruction* previous = got->GetPrevious();
1690 HLoopInformation* info = block->GetLoopInformation();
1691
1692 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1693 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1694 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1695 return;
1696 }
1697 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1698 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1699 }
1700 if (!codegen_->GoesToNextBlock(block, successor)) {
1701 __ B(codegen_->GetLabelOf(successor));
1702 }
1703}
1704
1705void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
1706 got->SetLocations(nullptr);
1707}
1708
1709void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
1710 HandleGoto(got, got->GetSuccessor());
1711}
1712
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001713void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1714 try_boundary->SetLocations(nullptr);
1715}
1716
1717void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
1718 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1719 if (!successor->IsExitBlock()) {
1720 HandleGoto(try_boundary, successor);
1721 }
1722}
1723
Scott Wakelingfe885462016-09-22 10:24:38 +01001724void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
1725 exit->SetLocations(nullptr);
1726}
1727
1728void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1729}
1730
1731void InstructionCodeGeneratorARMVIXL::GenerateVcmp(HInstruction* instruction) {
1732 Primitive::Type type = instruction->InputAt(0)->GetType();
1733 Location lhs_loc = instruction->GetLocations()->InAt(0);
1734 Location rhs_loc = instruction->GetLocations()->InAt(1);
1735 if (rhs_loc.IsConstant()) {
1736 // 0.0 is the only immediate that can be encoded directly in
1737 // a VCMP instruction.
1738 //
1739 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1740 // specify that in a floating-point comparison, positive zero
1741 // and negative zero are considered equal, so we can use the
1742 // literal 0.0 for both cases here.
1743 //
1744 // Note however that some methods (Float.equal, Float.compare,
1745 // Float.compareTo, Double.equal, Double.compare,
1746 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1747 // StrictMath.min) consider 0.0 to be (strictly) greater than
1748 // -0.0. So if we ever translate calls to these methods into a
1749 // HCompare instruction, we must handle the -0.0 case with
1750 // care here.
1751 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1752 if (type == Primitive::kPrimFloat) {
1753 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1754 } else {
1755 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001756 __ Vcmp(F64, DRegisterFrom(lhs_loc), 0.0);
Scott Wakelingfe885462016-09-22 10:24:38 +01001757 }
1758 } else {
1759 if (type == Primitive::kPrimFloat) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001760 __ Vcmp(InputSRegisterAt(instruction, 0), InputSRegisterAt(instruction, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01001761 } else {
1762 DCHECK_EQ(type, Primitive::kPrimDouble);
Scott Wakelingc34dba72016-10-03 10:14:44 +01001763 __ Vcmp(DRegisterFrom(lhs_loc), DRegisterFrom(rhs_loc));
Scott Wakelingfe885462016-09-22 10:24:38 +01001764 }
1765 }
1766}
1767
1768void InstructionCodeGeneratorARMVIXL::GenerateFPJumps(HCondition* cond,
1769 vixl32::Label* true_label,
1770 vixl32::Label* false_label ATTRIBUTE_UNUSED) {
1771 // To branch on the result of the FP compare we transfer FPSCR to APSR (encoded as PC in VMRS).
1772 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1773 __ B(ARMFPCondition(cond->GetCondition(), cond->IsGtBias()), true_label);
1774}
1775
1776void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
1777 vixl32::Label* true_label,
1778 vixl32::Label* false_label) {
1779 LocationSummary* locations = cond->GetLocations();
1780 Location left = locations->InAt(0);
1781 Location right = locations->InAt(1);
1782 IfCondition if_cond = cond->GetCondition();
1783
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001784 vixl32::Register left_high = HighRegisterFrom(left);
1785 vixl32::Register left_low = LowRegisterFrom(left);
Scott Wakelingfe885462016-09-22 10:24:38 +01001786 IfCondition true_high_cond = if_cond;
1787 IfCondition false_high_cond = cond->GetOppositeCondition();
1788 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
1789
1790 // Set the conditions for the test, remembering that == needs to be
1791 // decided using the low words.
1792 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1793 switch (if_cond) {
1794 case kCondEQ:
1795 case kCondNE:
1796 // Nothing to do.
1797 break;
1798 case kCondLT:
1799 false_high_cond = kCondGT;
1800 break;
1801 case kCondLE:
1802 true_high_cond = kCondLT;
1803 break;
1804 case kCondGT:
1805 false_high_cond = kCondLT;
1806 break;
1807 case kCondGE:
1808 true_high_cond = kCondGT;
1809 break;
1810 case kCondB:
1811 false_high_cond = kCondA;
1812 break;
1813 case kCondBE:
1814 true_high_cond = kCondB;
1815 break;
1816 case kCondA:
1817 false_high_cond = kCondB;
1818 break;
1819 case kCondAE:
1820 true_high_cond = kCondA;
1821 break;
1822 }
1823 if (right.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001824 int64_t value = Int64ConstantFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001825 int32_t val_low = Low32Bits(value);
1826 int32_t val_high = High32Bits(value);
1827
1828 __ Cmp(left_high, val_high);
1829 if (if_cond == kCondNE) {
1830 __ B(ARMCondition(true_high_cond), true_label);
1831 } else if (if_cond == kCondEQ) {
1832 __ B(ARMCondition(false_high_cond), false_label);
1833 } else {
1834 __ B(ARMCondition(true_high_cond), true_label);
1835 __ B(ARMCondition(false_high_cond), false_label);
1836 }
1837 // Must be equal high, so compare the lows.
1838 __ Cmp(left_low, val_low);
1839 } else {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001840 vixl32::Register right_high = HighRegisterFrom(right);
1841 vixl32::Register right_low = LowRegisterFrom(right);
Scott Wakelingfe885462016-09-22 10:24:38 +01001842
1843 __ Cmp(left_high, right_high);
1844 if (if_cond == kCondNE) {
1845 __ B(ARMCondition(true_high_cond), true_label);
1846 } else if (if_cond == kCondEQ) {
1847 __ B(ARMCondition(false_high_cond), false_label);
1848 } else {
1849 __ B(ARMCondition(true_high_cond), true_label);
1850 __ B(ARMCondition(false_high_cond), false_label);
1851 }
1852 // Must be equal high, so compare the lows.
1853 __ Cmp(left_low, right_low);
1854 }
1855 // The last comparison might be unsigned.
1856 // TODO: optimize cases where this is always true/false
1857 __ B(final_condition, true_label);
1858}
1859
1860void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
1861 vixl32::Label* true_target_in,
1862 vixl32::Label* false_target_in) {
1863 // Generated branching requires both targets to be explicit. If either of the
1864 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
1865 vixl32::Label fallthrough;
1866 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
1867 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
1868
1869 Primitive::Type type = condition->InputAt(0)->GetType();
1870 switch (type) {
1871 case Primitive::kPrimLong:
1872 GenerateLongComparesAndJumps(condition, true_target, false_target);
1873 break;
1874 case Primitive::kPrimFloat:
1875 case Primitive::kPrimDouble:
1876 GenerateVcmp(condition);
1877 GenerateFPJumps(condition, true_target, false_target);
1878 break;
1879 default:
1880 LOG(FATAL) << "Unexpected compare type " << type;
1881 }
1882
1883 if (false_target != &fallthrough) {
1884 __ B(false_target);
1885 }
1886
1887 if (true_target_in == nullptr || false_target_in == nullptr) {
1888 __ Bind(&fallthrough);
1889 }
1890}
1891
1892void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
1893 size_t condition_input_index,
1894 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00001895 vixl32::Label* false_target,
1896 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001897 HInstruction* cond = instruction->InputAt(condition_input_index);
1898
1899 if (true_target == nullptr && false_target == nullptr) {
1900 // Nothing to do. The code always falls through.
1901 return;
1902 } else if (cond->IsIntConstant()) {
1903 // Constant condition, statically compared against "true" (integer value 1).
1904 if (cond->AsIntConstant()->IsTrue()) {
1905 if (true_target != nullptr) {
1906 __ B(true_target);
1907 }
1908 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00001909 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01001910 if (false_target != nullptr) {
1911 __ B(false_target);
1912 }
1913 }
1914 return;
1915 }
1916
1917 // The following code generates these patterns:
1918 // (1) true_target == nullptr && false_target != nullptr
1919 // - opposite condition true => branch to false_target
1920 // (2) true_target != nullptr && false_target == nullptr
1921 // - condition true => branch to true_target
1922 // (3) true_target != nullptr && false_target != nullptr
1923 // - condition true => branch to true_target
1924 // - branch to false_target
1925 if (IsBooleanValueOrMaterializedCondition(cond)) {
1926 // Condition has been materialized, compare the output to 0.
1927 if (kIsDebugBuild) {
1928 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1929 DCHECK(cond_val.IsRegister());
1930 }
1931 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001932 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
1933 false_target,
1934 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001935 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00001936 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
1937 true_target,
1938 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01001939 }
1940 } else {
1941 // Condition has not been materialized. Use its inputs as the comparison and
1942 // its condition as the branch condition.
1943 HCondition* condition = cond->AsCondition();
1944
1945 // If this is a long or FP comparison that has been folded into
1946 // the HCondition, generate the comparison directly.
1947 Primitive::Type type = condition->InputAt(0)->GetType();
1948 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1949 GenerateCompareTestAndBranch(condition, true_target, false_target);
1950 return;
1951 }
1952
1953 LocationSummary* locations = cond->GetLocations();
1954 DCHECK(locations->InAt(0).IsRegister());
1955 vixl32::Register left = InputRegisterAt(cond, 0);
1956 Location right = locations->InAt(1);
1957 if (right.IsRegister()) {
1958 __ Cmp(left, InputRegisterAt(cond, 1));
1959 } else {
1960 DCHECK(right.IsConstant());
1961 __ Cmp(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1962 }
1963 if (true_target == nullptr) {
1964 __ B(ARMCondition(condition->GetOppositeCondition()), false_target);
1965 } else {
1966 __ B(ARMCondition(condition->GetCondition()), true_target);
1967 }
1968 }
1969
1970 // If neither branch falls through (case 3), the conditional branch to `true_target`
1971 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1972 if (true_target != nullptr && false_target != nullptr) {
1973 __ B(false_target);
1974 }
1975}
1976
1977void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
1978 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1979 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1980 locations->SetInAt(0, Location::RequiresRegister());
1981 }
1982}
1983
1984void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
1985 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1986 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001987 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1988 nullptr : codegen_->GetLabelOf(true_successor);
1989 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1990 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01001991 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1992}
1993
Scott Wakelingc34dba72016-10-03 10:14:44 +01001994void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
1995 LocationSummary* locations = new (GetGraph()->GetArena())
1996 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1997 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1998 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1999 locations->SetInAt(0, Location::RequiresRegister());
2000 }
2001}
2002
2003void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2004 SlowPathCodeARMVIXL* slow_path =
2005 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2006 GenerateTestAndBranch(deoptimize,
2007 /* condition_input_index */ 0,
2008 slow_path->GetEntryLabel(),
2009 /* false_target */ nullptr);
2010}
2011
Artem Serovd4cc5b22016-11-04 11:19:09 +00002012void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2013 LocationSummary* locations = new (GetGraph()->GetArena())
2014 LocationSummary(flag, LocationSummary::kNoCall);
2015 locations->SetOut(Location::RequiresRegister());
2016}
2017
2018void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2019 GetAssembler()->LoadFromOffset(kLoadWord,
2020 OutputRegister(flag),
2021 sp,
2022 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2023}
2024
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002025void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2027 if (Primitive::IsFloatingPointType(select->GetType())) {
2028 locations->SetInAt(0, Location::RequiresFpuRegister());
2029 locations->SetInAt(1, Location::RequiresFpuRegister());
2030 } else {
2031 locations->SetInAt(0, Location::RequiresRegister());
2032 locations->SetInAt(1, Location::RequiresRegister());
2033 }
2034 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2035 locations->SetInAt(2, Location::RequiresRegister());
2036 }
2037 locations->SetOut(Location::SameAsFirstInput());
2038}
2039
2040void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
2041 LocationSummary* locations = select->GetLocations();
2042 vixl32::Label false_target;
2043 GenerateTestAndBranch(select,
2044 /* condition_input_index */ 2,
2045 /* true_target */ nullptr,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002046 &false_target,
2047 /* far_target */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002048 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2049 __ Bind(&false_target);
2050}
2051
Artem Serov551b28f2016-10-18 19:11:30 +01002052void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2053 new (GetGraph()->GetArena()) LocationSummary(info);
2054}
2055
2056void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2057 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2058}
2059
Scott Wakelingfe885462016-09-22 10:24:38 +01002060void CodeGeneratorARMVIXL::GenerateNop() {
2061 __ Nop();
2062}
2063
2064void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2067 // Handle the long/FP comparisons made in instruction simplification.
2068 switch (cond->InputAt(0)->GetType()) {
2069 case Primitive::kPrimLong:
2070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2072 if (!cond->IsEmittedAtUseSite()) {
2073 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2074 }
2075 break;
2076
Scott Wakelingfe885462016-09-22 10:24:38 +01002077 case Primitive::kPrimFloat:
2078 case Primitive::kPrimDouble:
2079 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002080 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002081 if (!cond->IsEmittedAtUseSite()) {
2082 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2083 }
2084 break;
2085
2086 default:
2087 locations->SetInAt(0, Location::RequiresRegister());
2088 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2089 if (!cond->IsEmittedAtUseSite()) {
2090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2091 }
2092 }
2093}
2094
2095void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2096 if (cond->IsEmittedAtUseSite()) {
2097 return;
2098 }
2099
Artem Serov657022c2016-11-23 14:19:38 +00002100 Location right = cond->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002101 vixl32::Register out = OutputRegister(cond);
2102 vixl32::Label true_label, false_label;
2103
2104 switch (cond->InputAt(0)->GetType()) {
2105 default: {
2106 // Integer case.
Artem Serov657022c2016-11-23 14:19:38 +00002107 if (right.IsRegister()) {
2108 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
2109 } else {
2110 DCHECK(right.IsConstant());
2111 __ Cmp(InputRegisterAt(cond, 0),
2112 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2113 }
Artem Serov0fb37192016-12-06 18:13:40 +00002114 ExactAssemblyScope aas(GetVIXLAssembler(),
2115 3 * vixl32::kMaxInstructionSizeInBytes,
2116 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002117 __ ite(ARMCondition(cond->GetCondition()));
2118 __ mov(ARMCondition(cond->GetCondition()), OutputRegister(cond), 1);
2119 __ mov(ARMCondition(cond->GetOppositeCondition()), OutputRegister(cond), 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002120 return;
2121 }
2122 case Primitive::kPrimLong:
2123 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2124 break;
2125 case Primitive::kPrimFloat:
2126 case Primitive::kPrimDouble:
2127 GenerateVcmp(cond);
2128 GenerateFPJumps(cond, &true_label, &false_label);
2129 break;
2130 }
2131
2132 // Convert the jumps into the result.
2133 vixl32::Label done_label;
2134
2135 // False case: result = 0.
2136 __ Bind(&false_label);
2137 __ Mov(out, 0);
2138 __ B(&done_label);
2139
2140 // True case: result = 1.
2141 __ Bind(&true_label);
2142 __ Mov(out, 1);
2143 __ Bind(&done_label);
2144}
2145
2146void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2147 HandleCondition(comp);
2148}
2149
2150void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2151 HandleCondition(comp);
2152}
2153
2154void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2155 HandleCondition(comp);
2156}
2157
2158void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2159 HandleCondition(comp);
2160}
2161
2162void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2163 HandleCondition(comp);
2164}
2165
2166void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2167 HandleCondition(comp);
2168}
2169
2170void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2171 HandleCondition(comp);
2172}
2173
2174void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2175 HandleCondition(comp);
2176}
2177
2178void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2179 HandleCondition(comp);
2180}
2181
2182void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2183 HandleCondition(comp);
2184}
2185
2186void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2187 HandleCondition(comp);
2188}
2189
2190void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
2191 HandleCondition(comp);
2192}
2193
2194void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
2195 HandleCondition(comp);
2196}
2197
2198void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
2199 HandleCondition(comp);
2200}
2201
2202void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2203 HandleCondition(comp);
2204}
2205
2206void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
2207 HandleCondition(comp);
2208}
2209
2210void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
2211 HandleCondition(comp);
2212}
2213
2214void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
2215 HandleCondition(comp);
2216}
2217
2218void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2219 HandleCondition(comp);
2220}
2221
2222void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
2223 HandleCondition(comp);
2224}
2225
2226void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
2227 LocationSummary* locations =
2228 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2229 locations->SetOut(Location::ConstantLocation(constant));
2230}
2231
2232void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2233 // Will be generated at use site.
2234}
2235
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002236void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
2237 LocationSummary* locations =
2238 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2239 locations->SetOut(Location::ConstantLocation(constant));
2240}
2241
2242void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2243 // Will be generated at use site.
2244}
2245
Scott Wakelingfe885462016-09-22 10:24:38 +01002246void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
2247 LocationSummary* locations =
2248 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2249 locations->SetOut(Location::ConstantLocation(constant));
2250}
2251
2252void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2253 // Will be generated at use site.
2254}
2255
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002256void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
2257 LocationSummary* locations =
2258 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2259 locations->SetOut(Location::ConstantLocation(constant));
2260}
2261
Scott Wakelingc34dba72016-10-03 10:14:44 +01002262void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
2263 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002264 // Will be generated at use site.
2265}
2266
2267void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2270 locations->SetOut(Location::ConstantLocation(constant));
2271}
2272
Scott Wakelingc34dba72016-10-03 10:14:44 +01002273void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
2274 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01002275 // Will be generated at use site.
2276}
2277
Scott Wakelingfe885462016-09-22 10:24:38 +01002278void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2279 memory_barrier->SetLocations(nullptr);
2280}
2281
2282void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2283 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2284}
2285
2286void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
2287 ret->SetLocations(nullptr);
2288}
2289
2290void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
2291 codegen_->GenerateFrameExit();
2292}
2293
2294void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
2295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
2297 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
2298}
2299
2300void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
2301 codegen_->GenerateFrameExit();
2302}
2303
Artem Serovcfbe9132016-10-14 15:58:56 +01002304void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2305 // The trampoline uses the same calling convention as dex calling conventions,
2306 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2307 // the method_idx.
2308 HandleInvoke(invoke);
2309}
2310
2311void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2312 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2313}
2314
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002315void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2316 // Explicit clinit checks triggered by static invokes must have been pruned by
2317 // art::PrepareForRegisterAllocation.
2318 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2319
Anton Kirilov5ec62182016-10-13 20:16:02 +01002320 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2321 if (intrinsic.TryDispatch(invoke)) {
2322 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2323 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2324 }
2325 return;
2326 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002327
2328 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01002329
Artem Serovd4cc5b22016-11-04 11:19:09 +00002330 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2331 if (invoke->HasPcRelativeDexCache()) {
2332 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2333 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002334}
2335
Anton Kirilov5ec62182016-10-13 20:16:02 +01002336static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
2337 if (invoke->GetLocations()->Intrinsified()) {
2338 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
2339 intrinsic.Dispatch(invoke);
2340 return true;
2341 }
2342 return false;
2343}
2344
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002345void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2346 // Explicit clinit checks triggered by static invokes must have been pruned by
2347 // art::PrepareForRegisterAllocation.
2348 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2349
Anton Kirilov5ec62182016-10-13 20:16:02 +01002350 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2351 return;
2352 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002353
2354 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002355 codegen_->GenerateStaticOrDirectCall(
2356 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002357 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2358}
2359
2360void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002361 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002362 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2363}
2364
2365void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002366 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
2367 if (intrinsic.TryDispatch(invoke)) {
2368 return;
2369 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002370
2371 HandleInvoke(invoke);
2372}
2373
2374void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002375 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2376 return;
2377 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002378
2379 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002381 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002382}
2383
Artem Serovcfbe9132016-10-14 15:58:56 +01002384void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2385 HandleInvoke(invoke);
2386 // Add the hidden argument.
2387 invoke->GetLocations()->AddTemp(LocationFrom(r12));
2388}
2389
2390void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
2391 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2392 LocationSummary* locations = invoke->GetLocations();
2393 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
2394 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
2395 Location receiver = locations->InAt(0);
2396 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2397
2398 DCHECK(!receiver.IsStackSlot());
2399
Alexandre Rames374ddf32016-11-04 10:40:49 +00002400 // Ensure the pc position is recorded immediately after the `ldr` instruction.
2401 {
Artem Serov0fb37192016-12-06 18:13:40 +00002402 ExactAssemblyScope aas(GetVIXLAssembler(),
2403 vixl32::kMaxInstructionSizeInBytes,
2404 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002405 // /* HeapReference<Class> */ temp = receiver->klass_
2406 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
2407 codegen_->MaybeRecordImplicitNullCheck(invoke);
2408 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002409 // Instead of simply (possibly) unpoisoning `temp` here, we should
2410 // emit a read barrier for the previous class reference load.
2411 // However this is not required in practice, as this is an
2412 // intermediate/temporary reference and because the current
2413 // concurrent copying collector keeps the from-space memory
2414 // intact/accessible until the end of the marking phase (the
2415 // concurrent copying collector may not in the future).
2416 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2417 GetAssembler()->LoadFromOffset(kLoadWord,
2418 temp,
2419 temp,
2420 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2421 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2422 invoke->GetImtIndex(), kArmPointerSize));
2423 // temp = temp->GetImtEntryAt(method_offset);
2424 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
2425 uint32_t entry_point =
2426 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
2427 // LR = temp->GetEntryPoint();
2428 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
2429
2430 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
2431 // instruction from clobbering it as they might use r12 as a scratch register.
2432 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00002433
2434 {
2435 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
2436 // so it checks if the application is using them (by passing them to the macro assembler
2437 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
2438 // what is available, and is the opposite of the standard usage: Instead of requesting a
2439 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
2440 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
2441 // (to materialize the constant), since the destination register becomes available for such use
2442 // internally for the duration of the macro instruction.
2443 UseScratchRegisterScope temps(GetVIXLAssembler());
2444 temps.Exclude(hidden_reg);
2445 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
2446 }
Artem Serovcfbe9132016-10-14 15:58:56 +01002447 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00002448 // Ensure the pc position is recorded immediately after the `blx` instruction.
2449 // 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 +00002450 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00002451 vixl32::k16BitT32InstructionSizeInBytes,
2452 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01002453 // LR();
2454 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01002455 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00002456 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01002457 }
2458}
2459
Orion Hodsonac141392017-01-13 11:53:47 +00002460void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2461 HandleInvoke(invoke);
2462}
2463
2464void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2465 codegen_->GenerateInvokePolymorphicCall(invoke);
2466}
2467
Artem Serov02109dd2016-09-23 17:17:54 +01002468void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
2469 LocationSummary* locations =
2470 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2471 switch (neg->GetResultType()) {
2472 case Primitive::kPrimInt: {
2473 locations->SetInAt(0, Location::RequiresRegister());
2474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2475 break;
2476 }
2477 case Primitive::kPrimLong: {
2478 locations->SetInAt(0, Location::RequiresRegister());
2479 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2480 break;
2481 }
2482
2483 case Primitive::kPrimFloat:
2484 case Primitive::kPrimDouble:
2485 locations->SetInAt(0, Location::RequiresFpuRegister());
2486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2487 break;
2488
2489 default:
2490 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2491 }
2492}
2493
2494void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
2495 LocationSummary* locations = neg->GetLocations();
2496 Location out = locations->Out();
2497 Location in = locations->InAt(0);
2498 switch (neg->GetResultType()) {
2499 case Primitive::kPrimInt:
2500 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
2501 break;
2502
2503 case Primitive::kPrimLong:
2504 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2505 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
2506 // We cannot emit an RSC (Reverse Subtract with Carry)
2507 // instruction here, as it does not exist in the Thumb-2
2508 // instruction set. We use the following approach
2509 // using SBC and SUB instead.
2510 //
2511 // out.hi = -C
2512 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
2513 // out.hi = out.hi - in.hi
2514 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
2515 break;
2516
2517 case Primitive::kPrimFloat:
2518 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00002519 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2524 }
2525}
2526
Scott Wakelingfe885462016-09-22 10:24:38 +01002527void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2528 Primitive::Type result_type = conversion->GetResultType();
2529 Primitive::Type input_type = conversion->GetInputType();
2530 DCHECK_NE(result_type, input_type);
2531
2532 // The float-to-long, double-to-long and long-to-float type conversions
2533 // rely on a call to the runtime.
2534 LocationSummary::CallKind call_kind =
2535 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2536 && result_type == Primitive::kPrimLong)
2537 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2538 ? LocationSummary::kCallOnMainOnly
2539 : LocationSummary::kNoCall;
2540 LocationSummary* locations =
2541 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2542
2543 // The Java language does not allow treating boolean as an integral type but
2544 // our bit representation makes it safe.
2545
2546 switch (result_type) {
2547 case Primitive::kPrimByte:
2548 switch (input_type) {
2549 case Primitive::kPrimLong:
2550 // Type conversion from long to byte is a result of code transformations.
2551 case Primitive::kPrimBoolean:
2552 // Boolean input is a result of code transformations.
2553 case Primitive::kPrimShort:
2554 case Primitive::kPrimInt:
2555 case Primitive::kPrimChar:
2556 // Processing a Dex `int-to-byte' instruction.
2557 locations->SetInAt(0, Location::RequiresRegister());
2558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2559 break;
2560
2561 default:
2562 LOG(FATAL) << "Unexpected type conversion from " << input_type
2563 << " to " << result_type;
2564 }
2565 break;
2566
2567 case Primitive::kPrimShort:
2568 switch (input_type) {
2569 case Primitive::kPrimLong:
2570 // Type conversion from long to short is a result of code transformations.
2571 case Primitive::kPrimBoolean:
2572 // Boolean input is a result of code transformations.
2573 case Primitive::kPrimByte:
2574 case Primitive::kPrimInt:
2575 case Primitive::kPrimChar:
2576 // Processing a Dex `int-to-short' instruction.
2577 locations->SetInAt(0, Location::RequiresRegister());
2578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 }
2585 break;
2586
2587 case Primitive::kPrimInt:
2588 switch (input_type) {
2589 case Primitive::kPrimLong:
2590 // Processing a Dex `long-to-int' instruction.
2591 locations->SetInAt(0, Location::Any());
2592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2593 break;
2594
2595 case Primitive::kPrimFloat:
2596 // Processing a Dex `float-to-int' instruction.
2597 locations->SetInAt(0, Location::RequiresFpuRegister());
2598 locations->SetOut(Location::RequiresRegister());
2599 locations->AddTemp(Location::RequiresFpuRegister());
2600 break;
2601
2602 case Primitive::kPrimDouble:
2603 // Processing a Dex `double-to-int' instruction.
2604 locations->SetInAt(0, Location::RequiresFpuRegister());
2605 locations->SetOut(Location::RequiresRegister());
2606 locations->AddTemp(Location::RequiresFpuRegister());
2607 break;
2608
2609 default:
2610 LOG(FATAL) << "Unexpected type conversion from " << input_type
2611 << " to " << result_type;
2612 }
2613 break;
2614
2615 case Primitive::kPrimLong:
2616 switch (input_type) {
2617 case Primitive::kPrimBoolean:
2618 // Boolean input is a result of code transformations.
2619 case Primitive::kPrimByte:
2620 case Primitive::kPrimShort:
2621 case Primitive::kPrimInt:
2622 case Primitive::kPrimChar:
2623 // Processing a Dex `int-to-long' instruction.
2624 locations->SetInAt(0, Location::RequiresRegister());
2625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2626 break;
2627
2628 case Primitive::kPrimFloat: {
2629 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002630 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2631 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2632 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002633 break;
2634 }
2635
2636 case Primitive::kPrimDouble: {
2637 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002638 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2639 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
2640 calling_convention.GetFpuRegisterAt(1)));
2641 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002642 break;
2643 }
2644
2645 default:
2646 LOG(FATAL) << "Unexpected type conversion from " << input_type
2647 << " to " << result_type;
2648 }
2649 break;
2650
2651 case Primitive::kPrimChar:
2652 switch (input_type) {
2653 case Primitive::kPrimLong:
2654 // Type conversion from long to char is a result of code transformations.
2655 case Primitive::kPrimBoolean:
2656 // Boolean input is a result of code transformations.
2657 case Primitive::kPrimByte:
2658 case Primitive::kPrimShort:
2659 case Primitive::kPrimInt:
2660 // Processing a Dex `int-to-char' instruction.
2661 locations->SetInAt(0, Location::RequiresRegister());
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663 break;
2664
2665 default:
2666 LOG(FATAL) << "Unexpected type conversion from " << input_type
2667 << " to " << result_type;
2668 }
2669 break;
2670
2671 case Primitive::kPrimFloat:
2672 switch (input_type) {
2673 case Primitive::kPrimBoolean:
2674 // Boolean input is a result of code transformations.
2675 case Primitive::kPrimByte:
2676 case Primitive::kPrimShort:
2677 case Primitive::kPrimInt:
2678 case Primitive::kPrimChar:
2679 // Processing a Dex `int-to-float' instruction.
2680 locations->SetInAt(0, Location::RequiresRegister());
2681 locations->SetOut(Location::RequiresFpuRegister());
2682 break;
2683
2684 case Primitive::kPrimLong: {
2685 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002686 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2687 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
2688 calling_convention.GetRegisterAt(1)));
2689 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002690 break;
2691 }
2692
2693 case Primitive::kPrimDouble:
2694 // Processing a Dex `double-to-float' instruction.
2695 locations->SetInAt(0, Location::RequiresFpuRegister());
2696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
2702 };
2703 break;
2704
2705 case Primitive::kPrimDouble:
2706 switch (input_type) {
2707 case Primitive::kPrimBoolean:
2708 // Boolean input is a result of code transformations.
2709 case Primitive::kPrimByte:
2710 case Primitive::kPrimShort:
2711 case Primitive::kPrimInt:
2712 case Primitive::kPrimChar:
2713 // Processing a Dex `int-to-double' instruction.
2714 locations->SetInAt(0, Location::RequiresRegister());
2715 locations->SetOut(Location::RequiresFpuRegister());
2716 break;
2717
2718 case Primitive::kPrimLong:
2719 // Processing a Dex `long-to-double' instruction.
2720 locations->SetInAt(0, Location::RequiresRegister());
2721 locations->SetOut(Location::RequiresFpuRegister());
2722 locations->AddTemp(Location::RequiresFpuRegister());
2723 locations->AddTemp(Location::RequiresFpuRegister());
2724 break;
2725
2726 case Primitive::kPrimFloat:
2727 // Processing a Dex `float-to-double' instruction.
2728 locations->SetInAt(0, Location::RequiresFpuRegister());
2729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2730 break;
2731
2732 default:
2733 LOG(FATAL) << "Unexpected type conversion from " << input_type
2734 << " to " << result_type;
2735 };
2736 break;
2737
2738 default:
2739 LOG(FATAL) << "Unexpected type conversion from " << input_type
2740 << " to " << result_type;
2741 }
2742}
2743
2744void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
2745 LocationSummary* locations = conversion->GetLocations();
2746 Location out = locations->Out();
2747 Location in = locations->InAt(0);
2748 Primitive::Type result_type = conversion->GetResultType();
2749 Primitive::Type input_type = conversion->GetInputType();
2750 DCHECK_NE(result_type, input_type);
2751 switch (result_type) {
2752 case Primitive::kPrimByte:
2753 switch (input_type) {
2754 case Primitive::kPrimLong:
2755 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002756 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01002757 break;
2758 case Primitive::kPrimBoolean:
2759 // Boolean input is a result of code transformations.
2760 case Primitive::kPrimShort:
2761 case Primitive::kPrimInt:
2762 case Primitive::kPrimChar:
2763 // Processing a Dex `int-to-byte' instruction.
2764 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
2765 break;
2766
2767 default:
2768 LOG(FATAL) << "Unexpected type conversion from " << input_type
2769 << " to " << result_type;
2770 }
2771 break;
2772
2773 case Primitive::kPrimShort:
2774 switch (input_type) {
2775 case Primitive::kPrimLong:
2776 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002777 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002778 break;
2779 case Primitive::kPrimBoolean:
2780 // Boolean input is a result of code transformations.
2781 case Primitive::kPrimByte:
2782 case Primitive::kPrimInt:
2783 case Primitive::kPrimChar:
2784 // Processing a Dex `int-to-short' instruction.
2785 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2786 break;
2787
2788 default:
2789 LOG(FATAL) << "Unexpected type conversion from " << input_type
2790 << " to " << result_type;
2791 }
2792 break;
2793
2794 case Primitive::kPrimInt:
2795 switch (input_type) {
2796 case Primitive::kPrimLong:
2797 // Processing a Dex `long-to-int' instruction.
2798 DCHECK(out.IsRegister());
2799 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002800 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002801 } else if (in.IsDoubleStackSlot()) {
2802 GetAssembler()->LoadFromOffset(kLoadWord,
2803 OutputRegister(conversion),
2804 sp,
2805 in.GetStackIndex());
2806 } else {
2807 DCHECK(in.IsConstant());
2808 DCHECK(in.GetConstant()->IsLongConstant());
Anton Kirilov644032c2016-12-06 17:51:43 +00002809 int32_t value = Int32ConstantFrom(in);
2810 __ Mov(OutputRegister(conversion), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002811 }
2812 break;
2813
2814 case Primitive::kPrimFloat: {
2815 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002816 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002817 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002818 __ Vmov(OutputRegister(conversion), temp);
2819 break;
2820 }
2821
2822 case Primitive::kPrimDouble: {
2823 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002824 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002825 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002826 __ Vmov(OutputRegister(conversion), temp_s);
2827 break;
2828 }
2829
2830 default:
2831 LOG(FATAL) << "Unexpected type conversion from " << input_type
2832 << " to " << result_type;
2833 }
2834 break;
2835
2836 case Primitive::kPrimLong:
2837 switch (input_type) {
2838 case Primitive::kPrimBoolean:
2839 // Boolean input is a result of code transformations.
2840 case Primitive::kPrimByte:
2841 case Primitive::kPrimShort:
2842 case Primitive::kPrimInt:
2843 case Primitive::kPrimChar:
2844 // Processing a Dex `int-to-long' instruction.
2845 DCHECK(out.IsRegisterPair());
2846 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002848 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002849 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01002850 break;
2851
2852 case Primitive::kPrimFloat:
2853 // Processing a Dex `float-to-long' instruction.
2854 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
2855 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2856 break;
2857
2858 case Primitive::kPrimDouble:
2859 // Processing a Dex `double-to-long' instruction.
2860 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
2861 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2862 break;
2863
2864 default:
2865 LOG(FATAL) << "Unexpected type conversion from " << input_type
2866 << " to " << result_type;
2867 }
2868 break;
2869
2870 case Primitive::kPrimChar:
2871 switch (input_type) {
2872 case Primitive::kPrimLong:
2873 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002874 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01002875 break;
2876 case Primitive::kPrimBoolean:
2877 // Boolean input is a result of code transformations.
2878 case Primitive::kPrimByte:
2879 case Primitive::kPrimShort:
2880 case Primitive::kPrimInt:
2881 // Processing a Dex `int-to-char' instruction.
2882 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
2883 break;
2884
2885 default:
2886 LOG(FATAL) << "Unexpected type conversion from " << input_type
2887 << " to " << result_type;
2888 }
2889 break;
2890
2891 case Primitive::kPrimFloat:
2892 switch (input_type) {
2893 case Primitive::kPrimBoolean:
2894 // Boolean input is a result of code transformations.
2895 case Primitive::kPrimByte:
2896 case Primitive::kPrimShort:
2897 case Primitive::kPrimInt:
2898 case Primitive::kPrimChar: {
2899 // Processing a Dex `int-to-float' instruction.
2900 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002901 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01002902 break;
2903 }
2904
2905 case Primitive::kPrimLong:
2906 // Processing a Dex `long-to-float' instruction.
2907 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
2908 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2909 break;
2910
2911 case Primitive::kPrimDouble:
2912 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002913 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 };
2920 break;
2921
2922 case Primitive::kPrimDouble:
2923 switch (input_type) {
2924 case Primitive::kPrimBoolean:
2925 // Boolean input is a result of code transformations.
2926 case Primitive::kPrimByte:
2927 case Primitive::kPrimShort:
2928 case Primitive::kPrimInt:
2929 case Primitive::kPrimChar: {
2930 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002931 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002932 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01002933 break;
2934 }
2935
2936 case Primitive::kPrimLong: {
2937 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002938 vixl32::Register low = LowRegisterFrom(in);
2939 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002940 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002941 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002942 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01002943 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002944 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01002945
2946 // temp_d = int-to-double(high)
2947 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01002948 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01002949 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002950 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01002951 // out_d = unsigned-to-double(low)
2952 __ Vmov(out_s, low);
2953 __ Vcvt(F64, U32, out_d, out_s);
2954 // out_d += temp_d * constant_d
2955 __ Vmla(F64, out_d, temp_d, constant_d);
2956 break;
2957 }
2958
2959 case Primitive::kPrimFloat:
2960 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01002961 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01002962 break;
2963
2964 default:
2965 LOG(FATAL) << "Unexpected type conversion from " << input_type
2966 << " to " << result_type;
2967 };
2968 break;
2969
2970 default:
2971 LOG(FATAL) << "Unexpected type conversion from " << input_type
2972 << " to " << result_type;
2973 }
2974}
2975
2976void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
2977 LocationSummary* locations =
2978 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2979 switch (add->GetResultType()) {
2980 case Primitive::kPrimInt: {
2981 locations->SetInAt(0, Location::RequiresRegister());
2982 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2984 break;
2985 }
2986
Scott Wakelingfe885462016-09-22 10:24:38 +01002987 case Primitive::kPrimLong: {
2988 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00002989 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01002990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2991 break;
2992 }
2993
2994 case Primitive::kPrimFloat:
2995 case Primitive::kPrimDouble: {
2996 locations->SetInAt(0, Location::RequiresFpuRegister());
2997 locations->SetInAt(1, Location::RequiresFpuRegister());
2998 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2999 break;
3000 }
3001
3002 default:
3003 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3004 }
3005}
3006
3007void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3008 LocationSummary* locations = add->GetLocations();
3009 Location out = locations->Out();
3010 Location first = locations->InAt(0);
3011 Location second = locations->InAt(1);
3012
3013 switch (add->GetResultType()) {
3014 case Primitive::kPrimInt: {
3015 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3016 }
3017 break;
3018
Scott Wakelingfe885462016-09-22 10:24:38 +01003019 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003020 if (second.IsConstant()) {
3021 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3022 GenerateAddLongConst(out, first, value);
3023 } else {
3024 DCHECK(second.IsRegisterPair());
3025 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3026 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3027 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003028 break;
3029 }
3030
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003031 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003032 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003033 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003034 break;
3035
3036 default:
3037 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3038 }
3039}
3040
3041void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3044 switch (sub->GetResultType()) {
3045 case Primitive::kPrimInt: {
3046 locations->SetInAt(0, Location::RequiresRegister());
3047 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3049 break;
3050 }
3051
Scott Wakelingfe885462016-09-22 10:24:38 +01003052 case Primitive::kPrimLong: {
3053 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003054 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3056 break;
3057 }
3058 case Primitive::kPrimFloat:
3059 case Primitive::kPrimDouble: {
3060 locations->SetInAt(0, Location::RequiresFpuRegister());
3061 locations->SetInAt(1, Location::RequiresFpuRegister());
3062 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3063 break;
3064 }
3065 default:
3066 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3067 }
3068}
3069
3070void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3071 LocationSummary* locations = sub->GetLocations();
3072 Location out = locations->Out();
3073 Location first = locations->InAt(0);
3074 Location second = locations->InAt(1);
3075 switch (sub->GetResultType()) {
3076 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003077 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003078 break;
3079 }
3080
Scott Wakelingfe885462016-09-22 10:24:38 +01003081 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003082 if (second.IsConstant()) {
3083 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3084 GenerateAddLongConst(out, first, -value);
3085 } else {
3086 DCHECK(second.IsRegisterPair());
3087 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3088 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3089 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003090 break;
3091 }
3092
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble:
3095 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003096 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003097
3098 default:
3099 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3100 }
3101}
3102
3103void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3104 LocationSummary* locations =
3105 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3106 switch (mul->GetResultType()) {
3107 case Primitive::kPrimInt:
3108 case Primitive::kPrimLong: {
3109 locations->SetInAt(0, Location::RequiresRegister());
3110 locations->SetInAt(1, Location::RequiresRegister());
3111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3112 break;
3113 }
3114
3115 case Primitive::kPrimFloat:
3116 case Primitive::kPrimDouble: {
3117 locations->SetInAt(0, Location::RequiresFpuRegister());
3118 locations->SetInAt(1, Location::RequiresFpuRegister());
3119 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3120 break;
3121 }
3122
3123 default:
3124 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3125 }
3126}
3127
3128void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3129 LocationSummary* locations = mul->GetLocations();
3130 Location out = locations->Out();
3131 Location first = locations->InAt(0);
3132 Location second = locations->InAt(1);
3133 switch (mul->GetResultType()) {
3134 case Primitive::kPrimInt: {
3135 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3136 break;
3137 }
3138 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003139 vixl32::Register out_hi = HighRegisterFrom(out);
3140 vixl32::Register out_lo = LowRegisterFrom(out);
3141 vixl32::Register in1_hi = HighRegisterFrom(first);
3142 vixl32::Register in1_lo = LowRegisterFrom(first);
3143 vixl32::Register in2_hi = HighRegisterFrom(second);
3144 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003145
3146 // Extra checks to protect caused by the existence of R1_R2.
3147 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3148 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003149 DCHECK(!out_hi.Is(in1_lo));
3150 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003151
3152 // input: in1 - 64 bits, in2 - 64 bits
3153 // output: out
3154 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3155 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3156 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3157
3158 UseScratchRegisterScope temps(GetVIXLAssembler());
3159 vixl32::Register temp = temps.Acquire();
3160 // temp <- in1.lo * in2.hi
3161 __ Mul(temp, in1_lo, in2_hi);
3162 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3163 __ Mla(out_hi, in1_hi, in2_lo, temp);
3164 // out.lo <- (in1.lo * in2.lo)[31:0];
3165 __ Umull(out_lo, temp, in1_lo, in2_lo);
3166 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003167 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003168 break;
3169 }
3170
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003171 case Primitive::kPrimFloat:
3172 case Primitive::kPrimDouble:
3173 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003174 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003175
3176 default:
3177 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3178 }
3179}
3180
Scott Wakelingfe885462016-09-22 10:24:38 +01003181void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3182 DCHECK(instruction->IsDiv() || instruction->IsRem());
3183 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3184
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003185 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003186 DCHECK(second.IsConstant());
3187
3188 vixl32::Register out = OutputRegister(instruction);
3189 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003190 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003191 DCHECK(imm == 1 || imm == -1);
3192
3193 if (instruction->IsRem()) {
3194 __ Mov(out, 0);
3195 } else {
3196 if (imm == 1) {
3197 __ Mov(out, dividend);
3198 } else {
3199 __ Rsb(out, dividend, 0);
3200 }
3201 }
3202}
3203
3204void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3205 DCHECK(instruction->IsDiv() || instruction->IsRem());
3206 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3207
3208 LocationSummary* locations = instruction->GetLocations();
3209 Location second = locations->InAt(1);
3210 DCHECK(second.IsConstant());
3211
3212 vixl32::Register out = OutputRegister(instruction);
3213 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003214 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003215 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003216 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3217 int ctz_imm = CTZ(abs_imm);
3218
3219 if (ctz_imm == 1) {
3220 __ Lsr(temp, dividend, 32 - ctz_imm);
3221 } else {
3222 __ Asr(temp, dividend, 31);
3223 __ Lsr(temp, temp, 32 - ctz_imm);
3224 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003225 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003226
3227 if (instruction->IsDiv()) {
3228 __ Asr(out, out, ctz_imm);
3229 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003230 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003231 }
3232 } else {
3233 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003234 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003235 }
3236}
3237
3238void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3239 DCHECK(instruction->IsDiv() || instruction->IsRem());
3240 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3241
3242 LocationSummary* locations = instruction->GetLocations();
3243 Location second = locations->InAt(1);
3244 DCHECK(second.IsConstant());
3245
3246 vixl32::Register out = OutputRegister(instruction);
3247 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003248 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
3249 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003250 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003251
3252 int64_t magic;
3253 int shift;
3254 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3255
Anton Kirilovdda43962016-11-21 19:55:20 +00003256 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
3257 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01003258 __ Smull(temp2, temp1, dividend, temp1);
3259
3260 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003261 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003262 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003263 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003264 }
3265
3266 if (shift != 0) {
3267 __ Asr(temp1, temp1, shift);
3268 }
3269
3270 if (instruction->IsDiv()) {
3271 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3272 } else {
3273 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
3274 // TODO: Strength reduction for mls.
3275 __ Mov(temp2, imm);
3276 __ Mls(out, temp1, temp2, dividend);
3277 }
3278}
3279
3280void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
3281 HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3284
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003285 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003286 DCHECK(second.IsConstant());
3287
Anton Kirilov644032c2016-12-06 17:51:43 +00003288 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003289 if (imm == 0) {
3290 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3291 } else if (imm == 1 || imm == -1) {
3292 DivRemOneOrMinusOne(instruction);
3293 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3294 DivRemByPowerOfTwo(instruction);
3295 } else {
3296 DCHECK(imm <= -2 || imm >= 2);
3297 GenerateDivRemWithAnyConstant(instruction);
3298 }
3299}
3300
3301void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
3302 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3303 if (div->GetResultType() == Primitive::kPrimLong) {
3304 // pLdiv runtime call.
3305 call_kind = LocationSummary::kCallOnMainOnly;
3306 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3307 // sdiv will be replaced by other instruction sequence.
3308 } else if (div->GetResultType() == Primitive::kPrimInt &&
3309 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3310 // pIdivmod runtime call.
3311 call_kind = LocationSummary::kCallOnMainOnly;
3312 }
3313
3314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3315
3316 switch (div->GetResultType()) {
3317 case Primitive::kPrimInt: {
3318 if (div->InputAt(1)->IsConstant()) {
3319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
3321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003322 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003323 if (value == 1 || value == 0 || value == -1) {
3324 // No temp register required.
3325 } else {
3326 locations->AddTemp(Location::RequiresRegister());
3327 if (!IsPowerOfTwo(AbsOrMin(value))) {
3328 locations->AddTemp(Location::RequiresRegister());
3329 }
3330 }
3331 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RequiresRegister());
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003336 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3337 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3338 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003339 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003340 // we only need the former.
3341 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003342 }
3343 break;
3344 }
3345 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003346 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3347 locations->SetInAt(0, LocationFrom(
3348 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3349 locations->SetInAt(1, LocationFrom(
3350 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3351 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003352 break;
3353 }
3354 case Primitive::kPrimFloat:
3355 case Primitive::kPrimDouble: {
3356 locations->SetInAt(0, Location::RequiresFpuRegister());
3357 locations->SetInAt(1, Location::RequiresFpuRegister());
3358 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3359 break;
3360 }
3361
3362 default:
3363 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3364 }
3365}
3366
3367void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003368 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003369 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003370
3371 switch (div->GetResultType()) {
3372 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003373 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003374 GenerateDivRemConstantIntegral(div);
3375 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3376 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
3377 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01003378 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3379 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
3380 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
3381 DCHECK(r0.Is(OutputRegister(div)));
3382
3383 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
3384 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003385 }
3386 break;
3387 }
3388
3389 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003390 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3391 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
3392 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
3393 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
3394 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
3395 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
3396 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
3397
3398 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
3399 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01003400 break;
3401 }
3402
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003403 case Primitive::kPrimFloat:
3404 case Primitive::kPrimDouble:
3405 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003406 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003407
3408 default:
3409 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3410 }
3411}
3412
Artem Serov551b28f2016-10-18 19:11:30 +01003413void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
3414 Primitive::Type type = rem->GetResultType();
3415
3416 // Most remainders are implemented in the runtime.
3417 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3418 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3419 // sdiv will be replaced by other instruction sequence.
3420 call_kind = LocationSummary::kNoCall;
3421 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3422 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3423 // Have hardware divide instruction for int, do it with three instructions.
3424 call_kind = LocationSummary::kNoCall;
3425 }
3426
3427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3428
3429 switch (type) {
3430 case Primitive::kPrimInt: {
3431 if (rem->InputAt(1)->IsConstant()) {
3432 locations->SetInAt(0, Location::RequiresRegister());
3433 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00003435 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01003436 if (value == 1 || value == 0 || value == -1) {
3437 // No temp register required.
3438 } else {
3439 locations->AddTemp(Location::RequiresRegister());
3440 if (!IsPowerOfTwo(AbsOrMin(value))) {
3441 locations->AddTemp(Location::RequiresRegister());
3442 }
3443 }
3444 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3445 locations->SetInAt(0, Location::RequiresRegister());
3446 locations->SetInAt(1, Location::RequiresRegister());
3447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3448 locations->AddTemp(Location::RequiresRegister());
3449 } else {
3450 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3451 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3452 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003453 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01003454 // we only need the latter.
3455 locations->SetOut(LocationFrom(r1));
3456 }
3457 break;
3458 }
3459 case Primitive::kPrimLong: {
3460 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3461 locations->SetInAt(0, LocationFrom(
3462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3463 locations->SetInAt(1, LocationFrom(
3464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3465 // The runtime helper puts the output in R2,R3.
3466 locations->SetOut(LocationFrom(r2, r3));
3467 break;
3468 }
3469 case Primitive::kPrimFloat: {
3470 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3471 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3472 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3473 locations->SetOut(LocationFrom(s0));
3474 break;
3475 }
3476
3477 case Primitive::kPrimDouble: {
3478 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3479 locations->SetInAt(0, LocationFrom(
3480 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3481 locations->SetInAt(1, LocationFrom(
3482 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3483 locations->SetOut(LocationFrom(s0, s1));
3484 break;
3485 }
3486
3487 default:
3488 LOG(FATAL) << "Unexpected rem type " << type;
3489 }
3490}
3491
3492void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
3493 LocationSummary* locations = rem->GetLocations();
3494 Location second = locations->InAt(1);
3495
3496 Primitive::Type type = rem->GetResultType();
3497 switch (type) {
3498 case Primitive::kPrimInt: {
3499 vixl32::Register reg1 = InputRegisterAt(rem, 0);
3500 vixl32::Register out_reg = OutputRegister(rem);
3501 if (second.IsConstant()) {
3502 GenerateDivRemConstantIntegral(rem);
3503 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3504 vixl32::Register reg2 = RegisterFrom(second);
3505 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3506
3507 // temp = reg1 / reg2 (integer division)
3508 // dest = reg1 - temp * reg2
3509 __ Sdiv(temp, reg1, reg2);
3510 __ Mls(out_reg, temp, reg2, reg1);
3511 } else {
3512 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3513 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
3514 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
3515 DCHECK(out_reg.Is(r1));
3516
3517 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
3518 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3519 }
3520 break;
3521 }
3522
3523 case Primitive::kPrimLong: {
3524 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
3525 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3526 break;
3527 }
3528
3529 case Primitive::kPrimFloat: {
3530 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
3531 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3532 break;
3533 }
3534
3535 case Primitive::kPrimDouble: {
3536 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
3537 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3538 break;
3539 }
3540
3541 default:
3542 LOG(FATAL) << "Unexpected rem type " << type;
3543 }
3544}
3545
3546
Scott Wakelingfe885462016-09-22 10:24:38 +01003547void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00003548 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01003549 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003550}
3551
3552void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3553 DivZeroCheckSlowPathARMVIXL* slow_path =
3554 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
3555 codegen_->AddSlowPath(slow_path);
3556
3557 LocationSummary* locations = instruction->GetLocations();
3558 Location value = locations->InAt(0);
3559
3560 switch (instruction->GetType()) {
3561 case Primitive::kPrimBoolean:
3562 case Primitive::kPrimByte:
3563 case Primitive::kPrimChar:
3564 case Primitive::kPrimShort:
3565 case Primitive::kPrimInt: {
3566 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003567 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01003568 } else {
3569 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003570 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003571 __ B(slow_path->GetEntryLabel());
3572 }
3573 }
3574 break;
3575 }
3576 case Primitive::kPrimLong: {
3577 if (value.IsRegisterPair()) {
3578 UseScratchRegisterScope temps(GetVIXLAssembler());
3579 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003580 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003581 __ B(eq, slow_path->GetEntryLabel());
3582 } else {
3583 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00003584 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003585 __ B(slow_path->GetEntryLabel());
3586 }
3587 }
3588 break;
3589 }
3590 default:
3591 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3592 }
3593}
3594
Artem Serov02109dd2016-09-23 17:17:54 +01003595void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
3596 LocationSummary* locations = ror->GetLocations();
3597 vixl32::Register in = InputRegisterAt(ror, 0);
3598 Location rhs = locations->InAt(1);
3599 vixl32::Register out = OutputRegister(ror);
3600
3601 if (rhs.IsConstant()) {
3602 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3603 // so map all rotations to a +ve. equivalent in that range.
3604 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3605 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3606 if (rot) {
3607 // Rotate, mapping left rotations to right equivalents if necessary.
3608 // (e.g. left by 2 bits == right by 30.)
3609 __ Ror(out, in, rot);
3610 } else if (!out.Is(in)) {
3611 __ Mov(out, in);
3612 }
3613 } else {
3614 __ Ror(out, in, RegisterFrom(rhs));
3615 }
3616}
3617
3618// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3619// rotates by swapping input regs (effectively rotating by the first 32-bits of
3620// a larger rotation) or flipping direction (thus treating larger right/left
3621// rotations as sub-word sized rotations in the other direction) as appropriate.
3622void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
3623 LocationSummary* locations = ror->GetLocations();
3624 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
3625 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
3626 Location rhs = locations->InAt(1);
3627 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
3628 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
3629
3630 if (rhs.IsConstant()) {
3631 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3632 // Map all rotations to +ve. equivalents on the interval [0,63].
3633 rot &= kMaxLongShiftDistance;
3634 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3635 // logic below to a simple pair of binary orr.
3636 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3637 if (rot >= kArmBitsPerWord) {
3638 rot -= kArmBitsPerWord;
3639 std::swap(in_reg_hi, in_reg_lo);
3640 }
3641 // Rotate, or mov to out for zero or word size rotations.
3642 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00003643 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003644 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003645 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01003646 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
3647 } else {
3648 __ Mov(out_reg_lo, in_reg_lo);
3649 __ Mov(out_reg_hi, in_reg_hi);
3650 }
3651 } else {
3652 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
3653 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
3654 vixl32::Label end;
3655 vixl32::Label shift_by_32_plus_shift_right;
3656
3657 __ And(shift_right, RegisterFrom(rhs), 0x1F);
3658 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00003659 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00003660 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01003661
3662 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3663 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3664 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3665 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3666 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3667 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3668 __ Lsr(shift_left, in_reg_hi, shift_right);
3669 __ Add(out_reg_lo, out_reg_lo, shift_left);
3670 __ B(&end);
3671
3672 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3673 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3674 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3675 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3676 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3677 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
3678 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3679 __ Lsl(shift_right, in_reg_hi, shift_left);
3680 __ Add(out_reg_lo, out_reg_lo, shift_right);
3681
3682 __ Bind(&end);
3683 }
3684}
3685
3686void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
3687 LocationSummary* locations =
3688 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3689 switch (ror->GetResultType()) {
3690 case Primitive::kPrimInt: {
3691 locations->SetInAt(0, Location::RequiresRegister());
3692 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3694 break;
3695 }
3696 case Primitive::kPrimLong: {
3697 locations->SetInAt(0, Location::RequiresRegister());
3698 if (ror->InputAt(1)->IsConstant()) {
3699 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3700 } else {
3701 locations->SetInAt(1, Location::RequiresRegister());
3702 locations->AddTemp(Location::RequiresRegister());
3703 locations->AddTemp(Location::RequiresRegister());
3704 }
3705 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3706 break;
3707 }
3708 default:
3709 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3710 }
3711}
3712
3713void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
3714 Primitive::Type type = ror->GetResultType();
3715 switch (type) {
3716 case Primitive::kPrimInt: {
3717 HandleIntegerRotate(ror);
3718 break;
3719 }
3720 case Primitive::kPrimLong: {
3721 HandleLongRotate(ror);
3722 break;
3723 }
3724 default:
3725 LOG(FATAL) << "Unexpected operation type " << type;
3726 UNREACHABLE();
3727 }
3728}
3729
Artem Serov02d37832016-10-25 15:25:33 +01003730void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
3731 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3732
3733 LocationSummary* locations =
3734 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3735
3736 switch (op->GetResultType()) {
3737 case Primitive::kPrimInt: {
3738 locations->SetInAt(0, Location::RequiresRegister());
3739 if (op->InputAt(1)->IsConstant()) {
3740 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3742 } else {
3743 locations->SetInAt(1, Location::RequiresRegister());
3744 // Make the output overlap, as it will be used to hold the masked
3745 // second input.
3746 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3747 }
3748 break;
3749 }
3750 case Primitive::kPrimLong: {
3751 locations->SetInAt(0, Location::RequiresRegister());
3752 if (op->InputAt(1)->IsConstant()) {
3753 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3754 // For simplicity, use kOutputOverlap even though we only require that low registers
3755 // don't clash with high registers which the register allocator currently guarantees.
3756 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3757 } else {
3758 locations->SetInAt(1, Location::RequiresRegister());
3759 locations->AddTemp(Location::RequiresRegister());
3760 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3761 }
3762 break;
3763 }
3764 default:
3765 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3766 }
3767}
3768
3769void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
3770 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3771
3772 LocationSummary* locations = op->GetLocations();
3773 Location out = locations->Out();
3774 Location first = locations->InAt(0);
3775 Location second = locations->InAt(1);
3776
3777 Primitive::Type type = op->GetResultType();
3778 switch (type) {
3779 case Primitive::kPrimInt: {
3780 vixl32::Register out_reg = OutputRegister(op);
3781 vixl32::Register first_reg = InputRegisterAt(op, 0);
3782 if (second.IsRegister()) {
3783 vixl32::Register second_reg = RegisterFrom(second);
3784 // ARM doesn't mask the shift count so we need to do it ourselves.
3785 __ And(out_reg, second_reg, kMaxIntShiftDistance);
3786 if (op->IsShl()) {
3787 __ Lsl(out_reg, first_reg, out_reg);
3788 } else if (op->IsShr()) {
3789 __ Asr(out_reg, first_reg, out_reg);
3790 } else {
3791 __ Lsr(out_reg, first_reg, out_reg);
3792 }
3793 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00003794 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003795 uint32_t shift_value = cst & kMaxIntShiftDistance;
3796 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
3797 __ Mov(out_reg, first_reg);
3798 } else if (op->IsShl()) {
3799 __ Lsl(out_reg, first_reg, shift_value);
3800 } else if (op->IsShr()) {
3801 __ Asr(out_reg, first_reg, shift_value);
3802 } else {
3803 __ Lsr(out_reg, first_reg, shift_value);
3804 }
3805 }
3806 break;
3807 }
3808 case Primitive::kPrimLong: {
3809 vixl32::Register o_h = HighRegisterFrom(out);
3810 vixl32::Register o_l = LowRegisterFrom(out);
3811
3812 vixl32::Register high = HighRegisterFrom(first);
3813 vixl32::Register low = LowRegisterFrom(first);
3814
3815 if (second.IsRegister()) {
3816 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3817
3818 vixl32::Register second_reg = RegisterFrom(second);
3819
3820 if (op->IsShl()) {
3821 __ And(o_l, second_reg, kMaxLongShiftDistance);
3822 // Shift the high part
3823 __ Lsl(o_h, high, o_l);
3824 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003825 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003826 __ Lsr(temp, low, temp);
3827 __ Orr(o_h, o_h, temp);
3828 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003829 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003830 {
Artem Serov0fb37192016-12-06 18:13:40 +00003831 ExactAssemblyScope guard(GetVIXLAssembler(),
3832 2 * vixl32::kMaxInstructionSizeInBytes,
3833 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003834 __ it(pl);
3835 __ lsl(pl, o_h, low, temp);
3836 }
3837 // Shift the low part
3838 __ Lsl(o_l, low, o_l);
3839 } else if (op->IsShr()) {
3840 __ And(o_h, second_reg, kMaxLongShiftDistance);
3841 // Shift the low part
3842 __ Lsr(o_l, low, o_h);
3843 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003844 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003845 __ Lsl(temp, high, temp);
3846 __ Orr(o_l, o_l, temp);
3847 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00003848 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003849 {
Artem Serov0fb37192016-12-06 18:13:40 +00003850 ExactAssemblyScope guard(GetVIXLAssembler(),
3851 2 * vixl32::kMaxInstructionSizeInBytes,
3852 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003853 __ it(pl);
3854 __ asr(pl, o_l, high, temp);
3855 }
3856 // Shift the high part
3857 __ Asr(o_h, high, o_h);
3858 } else {
3859 __ And(o_h, second_reg, kMaxLongShiftDistance);
3860 // same as Shr except we use `Lsr`s and not `Asr`s
3861 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003862 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003863 __ Lsl(temp, high, temp);
3864 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003865 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01003866 {
Artem Serov0fb37192016-12-06 18:13:40 +00003867 ExactAssemblyScope guard(GetVIXLAssembler(),
3868 2 * vixl32::kMaxInstructionSizeInBytes,
3869 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01003870 __ it(pl);
3871 __ lsr(pl, o_l, high, temp);
3872 }
3873 __ Lsr(o_h, high, o_h);
3874 }
3875 } else {
3876 // Register allocator doesn't create partial overlap.
3877 DCHECK(!o_l.Is(high));
3878 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00003879 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01003880 uint32_t shift_value = cst & kMaxLongShiftDistance;
3881 if (shift_value > 32) {
3882 if (op->IsShl()) {
3883 __ Lsl(o_h, low, shift_value - 32);
3884 __ Mov(o_l, 0);
3885 } else if (op->IsShr()) {
3886 __ Asr(o_l, high, shift_value - 32);
3887 __ Asr(o_h, high, 31);
3888 } else {
3889 __ Lsr(o_l, high, shift_value - 32);
3890 __ Mov(o_h, 0);
3891 }
3892 } else if (shift_value == 32) {
3893 if (op->IsShl()) {
3894 __ Mov(o_h, low);
3895 __ Mov(o_l, 0);
3896 } else if (op->IsShr()) {
3897 __ Mov(o_l, high);
3898 __ Asr(o_h, high, 31);
3899 } else {
3900 __ Mov(o_l, high);
3901 __ Mov(o_h, 0);
3902 }
3903 } else if (shift_value == 1) {
3904 if (op->IsShl()) {
3905 __ Lsls(o_l, low, 1);
3906 __ Adc(o_h, high, high);
3907 } else if (op->IsShr()) {
3908 __ Asrs(o_h, high, 1);
3909 __ Rrx(o_l, low);
3910 } else {
3911 __ Lsrs(o_h, high, 1);
3912 __ Rrx(o_l, low);
3913 }
3914 } else {
3915 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3916 if (op->IsShl()) {
3917 __ Lsl(o_h, high, shift_value);
3918 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
3919 __ Lsl(o_l, low, shift_value);
3920 } else if (op->IsShr()) {
3921 __ Lsr(o_l, low, shift_value);
3922 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3923 __ Asr(o_h, high, shift_value);
3924 } else {
3925 __ Lsr(o_l, low, shift_value);
3926 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
3927 __ Lsr(o_h, high, shift_value);
3928 }
3929 }
3930 }
3931 break;
3932 }
3933 default:
3934 LOG(FATAL) << "Unexpected operation type " << type;
3935 UNREACHABLE();
3936 }
3937}
3938
3939void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
3940 HandleShift(shl);
3941}
3942
3943void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
3944 HandleShift(shl);
3945}
3946
3947void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
3948 HandleShift(shr);
3949}
3950
3951void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
3952 HandleShift(shr);
3953}
3954
3955void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
3956 HandleShift(ushr);
3957}
3958
3959void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
3960 HandleShift(ushr);
3961}
3962
3963void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3964 LocationSummary* locations =
3965 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3966 if (instruction->IsStringAlloc()) {
3967 locations->AddTemp(LocationFrom(kMethodRegister));
3968 } else {
3969 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3970 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01003971 }
3972 locations->SetOut(LocationFrom(r0));
3973}
3974
3975void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
3976 // Note: if heap poisoning is enabled, the entry point takes cares
3977 // of poisoning the reference.
3978 if (instruction->IsStringAlloc()) {
3979 // String is allocated through StringFactory. Call NewEmptyString entry point.
3980 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
3981 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3982 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
3983 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003984 // 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 +00003985 ExactAssemblyScope aas(GetVIXLAssembler(),
3986 vixl32::k16BitT32InstructionSizeInBytes,
3987 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01003988 __ blx(lr);
3989 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3990 } else {
3991 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003992 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01003993 }
3994}
3995
3996void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3999 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004000 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004001 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4002 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004003}
4004
4005void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004006 // Note: if heap poisoning is enabled, the entry point takes cares
4007 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004008 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4009 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov02d37832016-10-25 15:25:33 +01004010}
4011
4012void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4013 LocationSummary* locations =
4014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4015 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4016 if (location.IsStackSlot()) {
4017 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4018 } else if (location.IsDoubleStackSlot()) {
4019 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4020 }
4021 locations->SetOut(location);
4022}
4023
4024void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4025 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4026 // Nothing to do, the parameter is already at its location.
4027}
4028
4029void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4032 locations->SetOut(LocationFrom(kMethodRegister));
4033}
4034
4035void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4036 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4037 // Nothing to do, the method is already at its location.
4038}
4039
4040void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4041 LocationSummary* locations =
4042 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4043 locations->SetInAt(0, Location::RequiresRegister());
4044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4045}
4046
4047void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4048 LocationSummary* locations = not_->GetLocations();
4049 Location out = locations->Out();
4050 Location in = locations->InAt(0);
4051 switch (not_->GetResultType()) {
4052 case Primitive::kPrimInt:
4053 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4054 break;
4055
4056 case Primitive::kPrimLong:
4057 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4058 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4059 break;
4060
4061 default:
4062 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4063 }
4064}
4065
Scott Wakelingc34dba72016-10-03 10:14:44 +01004066void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4069 locations->SetInAt(0, Location::RequiresRegister());
4070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4071}
4072
4073void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4074 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
4075}
4076
Artem Serov02d37832016-10-25 15:25:33 +01004077void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
4080 switch (compare->InputAt(0)->GetType()) {
4081 case Primitive::kPrimBoolean:
4082 case Primitive::kPrimByte:
4083 case Primitive::kPrimShort:
4084 case Primitive::kPrimChar:
4085 case Primitive::kPrimInt:
4086 case Primitive::kPrimLong: {
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::RequiresRegister());
4089 // Output overlaps because it is written before doing the low comparison.
4090 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4091 break;
4092 }
4093 case Primitive::kPrimFloat:
4094 case Primitive::kPrimDouble: {
4095 locations->SetInAt(0, Location::RequiresFpuRegister());
4096 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
4097 locations->SetOut(Location::RequiresRegister());
4098 break;
4099 }
4100 default:
4101 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4102 }
4103}
4104
4105void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
4106 LocationSummary* locations = compare->GetLocations();
4107 vixl32::Register out = OutputRegister(compare);
4108 Location left = locations->InAt(0);
4109 Location right = locations->InAt(1);
4110
4111 vixl32::Label less, greater, done;
4112 Primitive::Type type = compare->InputAt(0)->GetType();
4113 vixl32::Condition less_cond = vixl32::Condition(kNone);
4114 switch (type) {
4115 case Primitive::kPrimBoolean:
4116 case Primitive::kPrimByte:
4117 case Primitive::kPrimShort:
4118 case Primitive::kPrimChar:
4119 case Primitive::kPrimInt: {
4120 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
4121 __ Mov(out, 0);
4122 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
4123 less_cond = lt;
4124 break;
4125 }
4126 case Primitive::kPrimLong: {
4127 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00004128 __ B(lt, &less, /* far_target */ false);
4129 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004130 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
4131 __ Mov(out, 0);
4132 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
4133 less_cond = lo;
4134 break;
4135 }
4136 case Primitive::kPrimFloat:
4137 case Primitive::kPrimDouble: {
4138 __ Mov(out, 0);
4139 GenerateVcmp(compare);
4140 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
4141 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4142 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
4143 break;
4144 }
4145 default:
4146 LOG(FATAL) << "Unexpected compare type " << type;
4147 UNREACHABLE();
4148 }
4149
Artem Serov517d9f62016-12-12 15:51:15 +00004150 __ B(eq, &done, /* far_target */ false);
4151 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01004152
4153 __ Bind(&greater);
4154 __ Mov(out, 1);
4155 __ B(&done);
4156
4157 __ Bind(&less);
4158 __ Mov(out, -1);
4159
4160 __ Bind(&done);
4161}
4162
4163void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
4164 LocationSummary* locations =
4165 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4166 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
4167 locations->SetInAt(i, Location::Any());
4168 }
4169 locations->SetOut(Location::Any());
4170}
4171
4172void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4173 LOG(FATAL) << "Unreachable";
4174}
4175
4176void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
4177 // TODO (ported from quick): revisit ARM barrier kinds.
4178 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
4179 switch (kind) {
4180 case MemBarrierKind::kAnyStore:
4181 case MemBarrierKind::kLoadAny:
4182 case MemBarrierKind::kAnyAny: {
4183 flavor = DmbOptions::ISH;
4184 break;
4185 }
4186 case MemBarrierKind::kStoreStore: {
4187 flavor = DmbOptions::ISHST;
4188 break;
4189 }
4190 default:
4191 LOG(FATAL) << "Unexpected memory barrier " << kind;
4192 }
4193 __ Dmb(flavor);
4194}
4195
4196void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
4197 uint32_t offset,
4198 vixl32::Register out_lo,
4199 vixl32::Register out_hi) {
4200 UseScratchRegisterScope temps(GetVIXLAssembler());
4201 if (offset != 0) {
4202 vixl32::Register temp = temps.Acquire();
4203 __ Add(temp, addr, offset);
4204 addr = temp;
4205 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004206 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01004207}
4208
4209void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
4210 uint32_t offset,
4211 vixl32::Register value_lo,
4212 vixl32::Register value_hi,
4213 vixl32::Register temp1,
4214 vixl32::Register temp2,
4215 HInstruction* instruction) {
4216 UseScratchRegisterScope temps(GetVIXLAssembler());
4217 vixl32::Label fail;
4218 if (offset != 0) {
4219 vixl32::Register temp = temps.Acquire();
4220 __ Add(temp, addr, offset);
4221 addr = temp;
4222 }
4223 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004224 {
4225 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004226 ExactAssemblyScope aas(GetVIXLAssembler(),
4227 vixl32::kMaxInstructionSizeInBytes,
4228 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00004229 // We need a load followed by store. (The address used in a STREX instruction must
4230 // be the same as the address in the most recently executed LDREX instruction.)
4231 __ ldrexd(temp1, temp2, MemOperand(addr));
4232 codegen_->MaybeRecordImplicitNullCheck(instruction);
4233 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00004234 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00004235 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01004236}
Artem Serov02109dd2016-09-23 17:17:54 +01004237
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004238void LocationsBuilderARMVIXL::HandleFieldSet(
4239 HInstruction* instruction, const FieldInfo& field_info) {
4240 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4241
4242 LocationSummary* locations =
4243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4244 locations->SetInAt(0, Location::RequiresRegister());
4245
4246 Primitive::Type field_type = field_info.GetFieldType();
4247 if (Primitive::IsFloatingPointType(field_type)) {
4248 locations->SetInAt(1, Location::RequiresFpuRegister());
4249 } else {
4250 locations->SetInAt(1, Location::RequiresRegister());
4251 }
4252
4253 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
4254 bool generate_volatile = field_info.IsVolatile()
4255 && is_wide
4256 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4257 bool needs_write_barrier =
4258 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4259 // Temporary registers for the write barrier.
4260 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
4261 if (needs_write_barrier) {
4262 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
4263 locations->AddTemp(Location::RequiresRegister());
4264 } else if (generate_volatile) {
4265 // ARM encoding have some additional constraints for ldrexd/strexd:
4266 // - registers need to be consecutive
4267 // - the first register should be even but not R14.
4268 // We don't test for ARM yet, and the assertion makes sure that we
4269 // revisit this if we ever enable ARM encoding.
4270 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4271
4272 locations->AddTemp(Location::RequiresRegister());
4273 locations->AddTemp(Location::RequiresRegister());
4274 if (field_type == Primitive::kPrimDouble) {
4275 // For doubles we need two more registers to copy the value.
4276 locations->AddTemp(LocationFrom(r2));
4277 locations->AddTemp(LocationFrom(r3));
4278 }
4279 }
4280}
4281
4282void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
4283 const FieldInfo& field_info,
4284 bool value_can_be_null) {
4285 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4286
4287 LocationSummary* locations = instruction->GetLocations();
4288 vixl32::Register base = InputRegisterAt(instruction, 0);
4289 Location value = locations->InAt(1);
4290
4291 bool is_volatile = field_info.IsVolatile();
4292 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4293 Primitive::Type field_type = field_info.GetFieldType();
4294 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4295 bool needs_write_barrier =
4296 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
4297
4298 if (is_volatile) {
4299 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4300 }
4301
4302 switch (field_type) {
4303 case Primitive::kPrimBoolean:
4304 case Primitive::kPrimByte: {
4305 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
4306 break;
4307 }
4308
4309 case Primitive::kPrimShort:
4310 case Primitive::kPrimChar: {
4311 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
4312 break;
4313 }
4314
4315 case Primitive::kPrimInt:
4316 case Primitive::kPrimNot: {
4317 if (kPoisonHeapReferences && needs_write_barrier) {
4318 // Note that in the case where `value` is a null reference,
4319 // we do not enter this block, as a null reference does not
4320 // need poisoning.
4321 DCHECK_EQ(field_type, Primitive::kPrimNot);
4322 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4323 __ Mov(temp, RegisterFrom(value));
4324 GetAssembler()->PoisonHeapReference(temp);
4325 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
4326 } else {
4327 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
4328 }
4329 break;
4330 }
4331
4332 case Primitive::kPrimLong: {
4333 if (is_volatile && !atomic_ldrd_strd) {
4334 GenerateWideAtomicStore(base,
4335 offset,
4336 LowRegisterFrom(value),
4337 HighRegisterFrom(value),
4338 RegisterFrom(locations->GetTemp(0)),
4339 RegisterFrom(locations->GetTemp(1)),
4340 instruction);
4341 } else {
4342 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
4343 codegen_->MaybeRecordImplicitNullCheck(instruction);
4344 }
4345 break;
4346 }
4347
4348 case Primitive::kPrimFloat: {
4349 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
4350 break;
4351 }
4352
4353 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004354 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004355 if (is_volatile && !atomic_ldrd_strd) {
4356 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
4357 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
4358
4359 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
4360
4361 GenerateWideAtomicStore(base,
4362 offset,
4363 value_reg_lo,
4364 value_reg_hi,
4365 RegisterFrom(locations->GetTemp(2)),
4366 RegisterFrom(locations->GetTemp(3)),
4367 instruction);
4368 } else {
4369 GetAssembler()->StoreDToOffset(value_reg, base, offset);
4370 codegen_->MaybeRecordImplicitNullCheck(instruction);
4371 }
4372 break;
4373 }
4374
4375 case Primitive::kPrimVoid:
4376 LOG(FATAL) << "Unreachable type " << field_type;
4377 UNREACHABLE();
4378 }
4379
4380 // Longs and doubles are handled in the switch.
4381 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00004382 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4383 // should use a scope and the assembler to emit the store instruction to guarantee that we
4384 // record the pc at the correct position. But the `Assembler` does not automatically handle
4385 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4386 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004387 codegen_->MaybeRecordImplicitNullCheck(instruction);
4388 }
4389
4390 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4391 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4392 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
4393 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
4394 }
4395
4396 if (is_volatile) {
4397 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4398 }
4399}
4400
Artem Serov02d37832016-10-25 15:25:33 +01004401void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
4402 const FieldInfo& field_info) {
4403 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4404
4405 bool object_field_get_with_read_barrier =
4406 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
4407 LocationSummary* locations =
4408 new (GetGraph()->GetArena()) LocationSummary(instruction,
4409 object_field_get_with_read_barrier ?
4410 LocationSummary::kCallOnSlowPath :
4411 LocationSummary::kNoCall);
4412 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4413 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4414 }
4415 locations->SetInAt(0, Location::RequiresRegister());
4416
4417 bool volatile_for_double = field_info.IsVolatile()
4418 && (field_info.GetFieldType() == Primitive::kPrimDouble)
4419 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4420 // The output overlaps in case of volatile long: we don't want the
4421 // code generated by GenerateWideAtomicLoad to overwrite the
4422 // object's location. Likewise, in the case of an object field get
4423 // with read barriers enabled, we do not want the load to overwrite
4424 // the object's location, as we need it to emit the read barrier.
4425 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4426 object_field_get_with_read_barrier;
4427
4428 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4429 locations->SetOut(Location::RequiresFpuRegister());
4430 } else {
4431 locations->SetOut(Location::RequiresRegister(),
4432 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4433 }
4434 if (volatile_for_double) {
4435 // ARM encoding have some additional constraints for ldrexd/strexd:
4436 // - registers need to be consecutive
4437 // - the first register should be even but not R14.
4438 // We don't test for ARM yet, and the assertion makes sure that we
4439 // revisit this if we ever enable ARM encoding.
4440 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4441 locations->AddTemp(Location::RequiresRegister());
4442 locations->AddTemp(Location::RequiresRegister());
4443 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4444 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004445 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Artem Serov02d37832016-10-25 15:25:33 +01004446 locations->AddTemp(Location::RequiresRegister());
4447 }
4448}
4449
4450Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4451 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
4452 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4453 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4454 return Location::ConstantLocation(input->AsConstant());
4455 } else {
4456 return Location::RequiresFpuRegister();
4457 }
4458}
4459
Artem Serov02109dd2016-09-23 17:17:54 +01004460Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
4461 Opcode opcode) {
4462 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4463 if (constant->IsConstant() &&
4464 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4465 return Location::ConstantLocation(constant->AsConstant());
4466 }
4467 return Location::RequiresRegister();
4468}
4469
4470bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
4471 Opcode opcode) {
4472 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4473 if (Primitive::Is64BitType(input_cst->GetType())) {
4474 Opcode high_opcode = opcode;
4475 SetCc low_set_cc = kCcDontCare;
4476 switch (opcode) {
4477 case SUB:
4478 // Flip the operation to an ADD.
4479 value = -value;
4480 opcode = ADD;
4481 FALLTHROUGH_INTENDED;
4482 case ADD:
4483 if (Low32Bits(value) == 0u) {
4484 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4485 }
4486 high_opcode = ADC;
4487 low_set_cc = kCcSet;
4488 break;
4489 default:
4490 break;
4491 }
4492 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4493 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4494 } else {
4495 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4496 }
4497}
4498
4499// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
4500// enabled.
4501bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
4502 Opcode opcode,
4503 SetCc set_cc) {
4504 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
4505 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
4506 return true;
4507 }
4508 Opcode neg_opcode = kNoOperand;
4509 switch (opcode) {
4510 case AND: neg_opcode = BIC; value = ~value; break;
4511 case ORR: neg_opcode = ORN; value = ~value; break;
4512 case ADD: neg_opcode = SUB; value = -value; break;
4513 case ADC: neg_opcode = SBC; value = ~value; break;
4514 case SUB: neg_opcode = ADD; value = -value; break;
4515 case SBC: neg_opcode = ADC; value = ~value; break;
4516 default:
4517 return false;
4518 }
4519 return assembler->ShifterOperandCanHold(neg_opcode, value, set_cc);
4520}
4521
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004522void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
4523 const FieldInfo& field_info) {
4524 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4525
4526 LocationSummary* locations = instruction->GetLocations();
4527 vixl32::Register base = InputRegisterAt(instruction, 0);
4528 Location out = locations->Out();
4529 bool is_volatile = field_info.IsVolatile();
4530 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4531 Primitive::Type field_type = field_info.GetFieldType();
4532 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4533
4534 switch (field_type) {
4535 case Primitive::kPrimBoolean:
4536 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
4537 break;
4538
4539 case Primitive::kPrimByte:
4540 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
4541 break;
4542
4543 case Primitive::kPrimShort:
4544 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
4545 break;
4546
4547 case Primitive::kPrimChar:
4548 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
4549 break;
4550
4551 case Primitive::kPrimInt:
4552 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
4553 break;
4554
4555 case Primitive::kPrimNot: {
4556 // /* HeapReference<Object> */ out = *(base + offset)
4557 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004558 Location temp_loc = locations->GetTemp(0);
4559 // Note that a potential implicit null check is handled in this
4560 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
4561 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4562 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4563 if (is_volatile) {
4564 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4565 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004566 } else {
4567 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004568 codegen_->MaybeRecordImplicitNullCheck(instruction);
4569 if (is_volatile) {
4570 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4571 }
4572 // If read barriers are enabled, emit read barriers other than
4573 // Baker's using a slow path (and also unpoison the loaded
4574 // reference, if heap poisoning is enabled).
4575 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
4576 }
4577 break;
4578 }
4579
4580 case Primitive::kPrimLong:
4581 if (is_volatile && !atomic_ldrd_strd) {
4582 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
4583 } else {
4584 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
4585 }
4586 break;
4587
4588 case Primitive::kPrimFloat:
4589 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
4590 break;
4591
4592 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004593 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004594 if (is_volatile && !atomic_ldrd_strd) {
4595 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
4596 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
4597 GenerateWideAtomicLoad(base, offset, lo, hi);
4598 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
4599 // scope.
4600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 __ Vmov(out_dreg, lo, hi);
4602 } else {
4603 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004604 codegen_->MaybeRecordImplicitNullCheck(instruction);
4605 }
4606 break;
4607 }
4608
4609 case Primitive::kPrimVoid:
4610 LOG(FATAL) << "Unreachable type " << field_type;
4611 UNREACHABLE();
4612 }
4613
4614 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4615 // Potential implicit null checks, in the case of reference or
4616 // double fields, are handled in the previous switch statement.
4617 } else {
4618 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00004619 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
4620 // should use a scope and the assembler to emit the load instruction to guarantee that we
4621 // record the pc at the correct position. But the `Assembler` does not automatically handle
4622 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
4623 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004624 codegen_->MaybeRecordImplicitNullCheck(instruction);
4625 }
4626
4627 if (is_volatile) {
4628 if (field_type == Primitive::kPrimNot) {
4629 // Memory barriers, in the case of references, are also handled
4630 // in the previous switch statement.
4631 } else {
4632 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4633 }
4634 }
4635}
4636
4637void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4638 HandleFieldSet(instruction, instruction->GetFieldInfo());
4639}
4640
4641void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4642 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4643}
4644
4645void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4646 HandleFieldGet(instruction, instruction->GetFieldInfo());
4647}
4648
4649void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4650 HandleFieldGet(instruction, instruction->GetFieldInfo());
4651}
4652
4653void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4654 HandleFieldGet(instruction, instruction->GetFieldInfo());
4655}
4656
4657void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4658 HandleFieldGet(instruction, instruction->GetFieldInfo());
4659}
4660
Scott Wakelingc34dba72016-10-03 10:14:44 +01004661void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4662 HandleFieldSet(instruction, instruction->GetFieldInfo());
4663}
4664
4665void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4666 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4667}
4668
Artem Serovcfbe9132016-10-14 15:58:56 +01004669void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
4670 HUnresolvedInstanceFieldGet* instruction) {
4671 FieldAccessCallingConventionARMVIXL calling_convention;
4672 codegen_->CreateUnresolvedFieldLocationSummary(
4673 instruction, instruction->GetFieldType(), calling_convention);
4674}
4675
4676void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
4677 HUnresolvedInstanceFieldGet* instruction) {
4678 FieldAccessCallingConventionARMVIXL calling_convention;
4679 codegen_->GenerateUnresolvedFieldAccess(instruction,
4680 instruction->GetFieldType(),
4681 instruction->GetFieldIndex(),
4682 instruction->GetDexPc(),
4683 calling_convention);
4684}
4685
4686void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
4687 HUnresolvedInstanceFieldSet* instruction) {
4688 FieldAccessCallingConventionARMVIXL calling_convention;
4689 codegen_->CreateUnresolvedFieldLocationSummary(
4690 instruction, instruction->GetFieldType(), calling_convention);
4691}
4692
4693void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
4694 HUnresolvedInstanceFieldSet* instruction) {
4695 FieldAccessCallingConventionARMVIXL calling_convention;
4696 codegen_->GenerateUnresolvedFieldAccess(instruction,
4697 instruction->GetFieldType(),
4698 instruction->GetFieldIndex(),
4699 instruction->GetDexPc(),
4700 calling_convention);
4701}
4702
4703void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
4704 HUnresolvedStaticFieldGet* instruction) {
4705 FieldAccessCallingConventionARMVIXL calling_convention;
4706 codegen_->CreateUnresolvedFieldLocationSummary(
4707 instruction, instruction->GetFieldType(), calling_convention);
4708}
4709
4710void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
4711 HUnresolvedStaticFieldGet* instruction) {
4712 FieldAccessCallingConventionARMVIXL calling_convention;
4713 codegen_->GenerateUnresolvedFieldAccess(instruction,
4714 instruction->GetFieldType(),
4715 instruction->GetFieldIndex(),
4716 instruction->GetDexPc(),
4717 calling_convention);
4718}
4719
4720void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
4721 HUnresolvedStaticFieldSet* instruction) {
4722 FieldAccessCallingConventionARMVIXL calling_convention;
4723 codegen_->CreateUnresolvedFieldLocationSummary(
4724 instruction, instruction->GetFieldType(), calling_convention);
4725}
4726
4727void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
4728 HUnresolvedStaticFieldSet* instruction) {
4729 FieldAccessCallingConventionARMVIXL calling_convention;
4730 codegen_->GenerateUnresolvedFieldAccess(instruction,
4731 instruction->GetFieldType(),
4732 instruction->GetFieldIndex(),
4733 instruction->GetDexPc(),
4734 calling_convention);
4735}
4736
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004737void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004738 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004739 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004740}
4741
4742void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
4743 if (CanMoveNullCheckToUser(instruction)) {
4744 return;
4745 }
4746
4747 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004748 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00004749 ExactAssemblyScope aas(GetVIXLAssembler(),
4750 vixl32::kMaxInstructionSizeInBytes,
4751 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004752 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
4753 RecordPcInfo(instruction, instruction->GetDexPc());
4754}
4755
4756void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
4757 NullCheckSlowPathARMVIXL* slow_path =
4758 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
4759 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00004760 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004761}
4762
4763void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
4764 codegen_->GenerateNullCheck(instruction);
4765}
4766
Scott Wakelingc34dba72016-10-03 10:14:44 +01004767static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4768 switch (type) {
4769 case Primitive::kPrimNot:
4770 return kLoadWord;
4771 case Primitive::kPrimBoolean:
4772 return kLoadUnsignedByte;
4773 case Primitive::kPrimByte:
4774 return kLoadSignedByte;
4775 case Primitive::kPrimChar:
4776 return kLoadUnsignedHalfword;
4777 case Primitive::kPrimShort:
4778 return kLoadSignedHalfword;
4779 case Primitive::kPrimInt:
4780 return kLoadWord;
4781 case Primitive::kPrimLong:
4782 return kLoadWordPair;
4783 case Primitive::kPrimFloat:
4784 return kLoadSWord;
4785 case Primitive::kPrimDouble:
4786 return kLoadDWord;
4787 default:
4788 LOG(FATAL) << "Unreachable type " << type;
4789 UNREACHABLE();
4790 }
4791}
4792
4793static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4794 switch (type) {
4795 case Primitive::kPrimNot:
4796 return kStoreWord;
4797 case Primitive::kPrimBoolean:
4798 case Primitive::kPrimByte:
4799 return kStoreByte;
4800 case Primitive::kPrimChar:
4801 case Primitive::kPrimShort:
4802 return kStoreHalfword;
4803 case Primitive::kPrimInt:
4804 return kStoreWord;
4805 case Primitive::kPrimLong:
4806 return kStoreWordPair;
4807 case Primitive::kPrimFloat:
4808 return kStoreSWord;
4809 case Primitive::kPrimDouble:
4810 return kStoreDWord;
4811 default:
4812 LOG(FATAL) << "Unreachable type " << type;
4813 UNREACHABLE();
4814 }
4815}
4816
4817void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
4818 Location out_loc,
4819 vixl32::Register base,
4820 vixl32::Register reg_index,
4821 vixl32::Condition cond) {
4822 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4823 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4824
4825 switch (type) {
4826 case Primitive::kPrimByte:
4827 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
4828 break;
4829 case Primitive::kPrimBoolean:
4830 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
4831 break;
4832 case Primitive::kPrimShort:
4833 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
4834 break;
4835 case Primitive::kPrimChar:
4836 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
4837 break;
4838 case Primitive::kPrimNot:
4839 case Primitive::kPrimInt:
4840 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
4841 break;
4842 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4843 case Primitive::kPrimLong:
4844 case Primitive::kPrimFloat:
4845 case Primitive::kPrimDouble:
4846 default:
4847 LOG(FATAL) << "Unreachable type " << type;
4848 UNREACHABLE();
4849 }
4850}
4851
4852void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
4853 Location loc,
4854 vixl32::Register base,
4855 vixl32::Register reg_index,
4856 vixl32::Condition cond) {
4857 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4858 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
4859
4860 switch (type) {
4861 case Primitive::kPrimByte:
4862 case Primitive::kPrimBoolean:
4863 __ Strb(cond, RegisterFrom(loc), mem_address);
4864 break;
4865 case Primitive::kPrimShort:
4866 case Primitive::kPrimChar:
4867 __ Strh(cond, RegisterFrom(loc), mem_address);
4868 break;
4869 case Primitive::kPrimNot:
4870 case Primitive::kPrimInt:
4871 __ Str(cond, RegisterFrom(loc), mem_address);
4872 break;
4873 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4874 case Primitive::kPrimLong:
4875 case Primitive::kPrimFloat:
4876 case Primitive::kPrimDouble:
4877 default:
4878 LOG(FATAL) << "Unreachable type " << type;
4879 UNREACHABLE();
4880 }
4881}
4882
4883void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
4884 bool object_array_get_with_read_barrier =
4885 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4886 LocationSummary* locations =
4887 new (GetGraph()->GetArena()) LocationSummary(instruction,
4888 object_array_get_with_read_barrier ?
4889 LocationSummary::kCallOnSlowPath :
4890 LocationSummary::kNoCall);
4891 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004892 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004893 }
4894 locations->SetInAt(0, Location::RequiresRegister());
4895 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4896 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4897 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4898 } else {
4899 // The output overlaps in the case of an object array get with
4900 // read barriers enabled: we do not want the move to overwrite the
4901 // array's location, as we need it to emit the read barrier.
4902 locations->SetOut(
4903 Location::RequiresRegister(),
4904 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4905 }
4906 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00004907 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004908 // Also need for String compression feature.
4909 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4910 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004911 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004912 }
4913}
4914
4915void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01004916 LocationSummary* locations = instruction->GetLocations();
4917 Location obj_loc = locations->InAt(0);
4918 vixl32::Register obj = InputRegisterAt(instruction, 0);
4919 Location index = locations->InAt(1);
4920 Location out_loc = locations->Out();
4921 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4922 Primitive::Type type = instruction->GetType();
4923 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4924 instruction->IsStringCharAt();
4925 HInstruction* array_instr = instruction->GetArray();
4926 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01004927
4928 switch (type) {
4929 case Primitive::kPrimBoolean:
4930 case Primitive::kPrimByte:
4931 case Primitive::kPrimShort:
4932 case Primitive::kPrimChar:
4933 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004934 vixl32::Register length;
4935 if (maybe_compressed_char_at) {
4936 length = RegisterFrom(locations->GetTemp(0));
4937 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4938 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
4939 codegen_->MaybeRecordImplicitNullCheck(instruction);
4940 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01004941 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00004942 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004943 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004944 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004945 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4946 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4947 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00004948 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004949 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
4950 RegisterFrom(out_loc),
4951 obj,
4952 data_offset + const_index);
4953 __ B(&done);
4954 __ Bind(&uncompressed_load);
4955 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4956 RegisterFrom(out_loc),
4957 obj,
4958 data_offset + (const_index << 1));
4959 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004960 } else {
4961 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4962
4963 LoadOperandType load_type = GetLoadOperandType(type);
4964 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
4965 }
4966 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00004967 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01004968 vixl32::Register temp = temps.Acquire();
4969
4970 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01004971 // We do not need to compute the intermediate address from the array: the
4972 // input instruction has done it already. See the comment in
4973 // `TryExtractArrayAccessAddress()`.
4974 if (kIsDebugBuild) {
4975 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00004976 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01004977 }
4978 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01004979 } else {
4980 __ Add(temp, obj, data_offset);
4981 }
4982 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004983 vixl32::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004984 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4985 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4986 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00004987 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004988 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
4989 __ B(&done);
4990 __ Bind(&uncompressed_load);
4991 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
4992 __ Bind(&done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004993 } else {
4994 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
4995 }
4996 }
4997 break;
4998 }
4999
5000 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005001 // The read barrier instrumentation of object ArrayGet
5002 // instructions does not support the HIntermediateAddress
5003 // instruction.
5004 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5005
Scott Wakelingc34dba72016-10-03 10:14:44 +01005006 static_assert(
5007 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5008 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5009 // /* HeapReference<Object> */ out =
5010 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5011 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005012 Location temp = locations->GetTemp(0);
5013 // Note that a potential implicit null check is handled in this
5014 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
5015 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5016 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005017 } else {
5018 vixl32::Register out = OutputRegister(instruction);
5019 if (index.IsConstant()) {
5020 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005021 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005022 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005023 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
5024 // we should use a scope and the assembler to emit the load instruction to guarantee that
5025 // we record the pc at the correct position. But the `Assembler` does not automatically
5026 // handle unencodable offsets. Practically, everything is fine because the helper and
5027 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005028 codegen_->MaybeRecordImplicitNullCheck(instruction);
5029 // If read barriers are enabled, emit read barriers other than
5030 // Baker's using a slow path (and also unpoison the loaded
5031 // reference, if heap poisoning is enabled).
5032 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5033 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005034 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005035 vixl32::Register temp = temps.Acquire();
5036
5037 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005038 // We do not need to compute the intermediate address from the array: the
5039 // input instruction has done it already. See the comment in
5040 // `TryExtractArrayAccessAddress()`.
5041 if (kIsDebugBuild) {
5042 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005043 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005044 }
5045 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005046 } else {
5047 __ Add(temp, obj, data_offset);
5048 }
5049 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005050 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00005051 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
5052 // load instruction. Practically, everything is fine because the helper and VIXL, at the
5053 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005054 codegen_->MaybeRecordImplicitNullCheck(instruction);
5055 // If read barriers are enabled, emit read barriers other than
5056 // Baker's using a slow path (and also unpoison the loaded
5057 // reference, if heap poisoning is enabled).
5058 codegen_->MaybeGenerateReadBarrierSlow(
5059 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5060 }
5061 }
5062 break;
5063 }
5064
5065 case Primitive::kPrimLong: {
5066 if (index.IsConstant()) {
5067 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005068 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005069 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
5070 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005071 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005072 vixl32::Register temp = temps.Acquire();
5073 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5074 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
5075 }
5076 break;
5077 }
5078
5079 case Primitive::kPrimFloat: {
5080 vixl32::SRegister out = SRegisterFrom(out_loc);
5081 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005082 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005083 GetAssembler()->LoadSFromOffset(out, obj, offset);
5084 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005085 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005086 vixl32::Register temp = temps.Acquire();
5087 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5088 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
5089 }
5090 break;
5091 }
5092
5093 case Primitive::kPrimDouble: {
5094 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005095 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005096 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
5097 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005098 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005099 vixl32::Register temp = temps.Acquire();
5100 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5101 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
5102 }
5103 break;
5104 }
5105
5106 case Primitive::kPrimVoid:
5107 LOG(FATAL) << "Unreachable type " << type;
5108 UNREACHABLE();
5109 }
5110
5111 if (type == Primitive::kPrimNot) {
5112 // Potential implicit null checks, in the case of reference
5113 // arrays, are handled in the previous switch statement.
5114 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005115 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
5116 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005117 codegen_->MaybeRecordImplicitNullCheck(instruction);
5118 }
5119}
5120
5121void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
5122 Primitive::Type value_type = instruction->GetComponentType();
5123
5124 bool needs_write_barrier =
5125 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5126 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5127
5128 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5129 instruction,
5130 may_need_runtime_call_for_type_check ?
5131 LocationSummary::kCallOnSlowPath :
5132 LocationSummary::kNoCall);
5133
5134 locations->SetInAt(0, Location::RequiresRegister());
5135 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5136 if (Primitive::IsFloatingPointType(value_type)) {
5137 locations->SetInAt(2, Location::RequiresFpuRegister());
5138 } else {
5139 locations->SetInAt(2, Location::RequiresRegister());
5140 }
5141 if (needs_write_barrier) {
5142 // Temporary registers for the write barrier.
5143 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5144 locations->AddTemp(Location::RequiresRegister());
5145 }
5146}
5147
5148void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005149 LocationSummary* locations = instruction->GetLocations();
5150 vixl32::Register array = InputRegisterAt(instruction, 0);
5151 Location index = locations->InAt(1);
5152 Primitive::Type value_type = instruction->GetComponentType();
5153 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5154 bool needs_write_barrier =
5155 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
5156 uint32_t data_offset =
5157 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5158 Location value_loc = locations->InAt(2);
5159 HInstruction* array_instr = instruction->GetArray();
5160 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005161
5162 switch (value_type) {
5163 case Primitive::kPrimBoolean:
5164 case Primitive::kPrimByte:
5165 case Primitive::kPrimShort:
5166 case Primitive::kPrimChar:
5167 case Primitive::kPrimInt: {
5168 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005169 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005170 uint32_t full_offset =
5171 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5172 StoreOperandType store_type = GetStoreOperandType(value_type);
5173 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
5174 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005175 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005176 vixl32::Register temp = temps.Acquire();
5177
5178 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005179 // We do not need to compute the intermediate address from the array: the
5180 // input instruction has done it already. See the comment in
5181 // `TryExtractArrayAccessAddress()`.
5182 if (kIsDebugBuild) {
5183 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005184 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005185 }
5186 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005187 } else {
5188 __ Add(temp, array, data_offset);
5189 }
5190 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5191 }
5192 break;
5193 }
5194
5195 case Primitive::kPrimNot: {
5196 vixl32::Register value = RegisterFrom(value_loc);
5197 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5198 // See the comment in instruction_simplifier_shared.cc.
5199 DCHECK(!has_intermediate_address);
5200
5201 if (instruction->InputAt(2)->IsNullConstant()) {
5202 // Just setting null.
5203 if (index.IsConstant()) {
5204 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005205 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005206 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5207 } else {
5208 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005209 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005210 vixl32::Register temp = temps.Acquire();
5211 __ Add(temp, array, data_offset);
5212 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5213 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005214 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5215 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005216 codegen_->MaybeRecordImplicitNullCheck(instruction);
5217 DCHECK(!needs_write_barrier);
5218 DCHECK(!may_need_runtime_call_for_type_check);
5219 break;
5220 }
5221
5222 DCHECK(needs_write_barrier);
5223 Location temp1_loc = locations->GetTemp(0);
5224 vixl32::Register temp1 = RegisterFrom(temp1_loc);
5225 Location temp2_loc = locations->GetTemp(1);
5226 vixl32::Register temp2 = RegisterFrom(temp2_loc);
5227 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5228 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5229 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5230 vixl32::Label done;
5231 SlowPathCodeARMVIXL* slow_path = nullptr;
5232
5233 if (may_need_runtime_call_for_type_check) {
5234 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
5235 codegen_->AddSlowPath(slow_path);
5236 if (instruction->GetValueCanBeNull()) {
5237 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00005238 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005239 if (index.IsConstant()) {
5240 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005241 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005242 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
5243 } else {
5244 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005245 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005246 vixl32::Register temp = temps.Acquire();
5247 __ Add(temp, array, data_offset);
5248 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
5249 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00005250 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
5251 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005252 codegen_->MaybeRecordImplicitNullCheck(instruction);
5253 __ B(&done);
5254 __ Bind(&non_zero);
5255 }
5256
5257 // Note that when read barriers are enabled, the type checks
5258 // are performed without read barriers. This is fine, even in
5259 // the case where a class object is in the from-space after
5260 // the flip, as a comparison involving such a type would not
5261 // produce a false positive; it may of course produce a false
5262 // negative, in which case we would take the ArraySet slow
5263 // path.
5264
Alexandre Rames374ddf32016-11-04 10:40:49 +00005265 {
5266 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005267 ExactAssemblyScope aas(GetVIXLAssembler(),
5268 vixl32::kMaxInstructionSizeInBytes,
5269 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005270 // /* HeapReference<Class> */ temp1 = array->klass_
5271 __ ldr(temp1, MemOperand(array, class_offset));
5272 codegen_->MaybeRecordImplicitNullCheck(instruction);
5273 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005274 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5275
5276 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5277 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5278 // /* HeapReference<Class> */ temp2 = value->klass_
5279 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
5280 // If heap poisoning is enabled, no need to unpoison `temp1`
5281 // nor `temp2`, as we are comparing two poisoned references.
5282 __ Cmp(temp1, temp2);
5283
5284 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5285 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00005286 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005287 // If heap poisoning is enabled, the `temp1` reference has
5288 // not been unpoisoned yet; unpoison it now.
5289 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
5290
5291 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5292 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5293 // If heap poisoning is enabled, no need to unpoison
5294 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00005295 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005296 __ Bind(&do_put);
5297 } else {
5298 __ B(ne, slow_path->GetEntryLabel());
5299 }
5300 }
5301
5302 vixl32::Register source = value;
5303 if (kPoisonHeapReferences) {
5304 // Note that in the case where `value` is a null reference,
5305 // we do not enter this block, as a null reference does not
5306 // need poisoning.
5307 DCHECK_EQ(value_type, Primitive::kPrimNot);
5308 __ Mov(temp1, value);
5309 GetAssembler()->PoisonHeapReference(temp1);
5310 source = temp1;
5311 }
5312
5313 if (index.IsConstant()) {
5314 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005315 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005316 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
5317 } else {
5318 DCHECK(index.IsRegister()) << index;
5319
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005320 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005321 vixl32::Register temp = temps.Acquire();
5322 __ Add(temp, array, data_offset);
5323 codegen_->StoreToShiftedRegOffset(value_type,
5324 LocationFrom(source),
5325 temp,
5326 RegisterFrom(index));
5327 }
5328
5329 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005330 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5331 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005332 codegen_->MaybeRecordImplicitNullCheck(instruction);
5333 }
5334
5335 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5336
5337 if (done.IsReferenced()) {
5338 __ Bind(&done);
5339 }
5340
5341 if (slow_path != nullptr) {
5342 __ Bind(slow_path->GetExitLabel());
5343 }
5344
5345 break;
5346 }
5347
5348 case Primitive::kPrimLong: {
5349 Location value = locations->InAt(2);
5350 if (index.IsConstant()) {
5351 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00005352 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005353 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
5354 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005355 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005356 vixl32::Register temp = temps.Acquire();
5357 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5358 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
5359 }
5360 break;
5361 }
5362
5363 case Primitive::kPrimFloat: {
5364 Location value = locations->InAt(2);
5365 DCHECK(value.IsFpuRegister());
5366 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005367 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005368 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
5369 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005370 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005371 vixl32::Register temp = temps.Acquire();
5372 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
5373 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
5374 }
5375 break;
5376 }
5377
5378 case Primitive::kPrimDouble: {
5379 Location value = locations->InAt(2);
5380 DCHECK(value.IsFpuRegisterPair());
5381 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005382 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005383 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
5384 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005385 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005386 vixl32::Register temp = temps.Acquire();
5387 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
5388 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
5389 }
5390 break;
5391 }
5392
5393 case Primitive::kPrimVoid:
5394 LOG(FATAL) << "Unreachable type " << value_type;
5395 UNREACHABLE();
5396 }
5397
5398 // Objects are handled in the switch.
5399 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005400 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
5401 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 }
5404}
5405
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005406void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5407 LocationSummary* locations =
5408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5409 locations->SetInAt(0, Location::RequiresRegister());
5410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5411}
5412
5413void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
5414 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
5415 vixl32::Register obj = InputRegisterAt(instruction, 0);
5416 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005417 {
Artem Serov0fb37192016-12-06 18:13:40 +00005418 ExactAssemblyScope aas(GetVIXLAssembler(),
5419 vixl32::kMaxInstructionSizeInBytes,
5420 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005421 __ ldr(out, MemOperand(obj, offset));
5422 codegen_->MaybeRecordImplicitNullCheck(instruction);
5423 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005424 // Mask out compression flag from String's array length.
5425 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005426 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005427 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005428}
5429
Artem Serov2bbc9532016-10-21 11:51:50 +01005430void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005431 LocationSummary* locations =
5432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5433
5434 locations->SetInAt(0, Location::RequiresRegister());
5435 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5437}
5438
5439void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5440 vixl32::Register out = OutputRegister(instruction);
5441 vixl32::Register first = InputRegisterAt(instruction, 0);
5442 Location second = instruction->GetLocations()->InAt(1);
5443
Artem Serov2bbc9532016-10-21 11:51:50 +01005444 if (second.IsRegister()) {
5445 __ Add(out, first, RegisterFrom(second));
5446 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005447 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01005448 }
5449}
5450
Scott Wakelingc34dba72016-10-03 10:14:44 +01005451void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5452 RegisterSet caller_saves = RegisterSet::Empty();
5453 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5454 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
5455 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
5456 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
5457 locations->SetInAt(0, Location::RequiresRegister());
5458 locations->SetInAt(1, Location::RequiresRegister());
5459}
5460
5461void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
5462 SlowPathCodeARMVIXL* slow_path =
5463 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
5464 codegen_->AddSlowPath(slow_path);
5465
5466 vixl32::Register index = InputRegisterAt(instruction, 0);
5467 vixl32::Register length = InputRegisterAt(instruction, 1);
5468
5469 __ Cmp(index, length);
5470 __ B(hs, slow_path->GetEntryLabel());
5471}
5472
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005473void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
5474 vixl32::Register card,
5475 vixl32::Register object,
5476 vixl32::Register value,
5477 bool can_be_null) {
5478 vixl32::Label is_null;
5479 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005480 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005481 }
5482 GetAssembler()->LoadFromOffset(
5483 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00005484 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005485 __ Strb(card, MemOperand(card, temp));
5486 if (can_be_null) {
5487 __ Bind(&is_null);
5488 }
5489}
5490
Scott Wakelingfe885462016-09-22 10:24:38 +01005491void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5492 LOG(FATAL) << "Unreachable";
5493}
5494
5495void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
5496 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5497}
5498
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005499void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005500 LocationSummary* locations =
5501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005503}
5504
5505void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
5506 HBasicBlock* block = instruction->GetBlock();
5507 if (block->GetLoopInformation() != nullptr) {
5508 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5509 // The back edge will generate the suspend check.
5510 return;
5511 }
5512 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5513 // The goto will generate the suspend check.
5514 return;
5515 }
5516 GenerateSuspendCheck(instruction, nullptr);
5517}
5518
5519void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
5520 HBasicBlock* successor) {
5521 SuspendCheckSlowPathARMVIXL* slow_path =
5522 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
5523 if (slow_path == nullptr) {
5524 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
5525 instruction->SetSlowPath(slow_path);
5526 codegen_->AddSlowPath(slow_path);
5527 if (successor != nullptr) {
5528 DCHECK(successor->IsLoopHeader());
5529 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5530 }
5531 } else {
5532 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5533 }
5534
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005535 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005536 vixl32::Register temp = temps.Acquire();
5537 GetAssembler()->LoadFromOffset(
5538 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5539 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005540 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005541 __ Bind(slow_path->GetReturnLabel());
5542 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005543 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005544 __ B(slow_path->GetEntryLabel());
5545 }
5546}
5547
Scott Wakelingfe885462016-09-22 10:24:38 +01005548ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
5549 return codegen_->GetAssembler();
5550}
5551
5552void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005553 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01005554 MoveOperands* move = moves_[index];
5555 Location source = move->GetSource();
5556 Location destination = move->GetDestination();
5557
5558 if (source.IsRegister()) {
5559 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005560 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005561 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005562 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005563 } else {
5564 DCHECK(destination.IsStackSlot());
5565 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005566 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005567 sp,
5568 destination.GetStackIndex());
5569 }
5570 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005571 if (destination.IsRegister()) {
5572 GetAssembler()->LoadFromOffset(kLoadWord,
5573 RegisterFrom(destination),
5574 sp,
5575 source.GetStackIndex());
5576 } else if (destination.IsFpuRegister()) {
5577 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
5578 } else {
5579 DCHECK(destination.IsStackSlot());
5580 vixl32::Register temp = temps.Acquire();
5581 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
5582 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5583 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005584 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005585 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005586 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005587 } else if (destination.IsFpuRegister()) {
5588 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
5589 } else {
5590 DCHECK(destination.IsStackSlot());
5591 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
5592 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005593 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005594 if (destination.IsDoubleStackSlot()) {
5595 vixl32::DRegister temp = temps.AcquireD();
5596 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
5597 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
5598 } else if (destination.IsRegisterPair()) {
5599 DCHECK(ExpectedPairLayout(destination));
5600 GetAssembler()->LoadFromOffset(
5601 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
5602 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005603 DCHECK(destination.IsFpuRegisterPair()) << destination;
5604 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005605 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005606 } else if (source.IsRegisterPair()) {
5607 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005608 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
5609 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005610 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005611 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01005612 } else {
5613 DCHECK(destination.IsDoubleStackSlot()) << destination;
5614 DCHECK(ExpectedPairLayout(source));
5615 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005616 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01005617 sp,
5618 destination.GetStackIndex());
5619 }
5620 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005621 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005622 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01005623 } else if (destination.IsFpuRegisterPair()) {
5624 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
5625 } else {
5626 DCHECK(destination.IsDoubleStackSlot()) << destination;
5627 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
5628 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005629 } else {
5630 DCHECK(source.IsConstant()) << source;
5631 HConstant* constant = source.GetConstant();
5632 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5633 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5634 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005635 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005636 } else {
5637 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005638 vixl32::Register temp = temps.Acquire();
5639 __ Mov(temp, value);
5640 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5641 }
5642 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005643 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01005644 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005645 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
5646 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005647 } else {
5648 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01005649 vixl32::Register temp = temps.Acquire();
5650 __ Mov(temp, Low32Bits(value));
5651 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5652 __ Mov(temp, High32Bits(value));
5653 GetAssembler()->StoreToOffset(kStoreWord,
5654 temp,
5655 sp,
5656 destination.GetHighStackIndex(kArmWordSize));
5657 }
5658 } else if (constant->IsDoubleConstant()) {
5659 double value = constant->AsDoubleConstant()->GetValue();
5660 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005661 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005662 } else {
5663 DCHECK(destination.IsDoubleStackSlot()) << destination;
5664 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005665 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005667 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005668 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005669 GetAssembler()->StoreToOffset(kStoreWord,
5670 temp,
5671 sp,
5672 destination.GetHighStackIndex(kArmWordSize));
5673 }
5674 } else {
5675 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5676 float value = constant->AsFloatConstant()->GetValue();
5677 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005678 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01005679 } else {
5680 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01005681 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005682 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005683 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
5684 }
5685 }
5686 }
5687}
5688
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005689void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
5690 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5691 vixl32::Register temp = temps.Acquire();
5692 __ Mov(temp, reg);
5693 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
5694 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01005695}
5696
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005697void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
5698 // TODO(VIXL32): Double check the performance of this implementation.
5699 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Artem Serov4593f7d2016-12-29 16:21:49 +00005700 vixl32::SRegister temp_1 = temps.AcquireS();
5701 vixl32::SRegister temp_2 = temps.AcquireS();
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005702
Artem Serov4593f7d2016-12-29 16:21:49 +00005703 __ Vldr(temp_1, MemOperand(sp, mem1));
5704 __ Vldr(temp_2, MemOperand(sp, mem2));
5705 __ Vstr(temp_1, MemOperand(sp, mem2));
5706 __ Vstr(temp_2, MemOperand(sp, mem1));
Scott Wakelingfe885462016-09-22 10:24:38 +01005707}
5708
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005709void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
5710 MoveOperands* move = moves_[index];
5711 Location source = move->GetSource();
5712 Location destination = move->GetDestination();
5713 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
5714
5715 if (source.IsRegister() && destination.IsRegister()) {
5716 vixl32::Register temp = temps.Acquire();
5717 DCHECK(!RegisterFrom(source).Is(temp));
5718 DCHECK(!RegisterFrom(destination).Is(temp));
5719 __ Mov(temp, RegisterFrom(destination));
5720 __ Mov(RegisterFrom(destination), RegisterFrom(source));
5721 __ Mov(RegisterFrom(source), temp);
5722 } else if (source.IsRegister() && destination.IsStackSlot()) {
5723 Exchange(RegisterFrom(source), destination.GetStackIndex());
5724 } else if (source.IsStackSlot() && destination.IsRegister()) {
5725 Exchange(RegisterFrom(destination), source.GetStackIndex());
5726 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005727 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005728 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005729 vixl32::SRegister temp = temps.AcquireS();
5730 __ Vmov(temp, SRegisterFrom(source));
5731 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
5732 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005733 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5734 vixl32::DRegister temp = temps.AcquireD();
5735 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
5736 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
5737 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
5738 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
5739 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5740 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
5741 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
5742 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5743 vixl32::DRegister temp = temps.AcquireD();
5744 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
5745 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
5746 GetAssembler()->StoreDToOffset(temp, sp, mem);
5747 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005748 vixl32::DRegister first = DRegisterFrom(source);
5749 vixl32::DRegister second = DRegisterFrom(destination);
5750 vixl32::DRegister temp = temps.AcquireD();
5751 __ Vmov(temp, first);
5752 __ Vmov(first, second);
5753 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005754 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005755 vixl32::DRegister reg = source.IsFpuRegisterPair()
5756 ? DRegisterFrom(source)
5757 : DRegisterFrom(destination);
5758 int mem = source.IsFpuRegisterPair()
5759 ? destination.GetStackIndex()
5760 : source.GetStackIndex();
5761 vixl32::DRegister temp = temps.AcquireD();
5762 __ Vmov(temp, reg);
5763 GetAssembler()->LoadDFromOffset(reg, sp, mem);
5764 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005765 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00005766 vixl32::SRegister reg = source.IsFpuRegister()
5767 ? SRegisterFrom(source)
5768 : SRegisterFrom(destination);
5769 int mem = source.IsFpuRegister()
5770 ? destination.GetStackIndex()
5771 : source.GetStackIndex();
5772 vixl32::Register temp = temps.Acquire();
5773 __ Vmov(temp, reg);
5774 GetAssembler()->LoadSFromOffset(reg, sp, mem);
5775 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01005776 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5777 vixl32::DRegister temp1 = temps.AcquireD();
5778 vixl32::DRegister temp2 = temps.AcquireD();
5779 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
5780 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
5781 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
5782 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
5783 } else {
5784 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
5785 }
Scott Wakelingfe885462016-09-22 10:24:38 +01005786}
5787
5788void ParallelMoveResolverARMVIXL::SpillScratch(int reg ATTRIBUTE_UNUSED) {
5789 TODO_VIXL32(FATAL);
5790}
5791
5792void ParallelMoveResolverARMVIXL::RestoreScratch(int reg ATTRIBUTE_UNUSED) {
5793 TODO_VIXL32(FATAL);
5794}
5795
Artem Serov02d37832016-10-25 15:25:33 +01005796HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005797 HLoadClass::LoadKind desired_class_load_kind) {
5798 switch (desired_class_load_kind) {
5799 case HLoadClass::LoadKind::kReferrersClass:
5800 break;
5801 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005802 DCHECK(!GetCompilerOptions().GetCompilePic());
5803 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005804 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5805 DCHECK(GetCompilerOptions().GetCompilePic());
5806 break;
5807 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005808 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005809 case HLoadClass::LoadKind::kBssEntry:
5810 DCHECK(!Runtime::Current()->UseJitCompilation());
5811 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005812 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005813 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005814 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005815 case HLoadClass::LoadKind::kDexCacheViaMethod:
5816 break;
5817 }
5818 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005819}
5820
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005821void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005822 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5823 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005824 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005825 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005826 cls,
5827 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005828 LocationFrom(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 return;
5830 }
Vladimir Marko41559982017-01-06 14:04:23 +00005831 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01005832
Artem Serovd4cc5b22016-11-04 11:19:09 +00005833 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5834 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005835 ? LocationSummary::kCallOnSlowPath
5836 : LocationSummary::kNoCall;
5837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005838 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005839 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00005840 }
5841
Vladimir Marko41559982017-01-06 14:04:23 +00005842 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005843 locations->SetInAt(0, Location::RequiresRegister());
5844 }
5845 locations->SetOut(Location::RequiresRegister());
5846}
5847
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005848// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5849// move.
5850void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005851 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5852 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5853 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005854 return;
5855 }
Vladimir Marko41559982017-01-06 14:04:23 +00005856 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005857
Vladimir Marko41559982017-01-06 14:04:23 +00005858 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005859 Location out_loc = locations->Out();
5860 vixl32::Register out = OutputRegister(cls);
5861
Artem Serovd4cc5b22016-11-04 11:19:09 +00005862 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5863 ? kWithoutReadBarrier
5864 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005865 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005866 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005867 case HLoadClass::LoadKind::kReferrersClass: {
5868 DCHECK(!cls->CanCallRuntime());
5869 DCHECK(!cls->MustGenerateClinitCheck());
5870 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5871 vixl32::Register current_method = InputRegisterAt(cls, 0);
5872 GenerateGcRootFieldLoad(cls,
5873 out_loc,
5874 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01005875 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00005876 read_barrier_option);
5877 break;
5878 }
5879 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005880 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00005881 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5882 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5883 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005884 break;
5885 }
5886 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005887 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00005888 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5889 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
5890 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5891 codegen_->EmitMovwMovtPlaceholder(labels, out);
5892 break;
5893 }
5894 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005895 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005896 uint32_t address = dchecked_integral_cast<uint32_t>(
5897 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5898 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00005899 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00005900 break;
5901 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005902 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005903 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005904 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005905 codegen_->EmitMovwMovtPlaceholder(labels, out);
5906 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5907 generate_null_check = true;
5908 break;
5909 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005910 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00005911 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5912 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005913 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00005914 // /* GcRoot<mirror::Class> */ out = *out
5915 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00005916 break;
5917 }
Vladimir Marko41559982017-01-06 14:04:23 +00005918 case HLoadClass::LoadKind::kDexCacheViaMethod:
5919 LOG(FATAL) << "UNREACHABLE";
5920 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005921 }
5922
5923 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5924 DCHECK(cls->CanCallRuntime());
5925 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
5926 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5927 codegen_->AddSlowPath(slow_path);
5928 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005929 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005930 }
5931 if (cls->MustGenerateClinitCheck()) {
5932 GenerateClassInitializationCheck(slow_path, out);
5933 } else {
5934 __ Bind(slow_path->GetExitLabel());
5935 }
5936 }
5937}
5938
Artem Serov02d37832016-10-25 15:25:33 +01005939void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5940 LocationSummary* locations =
5941 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5942 locations->SetInAt(0, Location::RequiresRegister());
5943 if (check->HasUses()) {
5944 locations->SetOut(Location::SameAsFirstInput());
5945 }
5946}
5947
5948void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
5949 // We assume the class is not null.
5950 LoadClassSlowPathARMVIXL* slow_path =
5951 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
5952 check,
5953 check->GetDexPc(),
5954 /* do_clinit */ true);
5955 codegen_->AddSlowPath(slow_path);
5956 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
5957}
5958
5959void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
5960 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
5961 UseScratchRegisterScope temps(GetVIXLAssembler());
5962 vixl32::Register temp = temps.Acquire();
5963 GetAssembler()->LoadFromOffset(kLoadWord,
5964 temp,
5965 class_reg,
5966 mirror::Class::StatusOffset().Int32Value());
5967 __ Cmp(temp, mirror::Class::kStatusInitialized);
5968 __ B(lt, slow_path->GetEntryLabel());
5969 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5970 // properly. Therefore, we do a memory fence.
5971 __ Dmb(ISH);
5972 __ Bind(slow_path->GetExitLabel());
5973}
5974
Artem Serov02d37832016-10-25 15:25:33 +01005975HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00005976 HLoadString::LoadKind desired_string_load_kind) {
5977 switch (desired_string_load_kind) {
5978 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005979 DCHECK(!GetCompilerOptions().GetCompilePic());
5980 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005981 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5982 DCHECK(GetCompilerOptions().GetCompilePic());
5983 break;
5984 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00005985 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005986 case HLoadString::LoadKind::kBssEntry:
5987 DCHECK(!Runtime::Current()->UseJitCompilation());
5988 break;
5989 case HLoadString::LoadKind::kJitTableAddress:
5990 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00005991 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00005992 case HLoadString::LoadKind::kDexCacheViaMethod:
5993 break;
5994 }
5995 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01005996}
5997
5998void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00005999 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01006000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01006001 HLoadString::LoadKind load_kind = load->GetLoadKind();
6002 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01006003 locations->SetOut(LocationFrom(r0));
6004 } else {
6005 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006006 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6007 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6008 // Rely on the pResolveString and/or marking to save everything, including temps.
6009 // Note that IP may theoretically be clobbered by saving/restoring the live register
6010 // (only one thanks to the custom calling convention), so we request a different temp.
6011 locations->AddTemp(Location::RequiresRegister());
6012 RegisterSet caller_saves = RegisterSet::Empty();
6013 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6014 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6015 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6016 // that the the kPrimNot result register is the same as the first argument register.
6017 locations->SetCustomSlowPathCallerSaves(caller_saves);
6018 } else {
6019 // For non-Baker read barrier we have a temp-clobbering call.
6020 }
6021 }
Artem Serov02d37832016-10-25 15:25:33 +01006022 }
6023}
6024
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006025// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6026// move.
6027void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00006028 LocationSummary* locations = load->GetLocations();
6029 Location out_loc = locations->Out();
6030 vixl32::Register out = OutputRegister(load);
6031 HLoadString::LoadKind load_kind = load->GetLoadKind();
6032
6033 switch (load_kind) {
6034 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006035 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6036 load->GetStringIndex()));
6037 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006038 }
6039 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6040 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
6041 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006042 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006043 codegen_->EmitMovwMovtPlaceholder(labels, out);
6044 return; // No dex cache slow path.
6045 }
6046 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006047 uint32_t address = dchecked_integral_cast<uint32_t>(
6048 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6049 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006050 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6051 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006052 }
6053 case HLoadString::LoadKind::kBssEntry: {
6054 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6055 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
6056 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006057 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006058 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6059 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
6060 LoadStringSlowPathARMVIXL* slow_path =
6061 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
6062 codegen_->AddSlowPath(slow_path);
6063 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6064 __ Bind(slow_path->GetExitLabel());
6065 return;
6066 }
6067 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006068 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006069 load->GetStringIndex(),
6070 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006071 // /* GcRoot<mirror::String> */ out = *out
6072 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6073 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006074 }
6075 default:
6076 break;
6077 }
Artem Serov02d37832016-10-25 15:25:33 +01006078
6079 // TODO: Re-add the compiler code to do string dex cache lookup again.
6080 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
6081 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006082 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01006083 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6084 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
6085}
6086
6087static int32_t GetExceptionTlsOffset() {
6088 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
6089}
6090
6091void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
6092 LocationSummary* locations =
6093 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6094 locations->SetOut(Location::RequiresRegister());
6095}
6096
6097void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
6098 vixl32::Register out = OutputRegister(load);
6099 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
6100}
6101
6102
6103void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
6104 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6105}
6106
6107void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6108 UseScratchRegisterScope temps(GetVIXLAssembler());
6109 vixl32::Register temp = temps.Acquire();
6110 __ Mov(temp, 0);
6111 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
6112}
6113
6114void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
6115 LocationSummary* locations =
6116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6117 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6118 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6119}
6120
6121void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
6122 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
6123 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6124}
6125
Artem Serov657022c2016-11-23 14:19:38 +00006126// Temp is used for read barrier.
6127static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6128 if (kEmitCompilerReadBarrier &&
6129 (kUseBakerReadBarrier ||
6130 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6131 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6132 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6133 return 1;
6134 }
6135 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006136}
6137
Artem Serov657022c2016-11-23 14:19:38 +00006138// Interface case has 3 temps, one for holding the number of interfaces, one for the current
6139// interface pointer, one for loading the current interface.
6140// The other checks have one temp for loading the object's class.
6141static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6142 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6143 return 3;
6144 }
6145 return 1 + NumberOfInstanceOfTemps(type_check_kind);
6146}
Artem Serovcfbe9132016-10-14 15:58:56 +01006147
6148void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6149 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6150 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6151 bool baker_read_barrier_slow_path = false;
6152 switch (type_check_kind) {
6153 case TypeCheckKind::kExactCheck:
6154 case TypeCheckKind::kAbstractClassCheck:
6155 case TypeCheckKind::kClassHierarchyCheck:
6156 case TypeCheckKind::kArrayObjectCheck:
6157 call_kind =
6158 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6159 baker_read_barrier_slow_path = kUseBakerReadBarrier;
6160 break;
6161 case TypeCheckKind::kArrayCheck:
6162 case TypeCheckKind::kUnresolvedCheck:
6163 case TypeCheckKind::kInterfaceCheck:
6164 call_kind = LocationSummary::kCallOnSlowPath;
6165 break;
6166 }
6167
6168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6169 if (baker_read_barrier_slow_path) {
6170 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6171 }
6172 locations->SetInAt(0, Location::RequiresRegister());
6173 locations->SetInAt(1, Location::RequiresRegister());
6174 // The "out" register is used as a temporary, so it overlaps with the inputs.
6175 // Note that TypeCheckSlowPathARM uses this register too.
6176 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00006177 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01006178}
6179
6180void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
6181 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6182 LocationSummary* locations = instruction->GetLocations();
6183 Location obj_loc = locations->InAt(0);
6184 vixl32::Register obj = InputRegisterAt(instruction, 0);
6185 vixl32::Register cls = InputRegisterAt(instruction, 1);
6186 Location out_loc = locations->Out();
6187 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00006188 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6189 DCHECK_LE(num_temps, 1u);
6190 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01006191 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6192 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6193 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6194 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6195 vixl32::Label done, zero;
6196 SlowPathCodeARMVIXL* slow_path = nullptr;
6197
6198 // Return 0 if `obj` is null.
6199 // avoid null check if we know obj is not null.
6200 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006201 __ CompareAndBranchIfZero(obj, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006202 }
6203
Artem Serovcfbe9132016-10-14 15:58:56 +01006204 switch (type_check_kind) {
6205 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006206 // /* HeapReference<Class> */ out = obj->klass_
6207 GenerateReferenceLoadTwoRegisters(instruction,
6208 out_loc,
6209 obj_loc,
6210 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006211 maybe_temp_loc,
6212 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006213 __ Cmp(out, cls);
6214 // Classes must be equal for the instanceof to succeed.
Artem Serov517d9f62016-12-12 15:51:15 +00006215 __ B(ne, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006216 __ Mov(out, 1);
6217 __ B(&done);
6218 break;
6219 }
6220
6221 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006222 // /* HeapReference<Class> */ out = obj->klass_
6223 GenerateReferenceLoadTwoRegisters(instruction,
6224 out_loc,
6225 obj_loc,
6226 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006227 maybe_temp_loc,
6228 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006229 // If the class is abstract, we eagerly fetch the super class of the
6230 // object to avoid doing a comparison we know will fail.
6231 vixl32::Label loop;
6232 __ Bind(&loop);
6233 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006234 GenerateReferenceLoadOneRegister(instruction,
6235 out_loc,
6236 super_offset,
6237 maybe_temp_loc,
6238 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006239 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006240 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006241 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006242 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006243 __ Mov(out, 1);
6244 if (zero.IsReferenced()) {
6245 __ B(&done);
6246 }
6247 break;
6248 }
6249
6250 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006251 // /* HeapReference<Class> */ out = obj->klass_
6252 GenerateReferenceLoadTwoRegisters(instruction,
6253 out_loc,
6254 obj_loc,
6255 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006256 maybe_temp_loc,
6257 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006258 // Walk over the class hierarchy to find a match.
6259 vixl32::Label loop, success;
6260 __ Bind(&loop);
6261 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006262 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006263 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006264 GenerateReferenceLoadOneRegister(instruction,
6265 out_loc,
6266 super_offset,
6267 maybe_temp_loc,
6268 kCompilerReadBarrierOption);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006269 __ CompareAndBranchIfNonZero(out, &loop);
Artem Serovcfbe9132016-10-14 15:58:56 +01006270 // If `out` is null, we use it for the result, and jump to `done`.
6271 __ B(&done);
6272 __ Bind(&success);
6273 __ Mov(out, 1);
6274 if (zero.IsReferenced()) {
6275 __ B(&done);
6276 }
6277 break;
6278 }
6279
6280 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08006281 // /* HeapReference<Class> */ out = obj->klass_
6282 GenerateReferenceLoadTwoRegisters(instruction,
6283 out_loc,
6284 obj_loc,
6285 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006286 maybe_temp_loc,
6287 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006288 // Do an exact check.
6289 vixl32::Label exact_check;
6290 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006291 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006292 // Otherwise, we need to check that the object's class is a non-primitive array.
6293 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006294 GenerateReferenceLoadOneRegister(instruction,
6295 out_loc,
6296 component_offset,
6297 maybe_temp_loc,
6298 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01006299 // If `out` is null, we use it for the result, and jump to `done`.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006300 __ CompareAndBranchIfZero(out, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006301 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6302 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006303 __ CompareAndBranchIfNonZero(out, &zero, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006304 __ Bind(&exact_check);
6305 __ Mov(out, 1);
6306 __ B(&done);
6307 break;
6308 }
6309
6310 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006311 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08006312 // /* HeapReference<Class> */ out = obj->klass_
6313 GenerateReferenceLoadTwoRegisters(instruction,
6314 out_loc,
6315 obj_loc,
6316 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00006317 maybe_temp_loc,
6318 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006319 __ Cmp(out, cls);
6320 DCHECK(locations->OnlyCallsOnSlowPath());
6321 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6322 /* is_fatal */ false);
6323 codegen_->AddSlowPath(slow_path);
6324 __ B(ne, slow_path->GetEntryLabel());
6325 __ Mov(out, 1);
6326 if (zero.IsReferenced()) {
6327 __ B(&done);
6328 }
6329 break;
6330 }
6331
6332 case TypeCheckKind::kUnresolvedCheck:
6333 case TypeCheckKind::kInterfaceCheck: {
6334 // Note that we indeed only call on slow path, but we always go
6335 // into the slow path for the unresolved and interface check
6336 // cases.
6337 //
6338 // We cannot directly call the InstanceofNonTrivial runtime
6339 // entry point without resorting to a type checking slow path
6340 // here (i.e. by calling InvokeRuntime directly), as it would
6341 // require to assign fixed registers for the inputs of this
6342 // HInstanceOf instruction (following the runtime calling
6343 // convention), which might be cluttered by the potential first
6344 // read barrier emission at the beginning of this method.
6345 //
6346 // TODO: Introduce a new runtime entry point taking the object
6347 // to test (instead of its class) as argument, and let it deal
6348 // with the read barrier issues. This will let us refactor this
6349 // case of the `switch` code as it was previously (with a direct
6350 // call to the runtime not using a type checking slow path).
6351 // This should also be beneficial for the other cases above.
6352 DCHECK(locations->OnlyCallsOnSlowPath());
6353 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6354 /* is_fatal */ false);
6355 codegen_->AddSlowPath(slow_path);
6356 __ B(slow_path->GetEntryLabel());
6357 if (zero.IsReferenced()) {
6358 __ B(&done);
6359 }
6360 break;
6361 }
6362 }
6363
6364 if (zero.IsReferenced()) {
6365 __ Bind(&zero);
6366 __ Mov(out, 0);
6367 }
6368
6369 if (done.IsReferenced()) {
6370 __ Bind(&done);
6371 }
6372
6373 if (slow_path != nullptr) {
6374 __ Bind(slow_path->GetExitLabel());
6375 }
6376}
6377
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006378void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6379 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6380 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6381
6382 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6383 switch (type_check_kind) {
6384 case TypeCheckKind::kExactCheck:
6385 case TypeCheckKind::kAbstractClassCheck:
6386 case TypeCheckKind::kClassHierarchyCheck:
6387 case TypeCheckKind::kArrayObjectCheck:
6388 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6389 LocationSummary::kCallOnSlowPath :
6390 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
6391 break;
6392 case TypeCheckKind::kArrayCheck:
6393 case TypeCheckKind::kUnresolvedCheck:
6394 case TypeCheckKind::kInterfaceCheck:
6395 call_kind = LocationSummary::kCallOnSlowPath;
6396 break;
6397 }
6398
6399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6400 locations->SetInAt(0, Location::RequiresRegister());
6401 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00006402 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006403}
6404
6405void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
6406 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6407 LocationSummary* locations = instruction->GetLocations();
6408 Location obj_loc = locations->InAt(0);
6409 vixl32::Register obj = InputRegisterAt(instruction, 0);
6410 vixl32::Register cls = InputRegisterAt(instruction, 1);
6411 Location temp_loc = locations->GetTemp(0);
6412 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00006413 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6414 DCHECK_LE(num_temps, 3u);
6415 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6416 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6417 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6418 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6419 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6420 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6421 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6422 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6423 const uint32_t object_array_data_offset =
6424 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006425
Artem Serov657022c2016-11-23 14:19:38 +00006426 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6427 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6428 // read barriers is done for performance and code size reasons.
6429 bool is_type_check_slow_path_fatal = false;
6430 if (!kEmitCompilerReadBarrier) {
6431 is_type_check_slow_path_fatal =
6432 (type_check_kind == TypeCheckKind::kExactCheck ||
6433 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6434 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6435 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6436 !instruction->CanThrowIntoCatchBlock();
6437 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006438 SlowPathCodeARMVIXL* type_check_slow_path =
6439 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
6440 is_type_check_slow_path_fatal);
6441 codegen_->AddSlowPath(type_check_slow_path);
6442
6443 vixl32::Label done;
6444 // Avoid null check if we know obj is not null.
6445 if (instruction->MustDoNullCheck()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006446 __ CompareAndBranchIfZero(obj, &done, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006447 }
6448
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006449 switch (type_check_kind) {
6450 case TypeCheckKind::kExactCheck:
6451 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006452 // /* HeapReference<Class> */ temp = obj->klass_
6453 GenerateReferenceLoadTwoRegisters(instruction,
6454 temp_loc,
6455 obj_loc,
6456 class_offset,
6457 maybe_temp2_loc,
6458 kWithoutReadBarrier);
6459
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006460 __ Cmp(temp, cls);
6461 // Jump to slow path for throwing the exception or doing a
6462 // more involved array check.
6463 __ B(ne, type_check_slow_path->GetEntryLabel());
6464 break;
6465 }
6466
6467 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006468 // /* HeapReference<Class> */ temp = obj->klass_
6469 GenerateReferenceLoadTwoRegisters(instruction,
6470 temp_loc,
6471 obj_loc,
6472 class_offset,
6473 maybe_temp2_loc,
6474 kWithoutReadBarrier);
6475
Artem Serovcfbe9132016-10-14 15:58:56 +01006476 // If the class is abstract, we eagerly fetch the super class of the
6477 // object to avoid doing a comparison we know will fail.
6478 vixl32::Label loop;
6479 __ Bind(&loop);
6480 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006481 GenerateReferenceLoadOneRegister(instruction,
6482 temp_loc,
6483 super_offset,
6484 maybe_temp2_loc,
6485 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006486
6487 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6488 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006489 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006490
6491 // Otherwise, compare the classes.
6492 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006493 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006494 break;
6495 }
6496
6497 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006498 // /* HeapReference<Class> */ temp = obj->klass_
6499 GenerateReferenceLoadTwoRegisters(instruction,
6500 temp_loc,
6501 obj_loc,
6502 class_offset,
6503 maybe_temp2_loc,
6504 kWithoutReadBarrier);
6505
Artem Serovcfbe9132016-10-14 15:58:56 +01006506 // Walk over the class hierarchy to find a match.
6507 vixl32::Label loop;
6508 __ Bind(&loop);
6509 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006510 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006511
6512 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00006513 GenerateReferenceLoadOneRegister(instruction,
6514 temp_loc,
6515 super_offset,
6516 maybe_temp2_loc,
6517 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006518
6519 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6520 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006521 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006522 // Otherwise, jump to the beginning of the loop.
6523 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006524 break;
6525 }
6526
Artem Serovcfbe9132016-10-14 15:58:56 +01006527 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00006528 // /* HeapReference<Class> */ temp = obj->klass_
6529 GenerateReferenceLoadTwoRegisters(instruction,
6530 temp_loc,
6531 obj_loc,
6532 class_offset,
6533 maybe_temp2_loc,
6534 kWithoutReadBarrier);
6535
Artem Serovcfbe9132016-10-14 15:58:56 +01006536 // Do an exact check.
6537 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00006538 __ B(eq, &done, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01006539
6540 // Otherwise, we need to check that the object's class is a non-primitive array.
6541 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00006542 GenerateReferenceLoadOneRegister(instruction,
6543 temp_loc,
6544 component_offset,
6545 maybe_temp2_loc,
6546 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01006547 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006548 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01006549 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6550 // to further check that this component type is not a primitive type.
6551 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6552 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00006553 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006554 break;
6555 }
6556
6557 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00006558 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01006559 // We cannot directly call the CheckCast runtime entry point
6560 // without resorting to a type checking slow path here (i.e. by
6561 // calling InvokeRuntime directly), as it would require to
6562 // assign fixed registers for the inputs of this HInstanceOf
6563 // instruction (following the runtime calling convention), which
6564 // might be cluttered by the potential first read barrier
6565 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00006566
Artem Serovcfbe9132016-10-14 15:58:56 +01006567 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006568 break;
Artem Serov657022c2016-11-23 14:19:38 +00006569
6570 case TypeCheckKind::kInterfaceCheck: {
6571 // Avoid read barriers to improve performance of the fast path. We can not get false
6572 // positives by doing this.
6573 // /* HeapReference<Class> */ temp = obj->klass_
6574 GenerateReferenceLoadTwoRegisters(instruction,
6575 temp_loc,
6576 obj_loc,
6577 class_offset,
6578 maybe_temp2_loc,
6579 kWithoutReadBarrier);
6580
6581 // /* HeapReference<Class> */ temp = temp->iftable_
6582 GenerateReferenceLoadTwoRegisters(instruction,
6583 temp_loc,
6584 temp_loc,
6585 iftable_offset,
6586 maybe_temp2_loc,
6587 kWithoutReadBarrier);
6588 // Iftable is never null.
6589 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
6590 // Loop through the iftable and check if any class matches.
6591 vixl32::Label start_loop;
6592 __ Bind(&start_loop);
6593 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
6594 type_check_slow_path->GetEntryLabel());
6595 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
6596 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
6597 // Go to next interface.
6598 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
6599 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
6600 // Compare the classes and continue the loop if they do not match.
6601 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00006602 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00006603 break;
6604 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006605 }
6606 __ Bind(&done);
6607
6608 __ Bind(type_check_slow_path->GetExitLabel());
6609}
6610
Artem Serov551b28f2016-10-18 19:11:30 +01006611void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6612 LocationSummary* locations =
6613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6614 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6615 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6616}
6617
6618void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
6619 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6620 instruction,
6621 instruction->GetDexPc());
6622 if (instruction->IsEnter()) {
6623 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6624 } else {
6625 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6626 }
6627}
6628
Artem Serov02109dd2016-09-23 17:17:54 +01006629void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
6630 HandleBitwiseOperation(instruction, AND);
6631}
6632
6633void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
6634 HandleBitwiseOperation(instruction, ORR);
6635}
6636
6637void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
6638 HandleBitwiseOperation(instruction, EOR);
6639}
6640
6641void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6642 LocationSummary* locations =
6643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6644 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6645 || instruction->GetResultType() == Primitive::kPrimLong);
6646 // Note: GVN reorders commutative operations to have the constant on the right hand side.
6647 locations->SetInAt(0, Location::RequiresRegister());
6648 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6650}
6651
6652void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
6653 HandleBitwiseOperation(instruction);
6654}
6655
6656void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
6657 HandleBitwiseOperation(instruction);
6658}
6659
6660void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
6661 HandleBitwiseOperation(instruction);
6662}
6663
Artem Serov2bbc9532016-10-21 11:51:50 +01006664void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6665 LocationSummary* locations =
6666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6667 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6668 || instruction->GetResultType() == Primitive::kPrimLong);
6669
6670 locations->SetInAt(0, Location::RequiresRegister());
6671 locations->SetInAt(1, Location::RequiresRegister());
6672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6673}
6674
6675void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6676 LocationSummary* locations = instruction->GetLocations();
6677 Location first = locations->InAt(0);
6678 Location second = locations->InAt(1);
6679 Location out = locations->Out();
6680
6681 if (instruction->GetResultType() == Primitive::kPrimInt) {
6682 vixl32::Register first_reg = RegisterFrom(first);
6683 vixl32::Register second_reg = RegisterFrom(second);
6684 vixl32::Register out_reg = RegisterFrom(out);
6685
6686 switch (instruction->GetOpKind()) {
6687 case HInstruction::kAnd:
6688 __ Bic(out_reg, first_reg, second_reg);
6689 break;
6690 case HInstruction::kOr:
6691 __ Orn(out_reg, first_reg, second_reg);
6692 break;
6693 // There is no EON on arm.
6694 case HInstruction::kXor:
6695 default:
6696 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6697 UNREACHABLE();
6698 }
6699 return;
6700
6701 } else {
6702 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6703 vixl32::Register first_low = LowRegisterFrom(first);
6704 vixl32::Register first_high = HighRegisterFrom(first);
6705 vixl32::Register second_low = LowRegisterFrom(second);
6706 vixl32::Register second_high = HighRegisterFrom(second);
6707 vixl32::Register out_low = LowRegisterFrom(out);
6708 vixl32::Register out_high = HighRegisterFrom(out);
6709
6710 switch (instruction->GetOpKind()) {
6711 case HInstruction::kAnd:
6712 __ Bic(out_low, first_low, second_low);
6713 __ Bic(out_high, first_high, second_high);
6714 break;
6715 case HInstruction::kOr:
6716 __ Orn(out_low, first_low, second_low);
6717 __ Orn(out_high, first_high, second_high);
6718 break;
6719 // There is no EON on arm.
6720 case HInstruction::kXor:
6721 default:
6722 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6723 UNREACHABLE();
6724 }
6725 }
6726}
6727
Artem Serov02109dd2016-09-23 17:17:54 +01006728// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6729void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
6730 vixl32::Register first,
6731 uint32_t value) {
6732 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6733 if (value == 0xffffffffu) {
6734 if (!out.Is(first)) {
6735 __ Mov(out, first);
6736 }
6737 return;
6738 }
6739 if (value == 0u) {
6740 __ Mov(out, 0);
6741 return;
6742 }
6743 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
6744 __ And(out, first, value);
6745 } else {
6746 DCHECK(GetAssembler()->ShifterOperandCanHold(BIC, ~value));
6747 __ Bic(out, first, ~value);
6748 }
6749}
6750
6751// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6752void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
6753 vixl32::Register first,
6754 uint32_t value) {
6755 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6756 if (value == 0u) {
6757 if (!out.Is(first)) {
6758 __ Mov(out, first);
6759 }
6760 return;
6761 }
6762 if (value == 0xffffffffu) {
6763 __ Mvn(out, 0);
6764 return;
6765 }
6766 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
6767 __ Orr(out, first, value);
6768 } else {
6769 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
6770 __ Orn(out, first, ~value);
6771 }
6772}
6773
6774// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
6775void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
6776 vixl32::Register first,
6777 uint32_t value) {
6778 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6779 if (value == 0u) {
6780 if (!out.Is(first)) {
6781 __ Mov(out, first);
6782 }
6783 return;
6784 }
6785 __ Eor(out, first, value);
6786}
6787
Anton Kirilovdda43962016-11-21 19:55:20 +00006788void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
6789 Location first,
6790 uint64_t value) {
6791 vixl32::Register out_low = LowRegisterFrom(out);
6792 vixl32::Register out_high = HighRegisterFrom(out);
6793 vixl32::Register first_low = LowRegisterFrom(first);
6794 vixl32::Register first_high = HighRegisterFrom(first);
6795 uint32_t value_low = Low32Bits(value);
6796 uint32_t value_high = High32Bits(value);
6797 if (value_low == 0u) {
6798 if (!out_low.Is(first_low)) {
6799 __ Mov(out_low, first_low);
6800 }
6801 __ Add(out_high, first_high, value_high);
6802 return;
6803 }
6804 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006805 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006806 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00006807 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006808 __ Sbc(out_high, first_high, ~value_high);
6809 } else {
6810 LOG(FATAL) << "Unexpected constant " << value_high;
6811 UNREACHABLE();
6812 }
6813}
6814
Artem Serov02109dd2016-09-23 17:17:54 +01006815void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
6816 LocationSummary* locations = instruction->GetLocations();
6817 Location first = locations->InAt(0);
6818 Location second = locations->InAt(1);
6819 Location out = locations->Out();
6820
6821 if (second.IsConstant()) {
6822 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6823 uint32_t value_low = Low32Bits(value);
6824 if (instruction->GetResultType() == Primitive::kPrimInt) {
6825 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6826 vixl32::Register out_reg = OutputRegister(instruction);
6827 if (instruction->IsAnd()) {
6828 GenerateAndConst(out_reg, first_reg, value_low);
6829 } else if (instruction->IsOr()) {
6830 GenerateOrrConst(out_reg, first_reg, value_low);
6831 } else {
6832 DCHECK(instruction->IsXor());
6833 GenerateEorConst(out_reg, first_reg, value_low);
6834 }
6835 } else {
6836 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6837 uint32_t value_high = High32Bits(value);
6838 vixl32::Register first_low = LowRegisterFrom(first);
6839 vixl32::Register first_high = HighRegisterFrom(first);
6840 vixl32::Register out_low = LowRegisterFrom(out);
6841 vixl32::Register out_high = HighRegisterFrom(out);
6842 if (instruction->IsAnd()) {
6843 GenerateAndConst(out_low, first_low, value_low);
6844 GenerateAndConst(out_high, first_high, value_high);
6845 } else if (instruction->IsOr()) {
6846 GenerateOrrConst(out_low, first_low, value_low);
6847 GenerateOrrConst(out_high, first_high, value_high);
6848 } else {
6849 DCHECK(instruction->IsXor());
6850 GenerateEorConst(out_low, first_low, value_low);
6851 GenerateEorConst(out_high, first_high, value_high);
6852 }
6853 }
6854 return;
6855 }
6856
6857 if (instruction->GetResultType() == Primitive::kPrimInt) {
6858 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
6859 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
6860 vixl32::Register out_reg = OutputRegister(instruction);
6861 if (instruction->IsAnd()) {
6862 __ And(out_reg, first_reg, second_reg);
6863 } else if (instruction->IsOr()) {
6864 __ Orr(out_reg, first_reg, second_reg);
6865 } else {
6866 DCHECK(instruction->IsXor());
6867 __ Eor(out_reg, first_reg, second_reg);
6868 }
6869 } else {
6870 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6871 vixl32::Register first_low = LowRegisterFrom(first);
6872 vixl32::Register first_high = HighRegisterFrom(first);
6873 vixl32::Register second_low = LowRegisterFrom(second);
6874 vixl32::Register second_high = HighRegisterFrom(second);
6875 vixl32::Register out_low = LowRegisterFrom(out);
6876 vixl32::Register out_high = HighRegisterFrom(out);
6877 if (instruction->IsAnd()) {
6878 __ And(out_low, first_low, second_low);
6879 __ And(out_high, first_high, second_high);
6880 } else if (instruction->IsOr()) {
6881 __ Orr(out_low, first_low, second_low);
6882 __ Orr(out_high, first_high, second_high);
6883 } else {
6884 DCHECK(instruction->IsXor());
6885 __ Eor(out_low, first_low, second_low);
6886 __ Eor(out_high, first_high, second_high);
6887 }
6888 }
6889}
6890
Artem Serovcfbe9132016-10-14 15:58:56 +01006891void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006892 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01006893 Location out,
6894 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006895 Location maybe_temp,
6896 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01006897 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006898 if (read_barrier_option == kWithReadBarrier) {
6899 CHECK(kEmitCompilerReadBarrier);
6900 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6901 if (kUseBakerReadBarrier) {
6902 // Load with fast path based Baker's read barrier.
6903 // /* HeapReference<Object> */ out = *(out + offset)
6904 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6905 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
6906 } else {
6907 // Load with slow path based read barrier.
6908 // Save the value of `out` into `maybe_temp` before overwriting it
6909 // in the following move operation, as we will need it for the
6910 // read barrier below.
6911 __ Mov(RegisterFrom(maybe_temp), out_reg);
6912 // /* HeapReference<Object> */ out = *(out + offset)
6913 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6914 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6915 }
Artem Serovcfbe9132016-10-14 15:58:56 +01006916 } else {
6917 // Plain load with no read barrier.
6918 // /* HeapReference<Object> */ out = *(out + offset)
6919 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6920 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6921 }
6922}
6923
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006924void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006925 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006926 Location out,
6927 Location obj,
6928 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006929 Location maybe_temp,
6930 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006931 vixl32::Register out_reg = RegisterFrom(out);
6932 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006933 if (read_barrier_option == kWithReadBarrier) {
6934 CHECK(kEmitCompilerReadBarrier);
6935 if (kUseBakerReadBarrier) {
6936 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6937 // Load with fast path based Baker's read barrier.
6938 // /* HeapReference<Object> */ out = *(obj + offset)
6939 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6940 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
6941 } else {
6942 // Load with slow path based read barrier.
6943 // /* HeapReference<Object> */ out = *(obj + offset)
6944 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6945 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6946 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006947 } else {
6948 // Plain load with no read barrier.
6949 // /* HeapReference<Object> */ out = *(obj + offset)
6950 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6951 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6952 }
6953}
6954
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006955void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006956 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006957 Location root,
6958 vixl32::Register obj,
6959 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00006960 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006961 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006962 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006963 DCHECK(kEmitCompilerReadBarrier);
6964 if (kUseBakerReadBarrier) {
6965 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6966 // Baker's read barrier are used:
6967 //
6968 // root = obj.field;
6969 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6970 // if (temp != null) {
6971 // root = temp(root)
6972 // }
6973
6974 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6975 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
6976 static_assert(
6977 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6978 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6979 "have different sizes.");
6980 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6981 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6982 "have different sizes.");
6983
6984 // Slow path marking the GC root `root`.
6985 Location temp = LocationFrom(lr);
6986 SlowPathCodeARMVIXL* slow_path =
6987 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
6988 instruction,
6989 root,
6990 /*entrypoint*/ temp);
6991 codegen_->AddSlowPath(slow_path);
6992
6993 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6994 const int32_t entry_point_offset =
6995 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6996 // Loading the entrypoint does not require a load acquire since it is only changed when
6997 // threads are suspended or running a checkpoint.
6998 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
6999 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7000 // checking GetIsGcMarking.
7001 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
7002 __ Bind(slow_path->GetExitLabel());
7003 } else {
7004 // GC root loaded through a slow path for read barriers other
7005 // than Baker's.
7006 // /* GcRoot<mirror::Object>* */ root = obj + offset
7007 __ Add(root_reg, obj, offset);
7008 // /* mirror::Object* */ root = root->Read()
7009 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7010 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007011 } else {
7012 // Plain GC root load with no read barrier.
7013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7014 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
7015 // Note that GC roots are not affected by heap poisoning, thus we
7016 // do not have to unpoison `root_reg` here.
7017 }
7018}
7019
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007020void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7021 Location ref,
7022 vixl32::Register obj,
7023 uint32_t offset,
7024 Location temp,
7025 bool needs_null_check) {
7026 DCHECK(kEmitCompilerReadBarrier);
7027 DCHECK(kUseBakerReadBarrier);
7028
7029 // /* HeapReference<Object> */ ref = *(obj + offset)
7030 Location no_index = Location::NoLocation();
7031 ScaleFactor no_scale_factor = TIMES_1;
7032 GenerateReferenceLoadWithBakerReadBarrier(
7033 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007034}
7035
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007036void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7037 Location ref,
7038 vixl32::Register obj,
7039 uint32_t data_offset,
7040 Location index,
7041 Location temp,
7042 bool needs_null_check) {
7043 DCHECK(kEmitCompilerReadBarrier);
7044 DCHECK(kUseBakerReadBarrier);
7045
7046 static_assert(
7047 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7048 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
7049 // /* HeapReference<Object> */ ref =
7050 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7051 ScaleFactor scale_factor = TIMES_4;
7052 GenerateReferenceLoadWithBakerReadBarrier(
7053 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00007054}
7055
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007056void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7057 Location ref,
7058 vixl32::Register obj,
7059 uint32_t offset,
7060 Location index,
7061 ScaleFactor scale_factor,
7062 Location temp,
7063 bool needs_null_check,
7064 bool always_update_field,
7065 vixl32::Register* temp2) {
7066 DCHECK(kEmitCompilerReadBarrier);
7067 DCHECK(kUseBakerReadBarrier);
7068
7069 // In slow path based read barriers, the read barrier call is
7070 // inserted after the original load. However, in fast path based
7071 // Baker's read barriers, we need to perform the load of
7072 // mirror::Object::monitor_ *before* the original reference load.
7073 // This load-load ordering is required by the read barrier.
7074 // The fast path/slow path (for Baker's algorithm) should look like:
7075 //
7076 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7077 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7078 // HeapReference<Object> ref = *src; // Original reference load.
7079 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7080 // if (is_gray) {
7081 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7082 // }
7083 //
7084 // Note: the original implementation in ReadBarrier::Barrier is
7085 // slightly more complex as it performs additional checks that we do
7086 // not do here for performance reasons.
7087
7088 vixl32::Register ref_reg = RegisterFrom(ref);
7089 vixl32::Register temp_reg = RegisterFrom(temp);
7090 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7091
7092 // /* int32_t */ monitor = obj->monitor_
7093 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7094 if (needs_null_check) {
7095 MaybeRecordImplicitNullCheck(instruction);
7096 }
7097 // /* LockWord */ lock_word = LockWord(monitor)
7098 static_assert(sizeof(LockWord) == sizeof(int32_t),
7099 "art::LockWord and int32_t have different sizes.");
7100
7101 // Introduce a dependency on the lock_word including the rb_state,
7102 // which shall prevent load-load reordering without using
7103 // a memory barrier (which would be more expensive).
7104 // `obj` is unchanged by this operation, but its value now depends
7105 // on `temp_reg`.
7106 __ Add(obj, obj, Operand(temp_reg, ShiftType::LSR, 32));
7107
7108 // The actual reference load.
7109 if (index.IsValid()) {
7110 // Load types involving an "index": ArrayGet,
7111 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7112 // intrinsics.
7113 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7114 if (index.IsConstant()) {
7115 size_t computed_offset =
7116 (Int32ConstantFrom(index) << scale_factor) + offset;
7117 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7118 } else {
7119 // Handle the special case of the
7120 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7121 // intrinsics, which use a register pair as index ("long
7122 // offset"), of which only the low part contains data.
7123 vixl32::Register index_reg = index.IsRegisterPair()
7124 ? LowRegisterFrom(index)
7125 : RegisterFrom(index);
7126 UseScratchRegisterScope temps(GetVIXLAssembler());
7127 const vixl32::Register temp3 = temps.Acquire();
7128 __ Add(temp3, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
7129 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp3, offset);
7130 }
7131 } else {
7132 // /* HeapReference<Object> */ ref = *(obj + offset)
7133 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7134 }
7135
7136 // Object* ref = ref_addr->AsMirrorPtr()
7137 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
7138
7139 // Slow path marking the object `ref` when it is gray.
7140 SlowPathCodeARMVIXL* slow_path;
7141 if (always_update_field) {
7142 DCHECK(temp2 != nullptr);
7143 // ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL only supports address
7144 // of the form `obj + field_offset`, where `obj` is a register and
7145 // `field_offset` is a register pair (of which only the lower half
7146 // is used). Thus `offset` and `scale_factor` above are expected
7147 // to be null in this code path.
7148 DCHECK_EQ(offset, 0u);
7149 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7150 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARMVIXL(
7151 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7152 } else {
7153 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, ref);
7154 }
7155 AddSlowPath(slow_path);
7156
7157 // if (rb_state == ReadBarrier::GrayState())
7158 // ref = ReadBarrier::Mark(ref);
7159 // Given the numeric representation, it's enough to check the low bit of the
7160 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7161 // which can be a 16-bit instruction unlike the TST immediate.
7162 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7163 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7164 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7165 __ B(cs, slow_path->GetEntryLabel()); // Carry flag is the last bit shifted out by LSRS.
7166 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00007167}
7168
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007169void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
7170 Location out,
7171 Location ref,
7172 Location obj,
7173 uint32_t offset,
7174 Location index) {
7175 DCHECK(kEmitCompilerReadBarrier);
7176
7177 // Insert a slow path based read barrier *after* the reference load.
7178 //
7179 // If heap poisoning is enabled, the unpoisoning of the loaded
7180 // reference will be carried out by the runtime within the slow
7181 // path.
7182 //
7183 // Note that `ref` currently does not get unpoisoned (when heap
7184 // poisoning is enabled), which is alright as the `ref` argument is
7185 // not used by the artReadBarrierSlow entry point.
7186 //
7187 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7188 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
7189 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
7190 AddSlowPath(slow_path);
7191
7192 __ B(slow_path->GetEntryLabel());
7193 __ Bind(slow_path->GetExitLabel());
7194}
7195
7196void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01007197 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007198 Location ref,
7199 Location obj,
7200 uint32_t offset,
7201 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01007202 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007203 // Baker's read barriers shall be handled by the fast path
7204 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01007205 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007206 // If heap poisoning is enabled, unpoisoning will be taken care of
7207 // by the runtime within the slow path.
7208 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01007209 } else if (kPoisonHeapReferences) {
7210 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
7211 }
7212}
7213
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007214void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7215 Location out,
7216 Location root) {
7217 DCHECK(kEmitCompilerReadBarrier);
7218
7219 // Insert a slow path based read barrier *after* the GC root load.
7220 //
7221 // Note that GC roots are not affected by heap poisoning, so we do
7222 // not need to do anything special for this here.
7223 SlowPathCodeARMVIXL* slow_path =
7224 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
7225 AddSlowPath(slow_path);
7226
7227 __ B(slow_path->GetEntryLabel());
7228 __ Bind(slow_path->GetExitLabel());
7229}
7230
Artem Serov02d37832016-10-25 15:25:33 +01007231// Check if the desired_dispatch_info is supported. If it is, return it,
7232// otherwise return a fall-back info that should be used instead.
7233HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007234 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007235 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007236 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01007237}
7238
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007239vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
7240 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
7241 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7242 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7243 if (!invoke->GetLocations()->Intrinsified()) {
7244 return RegisterFrom(location);
7245 }
7246 // For intrinsics we allow any location, so it may be on the stack.
7247 if (!location.IsRegister()) {
7248 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
7249 return temp;
7250 }
7251 // For register locations, check if the register was saved. If so, get it from the stack.
7252 // Note: There is a chance that the register was saved but not overwritten, so we could
7253 // save one load. However, since this is just an intrinsic slow path we prefer this
7254 // simple and more robust approach rather that trying to determine if that's the case.
7255 SlowPathCode* slow_path = GetCurrentSlowPath();
7256 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7257 if (slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
7258 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
7259 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
7260 return temp;
7261 }
7262 return RegisterFrom(location);
7263}
7264
TatWai Chongd8c052a2016-11-02 16:12:48 +08007265Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007266 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007267 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007268 switch (invoke->GetMethodLoadKind()) {
7269 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7270 uint32_t offset =
7271 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
7272 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00007273 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
7274 break;
7275 }
7276 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
7277 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7278 break;
7279 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7280 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
7281 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007282 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7283 HArmDexCacheArraysBase* base =
7284 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7285 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
7286 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7287 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007288 break;
7289 }
7290 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
7291 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7292 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007293 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007294 if (current_method.IsRegister()) {
7295 method_reg = RegisterFrom(current_method);
7296 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007297 DCHECK(invoke->GetLocations()->Intrinsified());
7298 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007299 method_reg = reg;
7300 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007301 }
7302 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7303 GetAssembler()->LoadFromOffset(
7304 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00007305 reg,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007306 method_reg,
7307 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
7308 // temp = temp[index_in_cache];
7309 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7310 uint32_t index_in_cache = invoke->GetDexMethodIndex();
7311 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007312 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007313 break;
7314 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007315 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007316 return callee_method;
7317}
7318
7319void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7320 Location temp) {
7321 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007322
Artem Serovd4cc5b22016-11-04 11:19:09 +00007323 switch (invoke->GetCodePtrLocation()) {
7324 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7325 __ Bl(GetFrameEntryLabel());
7326 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7328 // LR = callee_method->entry_point_from_quick_compiled_code_
7329 GetAssembler()->LoadFromOffset(
7330 kLoadWord,
7331 lr,
7332 RegisterFrom(callee_method),
7333 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00007334 {
7335 // 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 +00007336 ExactAssemblyScope aas(GetVIXLAssembler(),
7337 vixl32::k16BitT32InstructionSizeInBytes,
7338 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007339 // LR()
7340 __ blx(lr);
7341 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007342 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007343 }
7344
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007345 DCHECK(!IsLeafMethod());
7346}
7347
7348void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7349 vixl32::Register temp = RegisterFrom(temp_location);
7350 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7351 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
7352
7353 // Use the calling convention instead of the location of the receiver, as
7354 // intrinsics may have put the receiver in a different register. In the intrinsics
7355 // slow path, the arguments have been moved to the right place, so here we are
7356 // guaranteed that the receiver is the first register of the calling convention.
7357 InvokeDexCallingConventionARMVIXL calling_convention;
7358 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
7359 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007360 {
7361 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00007362 ExactAssemblyScope aas(GetVIXLAssembler(),
7363 vixl32::kMaxInstructionSizeInBytes,
7364 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007365 // /* HeapReference<Class> */ temp = receiver->klass_
7366 __ ldr(temp, MemOperand(receiver, class_offset));
7367 MaybeRecordImplicitNullCheck(invoke);
7368 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007369 // Instead of simply (possibly) unpoisoning `temp` here, we should
7370 // emit a read barrier for the previous class reference load.
7371 // However this is not required in practice, as this is an
7372 // intermediate/temporary reference and because the current
7373 // concurrent copying collector keeps the from-space memory
7374 // intact/accessible until the end of the marking phase (the
7375 // concurrent copying collector may not in the future).
7376 GetAssembler()->MaybeUnpoisonHeapReference(temp);
7377
7378 // temp = temp->GetMethodAt(method_offset);
7379 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
7380 kArmPointerSize).Int32Value();
7381 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
7382 // LR = temp->GetEntryPoint();
7383 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
7384 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00007385 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
7386 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
7387 // that.
7388 // 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 +00007389 ExactAssemblyScope aas(GetVIXLAssembler(),
7390 vixl32::k16BitT32InstructionSizeInBytes,
7391 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00007392 __ blx(lr);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007393}
7394
Artem Serovd4cc5b22016-11-04 11:19:09 +00007395CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007396 const DexFile& dex_file, dex::StringIndex string_index) {
7397 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007398}
7399
7400CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
7401 const DexFile& dex_file, dex::TypeIndex type_index) {
7402 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
7403}
7404
Vladimir Marko1998cd02017-01-13 13:02:58 +00007405CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
7406 const DexFile& dex_file, dex::TypeIndex type_index) {
7407 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7408}
7409
Artem Serovd4cc5b22016-11-04 11:19:09 +00007410CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
7411 const DexFile& dex_file, uint32_t element_offset) {
7412 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7413}
7414
7415CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
7416 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7417 patches->emplace_back(dex_file, offset_or_index);
7418 return &patches->back();
7419}
7420
Artem Serovc5fcb442016-12-02 19:19:58 +00007421VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
7422 const DexFile& dex_file,
7423 dex::StringIndex string_index) {
7424 return boot_image_string_patches_.GetOrCreate(
7425 StringReference(&dex_file, string_index),
7426 [this]() {
7427 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7428 });
7429}
7430
7431VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
7432 const DexFile& dex_file,
7433 dex::TypeIndex type_index) {
7434 return boot_image_type_patches_.GetOrCreate(
7435 TypeReference(&dex_file, type_index),
7436 [this]() {
7437 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7438 });
7439}
7440
7441VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
7442 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7443 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7444 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7445}
7446
7447VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7448 return DeduplicateUint32Literal(address, &uint32_literals_);
7449}
7450
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007451VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
7452 const DexFile& dex_file,
7453 dex::StringIndex string_index,
7454 Handle<mirror::String> handle) {
7455 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7456 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007457 return jit_string_patches_.GetOrCreate(
7458 StringReference(&dex_file, string_index),
7459 [this]() {
7460 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7461 });
7462}
7463
7464VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
7465 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007466 Handle<mirror::Class> handle) {
7467 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7468 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007469 return jit_class_patches_.GetOrCreate(
7470 TypeReference(&dex_file, type_index),
7471 [this]() {
7472 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7473 });
7474}
7475
Artem Serovd4cc5b22016-11-04 11:19:09 +00007476template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7477inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
7478 const ArenaDeque<PcRelativePatchInfo>& infos,
7479 ArenaVector<LinkerPatch>* linker_patches) {
7480 for (const PcRelativePatchInfo& info : infos) {
7481 const DexFile& dex_file = info.target_dex_file;
7482 size_t offset_or_index = info.offset_or_index;
7483 DCHECK(info.add_pc_label.IsBound());
7484 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
7485 // Add MOVW patch.
7486 DCHECK(info.movw_label.IsBound());
7487 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
7488 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7489 // Add MOVT patch.
7490 DCHECK(info.movt_label.IsBound());
7491 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
7492 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7493 }
7494}
7495
7496void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7497 DCHECK(linker_patches->empty());
7498 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00007499 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007500 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00007501 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007502 boot_image_type_patches_.size() +
7503 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007504 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00007505 boot_image_address_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007506 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007507 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7508 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007509 for (const auto& entry : boot_image_string_patches_) {
7510 const StringReference& target_string = entry.first;
7511 VIXLUInt32Literal* literal = entry.second;
7512 DCHECK(literal->IsBound());
7513 uint32_t literal_offset = literal->GetLocation();
7514 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7515 target_string.dex_file,
7516 target_string.string_index.index_));
7517 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007518 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007519 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007520 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7521 linker_patches);
7522 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007523 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7524 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007525 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7526 linker_patches);
7527 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007528 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7529 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00007530 for (const auto& entry : boot_image_type_patches_) {
7531 const TypeReference& target_type = entry.first;
7532 VIXLUInt32Literal* literal = entry.second;
7533 DCHECK(literal->IsBound());
7534 uint32_t literal_offset = literal->GetLocation();
7535 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7536 target_type.dex_file,
7537 target_type.type_index.index_));
7538 }
Artem Serovc5fcb442016-12-02 19:19:58 +00007539 for (const auto& entry : boot_image_address_patches_) {
7540 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7541 VIXLUInt32Literal* literal = entry.second;
7542 DCHECK(literal->IsBound());
7543 uint32_t literal_offset = literal->GetLocation();
7544 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7545 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007546 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00007547}
7548
7549VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
7550 uint32_t value,
7551 Uint32ToLiteralMap* map) {
7552 return map->GetOrCreate(
7553 value,
7554 [this, value]() {
7555 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
7556 });
7557}
7558
7559VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
7560 MethodReference target_method,
7561 MethodToLiteralMap* map) {
7562 return map->GetOrCreate(
7563 target_method,
7564 [this]() {
7565 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
7566 });
7567}
7568
Artem Serov2bbc9532016-10-21 11:51:50 +01007569void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7570 LocationSummary* locations =
7571 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7572 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7573 Location::RequiresRegister());
7574 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7575 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7577}
7578
7579void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7580 vixl32::Register res = OutputRegister(instr);
7581 vixl32::Register accumulator =
7582 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
7583 vixl32::Register mul_left =
7584 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
7585 vixl32::Register mul_right =
7586 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
7587
7588 if (instr->GetOpKind() == HInstruction::kAdd) {
7589 __ Mla(res, mul_left, mul_right, accumulator);
7590 } else {
7591 __ Mls(res, mul_left, mul_right, accumulator);
7592 }
7593}
7594
Artem Serov551b28f2016-10-18 19:11:30 +01007595void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7596 // Nothing to do, this should be removed during prepare for register allocator.
7597 LOG(FATAL) << "Unreachable";
7598}
7599
7600void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7601 // Nothing to do, this should be removed during prepare for register allocator.
7602 LOG(FATAL) << "Unreachable";
7603}
7604
7605// Simple implementation of packed switch - generate cascaded compare/jumps.
7606void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7607 LocationSummary* locations =
7608 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7609 locations->SetInAt(0, Location::RequiresRegister());
7610 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7611 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7612 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7613 if (switch_instr->GetStartValue() != 0) {
7614 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7615 }
7616 }
7617}
7618
7619// TODO(VIXL): Investigate and reach the parity with old arm codegen.
7620void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7621 int32_t lower_bound = switch_instr->GetStartValue();
7622 uint32_t num_entries = switch_instr->GetNumEntries();
7623 LocationSummary* locations = switch_instr->GetLocations();
7624 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
7625 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7626
7627 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
7628 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
7629 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007630 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007631 vixl32::Register temp_reg = temps.Acquire();
7632 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7633 // the immediate, because IP is used as the destination register. For the other
7634 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7635 // and they can be encoded in the instruction without making use of IP register.
7636 __ Adds(temp_reg, value_reg, -lower_bound);
7637
7638 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7639 // Jump to successors[0] if value == lower_bound.
7640 __ B(eq, codegen_->GetLabelOf(successors[0]));
7641 int32_t last_index = 0;
7642 for (; num_entries - last_index > 2; last_index += 2) {
7643 __ Adds(temp_reg, temp_reg, -2);
7644 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7645 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
7646 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7647 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
7648 }
7649 if (num_entries - last_index == 2) {
7650 // The last missing case_value.
7651 __ Cmp(temp_reg, 1);
7652 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
7653 }
7654
7655 // And the default for any other value.
7656 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7657 __ B(codegen_->GetLabelOf(default_block));
7658 }
7659 } else {
7660 // Create a table lookup.
7661 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
7662
7663 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
7664
7665 // Remove the bias.
7666 vixl32::Register key_reg;
7667 if (lower_bound != 0) {
7668 key_reg = RegisterFrom(locations->GetTemp(1));
7669 __ Sub(key_reg, value_reg, lower_bound);
7670 } else {
7671 key_reg = value_reg;
7672 }
7673
7674 // Check whether the value is in the table, jump to default block if not.
7675 __ Cmp(key_reg, num_entries - 1);
7676 __ B(hi, codegen_->GetLabelOf(default_block));
7677
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007678 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01007679 vixl32::Register jump_offset = temps.Acquire();
7680
7681 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00007682 {
7683 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
7684 ExactAssemblyScope aas(GetVIXLAssembler(),
7685 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
7686 CodeBufferCheckScope::kMaximumSize);
7687 __ adr(table_base, jump_table->GetTableStartLabel());
7688 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01007689
Scott Wakeling86e9d262017-01-18 15:59:24 +00007690 // Jump to target block by branching to table_base(pc related) + offset.
7691 vixl32::Register target_address = table_base;
7692 __ add(target_address, table_base, jump_offset);
7693 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00007694
Scott Wakeling86e9d262017-01-18 15:59:24 +00007695 jump_table->EmitTable(codegen_);
7696 }
Artem Serov551b28f2016-10-18 19:11:30 +01007697 }
7698}
Artem Serovd4cc5b22016-11-04 11:19:09 +00007699void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7701 locations->SetOut(Location::RequiresRegister());
7702}
7703
7704void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7705 vixl32::Register base_reg = OutputRegister(base);
7706 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7707 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7708 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
7709}
Artem Serov551b28f2016-10-18 19:11:30 +01007710
Artem Serov02d37832016-10-25 15:25:33 +01007711// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007712void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7713 if (!trg.IsValid()) {
7714 DCHECK_EQ(type, Primitive::kPrimVoid);
7715 return;
7716 }
7717
7718 DCHECK_NE(type, Primitive::kPrimVoid);
7719
Artem Serovd4cc5b22016-11-04 11:19:09 +00007720 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007721 if (return_loc.Equals(trg)) {
7722 return;
7723 }
7724
7725 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7726 // with the last branch.
7727 if (type == Primitive::kPrimLong) {
7728 TODO_VIXL32(FATAL);
7729 } else if (type == Primitive::kPrimDouble) {
7730 TODO_VIXL32(FATAL);
7731 } else {
7732 // Let the parallel move resolver take care of all of this.
7733 HParallelMove parallel_move(GetGraph()->GetArena());
7734 parallel_move.AddMove(return_loc, trg, type, nullptr);
7735 GetMoveResolver()->EmitNativeCode(&parallel_move);
7736 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007737}
Scott Wakelingfe885462016-09-22 10:24:38 +01007738
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007739void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7740 LocationSummary* locations =
7741 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7742 locations->SetInAt(0, Location::RequiresRegister());
7743 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01007744}
7745
xueliang.zhong8d2c4592016-11-23 17:05:25 +00007746void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
7747 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7748 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7749 instruction->GetIndex(), kArmPointerSize).SizeValue();
7750 GetAssembler()->LoadFromOffset(kLoadWord,
7751 OutputRegister(instruction),
7752 InputRegisterAt(instruction, 0),
7753 method_offset);
7754 } else {
7755 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7756 instruction->GetIndex(), kArmPointerSize));
7757 GetAssembler()->LoadFromOffset(kLoadWord,
7758 OutputRegister(instruction),
7759 InputRegisterAt(instruction, 0),
7760 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7761 GetAssembler()->LoadFromOffset(kLoadWord,
7762 OutputRegister(instruction),
7763 OutputRegister(instruction),
7764 method_offset);
7765 }
Artem Serov551b28f2016-10-18 19:11:30 +01007766}
7767
Artem Serovc5fcb442016-12-02 19:19:58 +00007768static void PatchJitRootUse(uint8_t* code,
7769 const uint8_t* roots_data,
7770 VIXLUInt32Literal* literal,
7771 uint64_t index_in_table) {
7772 DCHECK(literal->IsBound());
7773 uint32_t literal_offset = literal->GetLocation();
7774 uintptr_t address =
7775 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7776 uint8_t* data = code + literal_offset;
7777 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7778}
7779
7780void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7781 for (const auto& entry : jit_string_patches_) {
7782 const auto& it = jit_string_roots_.find(entry.first);
7783 DCHECK(it != jit_string_roots_.end());
7784 PatchJitRootUse(code, roots_data, entry.second, it->second);
7785 }
7786 for (const auto& entry : jit_class_patches_) {
7787 const auto& it = jit_class_roots_.find(entry.first);
7788 DCHECK(it != jit_class_roots_.end());
7789 PatchJitRootUse(code, roots_data, entry.second, it->second);
7790 }
7791}
7792
Artem Serovd4cc5b22016-11-04 11:19:09 +00007793void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
7794 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
7795 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00007796 ExactAssemblyScope aas(GetVIXLAssembler(),
7797 3 * vixl32::kMaxInstructionSizeInBytes,
7798 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007799 // TODO(VIXL): Think about using mov instead of movw.
7800 __ bind(&labels->movw_label);
7801 __ movw(out, /* placeholder */ 0u);
7802 __ bind(&labels->movt_label);
7803 __ movt(out, /* placeholder */ 0u);
7804 __ bind(&labels->add_pc_label);
7805 __ add(out, out, pc);
7806}
7807
Scott Wakelingfe885462016-09-22 10:24:38 +01007808#undef __
7809#undef QUICK_ENTRY_POINT
7810#undef TODO_VIXL32
7811
7812} // namespace arm
7813} // namespace art