blob: 08227fc8c8e5aea28df702321e7fb6c1dc74aa44 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 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.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100110 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100374 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000375 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100377
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 LocationSummary* locations = at_->GetLocations();
380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100387 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
388 : kQuickInitializeType;
389 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000390 if (do_clinit_) {
391 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
392 } else {
393 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000395
396 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000397 Location out = locations->Out();
398 if (out.IsValid()) {
399 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000400 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
401 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 __ b(GetExitLabel());
404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 // The class this slow path will load.
410 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412 // The instruction where this slow path is happening.
413 // (Might be the load class or an initialization check).
414 HInstruction* const at_;
415
416 // The dex PC of `at_`.
417 const uint32_t dex_pc_;
418
419 // Whether to initialize the class.
420 const bool do_clinit_;
421
422 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423};
424
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425class LoadStringSlowPathARM : public SlowPathCodeARM {
426 public:
427 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
428
429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
430 LocationSummary* locations = instruction_->GetLocations();
431 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 HLoadString* load = instruction_->AsLoadString();
433 const uint32_t string_index = load->GetStringIndex();
434 Register out = locations->Out().AsRegister<Register>();
435 Register temp = locations->GetTemp(0).AsRegister<Register>();
436 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000437
438 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
439 __ Bind(GetEntryLabel());
440 SaveLiveRegisters(codegen, locations);
441
442 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100443 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
444 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
445 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
446 Register entry_address = temp_is_r0 ? out : temp;
447 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
448 if (call_saves_everything_except_r0 && temp_is_r0) {
449 __ mov(entry_address, ShifterOperand(temp));
450 }
451
Vladimir Markoaad75c62016-10-03 08:46:48 +0000452 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
453 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100455
456 // Store the resolved String to the .bss entry.
457 if (call_saves_everything_except_r0) {
458 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
459 __ str(R0, Address(entry_address));
460 } else {
461 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
462 CodeGeneratorARM::PcRelativePatchInfo* labels =
463 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
464 __ BindTrackedLabel(&labels->movw_label);
465 __ movw(entry_address, /* placeholder */ 0u);
466 __ BindTrackedLabel(&labels->movt_label);
467 __ movt(entry_address, /* placeholder */ 0u);
468 __ BindTrackedLabel(&labels->add_pc_label);
469 __ add(entry_address, entry_address, ShifterOperand(PC));
470 __ str(R0, Address(entry_address));
471 }
472
Vladimir Markoaad75c62016-10-03 08:46:48 +0000473 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000474 RestoreLiveRegisters(codegen, locations);
475
Vladimir Markoaad75c62016-10-03 08:46:48 +0000476 __ b(GetExitLabel());
477 }
478
479 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
480
481 private:
482 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
483};
484
Artem Serovf4d6aee2016-07-11 10:41:45 +0100485class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000486 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100488 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000489
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000491 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 Location arg0, arg1;
493 if (instruction_->IsInstanceOf()) {
494 arg0 = locations->InAt(1);
495 arg1 = locations->Out();
496 } else {
497 arg0 = locations->InAt(0);
498 arg1 = locations->InAt(1);
499 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000500 DCHECK(instruction_->IsCheckCast()
501 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000502
503 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
504 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 if (!is_fatal_) {
507 SaveLiveRegisters(codegen, locations);
508 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000509
510 // We're moving two locations to locations that could overlap, so we need a parallel
511 // move resolver.
512 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800513 codegen->EmitParallelMoves(arg0,
514 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 arg1,
517 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
518 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000519 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100520 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100521 instruction_,
522 instruction_->GetDexPc(),
523 this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800524 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
526 } else {
527 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800528 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
529 instruction_,
530 instruction_->GetDexPc(),
531 this);
532 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000533 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 RestoreLiveRegisters(codegen, locations);
537 __ b(GetExitLabel());
538 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 }
540
Alexandre Rames9931f312015-06-19 14:47:01 +0100541 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543 bool IsFatal() const OVERRIDE { return is_fatal_; }
544
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000545 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000546 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000547
548 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
549};
550
Artem Serovf4d6aee2016-07-11 10:41:45 +0100551class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 public:
Aart Bik42249c32016-01-07 15:33:50 -0800553 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100554 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555
556 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800557 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100559 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000560 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561 }
562
Alexandre Rames9931f312015-06-19 14:47:01 +0100563 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
564
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700565 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700566 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
567};
568
Artem Serovf4d6aee2016-07-11 10:41:45 +0100569class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100571 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 __ Bind(GetEntryLabel());
576 SaveLiveRegisters(codegen, locations);
577
578 InvokeRuntimeCallingConvention calling_convention;
579 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
580 parallel_move.AddMove(
581 locations->InAt(0),
582 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
583 Primitive::kPrimNot,
584 nullptr);
585 parallel_move.AddMove(
586 locations->InAt(1),
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimInt,
589 nullptr);
590 parallel_move.AddMove(
591 locations->InAt(2),
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimNot,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596
597 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100598 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000599 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600 RestoreLiveRegisters(codegen, locations);
601 __ b(GetExitLabel());
602 }
603
604 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
605
606 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
608};
609
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100610// Slow path marking an object reference `ref` during a read
611// barrier. The field `obj.field` in the object `obj` holding this
612// reference does not get updated by this slow path after marking (see
613// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
614//
615// This means that after the execution of this slow path, `ref` will
616// always be up-to-date, but `obj.field` may not; i.e., after the
617// flip, `ref` will be a to-space reference, but `obj.field` will
618// probably still be a from-space reference (unless it gets updated by
619// another thread, or if another thread installed another object
620// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100621class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000622 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100623 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location ref)
624 : SlowPathCodeARM(instruction), ref_(ref) {
Roland Levillainc9285912015-12-18 10:38:42 +0000625 DCHECK(kEmitCompilerReadBarrier);
626 }
627
628 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
629
630 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
631 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100632 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000633 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100634 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000635 DCHECK(instruction_->IsInstanceFieldGet() ||
636 instruction_->IsStaticFieldGet() ||
637 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100638 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000639 instruction_->IsLoadClass() ||
640 instruction_->IsLoadString() ||
641 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100642 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100643 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
644 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000645 << "Unexpected instruction in read barrier marking slow path: "
646 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000647 // The read barrier instrumentation of object ArrayGet
648 // instructions does not support the HIntermediateAddress
649 // instruction.
650 DCHECK(!(instruction_->IsArrayGet() &&
651 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000652
653 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100654 // No need to save live registers; it's taken care of by the
655 // entrypoint. Also, there is no need to update the stack mask,
656 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000657 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100658 DCHECK_NE(ref_reg, SP);
659 DCHECK_NE(ref_reg, LR);
660 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100661 // IP is used internally by the ReadBarrierMarkRegX entry point
662 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 DCHECK_NE(ref_reg, IP);
664 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100665 // "Compact" slow path, saving two moves.
666 //
667 // Instead of using the standard runtime calling convention (input
668 // and output in R0):
669 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100671 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100672 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100673 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100674 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100675 // of a dedicated entrypoint:
676 //
677 // rX <- ReadBarrierMarkRegX(rX)
678 //
679 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100681 // This runtime call does not require a stack map.
682 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000683 __ b(GetExitLabel());
684 }
685
686 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687 // The location (register) of the marked object reference.
688 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
691};
692
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693// Slow path marking an object reference `ref` during a read barrier,
694// and if needed, atomically updating the field `obj.field` in the
695// object `obj` holding this reference after marking (contrary to
696// ReadBarrierMarkSlowPathARM above, which never tries to update
697// `obj.field`).
698//
699// This means that after the execution of this slow path, both `ref`
700// and `obj.field` will be up-to-date; i.e., after the flip, both will
701// hold the same to-space reference (unless another thread installed
702// another object reference (different from `ref`) in `obj.field`).
703class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
704 public:
705 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
706 Location ref,
707 Register obj,
708 Location field_offset,
709 Register temp1,
710 Register temp2)
711 : SlowPathCodeARM(instruction),
712 ref_(ref),
713 obj_(obj),
714 field_offset_(field_offset),
715 temp1_(temp1),
716 temp2_(temp2) {
717 DCHECK(kEmitCompilerReadBarrier);
718 }
719
720 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 LocationSummary* locations = instruction_->GetLocations();
724 Register ref_reg = ref_.AsRegister<Register>();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
727 // This slow path is only used by the UnsafeCASObject intrinsic.
728 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
729 << "Unexpected instruction in read barrier marking and field updating slow path: "
730 << instruction_->DebugName();
731 DCHECK(instruction_->GetLocations()->Intrinsified());
732 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
733 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
734
735 __ Bind(GetEntryLabel());
736
737 // Save the old reference.
738 // Note that we cannot use IP to save the old reference, as IP is
739 // used internally by the ReadBarrierMarkRegX entry point, and we
740 // need the old reference after the call to that entry point.
741 DCHECK_NE(temp1_, IP);
742 __ Mov(temp1_, ref_reg);
743
744 // No need to save live registers; it's taken care of by the
745 // entrypoint. Also, there is no need to update the stack mask,
746 // as this runtime call will not trigger a garbage collection.
747 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
748 DCHECK_NE(ref_reg, SP);
749 DCHECK_NE(ref_reg, LR);
750 DCHECK_NE(ref_reg, PC);
751 // IP is used internally by the ReadBarrierMarkRegX entry point
752 // as a temporary, it cannot be the entry point's input/output.
753 DCHECK_NE(ref_reg, IP);
754 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
755 // "Compact" slow path, saving two moves.
756 //
757 // Instead of using the standard runtime calling convention (input
758 // and output in R0):
759 //
760 // R0 <- ref
761 // R0 <- ReadBarrierMark(R0)
762 // ref <- R0
763 //
764 // we just use rX (the register containing `ref`) as input and output
765 // of a dedicated entrypoint:
766 //
767 // rX <- ReadBarrierMarkRegX(rX)
768 //
769 int32_t entry_point_offset =
770 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
771 // This runtime call does not require a stack map.
772 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
773
774 // If the new reference is different from the old reference,
775 // update the field in the holder (`*(obj_ + field_offset_)`).
776 //
777 // Note that this field could also hold a different object, if
778 // another thread had concurrently changed it. In that case, the
779 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
780 // (CAS) operation below would abort the CAS, leaving the field
781 // as-is.
782 Label done;
783 __ cmp(temp1_, ShifterOperand(ref_reg));
784 __ b(&done, EQ);
785
786 // Update the the holder's field atomically. This may fail if
787 // mutator updates before us, but it's OK. This is achieved
788 // using a strong compare-and-set (CAS) operation with relaxed
789 // memory synchronization ordering, where the expected value is
790 // the old reference and the desired value is the new reference.
791
792 // Convenience aliases.
793 Register base = obj_;
794 // The UnsafeCASObject intrinsic uses a register pair as field
795 // offset ("long offset"), of which only the low part contains
796 // data.
797 Register offset = field_offset_.AsRegisterPairLow<Register>();
798 Register expected = temp1_;
799 Register value = ref_reg;
800 Register tmp_ptr = IP; // Pointer to actual memory.
801 Register tmp = temp2_; // Value in memory.
802
803 __ add(tmp_ptr, base, ShifterOperand(offset));
804
805 if (kPoisonHeapReferences) {
806 __ PoisonHeapReference(expected);
807 if (value == expected) {
808 // Do not poison `value`, as it is the same register as
809 // `expected`, which has just been poisoned.
810 } else {
811 __ PoisonHeapReference(value);
812 }
813 }
814
815 // do {
816 // tmp = [r_ptr] - expected;
817 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
818
Roland Levillain24a4d112016-10-26 13:10:46 +0100819 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100820 __ Bind(&loop_head);
821
822 __ ldrex(tmp, tmp_ptr);
823
824 __ subs(tmp, tmp, ShifterOperand(expected));
825
Roland Levillain24a4d112016-10-26 13:10:46 +0100826 __ it(NE);
827 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828
Roland Levillain24a4d112016-10-26 13:10:46 +0100829 __ b(&exit_loop, NE);
830
831 __ strex(tmp, value, tmp_ptr);
832 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100833 __ b(&loop_head, EQ);
834
Roland Levillain24a4d112016-10-26 13:10:46 +0100835 __ Bind(&exit_loop);
836
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 if (kPoisonHeapReferences) {
838 __ UnpoisonHeapReference(expected);
839 if (value == expected) {
840 // Do not unpoison `value`, as it is the same register as
841 // `expected`, which has just been unpoisoned.
842 } else {
843 __ UnpoisonHeapReference(value);
844 }
845 }
846
847 __ Bind(&done);
848 __ b(GetExitLabel());
849 }
850
851 private:
852 // The location (register) of the marked object reference.
853 const Location ref_;
854 // The register containing the object holding the marked object reference field.
855 const Register obj_;
856 // The location of the offset of the marked reference field within `obj_`.
857 Location field_offset_;
858
859 const Register temp1_;
860 const Register temp2_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
863};
864
Roland Levillain3b359c72015-11-17 19:35:12 +0000865// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100866class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000867 public:
868 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
869 Location out,
870 Location ref,
871 Location obj,
872 uint32_t offset,
873 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100874 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000875 out_(out),
876 ref_(ref),
877 obj_(obj),
878 offset_(offset),
879 index_(index) {
880 DCHECK(kEmitCompilerReadBarrier);
881 // If `obj` is equal to `out` or `ref`, it means the initial object
882 // has been overwritten by (or after) the heap object reference load
883 // to be instrumented, e.g.:
884 //
885 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000886 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000887 //
888 // In that case, we have lost the information about the original
889 // object, and the emitted read barrier cannot work properly.
890 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
891 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
892 }
893
894 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
895 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
896 LocationSummary* locations = instruction_->GetLocations();
897 Register reg_out = out_.AsRegister<Register>();
898 DCHECK(locations->CanCall());
899 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100900 DCHECK(instruction_->IsInstanceFieldGet() ||
901 instruction_->IsStaticFieldGet() ||
902 instruction_->IsArrayGet() ||
903 instruction_->IsInstanceOf() ||
904 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100905 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000906 << "Unexpected instruction in read barrier for heap reference slow path: "
907 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000908 // The read barrier instrumentation of object ArrayGet
909 // instructions does not support the HIntermediateAddress
910 // instruction.
911 DCHECK(!(instruction_->IsArrayGet() &&
912 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000913
914 __ Bind(GetEntryLabel());
915 SaveLiveRegisters(codegen, locations);
916
917 // We may have to change the index's value, but as `index_` is a
918 // constant member (like other "inputs" of this slow path),
919 // introduce a copy of it, `index`.
920 Location index = index_;
921 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100922 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000923 if (instruction_->IsArrayGet()) {
924 // Compute the actual memory offset and store it in `index`.
925 Register index_reg = index_.AsRegister<Register>();
926 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
927 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
928 // We are about to change the value of `index_reg` (see the
929 // calls to art::arm::Thumb2Assembler::Lsl and
930 // art::arm::Thumb2Assembler::AddConstant below), but it has
931 // not been saved by the previous call to
932 // art::SlowPathCode::SaveLiveRegisters, as it is a
933 // callee-save register --
934 // art::SlowPathCode::SaveLiveRegisters does not consider
935 // callee-save registers, as it has been designed with the
936 // assumption that callee-save registers are supposed to be
937 // handled by the called function. So, as a callee-save
938 // register, `index_reg` _would_ eventually be saved onto
939 // the stack, but it would be too late: we would have
940 // changed its value earlier. Therefore, we manually save
941 // it here into another freely available register,
942 // `free_reg`, chosen of course among the caller-save
943 // registers (as a callee-save `free_reg` register would
944 // exhibit the same problem).
945 //
946 // Note we could have requested a temporary register from
947 // the register allocator instead; but we prefer not to, as
948 // this is a slow path, and we know we can find a
949 // caller-save register that is available.
950 Register free_reg = FindAvailableCallerSaveRegister(codegen);
951 __ Mov(free_reg, index_reg);
952 index_reg = free_reg;
953 index = Location::RegisterLocation(index_reg);
954 } else {
955 // The initial register stored in `index_` has already been
956 // saved in the call to art::SlowPathCode::SaveLiveRegisters
957 // (as it is not a callee-save register), so we can freely
958 // use it.
959 }
960 // Shifting the index value contained in `index_reg` by the scale
961 // factor (2) cannot overflow in practice, as the runtime is
962 // unable to allocate object arrays with a size larger than
963 // 2^26 - 1 (that is, 2^28 - 4 bytes).
964 __ Lsl(index_reg, index_reg, TIMES_4);
965 static_assert(
966 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
967 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
968 __ AddConstant(index_reg, index_reg, offset_);
969 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100970 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
971 // intrinsics, `index_` is not shifted by a scale factor of 2
972 // (as in the case of ArrayGet), as it is actually an offset
973 // to an object field within an object.
974 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000975 DCHECK(instruction_->GetLocations()->Intrinsified());
976 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
977 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
978 << instruction_->AsInvoke()->GetIntrinsic();
979 DCHECK_EQ(offset_, 0U);
980 DCHECK(index_.IsRegisterPair());
981 // UnsafeGet's offset location is a register pair, the low
982 // part contains the correct offset.
983 index = index_.ToLow();
984 }
985 }
986
987 // We're moving two or three locations to locations that could
988 // overlap, so we need a parallel move resolver.
989 InvokeRuntimeCallingConvention calling_convention;
990 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
991 parallel_move.AddMove(ref_,
992 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
993 Primitive::kPrimNot,
994 nullptr);
995 parallel_move.AddMove(obj_,
996 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
997 Primitive::kPrimNot,
998 nullptr);
999 if (index.IsValid()) {
1000 parallel_move.AddMove(index,
1001 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1002 Primitive::kPrimInt,
1003 nullptr);
1004 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1005 } else {
1006 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1007 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1008 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001009 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001010 CheckEntrypointTypes<
1011 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1012 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1013
1014 RestoreLiveRegisters(codegen, locations);
1015 __ b(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1019
1020 private:
1021 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1022 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1023 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1024 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1025 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1026 return static_cast<Register>(i);
1027 }
1028 }
1029 // We shall never fail to find a free caller-save register, as
1030 // there are more than two core caller-save registers on ARM
1031 // (meaning it is possible to find one which is different from
1032 // `ref` and `obj`).
1033 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1034 LOG(FATAL) << "Could not find a free caller-save register";
1035 UNREACHABLE();
1036 }
1037
Roland Levillain3b359c72015-11-17 19:35:12 +00001038 const Location out_;
1039 const Location ref_;
1040 const Location obj_;
1041 const uint32_t offset_;
1042 // An additional location containing an index to an array.
1043 // Only used for HArrayGet and the UnsafeGetObject &
1044 // UnsafeGetObjectVolatile intrinsics.
1045 const Location index_;
1046
1047 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1048};
1049
1050// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001051class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001052 public:
1053 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001054 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001055 DCHECK(kEmitCompilerReadBarrier);
1056 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001057
1058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1059 LocationSummary* locations = instruction_->GetLocations();
1060 Register reg_out = out_.AsRegister<Register>();
1061 DCHECK(locations->CanCall());
1062 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001063 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1064 << "Unexpected instruction in read barrier for GC root slow path: "
1065 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001066
1067 __ Bind(GetEntryLabel());
1068 SaveLiveRegisters(codegen, locations);
1069
1070 InvokeRuntimeCallingConvention calling_convention;
1071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1072 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001073 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001074 instruction_,
1075 instruction_->GetDexPc(),
1076 this);
1077 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1078 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1079
1080 RestoreLiveRegisters(codegen, locations);
1081 __ b(GetExitLabel());
1082 }
1083
1084 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1085
1086 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001087 const Location out_;
1088 const Location root_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1091};
1092
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001093#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001094// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1095#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Aart Bike9f37602015-10-09 11:15:55 -07001097inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001098 switch (cond) {
1099 case kCondEQ: return EQ;
1100 case kCondNE: return NE;
1101 case kCondLT: return LT;
1102 case kCondLE: return LE;
1103 case kCondGT: return GT;
1104 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001105 case kCondB: return LO;
1106 case kCondBE: return LS;
1107 case kCondA: return HI;
1108 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001109 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 LOG(FATAL) << "Unreachable";
1111 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001112}
1113
Aart Bike9f37602015-10-09 11:15:55 -07001114// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001115inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001116 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001117 case kCondEQ: return EQ;
1118 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001119 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 case kCondLT: return LO;
1121 case kCondLE: return LS;
1122 case kCondGT: return HI;
1123 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001124 // Unsigned remain unchanged.
1125 case kCondB: return LO;
1126 case kCondBE: return LS;
1127 case kCondA: return HI;
1128 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001129 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001130 LOG(FATAL) << "Unreachable";
1131 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001132}
1133
Vladimir Markod6e069b2016-01-18 11:11:01 +00001134inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1135 // The ARM condition codes can express all the necessary branches, see the
1136 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1137 // There is no dex instruction or HIR that would need the missing conditions
1138 // "equal or unordered" or "not equal".
1139 switch (cond) {
1140 case kCondEQ: return EQ;
1141 case kCondNE: return NE /* unordered */;
1142 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1143 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1144 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1145 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1146 default:
1147 LOG(FATAL) << "UNREACHABLE";
1148 UNREACHABLE();
1149 }
1150}
1151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001152void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001153 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154}
1155
1156void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001157 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001158}
1159
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001160size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1161 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1162 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001163}
1164
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001165size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1166 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1167 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001168}
1169
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001170size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1171 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1172 return kArmWordSize;
1173}
1174
1175size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1176 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1177 return kArmWordSize;
1178}
1179
Calin Juravle34166012014-12-19 17:22:29 +00001180CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001181 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001182 const CompilerOptions& compiler_options,
1183 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001184 : CodeGenerator(graph,
1185 kNumberOfCoreRegisters,
1186 kNumberOfSRegisters,
1187 kNumberOfRegisterPairs,
1188 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1189 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001190 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1191 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001192 compiler_options,
1193 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001194 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001195 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001196 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001197 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001198 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001199 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001200 uint32_literals_(std::less<uint32_t>(),
1201 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001202 method_patches_(MethodReferenceComparator(),
1203 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1204 call_patches_(MethodReferenceComparator(),
1205 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001206 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001207 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1208 boot_image_string_patches_(StringReferenceValueComparator(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1210 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001211 boot_image_type_patches_(TypeReferenceValueComparator(),
1212 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1213 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001214 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray07c919f2016-11-09 17:29:03 +00001215 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001216 // Always save the LR register to mimic Quick.
1217 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001218}
1219
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001220void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1221 // Ensure that we fix up branches and literal loads and emit the literal pool.
1222 __ FinalizeCode();
1223
1224 // Adjust native pc offsets in stack maps.
1225 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1226 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1227 uint32_t new_position = __ GetAdjustedPosition(old_position);
1228 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1229 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001230 // Adjust pc offsets for the disassembly information.
1231 if (disasm_info_ != nullptr) {
1232 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1233 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1234 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1235 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1236 it.second.start = __ GetAdjustedPosition(it.second.start);
1237 it.second.end = __ GetAdjustedPosition(it.second.end);
1238 }
1239 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1240 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1241 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1242 }
1243 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001244
1245 CodeGenerator::Finalize(allocator);
1246}
1247
David Brazdil58282f42016-01-14 12:45:10 +00001248void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001249 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001250 blocked_core_registers_[SP] = true;
1251 blocked_core_registers_[LR] = true;
1252 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001253
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001254 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001255 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001256
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001257 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001258 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001259
David Brazdil58282f42016-01-14 12:45:10 +00001260 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001261 // Stubs do not save callee-save floating point registers. If the graph
1262 // is debuggable, we need to deal with these registers differently. For
1263 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001264 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1265 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1266 }
1267 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001268}
1269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001270InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001271 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001272 assembler_(codegen->GetAssembler()),
1273 codegen_(codegen) {}
1274
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001275void CodeGeneratorARM::ComputeSpillMask() {
1276 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1277 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001278 // There is no easy instruction to restore just the PC on thumb2. We spill and
1279 // restore another arbitrary register.
1280 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001281 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1282 // We use vpush and vpop for saving and restoring floating point registers, which take
1283 // a SRegister and the number of registers to save/restore after that SRegister. We
1284 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1285 // but in the range.
1286 if (fpu_spill_mask_ != 0) {
1287 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1288 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1289 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1290 fpu_spill_mask_ |= (1 << i);
1291 }
1292 }
1293}
1294
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001296 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297}
1298
1299static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001300 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001301}
1302
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001303void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001304 bool skip_overflow_check =
1305 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001307 __ Bind(&frame_entry_label_);
1308
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001309 if (HasEmptyFrame()) {
1310 return;
1311 }
1312
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001313 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001314 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1315 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1316 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001317 }
1318
Andreas Gampe501fd632015-09-10 16:11:06 -07001319 __ PushList(core_spill_mask_);
1320 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1321 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001322 if (fpu_spill_mask_ != 0) {
1323 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1324 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001325 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001326 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001327 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001328 int adjust = GetFrameSize() - FrameEntrySpillSize();
1329 __ AddConstant(SP, -adjust);
1330 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001331
1332 // Save the current method if we need it. Note that we do not
1333 // do this in HCurrentMethod, as the instruction might have been removed
1334 // in the SSA graph.
1335 if (RequiresCurrentMethod()) {
1336 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1337 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001338}
1339
1340void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001341 if (HasEmptyFrame()) {
1342 __ bx(LR);
1343 return;
1344 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001345 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001346 int adjust = GetFrameSize() - FrameEntrySpillSize();
1347 __ AddConstant(SP, adjust);
1348 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001349 if (fpu_spill_mask_ != 0) {
1350 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1351 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001352 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001353 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001354 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001355 // Pop LR into PC to return.
1356 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1357 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1358 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001359 __ cfi().RestoreState();
1360 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001361}
1362
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001363void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001364 Label* label = GetLabelOf(block);
1365 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001366}
1367
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001368Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001369 switch (type) {
1370 case Primitive::kPrimBoolean:
1371 case Primitive::kPrimByte:
1372 case Primitive::kPrimChar:
1373 case Primitive::kPrimShort:
1374 case Primitive::kPrimInt:
1375 case Primitive::kPrimNot: {
1376 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001377 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001378 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001379 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001380 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001381 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001382 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001383 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001384
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001385 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001386 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001387 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001388 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001389 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001390 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001391 if (calling_convention.GetRegisterAt(index) == R1) {
1392 // Skip R1, and use R2_R3 instead.
1393 gp_index_++;
1394 index++;
1395 }
1396 }
1397 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1398 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001399 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001400
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001401 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001402 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001403 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001404 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1405 }
1406 }
1407
1408 case Primitive::kPrimFloat: {
1409 uint32_t stack_index = stack_index_++;
1410 if (float_index_ % 2 == 0) {
1411 float_index_ = std::max(double_index_, float_index_);
1412 }
1413 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1414 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1415 } else {
1416 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1417 }
1418 }
1419
1420 case Primitive::kPrimDouble: {
1421 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1422 uint32_t stack_index = stack_index_;
1423 stack_index_ += 2;
1424 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1425 uint32_t index = double_index_;
1426 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001427 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001428 calling_convention.GetFpuRegisterAt(index),
1429 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001430 DCHECK(ExpectedPairLayout(result));
1431 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001432 } else {
1433 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001434 }
1435 }
1436
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001437 case Primitive::kPrimVoid:
1438 LOG(FATAL) << "Unexpected parameter type " << type;
1439 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001440 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001441 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001442}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001443
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001444Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001445 switch (type) {
1446 case Primitive::kPrimBoolean:
1447 case Primitive::kPrimByte:
1448 case Primitive::kPrimChar:
1449 case Primitive::kPrimShort:
1450 case Primitive::kPrimInt:
1451 case Primitive::kPrimNot: {
1452 return Location::RegisterLocation(R0);
1453 }
1454
1455 case Primitive::kPrimFloat: {
1456 return Location::FpuRegisterLocation(S0);
1457 }
1458
1459 case Primitive::kPrimLong: {
1460 return Location::RegisterPairLocation(R0, R1);
1461 }
1462
1463 case Primitive::kPrimDouble: {
1464 return Location::FpuRegisterPairLocation(S0, S1);
1465 }
1466
1467 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001468 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001469 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001470
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001471 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001472}
1473
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001474Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1475 return Location::RegisterLocation(kMethodRegisterArgument);
1476}
1477
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478void CodeGeneratorARM::Move32(Location destination, Location source) {
1479 if (source.Equals(destination)) {
1480 return;
1481 }
1482 if (destination.IsRegister()) {
1483 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001484 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 } else if (destination.IsFpuRegister()) {
1491 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001496 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001499 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001501 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001502 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001503 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001504 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001505 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001506 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1507 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508 }
1509 }
1510}
1511
1512void CodeGeneratorARM::Move64(Location destination, Location source) {
1513 if (source.Equals(destination)) {
1514 return;
1515 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001516 if (destination.IsRegisterPair()) {
1517 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001518 EmitParallelMoves(
1519 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1520 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001521 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001522 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001523 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1524 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001525 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001526 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 } else if (source.IsFpuRegisterPair()) {
1528 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1529 destination.AsRegisterPairHigh<Register>(),
1530 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531 } else {
1532 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001533 DCHECK(ExpectedPairLayout(destination));
1534 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1535 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001537 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001538 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001539 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1540 SP,
1541 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001542 } else if (source.IsRegisterPair()) {
1543 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1544 source.AsRegisterPairLow<Register>(),
1545 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001546 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001547 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001548 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001549 } else {
1550 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001552 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 if (source.AsRegisterPairLow<Register>() == R1) {
1554 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001555 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1556 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001558 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 SP, destination.GetStackIndex());
1560 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001561 } else if (source.IsFpuRegisterPair()) {
1562 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1563 SP,
1564 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 } else {
1566 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001567 EmitParallelMoves(
1568 Location::StackSlot(source.GetStackIndex()),
1569 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001570 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001571 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001572 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1573 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 }
1575 }
1576}
1577
Calin Juravle175dc732015-08-25 15:42:32 +01001578void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1579 DCHECK(location.IsRegister());
1580 __ LoadImmediate(location.AsRegister<Register>(), value);
1581}
1582
Calin Juravlee460d1d2015-09-29 04:52:17 +01001583void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001584 HParallelMove move(GetGraph()->GetArena());
1585 move.AddMove(src, dst, dst_type, nullptr);
1586 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001587}
1588
1589void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1590 if (location.IsRegister()) {
1591 locations->AddTemp(location);
1592 } else if (location.IsRegisterPair()) {
1593 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1594 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1595 } else {
1596 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1597 }
1598}
1599
Calin Juravle175dc732015-08-25 15:42:32 +01001600void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1601 HInstruction* instruction,
1602 uint32_t dex_pc,
1603 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001604 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001605 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001606 if (EntrypointRequiresStackMap(entrypoint)) {
1607 RecordPcInfo(instruction, dex_pc, slow_path);
1608 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001609}
1610
Roland Levillaindec8f632016-07-22 17:10:06 +01001611void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1612 HInstruction* instruction,
1613 SlowPathCode* slow_path) {
1614 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001615 GenerateInvokeRuntime(entry_point_offset);
1616}
1617
1618void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001619 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1620 __ blx(LR);
1621}
1622
David Brazdilfc6a86a2015-06-26 10:33:45 +00001623void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001624 DCHECK(!successor->IsExitBlock());
1625
1626 HBasicBlock* block = got->GetBlock();
1627 HInstruction* previous = got->GetPrevious();
1628
1629 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001630 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001631 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1632 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1633 return;
1634 }
1635
1636 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1637 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1638 }
1639 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001640 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001641 }
1642}
1643
David Brazdilfc6a86a2015-06-26 10:33:45 +00001644void LocationsBuilderARM::VisitGoto(HGoto* got) {
1645 got->SetLocations(nullptr);
1646}
1647
1648void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1649 HandleGoto(got, got->GetSuccessor());
1650}
1651
1652void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1653 try_boundary->SetLocations(nullptr);
1654}
1655
1656void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1657 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1658 if (!successor->IsExitBlock()) {
1659 HandleGoto(try_boundary, successor);
1660 }
1661}
1662
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001663void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001664 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665}
1666
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001667void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001668}
1669
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001670void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1671 Primitive::Type type = instruction->InputAt(0)->GetType();
1672 Location lhs_loc = instruction->GetLocations()->InAt(0);
1673 Location rhs_loc = instruction->GetLocations()->InAt(1);
1674 if (rhs_loc.IsConstant()) {
1675 // 0.0 is the only immediate that can be encoded directly in
1676 // a VCMP instruction.
1677 //
1678 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1679 // specify that in a floating-point comparison, positive zero
1680 // and negative zero are considered equal, so we can use the
1681 // literal 0.0 for both cases here.
1682 //
1683 // Note however that some methods (Float.equal, Float.compare,
1684 // Float.compareTo, Double.equal, Double.compare,
1685 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1686 // StrictMath.min) consider 0.0 to be (strictly) greater than
1687 // -0.0. So if we ever translate calls to these methods into a
1688 // HCompare instruction, we must handle the -0.0 case with
1689 // care here.
1690 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1691 if (type == Primitive::kPrimFloat) {
1692 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1693 } else {
1694 DCHECK_EQ(type, Primitive::kPrimDouble);
1695 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1696 }
1697 } else {
1698 if (type == Primitive::kPrimFloat) {
1699 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1700 } else {
1701 DCHECK_EQ(type, Primitive::kPrimDouble);
1702 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1703 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1704 }
1705 }
1706}
1707
Roland Levillain4fa13f62015-07-06 18:11:54 +01001708void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1709 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001710 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001712 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713}
1714
1715void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1716 Label* true_label,
1717 Label* false_label) {
1718 LocationSummary* locations = cond->GetLocations();
1719 Location left = locations->InAt(0);
1720 Location right = locations->InAt(1);
1721 IfCondition if_cond = cond->GetCondition();
1722
1723 Register left_high = left.AsRegisterPairHigh<Register>();
1724 Register left_low = left.AsRegisterPairLow<Register>();
1725 IfCondition true_high_cond = if_cond;
1726 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001727 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001728
1729 // Set the conditions for the test, remembering that == needs to be
1730 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001731 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001732 switch (if_cond) {
1733 case kCondEQ:
1734 case kCondNE:
1735 // Nothing to do.
1736 break;
1737 case kCondLT:
1738 false_high_cond = kCondGT;
1739 break;
1740 case kCondLE:
1741 true_high_cond = kCondLT;
1742 break;
1743 case kCondGT:
1744 false_high_cond = kCondLT;
1745 break;
1746 case kCondGE:
1747 true_high_cond = kCondGT;
1748 break;
Aart Bike9f37602015-10-09 11:15:55 -07001749 case kCondB:
1750 false_high_cond = kCondA;
1751 break;
1752 case kCondBE:
1753 true_high_cond = kCondB;
1754 break;
1755 case kCondA:
1756 false_high_cond = kCondB;
1757 break;
1758 case kCondAE:
1759 true_high_cond = kCondA;
1760 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 }
1762 if (right.IsConstant()) {
1763 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1764 int32_t val_low = Low32Bits(value);
1765 int32_t val_high = High32Bits(value);
1766
Vladimir Markoac6ac102015-12-17 12:14:00 +00001767 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001768 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001769 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001770 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001771 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001773 __ b(true_label, ARMCondition(true_high_cond));
1774 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001775 }
1776 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001777 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 } else {
1779 Register right_high = right.AsRegisterPairHigh<Register>();
1780 Register right_low = right.AsRegisterPairLow<Register>();
1781
1782 __ cmp(left_high, ShifterOperand(right_high));
1783 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001784 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001786 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001787 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001788 __ b(true_label, ARMCondition(true_high_cond));
1789 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001790 }
1791 // Must be equal high, so compare the lows.
1792 __ cmp(left_low, ShifterOperand(right_low));
1793 }
1794 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001795 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001796 __ b(true_label, final_condition);
1797}
1798
David Brazdil0debae72015-11-12 18:37:00 +00001799void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1800 Label* true_target_in,
1801 Label* false_target_in) {
1802 // Generated branching requires both targets to be explicit. If either of the
1803 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1804 Label fallthrough_target;
1805 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1806 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1807
Roland Levillain4fa13f62015-07-06 18:11:54 +01001808 Primitive::Type type = condition->InputAt(0)->GetType();
1809 switch (type) {
1810 case Primitive::kPrimLong:
1811 GenerateLongComparesAndJumps(condition, true_target, false_target);
1812 break;
1813 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001814 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001815 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 GenerateFPJumps(condition, true_target, false_target);
1817 break;
1818 default:
1819 LOG(FATAL) << "Unexpected compare type " << type;
1820 }
1821
David Brazdil0debae72015-11-12 18:37:00 +00001822 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001823 __ b(false_target);
1824 }
David Brazdil0debae72015-11-12 18:37:00 +00001825
1826 if (fallthrough_target.IsLinked()) {
1827 __ Bind(&fallthrough_target);
1828 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001829}
1830
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001831void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001832 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001833 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001834 Label* false_target) {
1835 HInstruction* cond = instruction->InputAt(condition_input_index);
1836
1837 if (true_target == nullptr && false_target == nullptr) {
1838 // Nothing to do. The code always falls through.
1839 return;
1840 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001841 // Constant condition, statically compared against "true" (integer value 1).
1842 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001843 if (true_target != nullptr) {
1844 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001845 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001846 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001847 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001848 if (false_target != nullptr) {
1849 __ b(false_target);
1850 }
1851 }
1852 return;
1853 }
1854
1855 // The following code generates these patterns:
1856 // (1) true_target == nullptr && false_target != nullptr
1857 // - opposite condition true => branch to false_target
1858 // (2) true_target != nullptr && false_target == nullptr
1859 // - condition true => branch to true_target
1860 // (3) true_target != nullptr && false_target != nullptr
1861 // - condition true => branch to true_target
1862 // - branch to false_target
1863 if (IsBooleanValueOrMaterializedCondition(cond)) {
1864 // Condition has been materialized, compare the output to 0.
1865 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1866 DCHECK(cond_val.IsRegister());
1867 if (true_target == nullptr) {
1868 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1869 } else {
1870 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001872 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001873 // Condition has not been materialized. Use its inputs as the comparison and
1874 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001875 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001876
1877 // If this is a long or FP comparison that has been folded into
1878 // the HCondition, generate the comparison directly.
1879 Primitive::Type type = condition->InputAt(0)->GetType();
1880 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1881 GenerateCompareTestAndBranch(condition, true_target, false_target);
1882 return;
1883 }
1884
1885 LocationSummary* locations = cond->GetLocations();
1886 DCHECK(locations->InAt(0).IsRegister());
1887 Register left = locations->InAt(0).AsRegister<Register>();
1888 Location right = locations->InAt(1);
1889 if (right.IsRegister()) {
1890 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001891 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001892 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001893 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001894 }
1895 if (true_target == nullptr) {
1896 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1897 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001898 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001899 }
Dave Allison20dfc792014-06-16 20:44:29 -07001900 }
David Brazdil0debae72015-11-12 18:37:00 +00001901
1902 // If neither branch falls through (case 3), the conditional branch to `true_target`
1903 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1904 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001905 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906 }
1907}
1908
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001909void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1911 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001912 locations->SetInAt(0, Location::RequiresRegister());
1913 }
1914}
1915
1916void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001917 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1918 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1919 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1920 nullptr : codegen_->GetLabelOf(true_successor);
1921 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1922 nullptr : codegen_->GetLabelOf(false_successor);
1923 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001924}
1925
1926void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1927 LocationSummary* locations = new (GetGraph()->GetArena())
1928 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001929 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001930 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001931 locations->SetInAt(0, Location::RequiresRegister());
1932 }
1933}
1934
1935void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001936 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001937 GenerateTestAndBranch(deoptimize,
1938 /* condition_input_index */ 0,
1939 slow_path->GetEntryLabel(),
1940 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001941}
Dave Allison20dfc792014-06-16 20:44:29 -07001942
David Brazdil74eb1b22015-12-14 11:44:01 +00001943void LocationsBuilderARM::VisitSelect(HSelect* select) {
1944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1945 if (Primitive::IsFloatingPointType(select->GetType())) {
1946 locations->SetInAt(0, Location::RequiresFpuRegister());
1947 locations->SetInAt(1, Location::RequiresFpuRegister());
1948 } else {
1949 locations->SetInAt(0, Location::RequiresRegister());
1950 locations->SetInAt(1, Location::RequiresRegister());
1951 }
1952 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1953 locations->SetInAt(2, Location::RequiresRegister());
1954 }
1955 locations->SetOut(Location::SameAsFirstInput());
1956}
1957
1958void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1959 LocationSummary* locations = select->GetLocations();
1960 Label false_target;
1961 GenerateTestAndBranch(select,
1962 /* condition_input_index */ 2,
1963 /* true_target */ nullptr,
1964 &false_target);
1965 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1966 __ Bind(&false_target);
1967}
1968
David Srbecky0cf44932015-12-09 14:09:59 +00001969void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1970 new (GetGraph()->GetArena()) LocationSummary(info);
1971}
1972
David Srbeckyd28f4a02016-03-14 17:14:24 +00001973void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1974 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001975}
1976
1977void CodeGeneratorARM::GenerateNop() {
1978 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001979}
1980
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001982 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001983 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001984 // Handle the long/FP comparisons made in instruction simplification.
1985 switch (cond->InputAt(0)->GetType()) {
1986 case Primitive::kPrimLong:
1987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001989 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001990 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1991 }
1992 break;
1993
1994 case Primitive::kPrimFloat:
1995 case Primitive::kPrimDouble:
1996 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001997 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001998 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2000 }
2001 break;
2002
2003 default:
2004 locations->SetInAt(0, Location::RequiresRegister());
2005 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002006 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2008 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010}
2011
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002013 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002014 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002015 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002016
2017 LocationSummary* locations = cond->GetLocations();
2018 Location left = locations->InAt(0);
2019 Location right = locations->InAt(1);
2020 Register out = locations->Out().AsRegister<Register>();
2021 Label true_label, false_label;
2022
2023 switch (cond->InputAt(0)->GetType()) {
2024 default: {
2025 // Integer case.
2026 if (right.IsRegister()) {
2027 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2028 } else {
2029 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002030 __ CmpConstant(left.AsRegister<Register>(),
2031 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002032 }
Aart Bike9f37602015-10-09 11:15:55 -07002033 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002034 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002035 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002036 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002037 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002038 return;
2039 }
2040 case Primitive::kPrimLong:
2041 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2042 break;
2043 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002044 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002045 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002046 GenerateFPJumps(cond, &true_label, &false_label);
2047 break;
2048 }
2049
2050 // Convert the jumps into the result.
2051 Label done_label;
2052
2053 // False case: result = 0.
2054 __ Bind(&false_label);
2055 __ LoadImmediate(out, 0);
2056 __ b(&done_label);
2057
2058 // True case: result = 1.
2059 __ Bind(&true_label);
2060 __ LoadImmediate(out, 1);
2061 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002062}
2063
2064void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002066}
2067
2068void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002069 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002070}
2071
2072void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002073 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002074}
2075
2076void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002078}
2079
2080void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002081 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002082}
2083
2084void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002086}
2087
2088void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002089 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002090}
2091
2092void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002094}
2095
2096void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002097 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002098}
2099
2100void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002101 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002102}
2103
2104void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002106}
2107
2108void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002109 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002110}
2111
Aart Bike9f37602015-10-09 11:15:55 -07002112void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002114}
2115
2116void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002118}
2119
2120void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002122}
2123
2124void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002125 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002126}
2127
2128void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002129 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002130}
2131
2132void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002133 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002134}
2135
2136void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002137 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002138}
2139
2140void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002141 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002142}
2143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002145 LocationSummary* locations =
2146 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002147 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148}
2149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002150void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002151 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152}
2153
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2155 LocationSummary* locations =
2156 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2157 locations->SetOut(Location::ConstantLocation(constant));
2158}
2159
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002160void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002162}
2163
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002165 LocationSummary* locations =
2166 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002167 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168}
2169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002170void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 // Will be generated at use site.
2172}
2173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002174void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2175 LocationSummary* locations =
2176 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2177 locations->SetOut(Location::ConstantLocation(constant));
2178}
2179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002180void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181 // Will be generated at use site.
2182}
2183
2184void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2187 locations->SetOut(Location::ConstantLocation(constant));
2188}
2189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002190void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002191 // Will be generated at use site.
2192}
2193
Calin Juravle27df7582015-04-17 19:12:31 +01002194void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2195 memory_barrier->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002199 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002200}
2201
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002203 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002204}
2205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002206void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002207 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002208}
2209
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002213 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002214}
2215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002216void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002217 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218}
2219
Calin Juravle175dc732015-08-25 15:42:32 +01002220void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2221 // The trampoline uses the same calling convention as dex calling conventions,
2222 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2223 // the method_idx.
2224 HandleInvoke(invoke);
2225}
2226
2227void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2228 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2229}
2230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002231void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002232 // Explicit clinit checks triggered by static invokes must have been pruned by
2233 // art::PrepareForRegisterAllocation.
2234 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002235
Vladimir Marko68c981f2016-08-26 13:13:33 +01002236 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002237 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002238 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2239 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2240 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002241 return;
2242 }
2243
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002244 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002245
2246 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2247 if (invoke->HasPcRelativeDexCache()) {
2248 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2249 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002250}
2251
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002252static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2253 if (invoke->GetLocations()->Intrinsified()) {
2254 IntrinsicCodeGeneratorARM intrinsic(codegen);
2255 intrinsic.Dispatch(invoke);
2256 return true;
2257 }
2258 return false;
2259}
2260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002261void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002262 // Explicit clinit checks triggered by static invokes must have been pruned by
2263 // art::PrepareForRegisterAllocation.
2264 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002265
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002266 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2267 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002268 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002269
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002270 LocationSummary* locations = invoke->GetLocations();
2271 codegen_->GenerateStaticOrDirectCall(
2272 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002273 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002274}
2275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002277 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002278 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002279}
2280
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002282 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002283 if (intrinsic.TryDispatch(invoke)) {
2284 return;
2285 }
2286
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 HandleInvoke(invoke);
2288}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002289
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002290void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002291 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2292 return;
2293 }
2294
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002295 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002296 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002297 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002298}
2299
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2301 HandleInvoke(invoke);
2302 // Add the hidden argument.
2303 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2304}
2305
2306void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2307 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002308 LocationSummary* locations = invoke->GetLocations();
2309 Register temp = locations->GetTemp(0).AsRegister<Register>();
2310 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002311 Location receiver = locations->InAt(0);
2312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2313
Roland Levillain3b359c72015-11-17 19:35:12 +00002314 // Set the hidden argument. This is safe to do this here, as R12
2315 // won't be modified thereafter, before the `blx` (call) instruction.
2316 DCHECK_EQ(R12, hidden_reg);
2317 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002318
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002319 if (receiver.IsStackSlot()) {
2320 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002321 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002322 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2323 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002324 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002327 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002328 // Instead of simply (possibly) unpoisoning `temp` here, we should
2329 // emit a read barrier for the previous class reference load.
2330 // However this is not required in practice, as this is an
2331 // intermediate/temporary reference and because the current
2332 // concurrent copying collector keeps the from-space memory
2333 // intact/accessible until the end of the marking phase (the
2334 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002335 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002336 __ LoadFromOffset(kLoadWord, temp, temp,
2337 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2338 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002339 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002340 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002341 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002342 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002343 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002344 // LR = temp->GetEntryPoint();
2345 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2346 // LR();
2347 __ blx(LR);
2348 DCHECK(!codegen_->IsLeafMethod());
2349 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2350}
2351
Roland Levillain88cb1752014-10-20 16:36:47 +01002352void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2355 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002356 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002357 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2359 break;
2360 }
2361 case Primitive::kPrimLong: {
2362 locations->SetInAt(0, Location::RequiresRegister());
2363 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002364 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002365 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002366
Roland Levillain88cb1752014-10-20 16:36:47 +01002367 case Primitive::kPrimFloat:
2368 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2375 }
2376}
2377
2378void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2379 LocationSummary* locations = neg->GetLocations();
2380 Location out = locations->Out();
2381 Location in = locations->InAt(0);
2382 switch (neg->GetResultType()) {
2383 case Primitive::kPrimInt:
2384 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002386 break;
2387
2388 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002389 DCHECK(in.IsRegisterPair());
2390 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2391 __ rsbs(out.AsRegisterPairLow<Register>(),
2392 in.AsRegisterPairLow<Register>(),
2393 ShifterOperand(0));
2394 // We cannot emit an RSC (Reverse Subtract with Carry)
2395 // instruction here, as it does not exist in the Thumb-2
2396 // instruction set. We use the following approach
2397 // using SBC and SUB instead.
2398 //
2399 // out.hi = -C
2400 __ sbc(out.AsRegisterPairHigh<Register>(),
2401 out.AsRegisterPairHigh<Register>(),
2402 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2403 // out.hi = out.hi - in.hi
2404 __ sub(out.AsRegisterPairHigh<Register>(),
2405 out.AsRegisterPairHigh<Register>(),
2406 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2407 break;
2408
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002410 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002412 break;
2413
Roland Levillain88cb1752014-10-20 16:36:47 +01002414 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002415 DCHECK(in.IsFpuRegisterPair());
2416 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2417 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2422 }
2423}
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 Primitive::Type result_type = conversion->GetResultType();
2427 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002428 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002429
Roland Levillain5b3ee562015-04-14 16:02:41 +01002430 // The float-to-long, double-to-long and long-to-float type conversions
2431 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002432 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002433 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2434 && result_type == Primitive::kPrimLong)
2435 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002436 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002437 : LocationSummary::kNoCall;
2438 LocationSummary* locations =
2439 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2440
David Brazdilb2bd1c52015-03-25 11:17:37 +00002441 // The Java language does not allow treating boolean as an integral type but
2442 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002445 case Primitive::kPrimByte:
2446 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002447 case Primitive::kPrimLong:
2448 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002449 case Primitive::kPrimBoolean:
2450 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002451 case Primitive::kPrimShort:
2452 case Primitive::kPrimInt:
2453 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002454 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2457 break;
2458
2459 default:
2460 LOG(FATAL) << "Unexpected type conversion from " << input_type
2461 << " to " << result_type;
2462 }
2463 break;
2464
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 case Primitive::kPrimShort:
2466 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002467 case Primitive::kPrimLong:
2468 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002469 case Primitive::kPrimBoolean:
2470 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002471 case Primitive::kPrimByte:
2472 case Primitive::kPrimInt:
2473 case Primitive::kPrimChar:
2474 // Processing a Dex `int-to-short' instruction.
2475 locations->SetInAt(0, Location::RequiresRegister());
2476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2477 break;
2478
2479 default:
2480 LOG(FATAL) << "Unexpected type conversion from " << input_type
2481 << " to " << result_type;
2482 }
2483 break;
2484
Roland Levillain946e1432014-11-11 17:35:19 +00002485 case Primitive::kPrimInt:
2486 switch (input_type) {
2487 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002488 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002489 locations->SetInAt(0, Location::Any());
2490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2491 break;
2492
2493 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002494 // Processing a Dex `float-to-int' instruction.
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresRegister());
2497 locations->AddTemp(Location::RequiresFpuRegister());
2498 break;
2499
Roland Levillain946e1432014-11-11 17:35:19 +00002500 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002501 // Processing a Dex `double-to-int' instruction.
2502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresRegister());
2504 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511 break;
2512
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 case Primitive::kPrimLong:
2514 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002515 case Primitive::kPrimBoolean:
2516 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 case Primitive::kPrimByte:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002520 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002521 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 locations->SetInAt(0, Location::RequiresRegister());
2523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 break;
2525
Roland Levillain624279f2014-12-04 11:54:28 +00002526 case Primitive::kPrimFloat: {
2527 // Processing a Dex `float-to-long' instruction.
2528 InvokeRuntimeCallingConvention calling_convention;
2529 locations->SetInAt(0, Location::FpuRegisterLocation(
2530 calling_convention.GetFpuRegisterAt(0)));
2531 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2532 break;
2533 }
2534
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002535 case Primitive::kPrimDouble: {
2536 // Processing a Dex `double-to-long' instruction.
2537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2539 calling_convention.GetFpuRegisterAt(0),
2540 calling_convention.GetFpuRegisterAt(1)));
2541 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002542 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002543 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 }
2549 break;
2550
Roland Levillain981e4542014-11-14 11:47:14 +00002551 case Primitive::kPrimChar:
2552 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002553 case Primitive::kPrimLong:
2554 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002555 case Primitive::kPrimBoolean:
2556 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002557 case Primitive::kPrimByte:
2558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002560 // Processing a Dex `int-to-char' instruction.
2561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
Roland Levillaindff1f282014-11-05 14:15:05 +00002571 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002572 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002573 case Primitive::kPrimBoolean:
2574 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002575 case Primitive::kPrimByte:
2576 case Primitive::kPrimShort:
2577 case Primitive::kPrimInt:
2578 case Primitive::kPrimChar:
2579 // Processing a Dex `int-to-float' instruction.
2580 locations->SetInAt(0, Location::RequiresRegister());
2581 locations->SetOut(Location::RequiresFpuRegister());
2582 break;
2583
Roland Levillain5b3ee562015-04-14 16:02:41 +01002584 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002585 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002586 InvokeRuntimeCallingConvention calling_convention;
2587 locations->SetInAt(0, Location::RegisterPairLocation(
2588 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2589 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002590 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002591 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002592
Roland Levillaincff13742014-11-17 14:32:17 +00002593 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002594 // Processing a Dex `double-to-float' instruction.
2595 locations->SetInAt(0, Location::RequiresFpuRegister());
2596 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 };
2603 break;
2604
Roland Levillaindff1f282014-11-05 14:15:05 +00002605 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002606 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002607 case Primitive::kPrimBoolean:
2608 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002609 case Primitive::kPrimByte:
2610 case Primitive::kPrimShort:
2611 case Primitive::kPrimInt:
2612 case Primitive::kPrimChar:
2613 // Processing a Dex `int-to-double' instruction.
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetOut(Location::RequiresFpuRegister());
2616 break;
2617
2618 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002619 // Processing a Dex `long-to-double' instruction.
2620 locations->SetInAt(0, Location::RequiresRegister());
2621 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002622 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002623 locations->AddTemp(Location::RequiresFpuRegister());
2624 break;
2625
Roland Levillaincff13742014-11-17 14:32:17 +00002626 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002627 // Processing a Dex `float-to-double' instruction.
2628 locations->SetInAt(0, Location::RequiresFpuRegister());
2629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002636 break;
2637
2638 default:
2639 LOG(FATAL) << "Unexpected type conversion from " << input_type
2640 << " to " << result_type;
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2645 LocationSummary* locations = conversion->GetLocations();
2646 Location out = locations->Out();
2647 Location in = locations->InAt(0);
2648 Primitive::Type result_type = conversion->GetResultType();
2649 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002650 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002651 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002652 case Primitive::kPrimByte:
2653 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002654 case Primitive::kPrimLong:
2655 // Type conversion from long to byte is a result of code transformations.
2656 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2657 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002658 case Primitive::kPrimBoolean:
2659 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
2662 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002663 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002665 break;
2666
2667 default:
2668 LOG(FATAL) << "Unexpected type conversion from " << input_type
2669 << " to " << result_type;
2670 }
2671 break;
2672
Roland Levillain01a8d712014-11-14 16:27:39 +00002673 case Primitive::kPrimShort:
2674 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002675 case Primitive::kPrimLong:
2676 // Type conversion from long to short is a result of code transformations.
2677 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2678 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
2684 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 break;
2687
2688 default:
2689 LOG(FATAL) << "Unexpected type conversion from " << input_type
2690 << " to " << result_type;
2691 }
2692 break;
2693
Roland Levillain946e1432014-11-11 17:35:19 +00002694 case Primitive::kPrimInt:
2695 switch (input_type) {
2696 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002697 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002698 DCHECK(out.IsRegister());
2699 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002701 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002703 } else {
2704 DCHECK(in.IsConstant());
2705 DCHECK(in.GetConstant()->IsLongConstant());
2706 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002708 }
2709 break;
2710
Roland Levillain3f8f9362014-12-02 17:45:01 +00002711 case Primitive::kPrimFloat: {
2712 // Processing a Dex `float-to-int' instruction.
2713 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002714 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002715 __ vmovrs(out.AsRegister<Register>(), temp);
2716 break;
2717 }
2718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002719 case Primitive::kPrimDouble: {
2720 // Processing a Dex `double-to-int' instruction.
2721 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002722 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002723 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002724 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002725 }
Roland Levillain946e1432014-11-11 17:35:19 +00002726
2727 default:
2728 LOG(FATAL) << "Unexpected type conversion from " << input_type
2729 << " to " << result_type;
2730 }
2731 break;
2732
Roland Levillaindff1f282014-11-05 14:15:05 +00002733 case Primitive::kPrimLong:
2734 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002735 case Primitive::kPrimBoolean:
2736 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002737 case Primitive::kPrimByte:
2738 case Primitive::kPrimShort:
2739 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002740 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002741 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002742 DCHECK(out.IsRegisterPair());
2743 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 // Sign extension.
2746 __ Asr(out.AsRegisterPairHigh<Register>(),
2747 out.AsRegisterPairLow<Register>(),
2748 31);
2749 break;
2750
2751 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002752 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002753 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002754 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002755 break;
2756
Roland Levillaindff1f282014-11-05 14:15:05 +00002757 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002758 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002759 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002760 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002761 break;
2762
2763 default:
2764 LOG(FATAL) << "Unexpected type conversion from " << input_type
2765 << " to " << result_type;
2766 }
2767 break;
2768
Roland Levillain981e4542014-11-14 11:47:14 +00002769 case Primitive::kPrimChar:
2770 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 case Primitive::kPrimLong:
2772 // Type conversion from long to char is a result of code transformations.
2773 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2774 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002775 case Primitive::kPrimBoolean:
2776 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002777 case Primitive::kPrimByte:
2778 case Primitive::kPrimShort:
2779 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002780 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002782 break;
2783
2784 default:
2785 LOG(FATAL) << "Unexpected type conversion from " << input_type
2786 << " to " << result_type;
2787 }
2788 break;
2789
Roland Levillaindff1f282014-11-05 14:15:05 +00002790 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002791 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002792 case Primitive::kPrimBoolean:
2793 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002794 case Primitive::kPrimByte:
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimInt:
2797 case Primitive::kPrimChar: {
2798 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2800 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002801 break;
2802 }
2803
Roland Levillain5b3ee562015-04-14 16:02:41 +01002804 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002805 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002806 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002807 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002808 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002809
Roland Levillaincff13742014-11-17 14:32:17 +00002810 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002811 // Processing a Dex `double-to-float' instruction.
2812 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2813 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 };
2820 break;
2821
Roland Levillaindff1f282014-11-05 14:15:05 +00002822 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002823 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002824 case Primitive::kPrimBoolean:
2825 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002826 case Primitive::kPrimByte:
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimInt:
2829 case Primitive::kPrimChar: {
2830 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002832 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2833 out.AsFpuRegisterPairLow<SRegister>());
2834 break;
2835 }
2836
Roland Levillain647b9ed2014-11-27 12:06:00 +00002837 case Primitive::kPrimLong: {
2838 // Processing a Dex `long-to-double' instruction.
2839 Register low = in.AsRegisterPairLow<Register>();
2840 Register high = in.AsRegisterPairHigh<Register>();
2841 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2842 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002843 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002844 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002845 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2846 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002847
Roland Levillain682393c2015-04-14 15:57:52 +01002848 // temp_d = int-to-double(high)
2849 __ vmovsr(temp_s, high);
2850 __ vcvtdi(temp_d, temp_s);
2851 // constant_d = k2Pow32EncodingForDouble
2852 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2853 // out_d = unsigned-to-double(low)
2854 __ vmovsr(out_s, low);
2855 __ vcvtdu(out_d, out_s);
2856 // out_d += temp_d * constant_d
2857 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002858 break;
2859 }
2860
Roland Levillaincff13742014-11-17 14:32:17 +00002861 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002862 // Processing a Dex `float-to-double' instruction.
2863 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2864 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002865 break;
2866
2867 default:
2868 LOG(FATAL) << "Unexpected type conversion from " << input_type
2869 << " to " << result_type;
2870 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
2876 }
2877}
2878
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002879void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002882 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002883 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002884 locations->SetInAt(0, Location::RequiresRegister());
2885 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2887 break;
2888 }
2889
2890 case Primitive::kPrimLong: {
2891 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002892 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002894 break;
2895 }
2896
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002897 case Primitive::kPrimFloat:
2898 case Primitive::kPrimDouble: {
2899 locations->SetInAt(0, Location::RequiresFpuRegister());
2900 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002902 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002903 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002905 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002907 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002908}
2909
2910void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2911 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002912 Location out = locations->Out();
2913 Location first = locations->InAt(0);
2914 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002915 switch (add->GetResultType()) {
2916 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002917 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002918 __ add(out.AsRegister<Register>(),
2919 first.AsRegister<Register>(),
2920 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002922 __ AddConstant(out.AsRegister<Register>(),
2923 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002924 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002925 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002928 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002929 if (second.IsConstant()) {
2930 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2931 GenerateAddLongConst(out, first, value);
2932 } else {
2933 DCHECK(second.IsRegisterPair());
2934 __ adds(out.AsRegisterPairLow<Register>(),
2935 first.AsRegisterPairLow<Register>(),
2936 ShifterOperand(second.AsRegisterPairLow<Register>()));
2937 __ adc(out.AsRegisterPairHigh<Register>(),
2938 first.AsRegisterPairHigh<Register>(),
2939 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002945 __ vadds(out.AsFpuRegister<SRegister>(),
2946 first.AsFpuRegister<SRegister>(),
2947 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948 break;
2949
2950 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002951 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2952 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2953 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002954 break;
2955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 }
2959}
2960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002961void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002964 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002965 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2969 break;
2970 }
2971
2972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002974 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002976 break;
2977 }
Calin Juravle11351682014-10-23 15:38:15 +01002978 case Primitive::kPrimFloat:
2979 case Primitive::kPrimDouble: {
2980 locations->SetInAt(0, Location::RequiresFpuRegister());
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002983 break;
Calin Juravle11351682014-10-23 15:38:15 +01002984 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002985 default:
Calin Juravle11351682014-10-23 15:38:15 +01002986 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002987 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002988}
2989
2990void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2991 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002992 Location out = locations->Out();
2993 Location first = locations->InAt(0);
2994 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002995 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002996 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002997 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002998 __ sub(out.AsRegister<Register>(),
2999 first.AsRegister<Register>(),
3000 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003001 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ AddConstant(out.AsRegister<Register>(),
3003 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003004 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003005 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008
Calin Juravle11351682014-10-23 15:38:15 +01003009 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003010 if (second.IsConstant()) {
3011 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3012 GenerateAddLongConst(out, first, -value);
3013 } else {
3014 DCHECK(second.IsRegisterPair());
3015 __ subs(out.AsRegisterPairLow<Register>(),
3016 first.AsRegisterPairLow<Register>(),
3017 ShifterOperand(second.AsRegisterPairLow<Register>()));
3018 __ sbc(out.AsRegisterPairHigh<Register>(),
3019 first.AsRegisterPairHigh<Register>(),
3020 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003022 break;
Calin Juravle11351682014-10-23 15:38:15 +01003023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024
Calin Juravle11351682014-10-23 15:38:15 +01003025 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003026 __ vsubs(out.AsFpuRegister<SRegister>(),
3027 first.AsFpuRegister<SRegister>(),
3028 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029 break;
Calin Juravle11351682014-10-23 15:38:15 +01003030 }
3031
3032 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003033 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3034 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3035 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003036 break;
3037 }
3038
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 default:
Calin Juravle11351682014-10-23 15:38:15 +01003041 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003042 }
3043}
3044
Calin Juravle34bacdf2014-10-07 20:23:36 +01003045void LocationsBuilderARM::VisitMul(HMul* mul) {
3046 LocationSummary* locations =
3047 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3048 switch (mul->GetResultType()) {
3049 case Primitive::kPrimInt:
3050 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003051 locations->SetInAt(0, Location::RequiresRegister());
3052 locations->SetInAt(1, Location::RequiresRegister());
3053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003054 break;
3055 }
3056
Calin Juravleb5bfa962014-10-21 18:02:24 +01003057 case Primitive::kPrimFloat:
3058 case Primitive::kPrimDouble: {
3059 locations->SetInAt(0, Location::RequiresFpuRegister());
3060 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003061 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003062 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003063 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064
3065 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003066 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003067 }
3068}
3069
3070void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3071 LocationSummary* locations = mul->GetLocations();
3072 Location out = locations->Out();
3073 Location first = locations->InAt(0);
3074 Location second = locations->InAt(1);
3075 switch (mul->GetResultType()) {
3076 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003077 __ mul(out.AsRegister<Register>(),
3078 first.AsRegister<Register>(),
3079 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080 break;
3081 }
3082 case Primitive::kPrimLong: {
3083 Register out_hi = out.AsRegisterPairHigh<Register>();
3084 Register out_lo = out.AsRegisterPairLow<Register>();
3085 Register in1_hi = first.AsRegisterPairHigh<Register>();
3086 Register in1_lo = first.AsRegisterPairLow<Register>();
3087 Register in2_hi = second.AsRegisterPairHigh<Register>();
3088 Register in2_lo = second.AsRegisterPairLow<Register>();
3089
3090 // Extra checks to protect caused by the existence of R1_R2.
3091 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3092 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3093 DCHECK_NE(out_hi, in1_lo);
3094 DCHECK_NE(out_hi, in2_lo);
3095
3096 // input: in1 - 64 bits, in2 - 64 bits
3097 // output: out
3098 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3099 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3100 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3101
3102 // IP <- in1.lo * in2.hi
3103 __ mul(IP, in1_lo, in2_hi);
3104 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3105 __ mla(out_hi, in1_hi, in2_lo, IP);
3106 // out.lo <- (in1.lo * in2.lo)[31:0];
3107 __ umull(out_lo, IP, in1_lo, in2_lo);
3108 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3109 __ add(out_hi, out_hi, ShifterOperand(IP));
3110 break;
3111 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112
3113 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003114 __ vmuls(out.AsFpuRegister<SRegister>(),
3115 first.AsFpuRegister<SRegister>(),
3116 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003117 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 }
3119
3120 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003121 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3122 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3123 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 break;
3125 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126
3127 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 }
3130}
3131
Zheng Xuc6667102015-05-15 16:08:45 +08003132void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3133 DCHECK(instruction->IsDiv() || instruction->IsRem());
3134 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3135
3136 LocationSummary* locations = instruction->GetLocations();
3137 Location second = locations->InAt(1);
3138 DCHECK(second.IsConstant());
3139
3140 Register out = locations->Out().AsRegister<Register>();
3141 Register dividend = locations->InAt(0).AsRegister<Register>();
3142 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3143 DCHECK(imm == 1 || imm == -1);
3144
3145 if (instruction->IsRem()) {
3146 __ LoadImmediate(out, 0);
3147 } else {
3148 if (imm == 1) {
3149 __ Mov(out, dividend);
3150 } else {
3151 __ rsb(out, dividend, ShifterOperand(0));
3152 }
3153 }
3154}
3155
3156void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3157 DCHECK(instruction->IsDiv() || instruction->IsRem());
3158 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3159
3160 LocationSummary* locations = instruction->GetLocations();
3161 Location second = locations->InAt(1);
3162 DCHECK(second.IsConstant());
3163
3164 Register out = locations->Out().AsRegister<Register>();
3165 Register dividend = locations->InAt(0).AsRegister<Register>();
3166 Register temp = locations->GetTemp(0).AsRegister<Register>();
3167 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003168 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003169 int ctz_imm = CTZ(abs_imm);
3170
3171 if (ctz_imm == 1) {
3172 __ Lsr(temp, dividend, 32 - ctz_imm);
3173 } else {
3174 __ Asr(temp, dividend, 31);
3175 __ Lsr(temp, temp, 32 - ctz_imm);
3176 }
3177 __ add(out, temp, ShifterOperand(dividend));
3178
3179 if (instruction->IsDiv()) {
3180 __ Asr(out, out, ctz_imm);
3181 if (imm < 0) {
3182 __ rsb(out, out, ShifterOperand(0));
3183 }
3184 } else {
3185 __ ubfx(out, out, 0, ctz_imm);
3186 __ sub(out, out, ShifterOperand(temp));
3187 }
3188}
3189
3190void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3191 DCHECK(instruction->IsDiv() || instruction->IsRem());
3192 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3193
3194 LocationSummary* locations = instruction->GetLocations();
3195 Location second = locations->InAt(1);
3196 DCHECK(second.IsConstant());
3197
3198 Register out = locations->Out().AsRegister<Register>();
3199 Register dividend = locations->InAt(0).AsRegister<Register>();
3200 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3201 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3202 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3203
3204 int64_t magic;
3205 int shift;
3206 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3207
3208 __ LoadImmediate(temp1, magic);
3209 __ smull(temp2, temp1, dividend, temp1);
3210
3211 if (imm > 0 && magic < 0) {
3212 __ add(temp1, temp1, ShifterOperand(dividend));
3213 } else if (imm < 0 && magic > 0) {
3214 __ sub(temp1, temp1, ShifterOperand(dividend));
3215 }
3216
3217 if (shift != 0) {
3218 __ Asr(temp1, temp1, shift);
3219 }
3220
3221 if (instruction->IsDiv()) {
3222 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3223 } else {
3224 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3225 // TODO: Strength reduction for mls.
3226 __ LoadImmediate(temp2, imm);
3227 __ mls(out, temp1, temp2, dividend);
3228 }
3229}
3230
3231void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3232 DCHECK(instruction->IsDiv() || instruction->IsRem());
3233 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 Location second = locations->InAt(1);
3237 DCHECK(second.IsConstant());
3238
3239 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3240 if (imm == 0) {
3241 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3242 } else if (imm == 1 || imm == -1) {
3243 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003244 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003245 DivRemByPowerOfTwo(instruction);
3246 } else {
3247 DCHECK(imm <= -2 || imm >= 2);
3248 GenerateDivRemWithAnyConstant(instruction);
3249 }
3250}
3251
Calin Juravle7c4954d2014-10-28 16:57:40 +00003252void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003253 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3254 if (div->GetResultType() == Primitive::kPrimLong) {
3255 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003256 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003257 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3258 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003259 } else if (div->GetResultType() == Primitive::kPrimInt &&
3260 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3261 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003262 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003263 }
3264
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3266
Calin Juravle7c4954d2014-10-28 16:57:40 +00003267 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003268 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003269 if (div->InputAt(1)->IsConstant()) {
3270 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003271 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003273 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3274 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003275 // No temp register required.
3276 } else {
3277 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003278 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003279 locations->AddTemp(Location::RequiresRegister());
3280 }
3281 }
3282 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::RequiresRegister());
3285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3286 } else {
3287 InvokeRuntimeCallingConvention calling_convention;
3288 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3289 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3290 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3291 // we only need the former.
3292 locations->SetOut(Location::RegisterLocation(R0));
3293 }
Calin Juravled0d48522014-11-04 16:40:20 +00003294 break;
3295 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003296 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003297 InvokeRuntimeCallingConvention calling_convention;
3298 locations->SetInAt(0, Location::RegisterPairLocation(
3299 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3300 locations->SetInAt(1, Location::RegisterPairLocation(
3301 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003302 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003303 break;
3304 }
3305 case Primitive::kPrimFloat:
3306 case Primitive::kPrimDouble: {
3307 locations->SetInAt(0, Location::RequiresFpuRegister());
3308 locations->SetInAt(1, Location::RequiresFpuRegister());
3309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3310 break;
3311 }
3312
3313 default:
3314 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3315 }
3316}
3317
3318void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3319 LocationSummary* locations = div->GetLocations();
3320 Location out = locations->Out();
3321 Location first = locations->InAt(0);
3322 Location second = locations->InAt(1);
3323
3324 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003325 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003326 if (second.IsConstant()) {
3327 GenerateDivRemConstantIntegral(div);
3328 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003329 __ sdiv(out.AsRegister<Register>(),
3330 first.AsRegister<Register>(),
3331 second.AsRegister<Register>());
3332 } else {
3333 InvokeRuntimeCallingConvention calling_convention;
3334 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3335 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3336 DCHECK_EQ(R0, out.AsRegister<Register>());
3337
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003338 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003339 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003340 }
Calin Juravled0d48522014-11-04 16:40:20 +00003341 break;
3342 }
3343
Calin Juravle7c4954d2014-10-28 16:57:40 +00003344 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003345 InvokeRuntimeCallingConvention calling_convention;
3346 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3347 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3348 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3349 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3350 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003351 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003352
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003353 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003354 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003355 break;
3356 }
3357
3358 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003359 __ vdivs(out.AsFpuRegister<SRegister>(),
3360 first.AsFpuRegister<SRegister>(),
3361 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003362 break;
3363 }
3364
3365 case Primitive::kPrimDouble: {
3366 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3367 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3368 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3369 break;
3370 }
3371
3372 default:
3373 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3374 }
3375}
3376
Calin Juravlebacfec32014-11-14 15:54:36 +00003377void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003378 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003379
3380 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003381 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003382 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3383 // sdiv will be replaced by other instruction sequence.
3384 call_kind = LocationSummary::kNoCall;
3385 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3386 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003387 // Have hardware divide instruction for int, do it with three instructions.
3388 call_kind = LocationSummary::kNoCall;
3389 }
3390
Calin Juravlebacfec32014-11-14 15:54:36 +00003391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3392
Calin Juravled2ec87d2014-12-08 14:24:46 +00003393 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003394 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003395 if (rem->InputAt(1)->IsConstant()) {
3396 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003397 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003399 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3400 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003401 // No temp register required.
3402 } else {
3403 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003404 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003405 locations->AddTemp(Location::RequiresRegister());
3406 }
3407 }
3408 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003409 locations->SetInAt(0, Location::RequiresRegister());
3410 locations->SetInAt(1, Location::RequiresRegister());
3411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3412 locations->AddTemp(Location::RequiresRegister());
3413 } else {
3414 InvokeRuntimeCallingConvention calling_convention;
3415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3416 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3417 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3418 // we only need the latter.
3419 locations->SetOut(Location::RegisterLocation(R1));
3420 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 break;
3422 }
3423 case Primitive::kPrimLong: {
3424 InvokeRuntimeCallingConvention calling_convention;
3425 locations->SetInAt(0, Location::RegisterPairLocation(
3426 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3427 locations->SetInAt(1, Location::RegisterPairLocation(
3428 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3429 // The runtime helper puts the output in R2,R3.
3430 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3431 break;
3432 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003433 case Primitive::kPrimFloat: {
3434 InvokeRuntimeCallingConvention calling_convention;
3435 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3436 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3437 locations->SetOut(Location::FpuRegisterLocation(S0));
3438 break;
3439 }
3440
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003442 InvokeRuntimeCallingConvention calling_convention;
3443 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3444 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3445 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3446 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3447 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 break;
3449 }
3450
3451 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003452 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003453 }
3454}
3455
3456void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3457 LocationSummary* locations = rem->GetLocations();
3458 Location out = locations->Out();
3459 Location first = locations->InAt(0);
3460 Location second = locations->InAt(1);
3461
Calin Juravled2ec87d2014-12-08 14:24:46 +00003462 Primitive::Type type = rem->GetResultType();
3463 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003465 if (second.IsConstant()) {
3466 GenerateDivRemConstantIntegral(rem);
3467 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003468 Register reg1 = first.AsRegister<Register>();
3469 Register reg2 = second.AsRegister<Register>();
3470 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003471
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003472 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003473 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003474 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003475 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003476 } else {
3477 InvokeRuntimeCallingConvention calling_convention;
3478 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3479 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3480 DCHECK_EQ(R1, out.AsRegister<Register>());
3481
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003482 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003483 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003484 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 break;
3486 }
3487
3488 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003489 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003490 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003491 break;
3492 }
3493
Calin Juravled2ec87d2014-12-08 14:24:46 +00003494 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003495 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003496 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003497 break;
3498 }
3499
Calin Juravlebacfec32014-11-14 15:54:36 +00003500 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003501 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003502 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003503 break;
3504 }
3505
3506 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003507 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 }
3509}
3510
Calin Juravled0d48522014-11-04 16:40:20 +00003511void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003512 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003513 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003514}
3515
3516void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003517 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003518 codegen_->AddSlowPath(slow_path);
3519
3520 LocationSummary* locations = instruction->GetLocations();
3521 Location value = locations->InAt(0);
3522
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003523 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003524 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003525 case Primitive::kPrimByte:
3526 case Primitive::kPrimChar:
3527 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003528 case Primitive::kPrimInt: {
3529 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003530 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003531 } else {
3532 DCHECK(value.IsConstant()) << value;
3533 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3534 __ b(slow_path->GetEntryLabel());
3535 }
3536 }
3537 break;
3538 }
3539 case Primitive::kPrimLong: {
3540 if (value.IsRegisterPair()) {
3541 __ orrs(IP,
3542 value.AsRegisterPairLow<Register>(),
3543 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3544 __ b(slow_path->GetEntryLabel(), EQ);
3545 } else {
3546 DCHECK(value.IsConstant()) << value;
3547 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3548 __ b(slow_path->GetEntryLabel());
3549 }
3550 }
3551 break;
3552 default:
3553 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3554 }
3555 }
Calin Juravled0d48522014-11-04 16:40:20 +00003556}
3557
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003558void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3559 Register in = locations->InAt(0).AsRegister<Register>();
3560 Location rhs = locations->InAt(1);
3561 Register out = locations->Out().AsRegister<Register>();
3562
3563 if (rhs.IsConstant()) {
3564 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3565 // so map all rotations to a +ve. equivalent in that range.
3566 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3567 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3568 if (rot) {
3569 // Rotate, mapping left rotations to right equivalents if necessary.
3570 // (e.g. left by 2 bits == right by 30.)
3571 __ Ror(out, in, rot);
3572 } else if (out != in) {
3573 __ Mov(out, in);
3574 }
3575 } else {
3576 __ Ror(out, in, rhs.AsRegister<Register>());
3577 }
3578}
3579
3580// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3581// rotates by swapping input regs (effectively rotating by the first 32-bits of
3582// a larger rotation) or flipping direction (thus treating larger right/left
3583// rotations as sub-word sized rotations in the other direction) as appropriate.
3584void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3585 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3586 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3587 Location rhs = locations->InAt(1);
3588 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3589 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3590
3591 if (rhs.IsConstant()) {
3592 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3593 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003594 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003595 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3596 // logic below to a simple pair of binary orr.
3597 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3598 if (rot >= kArmBitsPerWord) {
3599 rot -= kArmBitsPerWord;
3600 std::swap(in_reg_hi, in_reg_lo);
3601 }
3602 // Rotate, or mov to out for zero or word size rotations.
3603 if (rot != 0u) {
3604 __ Lsr(out_reg_hi, in_reg_hi, rot);
3605 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3606 __ Lsr(out_reg_lo, in_reg_lo, rot);
3607 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3608 } else {
3609 __ Mov(out_reg_lo, in_reg_lo);
3610 __ Mov(out_reg_hi, in_reg_hi);
3611 }
3612 } else {
3613 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3614 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3615 Label end;
3616 Label shift_by_32_plus_shift_right;
3617
3618 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3619 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3620 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3621 __ b(&shift_by_32_plus_shift_right, CC);
3622
3623 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3624 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3625 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3626 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3627 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3628 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3629 __ Lsr(shift_left, in_reg_hi, shift_right);
3630 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3631 __ b(&end);
3632
3633 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3634 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3635 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3636 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3637 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3638 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3639 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3640 __ Lsl(shift_right, in_reg_hi, shift_left);
3641 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3642
3643 __ Bind(&end);
3644 }
3645}
Roland Levillain22c49222016-03-18 14:04:28 +00003646
3647void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3650 switch (ror->GetResultType()) {
3651 case Primitive::kPrimInt: {
3652 locations->SetInAt(0, Location::RequiresRegister());
3653 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3655 break;
3656 }
3657 case Primitive::kPrimLong: {
3658 locations->SetInAt(0, Location::RequiresRegister());
3659 if (ror->InputAt(1)->IsConstant()) {
3660 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3661 } else {
3662 locations->SetInAt(1, Location::RequiresRegister());
3663 locations->AddTemp(Location::RequiresRegister());
3664 locations->AddTemp(Location::RequiresRegister());
3665 }
3666 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3667 break;
3668 }
3669 default:
3670 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3671 }
3672}
3673
Roland Levillain22c49222016-03-18 14:04:28 +00003674void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003675 LocationSummary* locations = ror->GetLocations();
3676 Primitive::Type type = ror->GetResultType();
3677 switch (type) {
3678 case Primitive::kPrimInt: {
3679 HandleIntegerRotate(locations);
3680 break;
3681 }
3682 case Primitive::kPrimLong: {
3683 HandleLongRotate(locations);
3684 break;
3685 }
3686 default:
3687 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003688 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003689 }
3690}
3691
Calin Juravle9aec02f2014-11-18 23:06:35 +00003692void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3693 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3694
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003695 LocationSummary* locations =
3696 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697
3698 switch (op->GetResultType()) {
3699 case Primitive::kPrimInt: {
3700 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003701 if (op->InputAt(1)->IsConstant()) {
3702 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3704 } else {
3705 locations->SetInAt(1, Location::RequiresRegister());
3706 // Make the output overlap, as it will be used to hold the masked
3707 // second input.
3708 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3709 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 break;
3711 }
3712 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003713 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003714 if (op->InputAt(1)->IsConstant()) {
3715 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3716 // For simplicity, use kOutputOverlap even though we only require that low registers
3717 // don't clash with high registers which the register allocator currently guarantees.
3718 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3719 } else {
3720 locations->SetInAt(1, Location::RequiresRegister());
3721 locations->AddTemp(Location::RequiresRegister());
3722 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3723 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 break;
3725 }
3726 default:
3727 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3728 }
3729}
3730
3731void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3732 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3733
3734 LocationSummary* locations = op->GetLocations();
3735 Location out = locations->Out();
3736 Location first = locations->InAt(0);
3737 Location second = locations->InAt(1);
3738
3739 Primitive::Type type = op->GetResultType();
3740 switch (type) {
3741 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 Register out_reg = out.AsRegister<Register>();
3743 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003746 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003747 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003749 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003751 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003753 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 }
3755 } else {
3756 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003757 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003758 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 __ Mov(out_reg, first_reg);
3760 } else if (op->IsShl()) {
3761 __ Lsl(out_reg, first_reg, shift_value);
3762 } else if (op->IsShr()) {
3763 __ Asr(out_reg, first_reg, shift_value);
3764 } else {
3765 __ Lsr(out_reg, first_reg, shift_value);
3766 }
3767 }
3768 break;
3769 }
3770 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003771 Register o_h = out.AsRegisterPairHigh<Register>();
3772 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003774 Register high = first.AsRegisterPairHigh<Register>();
3775 Register low = first.AsRegisterPairLow<Register>();
3776
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003777 if (second.IsRegister()) {
3778 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003779
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003780 Register second_reg = second.AsRegister<Register>();
3781
3782 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003783 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003784 // Shift the high part
3785 __ Lsl(o_h, high, o_l);
3786 // Shift the low part and `or` what overflew on the high part
3787 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3788 __ Lsr(temp, low, temp);
3789 __ orr(o_h, o_h, ShifterOperand(temp));
3790 // If the shift is > 32 bits, override the high part
3791 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3792 __ it(PL);
3793 __ Lsl(o_h, low, temp, PL);
3794 // Shift the low part
3795 __ Lsl(o_l, low, o_l);
3796 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003797 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003798 // Shift the low part
3799 __ Lsr(o_l, low, o_h);
3800 // Shift the high part and `or` what underflew on the low part
3801 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3802 __ Lsl(temp, high, temp);
3803 __ orr(o_l, o_l, ShifterOperand(temp));
3804 // If the shift is > 32 bits, override the low part
3805 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3806 __ it(PL);
3807 __ Asr(o_l, high, temp, PL);
3808 // Shift the high part
3809 __ Asr(o_h, high, o_h);
3810 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003811 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003812 // same as Shr except we use `Lsr`s and not `Asr`s
3813 __ Lsr(o_l, low, o_h);
3814 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3815 __ Lsl(temp, high, temp);
3816 __ orr(o_l, o_l, ShifterOperand(temp));
3817 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3818 __ it(PL);
3819 __ Lsr(o_l, high, temp, PL);
3820 __ Lsr(o_h, high, o_h);
3821 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003823 // Register allocator doesn't create partial overlap.
3824 DCHECK_NE(o_l, high);
3825 DCHECK_NE(o_h, low);
3826 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003827 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003828 if (shift_value > 32) {
3829 if (op->IsShl()) {
3830 __ Lsl(o_h, low, shift_value - 32);
3831 __ LoadImmediate(o_l, 0);
3832 } else if (op->IsShr()) {
3833 __ Asr(o_l, high, shift_value - 32);
3834 __ Asr(o_h, high, 31);
3835 } else {
3836 __ Lsr(o_l, high, shift_value - 32);
3837 __ LoadImmediate(o_h, 0);
3838 }
3839 } else if (shift_value == 32) {
3840 if (op->IsShl()) {
3841 __ mov(o_h, ShifterOperand(low));
3842 __ LoadImmediate(o_l, 0);
3843 } else if (op->IsShr()) {
3844 __ mov(o_l, ShifterOperand(high));
3845 __ Asr(o_h, high, 31);
3846 } else {
3847 __ mov(o_l, ShifterOperand(high));
3848 __ LoadImmediate(o_h, 0);
3849 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003850 } else if (shift_value == 1) {
3851 if (op->IsShl()) {
3852 __ Lsls(o_l, low, 1);
3853 __ adc(o_h, high, ShifterOperand(high));
3854 } else if (op->IsShr()) {
3855 __ Asrs(o_h, high, 1);
3856 __ Rrx(o_l, low);
3857 } else {
3858 __ Lsrs(o_h, high, 1);
3859 __ Rrx(o_l, low);
3860 }
3861 } else {
3862 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003863 if (op->IsShl()) {
3864 __ Lsl(o_h, high, shift_value);
3865 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3866 __ Lsl(o_l, low, shift_value);
3867 } else if (op->IsShr()) {
3868 __ Lsr(o_l, low, shift_value);
3869 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3870 __ Asr(o_h, high, shift_value);
3871 } else {
3872 __ Lsr(o_l, low, shift_value);
3873 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3874 __ Lsr(o_h, high, shift_value);
3875 }
3876 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 break;
3879 }
3880 default:
3881 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003882 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883 }
3884}
3885
3886void LocationsBuilderARM::VisitShl(HShl* shl) {
3887 HandleShift(shl);
3888}
3889
3890void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void LocationsBuilderARM::VisitShr(HShr* shr) {
3895 HandleShift(shr);
3896}
3897
3898void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3903 HandleShift(ushr);
3904}
3905
3906void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003910void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003911 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003913 if (instruction->IsStringAlloc()) {
3914 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3915 } else {
3916 InvokeRuntimeCallingConvention calling_convention;
3917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3918 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3919 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003920 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003921}
3922
3923void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003924 // Note: if heap poisoning is enabled, the entry point takes cares
3925 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003926 if (instruction->IsStringAlloc()) {
3927 // String is allocated through StringFactory. Call NewEmptyString entry point.
3928 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003929 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003930 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3931 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3932 __ blx(LR);
3933 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3934 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003935 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003936 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3937 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003938}
3939
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003940void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3941 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003942 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003943 InvokeRuntimeCallingConvention calling_convention;
3944 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003945 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003946 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003947 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003948}
3949
3950void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3951 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003953 // Note: if heap poisoning is enabled, the entry point takes cares
3954 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003955 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003956 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957}
3958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003959void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003960 LocationSummary* locations =
3961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003962 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3963 if (location.IsStackSlot()) {
3964 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3965 } else if (location.IsDoubleStackSlot()) {
3966 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003967 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003968 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003969}
3970
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003971void InstructionCodeGeneratorARM::VisitParameterValue(
3972 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003973 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003974}
3975
3976void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3977 LocationSummary* locations =
3978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3979 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3980}
3981
3982void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3983 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003984}
3985
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003986void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003989 locations->SetInAt(0, Location::RequiresRegister());
3990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003991}
3992
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3994 LocationSummary* locations = not_->GetLocations();
3995 Location out = locations->Out();
3996 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003997 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 break;
4001
4002 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004003 __ mvn(out.AsRegisterPairLow<Register>(),
4004 ShifterOperand(in.AsRegisterPairLow<Register>()));
4005 __ mvn(out.AsRegisterPairHigh<Register>(),
4006 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004007 break;
4008
4009 default:
4010 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4011 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004012}
4013
David Brazdil66d126e2015-04-03 16:02:44 +01004014void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4015 LocationSummary* locations =
4016 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4017 locations->SetInAt(0, Location::RequiresRegister());
4018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4019}
4020
4021void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004022 LocationSummary* locations = bool_not->GetLocations();
4023 Location out = locations->Out();
4024 Location in = locations->InAt(0);
4025 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4026}
4027
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004028void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004031 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004032 case Primitive::kPrimBoolean:
4033 case Primitive::kPrimByte:
4034 case Primitive::kPrimShort:
4035 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004036 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004037 case Primitive::kPrimLong: {
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004040 // Output overlaps because it is written before doing the low comparison.
4041 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004042 break;
4043 }
4044 case Primitive::kPrimFloat:
4045 case Primitive::kPrimDouble: {
4046 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004047 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004048 locations->SetOut(Location::RequiresRegister());
4049 break;
4050 }
4051 default:
4052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004054}
4055
4056void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004058 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004059 Location left = locations->InAt(0);
4060 Location right = locations->InAt(1);
4061
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004062 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004063 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004064 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004065 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004066 case Primitive::kPrimBoolean:
4067 case Primitive::kPrimByte:
4068 case Primitive::kPrimShort:
4069 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004070 case Primitive::kPrimInt: {
4071 __ LoadImmediate(out, 0);
4072 __ cmp(left.AsRegister<Register>(),
4073 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4074 less_cond = LT;
4075 break;
4076 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004078 __ cmp(left.AsRegisterPairHigh<Register>(),
4079 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080 __ b(&less, LT);
4081 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004082 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004084 __ cmp(left.AsRegisterPairLow<Register>(),
4085 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004086 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004087 break;
4088 }
4089 case Primitive::kPrimFloat:
4090 case Primitive::kPrimDouble: {
4091 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004092 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004093 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004094 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004095 break;
4096 }
4097 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004098 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004099 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100 }
Aart Bika19616e2016-02-01 18:57:58 -08004101
Calin Juravleddb7df22014-11-25 20:56:51 +00004102 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004103 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004104
4105 __ Bind(&greater);
4106 __ LoadImmediate(out, 1);
4107 __ b(&done);
4108
4109 __ Bind(&less);
4110 __ LoadImmediate(out, -1);
4111
4112 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004113}
4114
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004115void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004116 LocationSummary* locations =
4117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004118 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004119 locations->SetInAt(i, Location::Any());
4120 }
4121 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004122}
4123
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004124void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004125 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004126}
4127
Roland Levillainc9285912015-12-18 10:38:42 +00004128void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4129 // TODO (ported from quick): revisit ARM barrier kinds.
4130 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004131 switch (kind) {
4132 case MemBarrierKind::kAnyStore:
4133 case MemBarrierKind::kLoadAny:
4134 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004135 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004136 break;
4137 }
4138 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004139 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004140 break;
4141 }
4142 default:
4143 LOG(FATAL) << "Unexpected memory barrier " << kind;
4144 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004145 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004146}
4147
4148void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4149 uint32_t offset,
4150 Register out_lo,
4151 Register out_hi) {
4152 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004153 // Ensure `out_lo` is different from `addr`, so that loading
4154 // `offset` into `out_lo` does not clutter `addr`.
4155 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004156 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004157 __ add(IP, addr, ShifterOperand(out_lo));
4158 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004159 }
4160 __ ldrexd(out_lo, out_hi, addr);
4161}
4162
4163void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4164 uint32_t offset,
4165 Register value_lo,
4166 Register value_hi,
4167 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 Register temp2,
4169 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004170 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004171 if (offset != 0) {
4172 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004173 __ add(IP, addr, ShifterOperand(temp1));
4174 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004175 }
4176 __ Bind(&fail);
4177 // We need a load followed by store. (The address used in a STREX instruction must
4178 // be the same as the address in the most recently executed LDREX instruction.)
4179 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004181 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004182 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004183}
4184
4185void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4186 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4187
Nicolas Geoffray39468442014-09-02 15:17:15 +01004188 LocationSummary* locations =
4189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004190 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004191
Calin Juravle52c48962014-12-16 17:02:57 +00004192 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004193 if (Primitive::IsFloatingPointType(field_type)) {
4194 locations->SetInAt(1, Location::RequiresFpuRegister());
4195 } else {
4196 locations->SetInAt(1, Location::RequiresRegister());
4197 }
4198
Calin Juravle52c48962014-12-16 17:02:57 +00004199 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004200 bool generate_volatile = field_info.IsVolatile()
4201 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004202 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004203 bool needs_write_barrier =
4204 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004205 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004206 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004207 if (needs_write_barrier) {
4208 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004209 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004210 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004211 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004212 // - registers need to be consecutive
4213 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004214 // We don't test for ARM yet, and the assertion makes sure that we
4215 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004216 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4217
4218 locations->AddTemp(Location::RequiresRegister());
4219 locations->AddTemp(Location::RequiresRegister());
4220 if (field_type == Primitive::kPrimDouble) {
4221 // For doubles we need two more registers to copy the value.
4222 locations->AddTemp(Location::RegisterLocation(R2));
4223 locations->AddTemp(Location::RegisterLocation(R3));
4224 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004225 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004226}
4227
Calin Juravle52c48962014-12-16 17:02:57 +00004228void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 const FieldInfo& field_info,
4230 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004231 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004234 Register base = locations->InAt(0).AsRegister<Register>();
4235 Location value = locations->InAt(1);
4236
4237 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004238 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004239 Primitive::Type field_type = field_info.GetFieldType();
4240 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004241 bool needs_write_barrier =
4242 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004243
4244 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004245 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247
4248 switch (field_type) {
4249 case Primitive::kPrimBoolean:
4250 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252 break;
4253 }
4254
4255 case Primitive::kPrimShort:
4256 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004257 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004258 break;
4259 }
4260
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004261 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004263 if (kPoisonHeapReferences && needs_write_barrier) {
4264 // Note that in the case where `value` is a null reference,
4265 // we do not enter this block, as a null reference does not
4266 // need poisoning.
4267 DCHECK_EQ(field_type, Primitive::kPrimNot);
4268 Register temp = locations->GetTemp(0).AsRegister<Register>();
4269 __ Mov(temp, value.AsRegister<Register>());
4270 __ PoisonHeapReference(temp);
4271 __ StoreToOffset(kStoreWord, temp, base, offset);
4272 } else {
4273 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275 break;
4276 }
4277
4278 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004279 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004280 GenerateWideAtomicStore(base, offset,
4281 value.AsRegisterPairLow<Register>(),
4282 value.AsRegisterPairHigh<Register>(),
4283 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004284 locations->GetTemp(1).AsRegister<Register>(),
4285 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004286 } else {
4287 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004289 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290 break;
4291 }
4292
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004293 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004294 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004295 break;
4296 }
4297
4298 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004299 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004300 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4302 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4303
4304 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4305
4306 GenerateWideAtomicStore(base, offset,
4307 value_reg_lo,
4308 value_reg_hi,
4309 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004310 locations->GetTemp(3).AsRegister<Register>(),
4311 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004312 } else {
4313 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004315 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004316 break;
4317 }
4318
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319 case Primitive::kPrimVoid:
4320 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004321 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 }
Calin Juravle52c48962014-12-16 17:02:57 +00004323
Calin Juravle77520bc2015-01-12 18:45:46 +00004324 // Longs and doubles are handled in the switch.
4325 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4326 codegen_->MaybeRecordImplicitNullCheck(instruction);
4327 }
4328
4329 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4330 Register temp = locations->GetTemp(0).AsRegister<Register>();
4331 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004332 codegen_->MarkGCCard(
4333 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004334 }
4335
Calin Juravle52c48962014-12-16 17:02:57 +00004336 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004337 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004338 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339}
4340
Calin Juravle52c48962014-12-16 17:02:57 +00004341void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4342 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004343
4344 bool object_field_get_with_read_barrier =
4345 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004346 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004347 new (GetGraph()->GetArena()) LocationSummary(instruction,
4348 object_field_get_with_read_barrier ?
4349 LocationSummary::kCallOnSlowPath :
4350 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004351 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004352 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004353 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004354 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004355
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004356 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004357 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004358 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004359 // The output overlaps in case of volatile long: we don't want the
4360 // code generated by GenerateWideAtomicLoad to overwrite the
4361 // object's location. Likewise, in the case of an object field get
4362 // with read barriers enabled, we do not want the load to overwrite
4363 // the object's location, as we need it to emit the read barrier.
4364 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4365 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004366
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004367 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4368 locations->SetOut(Location::RequiresFpuRegister());
4369 } else {
4370 locations->SetOut(Location::RequiresRegister(),
4371 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4372 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004373 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004374 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004375 // - registers need to be consecutive
4376 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004377 // We don't test for ARM yet, and the assertion makes sure that we
4378 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004379 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4380 locations->AddTemp(Location::RequiresRegister());
4381 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004382 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4383 // We need a temporary register for the read barrier marking slow
4384 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4385 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004386 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387}
4388
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004389Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4390 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4391 << input->GetType();
4392 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4393 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4394 return Location::ConstantLocation(input->AsConstant());
4395 } else {
4396 return Location::RequiresFpuRegister();
4397 }
4398}
4399
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004400Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4401 Opcode opcode) {
4402 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4403 if (constant->IsConstant() &&
4404 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4405 return Location::ConstantLocation(constant->AsConstant());
4406 }
4407 return Location::RequiresRegister();
4408}
4409
4410bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4411 Opcode opcode) {
4412 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4413 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004414 Opcode high_opcode = opcode;
4415 SetCc low_set_cc = kCcDontCare;
4416 switch (opcode) {
4417 case SUB:
4418 // Flip the operation to an ADD.
4419 value = -value;
4420 opcode = ADD;
4421 FALLTHROUGH_INTENDED;
4422 case ADD:
4423 if (Low32Bits(value) == 0u) {
4424 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4425 }
4426 high_opcode = ADC;
4427 low_set_cc = kCcSet;
4428 break;
4429 default:
4430 break;
4431 }
4432 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4433 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004434 } else {
4435 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4436 }
4437}
4438
Vladimir Marko59751a72016-08-05 14:37:27 +01004439bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4440 Opcode opcode,
4441 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004442 ShifterOperand so;
4443 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004444 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004445 return true;
4446 }
4447 Opcode neg_opcode = kNoOperand;
4448 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004449 case AND: neg_opcode = BIC; value = ~value; break;
4450 case ORR: neg_opcode = ORN; value = ~value; break;
4451 case ADD: neg_opcode = SUB; value = -value; break;
4452 case ADC: neg_opcode = SBC; value = ~value; break;
4453 case SUB: neg_opcode = ADD; value = -value; break;
4454 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004455 default:
4456 return false;
4457 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004458 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004459}
4460
Calin Juravle52c48962014-12-16 17:02:57 +00004461void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4462 const FieldInfo& field_info) {
4463 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464
Calin Juravle52c48962014-12-16 17:02:57 +00004465 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004466 Location base_loc = locations->InAt(0);
4467 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004468 Location out = locations->Out();
4469 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004470 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004471 Primitive::Type field_type = field_info.GetFieldType();
4472 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4473
4474 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004475 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004476 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004478
Roland Levillainc9285912015-12-18 10:38:42 +00004479 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004480 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482
Roland Levillainc9285912015-12-18 10:38:42 +00004483 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004484 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486
Roland Levillainc9285912015-12-18 10:38:42 +00004487 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004488 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490
4491 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004492 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004494
4495 case Primitive::kPrimNot: {
4496 // /* HeapReference<Object> */ out = *(base + offset)
4497 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4498 Location temp_loc = locations->GetTemp(0);
4499 // Note that a potential implicit null check is handled in this
4500 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4501 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4502 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4503 if (is_volatile) {
4504 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4505 }
4506 } else {
4507 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4508 codegen_->MaybeRecordImplicitNullCheck(instruction);
4509 if (is_volatile) {
4510 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4511 }
4512 // If read barriers are enabled, emit read barriers other than
4513 // Baker's using a slow path (and also unpoison the loaded
4514 // reference, if heap poisoning is enabled).
4515 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4516 }
4517 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518 }
4519
Roland Levillainc9285912015-12-18 10:38:42 +00004520 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004521 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004522 GenerateWideAtomicLoad(base, offset,
4523 out.AsRegisterPairLow<Register>(),
4524 out.AsRegisterPairHigh<Register>());
4525 } else {
4526 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004529
Roland Levillainc9285912015-12-18 10:38:42 +00004530 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004531 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004532 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004533
4534 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004535 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004536 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 Register lo = locations->GetTemp(0).AsRegister<Register>();
4538 Register hi = locations->GetTemp(1).AsRegister<Register>();
4539 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004540 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004541 __ vmovdrr(out_reg, lo, hi);
4542 } else {
4543 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004545 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004546 break;
4547 }
4548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004550 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004551 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552 }
Calin Juravle52c48962014-12-16 17:02:57 +00004553
Roland Levillainc9285912015-12-18 10:38:42 +00004554 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4555 // Potential implicit null checks, in the case of reference or
4556 // double fields, are handled in the previous switch statement.
4557 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004558 codegen_->MaybeRecordImplicitNullCheck(instruction);
4559 }
4560
Calin Juravle52c48962014-12-16 17:02:57 +00004561 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004562 if (field_type == Primitive::kPrimNot) {
4563 // Memory barriers, in the case of references, are also handled
4564 // in the previous switch statement.
4565 } else {
4566 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4567 }
Roland Levillain4d027112015-07-01 15:41:14 +01004568 }
Calin Juravle52c48962014-12-16 17:02:57 +00004569}
4570
4571void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4572 HandleFieldSet(instruction, instruction->GetFieldInfo());
4573}
4574
4575void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004576 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004577}
4578
4579void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4580 HandleFieldGet(instruction, instruction->GetFieldInfo());
4581}
4582
4583void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4584 HandleFieldGet(instruction, instruction->GetFieldInfo());
4585}
4586
4587void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4588 HandleFieldGet(instruction, instruction->GetFieldInfo());
4589}
4590
4591void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4592 HandleFieldGet(instruction, instruction->GetFieldInfo());
4593}
4594
4595void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4596 HandleFieldSet(instruction, instruction->GetFieldInfo());
4597}
4598
4599void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004600 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601}
4602
Calin Juravlee460d1d2015-09-29 04:52:17 +01004603void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4604 HUnresolvedInstanceFieldGet* instruction) {
4605 FieldAccessCallingConventionARM calling_convention;
4606 codegen_->CreateUnresolvedFieldLocationSummary(
4607 instruction, instruction->GetFieldType(), calling_convention);
4608}
4609
4610void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4611 HUnresolvedInstanceFieldGet* instruction) {
4612 FieldAccessCallingConventionARM calling_convention;
4613 codegen_->GenerateUnresolvedFieldAccess(instruction,
4614 instruction->GetFieldType(),
4615 instruction->GetFieldIndex(),
4616 instruction->GetDexPc(),
4617 calling_convention);
4618}
4619
4620void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4621 HUnresolvedInstanceFieldSet* instruction) {
4622 FieldAccessCallingConventionARM calling_convention;
4623 codegen_->CreateUnresolvedFieldLocationSummary(
4624 instruction, instruction->GetFieldType(), calling_convention);
4625}
4626
4627void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4628 HUnresolvedInstanceFieldSet* instruction) {
4629 FieldAccessCallingConventionARM calling_convention;
4630 codegen_->GenerateUnresolvedFieldAccess(instruction,
4631 instruction->GetFieldType(),
4632 instruction->GetFieldIndex(),
4633 instruction->GetDexPc(),
4634 calling_convention);
4635}
4636
4637void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4638 HUnresolvedStaticFieldGet* instruction) {
4639 FieldAccessCallingConventionARM calling_convention;
4640 codegen_->CreateUnresolvedFieldLocationSummary(
4641 instruction, instruction->GetFieldType(), calling_convention);
4642}
4643
4644void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4645 HUnresolvedStaticFieldGet* instruction) {
4646 FieldAccessCallingConventionARM calling_convention;
4647 codegen_->GenerateUnresolvedFieldAccess(instruction,
4648 instruction->GetFieldType(),
4649 instruction->GetFieldIndex(),
4650 instruction->GetDexPc(),
4651 calling_convention);
4652}
4653
4654void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4655 HUnresolvedStaticFieldSet* instruction) {
4656 FieldAccessCallingConventionARM calling_convention;
4657 codegen_->CreateUnresolvedFieldLocationSummary(
4658 instruction, instruction->GetFieldType(), calling_convention);
4659}
4660
4661void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4662 HUnresolvedStaticFieldSet* instruction) {
4663 FieldAccessCallingConventionARM calling_convention;
4664 codegen_->GenerateUnresolvedFieldAccess(instruction,
4665 instruction->GetFieldType(),
4666 instruction->GetFieldIndex(),
4667 instruction->GetDexPc(),
4668 calling_convention);
4669}
4670
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004672 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4673 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674}
4675
Calin Juravle2ae48182016-03-16 14:05:09 +00004676void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4677 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004678 return;
4679 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004680 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004681
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004682 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004683 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004684}
4685
Calin Juravle2ae48182016-03-16 14:05:09 +00004686void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004687 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004688 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004689
4690 LocationSummary* locations = instruction->GetLocations();
4691 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004693 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694}
4695
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004696void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004697 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004698}
4699
Artem Serov6c916792016-07-11 14:02:34 +01004700static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4701 switch (type) {
4702 case Primitive::kPrimNot:
4703 return kLoadWord;
4704 case Primitive::kPrimBoolean:
4705 return kLoadUnsignedByte;
4706 case Primitive::kPrimByte:
4707 return kLoadSignedByte;
4708 case Primitive::kPrimChar:
4709 return kLoadUnsignedHalfword;
4710 case Primitive::kPrimShort:
4711 return kLoadSignedHalfword;
4712 case Primitive::kPrimInt:
4713 return kLoadWord;
4714 case Primitive::kPrimLong:
4715 return kLoadWordPair;
4716 case Primitive::kPrimFloat:
4717 return kLoadSWord;
4718 case Primitive::kPrimDouble:
4719 return kLoadDWord;
4720 default:
4721 LOG(FATAL) << "Unreachable type " << type;
4722 UNREACHABLE();
4723 }
4724}
4725
4726static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4727 switch (type) {
4728 case Primitive::kPrimNot:
4729 return kStoreWord;
4730 case Primitive::kPrimBoolean:
4731 case Primitive::kPrimByte:
4732 return kStoreByte;
4733 case Primitive::kPrimChar:
4734 case Primitive::kPrimShort:
4735 return kStoreHalfword;
4736 case Primitive::kPrimInt:
4737 return kStoreWord;
4738 case Primitive::kPrimLong:
4739 return kStoreWordPair;
4740 case Primitive::kPrimFloat:
4741 return kStoreSWord;
4742 case Primitive::kPrimDouble:
4743 return kStoreDWord;
4744 default:
4745 LOG(FATAL) << "Unreachable type " << type;
4746 UNREACHABLE();
4747 }
4748}
4749
4750void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4751 Location out_loc,
4752 Register base,
4753 Register reg_offset,
4754 Condition cond) {
4755 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4756 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4757
4758 switch (type) {
4759 case Primitive::kPrimByte:
4760 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4761 break;
4762 case Primitive::kPrimBoolean:
4763 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4764 break;
4765 case Primitive::kPrimShort:
4766 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4767 break;
4768 case Primitive::kPrimChar:
4769 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4770 break;
4771 case Primitive::kPrimNot:
4772 case Primitive::kPrimInt:
4773 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4774 break;
4775 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4776 case Primitive::kPrimLong:
4777 case Primitive::kPrimFloat:
4778 case Primitive::kPrimDouble:
4779 default:
4780 LOG(FATAL) << "Unreachable type " << type;
4781 UNREACHABLE();
4782 }
4783}
4784
4785void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4786 Location loc,
4787 Register base,
4788 Register reg_offset,
4789 Condition cond) {
4790 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4791 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4792
4793 switch (type) {
4794 case Primitive::kPrimByte:
4795 case Primitive::kPrimBoolean:
4796 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4797 break;
4798 case Primitive::kPrimShort:
4799 case Primitive::kPrimChar:
4800 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4801 break;
4802 case Primitive::kPrimNot:
4803 case Primitive::kPrimInt:
4804 __ str(loc.AsRegister<Register>(), mem_address, cond);
4805 break;
4806 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4807 case Primitive::kPrimLong:
4808 case Primitive::kPrimFloat:
4809 case Primitive::kPrimDouble:
4810 default:
4811 LOG(FATAL) << "Unreachable type " << type;
4812 UNREACHABLE();
4813 }
4814}
4815
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004817 bool object_array_get_with_read_barrier =
4818 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004819 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004820 new (GetGraph()->GetArena()) LocationSummary(instruction,
4821 object_array_get_with_read_barrier ?
4822 LocationSummary::kCallOnSlowPath :
4823 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004824 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004825 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004826 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004827 locations->SetInAt(0, Location::RequiresRegister());
4828 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004829 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4831 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004832 // The output overlaps in the case of an object array get with
4833 // read barriers enabled: we do not want the move to overwrite the
4834 // array's location, as we need it to emit the read barrier.
4835 locations->SetOut(
4836 Location::RequiresRegister(),
4837 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004838 }
Roland Levillainc9285912015-12-18 10:38:42 +00004839 // We need a temporary register for the read barrier marking slow
4840 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004841 // Also need for String compression feature.
4842 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4843 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004844 locations->AddTemp(Location::RequiresRegister());
4845 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004846}
4847
4848void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4849 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004850 Location obj_loc = locations->InAt(0);
4851 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004853 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004854 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004855 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004856 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4857 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004858 HInstruction* array_instr = instruction->GetArray();
4859 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004860
Roland Levillain4d027112015-07-01 15:41:14 +01004861 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004862 case Primitive::kPrimBoolean:
4863 case Primitive::kPrimByte:
4864 case Primitive::kPrimShort:
4865 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004866 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004868 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004869 if (maybe_compressed_char_at) {
4870 Register length = IP;
4871 Label uncompressed_load, done;
4872 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4873 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4874 codegen_->MaybeRecordImplicitNullCheck(instruction);
4875 __ cmp(length, ShifterOperand(0));
4876 __ b(&uncompressed_load, GE);
4877 __ LoadFromOffset(kLoadUnsignedByte,
4878 out_loc.AsRegister<Register>(),
4879 obj,
4880 data_offset + const_index);
4881 __ b(&done);
4882 __ Bind(&uncompressed_load);
4883 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4884 out_loc.AsRegister<Register>(),
4885 obj,
4886 data_offset + (const_index << 1));
4887 __ Bind(&done);
4888 } else {
4889 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004890
jessicahandojo05765752016-09-09 19:01:32 -07004891 LoadOperandType load_type = GetLoadOperandType(type);
4892 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4893 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004895 Register temp = IP;
4896
4897 if (has_intermediate_address) {
4898 // We do not need to compute the intermediate address from the array: the
4899 // input instruction has done it already. See the comment in
4900 // `TryExtractArrayAccessAddress()`.
4901 if (kIsDebugBuild) {
4902 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4903 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4904 }
4905 temp = obj;
4906 } else {
4907 __ add(temp, obj, ShifterOperand(data_offset));
4908 }
jessicahandojo05765752016-09-09 19:01:32 -07004909 if (maybe_compressed_char_at) {
4910 Label uncompressed_load, done;
4911 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4912 Register length = locations->GetTemp(0).AsRegister<Register>();
4913 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4914 codegen_->MaybeRecordImplicitNullCheck(instruction);
4915 __ cmp(length, ShifterOperand(0));
4916 __ b(&uncompressed_load, GE);
4917 __ ldrb(out_loc.AsRegister<Register>(),
4918 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4919 __ b(&done);
4920 __ Bind(&uncompressed_load);
4921 __ ldrh(out_loc.AsRegister<Register>(),
4922 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4923 __ Bind(&done);
4924 } else {
4925 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4926 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927 }
4928 break;
4929 }
4930
Roland Levillainc9285912015-12-18 10:38:42 +00004931 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004932 // The read barrier instrumentation of object ArrayGet
4933 // instructions does not support the HIntermediateAddress
4934 // instruction.
4935 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4936
Roland Levillainc9285912015-12-18 10:38:42 +00004937 static_assert(
4938 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4939 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004940 // /* HeapReference<Object> */ out =
4941 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4942 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4943 Location temp = locations->GetTemp(0);
4944 // Note that a potential implicit null check is handled in this
4945 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4946 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4947 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4948 } else {
4949 Register out = out_loc.AsRegister<Register>();
4950 if (index.IsConstant()) {
4951 size_t offset =
4952 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4953 __ LoadFromOffset(kLoadWord, out, obj, offset);
4954 codegen_->MaybeRecordImplicitNullCheck(instruction);
4955 // If read barriers are enabled, emit read barriers other than
4956 // Baker's using a slow path (and also unpoison the loaded
4957 // reference, if heap poisoning is enabled).
4958 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4959 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004960 Register temp = IP;
4961
4962 if (has_intermediate_address) {
4963 // We do not need to compute the intermediate address from the array: the
4964 // input instruction has done it already. See the comment in
4965 // `TryExtractArrayAccessAddress()`.
4966 if (kIsDebugBuild) {
4967 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4968 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4969 }
4970 temp = obj;
4971 } else {
4972 __ add(temp, obj, ShifterOperand(data_offset));
4973 }
4974 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004975
Roland Levillainc9285912015-12-18 10:38:42 +00004976 codegen_->MaybeRecordImplicitNullCheck(instruction);
4977 // If read barriers are enabled, emit read barriers other than
4978 // Baker's using a slow path (and also unpoison the loaded
4979 // reference, if heap poisoning is enabled).
4980 codegen_->MaybeGenerateReadBarrierSlow(
4981 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4982 }
4983 }
4984 break;
4985 }
4986
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004989 size_t offset =
4990 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004991 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004993 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004994 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 }
4996 break;
4997 }
4998
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004999 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005000 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005001 if (index.IsConstant()) {
5002 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005003 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005004 } else {
5005 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005006 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005007 }
5008 break;
5009 }
5010
5011 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005012 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005013 if (index.IsConstant()) {
5014 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005015 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005016 } else {
5017 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005018 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005019 }
5020 break;
5021 }
5022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005024 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005025 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 }
Roland Levillain4d027112015-07-01 15:41:14 +01005027
5028 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005029 // Potential implicit null checks, in the case of reference
5030 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005031 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005032 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005033 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034}
5035
5036void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005037 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005038
5039 bool needs_write_barrier =
5040 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005041 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005042
Nicolas Geoffray39468442014-09-02 15:17:15 +01005043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005044 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005045 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005046 LocationSummary::kCallOnSlowPath :
5047 LocationSummary::kNoCall);
5048
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005049 locations->SetInAt(0, Location::RequiresRegister());
5050 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5051 if (Primitive::IsFloatingPointType(value_type)) {
5052 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005054 locations->SetInAt(2, Location::RequiresRegister());
5055 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005056 if (needs_write_barrier) {
5057 // Temporary registers for the write barrier.
5058 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005059 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061}
5062
5063void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5064 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005065 Location array_loc = locations->InAt(0);
5066 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005068 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005069 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005070 bool needs_write_barrier =
5071 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005072 uint32_t data_offset =
5073 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5074 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005075 HInstruction* array_instr = instruction->GetArray();
5076 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077
5078 switch (value_type) {
5079 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005080 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005082 case Primitive::kPrimChar:
5083 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005085 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5086 uint32_t full_offset =
5087 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5088 StoreOperandType store_type = GetStoreOperandType(value_type);
5089 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005091 Register temp = IP;
5092
5093 if (has_intermediate_address) {
5094 // We do not need to compute the intermediate address from the array: the
5095 // input instruction has done it already. See the comment in
5096 // `TryExtractArrayAccessAddress()`.
5097 if (kIsDebugBuild) {
5098 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5099 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5100 }
5101 temp = array;
5102 } else {
5103 __ add(temp, array, ShifterOperand(data_offset));
5104 }
Artem Serov6c916792016-07-11 14:02:34 +01005105 codegen_->StoreToShiftedRegOffset(value_type,
5106 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005107 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005108 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 }
5110 break;
5111 }
5112
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005114 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005115 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5116 // See the comment in instruction_simplifier_shared.cc.
5117 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005118
5119 if (instruction->InputAt(2)->IsNullConstant()) {
5120 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005122 size_t offset =
5123 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005124 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005125 } else {
5126 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005127 __ add(IP, array, ShifterOperand(data_offset));
5128 codegen_->StoreToShiftedRegOffset(value_type,
5129 value_loc,
5130 IP,
5131 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005132 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005133 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005134 DCHECK(!needs_write_barrier);
5135 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005136 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005137 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005138
5139 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005140 Location temp1_loc = locations->GetTemp(0);
5141 Register temp1 = temp1_loc.AsRegister<Register>();
5142 Location temp2_loc = locations->GetTemp(1);
5143 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005144 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5145 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5146 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5147 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005148 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005149
Roland Levillain3b359c72015-11-17 19:35:12 +00005150 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005151 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5152 codegen_->AddSlowPath(slow_path);
5153 if (instruction->GetValueCanBeNull()) {
5154 Label non_zero;
5155 __ CompareAndBranchIfNonZero(value, &non_zero);
5156 if (index.IsConstant()) {
5157 size_t offset =
5158 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5159 __ StoreToOffset(kStoreWord, value, array, offset);
5160 } else {
5161 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005162 __ add(IP, array, ShifterOperand(data_offset));
5163 codegen_->StoreToShiftedRegOffset(value_type,
5164 value_loc,
5165 IP,
5166 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005167 }
5168 codegen_->MaybeRecordImplicitNullCheck(instruction);
5169 __ b(&done);
5170 __ Bind(&non_zero);
5171 }
5172
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005173 // Note that when read barriers are enabled, the type checks
5174 // are performed without read barriers. This is fine, even in
5175 // the case where a class object is in the from-space after
5176 // the flip, as a comparison involving such a type would not
5177 // produce a false positive; it may of course produce a false
5178 // negative, in which case we would take the ArraySet slow
5179 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005180
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005181 // /* HeapReference<Class> */ temp1 = array->klass_
5182 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5183 codegen_->MaybeRecordImplicitNullCheck(instruction);
5184 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005185
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005186 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5187 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5188 // /* HeapReference<Class> */ temp2 = value->klass_
5189 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5190 // If heap poisoning is enabled, no need to unpoison `temp1`
5191 // nor `temp2`, as we are comparing two poisoned references.
5192 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005193
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005194 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5195 Label do_put;
5196 __ b(&do_put, EQ);
5197 // If heap poisoning is enabled, the `temp1` reference has
5198 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005199 __ MaybeUnpoisonHeapReference(temp1);
5200
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005201 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5202 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5203 // If heap poisoning is enabled, no need to unpoison
5204 // `temp1`, as we are comparing against null below.
5205 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5206 __ Bind(&do_put);
5207 } else {
5208 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 }
5210 }
5211
Artem Serov6c916792016-07-11 14:02:34 +01005212 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 if (kPoisonHeapReferences) {
5214 // Note that in the case where `value` is a null reference,
5215 // we do not enter this block, as a null reference does not
5216 // need poisoning.
5217 DCHECK_EQ(value_type, Primitive::kPrimNot);
5218 __ Mov(temp1, value);
5219 __ PoisonHeapReference(temp1);
5220 source = temp1;
5221 }
5222
5223 if (index.IsConstant()) {
5224 size_t offset =
5225 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5226 __ StoreToOffset(kStoreWord, source, array, offset);
5227 } else {
5228 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005229
5230 __ add(IP, array, ShifterOperand(data_offset));
5231 codegen_->StoreToShiftedRegOffset(value_type,
5232 Location::RegisterLocation(source),
5233 IP,
5234 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 }
5236
Roland Levillain3b359c72015-11-17 19:35:12 +00005237 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005238 codegen_->MaybeRecordImplicitNullCheck(instruction);
5239 }
5240
5241 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5242
5243 if (done.IsLinked()) {
5244 __ Bind(&done);
5245 }
5246
5247 if (slow_path != nullptr) {
5248 __ Bind(slow_path->GetExitLabel());
5249 }
5250
5251 break;
5252 }
5253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005255 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005257 size_t offset =
5258 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005259 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005261 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005262 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 }
5264 break;
5265 }
5266
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005267 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005268 Location value = locations->InAt(2);
5269 DCHECK(value.IsFpuRegister());
5270 if (index.IsConstant()) {
5271 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005273 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005275 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5276 }
5277 break;
5278 }
5279
5280 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005281 Location value = locations->InAt(2);
5282 DCHECK(value.IsFpuRegisterPair());
5283 if (index.IsConstant()) {
5284 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005286 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005288 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5289 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005290
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005291 break;
5292 }
5293
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005295 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005296 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005298
Roland Levillain80e67092016-01-08 16:04:55 +00005299 // Objects are handled in the switch.
5300 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005301 codegen_->MaybeRecordImplicitNullCheck(instruction);
5302 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303}
5304
5305void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005306 LocationSummary* locations =
5307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005308 locations->SetInAt(0, Location::RequiresRegister());
5309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310}
5311
5312void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5313 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005314 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005315 Register obj = locations->InAt(0).AsRegister<Register>();
5316 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005319 // Mask out compression flag from String's array length.
5320 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5321 __ bic(out, out, ShifterOperand(1u << 31));
5322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323}
5324
Artem Serov328429f2016-07-06 16:23:04 +01005325void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005326 LocationSummary* locations =
5327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5328
5329 locations->SetInAt(0, Location::RequiresRegister());
5330 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5332}
5333
5334void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5335 LocationSummary* locations = instruction->GetLocations();
5336 Location out = locations->Out();
5337 Location first = locations->InAt(0);
5338 Location second = locations->InAt(1);
5339
Artem Serov328429f2016-07-06 16:23:04 +01005340 if (second.IsRegister()) {
5341 __ add(out.AsRegister<Register>(),
5342 first.AsRegister<Register>(),
5343 ShifterOperand(second.AsRegister<Register>()));
5344 } else {
5345 __ AddConstant(out.AsRegister<Register>(),
5346 first.AsRegister<Register>(),
5347 second.GetConstant()->AsIntConstant()->GetValue());
5348 }
5349}
5350
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005352 RegisterSet caller_saves = RegisterSet::Empty();
5353 InvokeRuntimeCallingConvention calling_convention;
5354 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5355 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5356 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357 locations->SetInAt(0, Location::RequiresRegister());
5358 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359}
5360
5361void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5362 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005363 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005364 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365 codegen_->AddSlowPath(slow_path);
5366
Roland Levillain271ab9c2014-11-27 15:23:57 +00005367 Register index = locations->InAt(0).AsRegister<Register>();
5368 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369
5370 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005371 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372}
5373
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005374void CodeGeneratorARM::MarkGCCard(Register temp,
5375 Register card,
5376 Register object,
5377 Register value,
5378 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005379 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005380 if (can_be_null) {
5381 __ CompareAndBranchIfZero(value, &is_null);
5382 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005383 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5385 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005386 if (can_be_null) {
5387 __ Bind(&is_null);
5388 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389}
5390
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005391void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005392 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005393}
5394
5395void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005396 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5397}
5398
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005399void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005400 LocationSummary* locations =
5401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005402 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005403}
5404
5405void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005406 HBasicBlock* block = instruction->GetBlock();
5407 if (block->GetLoopInformation() != nullptr) {
5408 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5409 // The back edge will generate the suspend check.
5410 return;
5411 }
5412 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5413 // The goto will generate the suspend check.
5414 return;
5415 }
5416 GenerateSuspendCheck(instruction, nullptr);
5417}
5418
5419void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5420 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005421 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005422 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5423 if (slow_path == nullptr) {
5424 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5425 instruction->SetSlowPath(slow_path);
5426 codegen_->AddSlowPath(slow_path);
5427 if (successor != nullptr) {
5428 DCHECK(successor->IsLoopHeader());
5429 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5430 }
5431 } else {
5432 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5433 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005434
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005435 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005436 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005437 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005438 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005439 __ Bind(slow_path->GetReturnLabel());
5440 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005441 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005442 __ b(slow_path->GetEntryLabel());
5443 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444}
5445
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005446ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5447 return codegen_->GetAssembler();
5448}
5449
5450void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005451 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005452 Location source = move->GetSource();
5453 Location destination = move->GetDestination();
5454
5455 if (source.IsRegister()) {
5456 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005457 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005458 } else if (destination.IsFpuRegister()) {
5459 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005460 } else {
5461 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005462 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005463 SP, destination.GetStackIndex());
5464 }
5465 } else if (source.IsStackSlot()) {
5466 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005467 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005468 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005469 } else if (destination.IsFpuRegister()) {
5470 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005471 } else {
5472 DCHECK(destination.IsStackSlot());
5473 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5474 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5475 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005476 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005477 if (destination.IsRegister()) {
5478 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5479 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005480 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005481 } else {
5482 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005483 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5484 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005485 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005486 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005487 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5488 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005489 } else if (destination.IsRegisterPair()) {
5490 DCHECK(ExpectedPairLayout(destination));
5491 __ LoadFromOffset(
5492 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5493 } else {
5494 DCHECK(destination.IsFpuRegisterPair()) << destination;
5495 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5496 SP,
5497 source.GetStackIndex());
5498 }
5499 } else if (source.IsRegisterPair()) {
5500 if (destination.IsRegisterPair()) {
5501 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5502 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005503 } else if (destination.IsFpuRegisterPair()) {
5504 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5505 source.AsRegisterPairLow<Register>(),
5506 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005507 } else {
5508 DCHECK(destination.IsDoubleStackSlot()) << destination;
5509 DCHECK(ExpectedPairLayout(source));
5510 __ StoreToOffset(
5511 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5512 }
5513 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005514 if (destination.IsRegisterPair()) {
5515 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5516 destination.AsRegisterPairHigh<Register>(),
5517 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5518 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005519 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5520 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5521 } else {
5522 DCHECK(destination.IsDoubleStackSlot()) << destination;
5523 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5524 SP,
5525 destination.GetStackIndex());
5526 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005527 } else {
5528 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005529 HConstant* constant = source.GetConstant();
5530 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5531 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005532 if (destination.IsRegister()) {
5533 __ LoadImmediate(destination.AsRegister<Register>(), value);
5534 } else {
5535 DCHECK(destination.IsStackSlot());
5536 __ LoadImmediate(IP, value);
5537 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5538 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005539 } else if (constant->IsLongConstant()) {
5540 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005541 if (destination.IsRegisterPair()) {
5542 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5543 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005544 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005545 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005546 __ LoadImmediate(IP, Low32Bits(value));
5547 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5548 __ LoadImmediate(IP, High32Bits(value));
5549 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5550 }
5551 } else if (constant->IsDoubleConstant()) {
5552 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005553 if (destination.IsFpuRegisterPair()) {
5554 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005555 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005556 DCHECK(destination.IsDoubleStackSlot()) << destination;
5557 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005558 __ LoadImmediate(IP, Low32Bits(int_value));
5559 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5560 __ LoadImmediate(IP, High32Bits(int_value));
5561 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5562 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005563 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005564 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005565 float value = constant->AsFloatConstant()->GetValue();
5566 if (destination.IsFpuRegister()) {
5567 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5568 } else {
5569 DCHECK(destination.IsStackSlot());
5570 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5571 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5572 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005573 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005574 }
5575}
5576
5577void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5578 __ Mov(IP, reg);
5579 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5580 __ StoreToOffset(kStoreWord, IP, SP, mem);
5581}
5582
5583void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5584 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5585 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5586 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5587 SP, mem1 + stack_offset);
5588 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5589 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5590 SP, mem2 + stack_offset);
5591 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5592}
5593
5594void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005595 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005596 Location source = move->GetSource();
5597 Location destination = move->GetDestination();
5598
5599 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005600 DCHECK_NE(source.AsRegister<Register>(), IP);
5601 DCHECK_NE(destination.AsRegister<Register>(), IP);
5602 __ Mov(IP, source.AsRegister<Register>());
5603 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5604 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005605 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005606 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005607 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005608 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005609 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5610 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005611 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005612 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005613 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005614 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005615 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005616 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005617 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005618 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005619 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5620 destination.AsRegisterPairHigh<Register>(),
5621 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005622 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005623 Register low_reg = source.IsRegisterPair()
5624 ? source.AsRegisterPairLow<Register>()
5625 : destination.AsRegisterPairLow<Register>();
5626 int mem = source.IsRegisterPair()
5627 ? destination.GetStackIndex()
5628 : source.GetStackIndex();
5629 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005630 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005631 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005632 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005633 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005634 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5635 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005636 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005637 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005638 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005639 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5640 DRegister reg = source.IsFpuRegisterPair()
5641 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5642 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5643 int mem = source.IsFpuRegisterPair()
5644 ? destination.GetStackIndex()
5645 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005646 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005647 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005648 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005649 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5650 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5651 : destination.AsFpuRegister<SRegister>();
5652 int mem = source.IsFpuRegister()
5653 ? destination.GetStackIndex()
5654 : source.GetStackIndex();
5655
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005656 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005657 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005658 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005659 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005660 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5661 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005662 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005663 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005664 }
5665}
5666
5667void ParallelMoveResolverARM::SpillScratch(int reg) {
5668 __ Push(static_cast<Register>(reg));
5669}
5670
5671void ParallelMoveResolverARM::RestoreScratch(int reg) {
5672 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005673}
5674
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005675HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5676 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 switch (desired_class_load_kind) {
5678 case HLoadClass::LoadKind::kReferrersClass:
5679 break;
5680 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5681 DCHECK(!GetCompilerOptions().GetCompilePic());
5682 break;
5683 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5684 DCHECK(GetCompilerOptions().GetCompilePic());
5685 break;
5686 case HLoadClass::LoadKind::kBootImageAddress:
5687 break;
5688 case HLoadClass::LoadKind::kDexCacheAddress:
5689 DCHECK(Runtime::Current()->UseJitCompilation());
5690 break;
5691 case HLoadClass::LoadKind::kDexCachePcRelative:
5692 DCHECK(!Runtime::Current()->UseJitCompilation());
5693 // We disable pc-relative load when there is an irreducible loop, as the optimization
5694 // is incompatible with it.
5695 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5696 // with irreducible loops.
5697 if (GetGraph()->HasIrreducibleLoops()) {
5698 return HLoadClass::LoadKind::kDexCacheViaMethod;
5699 }
5700 break;
5701 case HLoadClass::LoadKind::kDexCacheViaMethod:
5702 break;
5703 }
5704 return desired_class_load_kind;
5705}
5706
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005707void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708 if (cls->NeedsAccessCheck()) {
5709 InvokeRuntimeCallingConvention calling_convention;
5710 CodeGenerator::CreateLoadClassLocationSummary(
5711 cls,
5712 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5713 Location::RegisterLocation(R0),
5714 /* code_generator_supports_read_barrier */ true);
5715 return;
5716 }
5717
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005718 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5719 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005720 ? LocationSummary::kCallOnSlowPath
5721 : LocationSummary::kNoCall;
5722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005723 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005724 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005725 }
5726
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005727 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5728 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5729 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5730 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5731 locations->SetInAt(0, Location::RequiresRegister());
5732 }
5733 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734}
5735
5736void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005737 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005738 if (cls->NeedsAccessCheck()) {
5739 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005740 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005741 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005742 return;
5743 }
5744
Roland Levillain3b359c72015-11-17 19:35:12 +00005745 Location out_loc = locations->Out();
5746 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005747
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005748 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 bool generate_null_check = false;
5750 switch (cls->GetLoadKind()) {
5751 case HLoadClass::LoadKind::kReferrersClass: {
5752 DCHECK(!cls->CanCallRuntime());
5753 DCHECK(!cls->MustGenerateClinitCheck());
5754 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5755 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005756 GenerateGcRootFieldLoad(cls,
5757 out_loc,
5758 current_method,
5759 ArtMethod::DeclaringClassOffset().Int32Value(),
5760 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005761 break;
5762 }
5763 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005764 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5766 cls->GetTypeIndex()));
5767 break;
5768 }
5769 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005770 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 CodeGeneratorARM::PcRelativePatchInfo* labels =
5772 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5773 __ BindTrackedLabel(&labels->movw_label);
5774 __ movw(out, /* placeholder */ 0u);
5775 __ BindTrackedLabel(&labels->movt_label);
5776 __ movt(out, /* placeholder */ 0u);
5777 __ BindTrackedLabel(&labels->add_pc_label);
5778 __ add(out, out, ShifterOperand(PC));
5779 break;
5780 }
5781 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005782 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005783 DCHECK_NE(cls->GetAddress(), 0u);
5784 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5785 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5786 break;
5787 }
5788 case HLoadClass::LoadKind::kDexCacheAddress: {
5789 DCHECK_NE(cls->GetAddress(), 0u);
5790 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5791 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5792 // a 128B range. To try and reduce the number of literals if we load multiple types,
5793 // simply split the dex cache address to a 128B aligned base loaded from a literal
5794 // and the remaining offset embedded in the load.
5795 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5796 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5797 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5798 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5799 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5800 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5801 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005802 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005803 generate_null_check = !cls->IsInDexCache();
5804 break;
5805 }
5806 case HLoadClass::LoadKind::kDexCachePcRelative: {
5807 Register base_reg = locations->InAt(0).AsRegister<Register>();
5808 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5809 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5810 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005811 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005812 generate_null_check = !cls->IsInDexCache();
5813 break;
5814 }
5815 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5816 // /* GcRoot<mirror::Class>[] */ out =
5817 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5818 Register current_method = locations->InAt(0).AsRegister<Register>();
5819 __ LoadFromOffset(kLoadWord,
5820 out,
5821 current_method,
5822 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5823 // /* GcRoot<mirror::Class> */ out = out[type_index]
5824 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005825 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826 generate_null_check = !cls->IsInDexCache();
5827 }
5828 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005829
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5831 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005832 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5834 codegen_->AddSlowPath(slow_path);
5835 if (generate_null_check) {
5836 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5837 }
5838 if (cls->MustGenerateClinitCheck()) {
5839 GenerateClassInitializationCheck(slow_path, out);
5840 } else {
5841 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005842 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005843 }
5844}
5845
5846void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5847 LocationSummary* locations =
5848 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5849 locations->SetInAt(0, Location::RequiresRegister());
5850 if (check->HasUses()) {
5851 locations->SetOut(Location::SameAsFirstInput());
5852 }
5853}
5854
5855void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005856 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005857 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005858 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005859 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005860 GenerateClassInitializationCheck(slow_path,
5861 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005862}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005863
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005864void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005865 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5867 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5868 __ b(slow_path->GetEntryLabel(), LT);
5869 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5870 // properly. Therefore, we do a memory fence.
5871 __ dmb(ISH);
5872 __ Bind(slow_path->GetExitLabel());
5873}
5874
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5876 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005877 switch (desired_string_load_kind) {
5878 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5879 DCHECK(!GetCompilerOptions().GetCompilePic());
5880 break;
5881 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5882 DCHECK(GetCompilerOptions().GetCompilePic());
5883 break;
5884 case HLoadString::LoadKind::kBootImageAddress:
5885 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005886 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005887 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888 break;
5889 case HLoadString::LoadKind::kDexCacheViaMethod:
5890 break;
5891 }
5892 return desired_string_load_kind;
5893}
5894
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005895void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005896 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005897 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5898 ? LocationSummary::kCallOnMainOnly
5899 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005900 : LocationSummary::kNoCall;
5901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005902
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005904 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005905 locations->SetOut(Location::RegisterLocation(R0));
5906 } else {
5907 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005908 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5909 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5910 // Rely on the pResolveString and/or marking to save everything, including temps.
5911 // Note that IP may theoretically be clobbered by saving/restoring the live register
5912 // (only one thanks to the custom calling convention), so we request a different temp.
5913 locations->AddTemp(Location::RequiresRegister());
5914 RegisterSet caller_saves = RegisterSet::Empty();
5915 InvokeRuntimeCallingConvention calling_convention;
5916 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5917 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5918 // that the the kPrimNot result register is the same as the first argument register.
5919 locations->SetCustomSlowPathCallerSaves(caller_saves);
5920 } else {
5921 // For non-Baker read barrier we have a temp-clobbering call.
5922 }
5923 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005925}
5926
5927void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005928 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005929 Location out_loc = locations->Out();
5930 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005931 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005932
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005933 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5936 load->GetStringIndex()));
5937 return; // No dex cache slow path.
5938 }
5939 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005940 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941 CodeGeneratorARM::PcRelativePatchInfo* labels =
5942 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5943 __ BindTrackedLabel(&labels->movw_label);
5944 __ movw(out, /* placeholder */ 0u);
5945 __ BindTrackedLabel(&labels->movt_label);
5946 __ movt(out, /* placeholder */ 0u);
5947 __ BindTrackedLabel(&labels->add_pc_label);
5948 __ add(out, out, ShifterOperand(PC));
5949 return; // No dex cache slow path.
5950 }
5951 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952 DCHECK_NE(load->GetAddress(), 0u);
5953 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5954 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5955 return; // No dex cache slow path.
5956 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005957 case HLoadString::LoadKind::kBssEntry: {
5958 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005959 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005960 CodeGeneratorARM::PcRelativePatchInfo* labels =
5961 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5962 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005963 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005964 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005965 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005966 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005967 __ add(temp, temp, ShifterOperand(PC));
Roland Levillain00468f32016-10-27 18:02:48 +01005968 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005969 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5970 codegen_->AddSlowPath(slow_path);
5971 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5972 __ Bind(slow_path->GetExitLabel());
5973 return;
5974 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005975 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005976 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005977 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005978
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005979 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5980 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5981 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005982 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005983 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5984 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5985 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005986}
5987
David Brazdilcb1c0552015-08-04 16:22:25 +01005988static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005989 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005990}
5991
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005992void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5993 LocationSummary* locations =
5994 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5995 locations->SetOut(Location::RequiresRegister());
5996}
5997
5998void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005999 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006000 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6001}
6002
6003void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6004 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6005}
6006
6007void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006008 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006009 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006010}
6011
6012void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6013 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006015 InvokeRuntimeCallingConvention calling_convention;
6016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6017}
6018
6019void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006020 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006021 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006022}
6023
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006024// Temp is used for read barrier.
6025static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6026 if (kEmitCompilerReadBarrier &&
6027 (kUseBakerReadBarrier ||
6028 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6029 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6030 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6031 return 1;
6032 }
6033 return 0;
6034}
6035
6036// InteraceCheck has 3 temps, one for holding the number of interfaces, one for the current
6037// interface pointer, one for loading the current interface.
6038// The other checks have one temp for loading the object's class.
6039static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6040 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6041 return 3;
6042 }
6043 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006044}
6045
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006046void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006048 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006049 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006050 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006051 case TypeCheckKind::kExactCheck:
6052 case TypeCheckKind::kAbstractClassCheck:
6053 case TypeCheckKind::kClassHierarchyCheck:
6054 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006055 call_kind =
6056 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006057 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006058 break;
6059 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006060 case TypeCheckKind::kUnresolvedCheck:
6061 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 call_kind = LocationSummary::kCallOnSlowPath;
6063 break;
6064 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006065
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006067 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006068 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006069 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006070 locations->SetInAt(0, Location::RequiresRegister());
6071 locations->SetInAt(1, Location::RequiresRegister());
6072 // The "out" register is used as a temporary, so it overlaps with the inputs.
6073 // Note that TypeCheckSlowPathARM uses this register too.
6074 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006075 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006076}
6077
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006078void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006079 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006080 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006081 Location obj_loc = locations->InAt(0);
6082 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006083 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006084 Location out_loc = locations->Out();
6085 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006086 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6087 DCHECK_LE(num_temps, 1u);
6088 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006089 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006090 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6091 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6092 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006093 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006094 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006095
6096 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006097 // avoid null check if we know obj is not null.
6098 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006099 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006100 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006101
Roland Levillain3b359c72015-11-17 19:35:12 +00006102 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006103 GenerateReferenceLoadTwoRegisters(instruction,
6104 out_loc,
6105 obj_loc,
6106 class_offset,
6107 maybe_temp_loc,
6108 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109
Roland Levillainc9285912015-12-18 10:38:42 +00006110 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 case TypeCheckKind::kExactCheck: {
6112 __ cmp(out, ShifterOperand(cls));
6113 // Classes must be equal for the instanceof to succeed.
6114 __ b(&zero, NE);
6115 __ LoadImmediate(out, 1);
6116 __ b(&done);
6117 break;
6118 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006120 case TypeCheckKind::kAbstractClassCheck: {
6121 // If the class is abstract, we eagerly fetch the super class of the
6122 // object to avoid doing a comparison we know will fail.
6123 Label loop;
6124 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006125 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006126 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 // If `out` is null, we use it for the result, and jump to `done`.
6128 __ CompareAndBranchIfZero(out, &done);
6129 __ cmp(out, ShifterOperand(cls));
6130 __ b(&loop, NE);
6131 __ LoadImmediate(out, 1);
6132 if (zero.IsLinked()) {
6133 __ b(&done);
6134 }
6135 break;
6136 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006137
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 case TypeCheckKind::kClassHierarchyCheck: {
6139 // Walk over the class hierarchy to find a match.
6140 Label loop, success;
6141 __ Bind(&loop);
6142 __ cmp(out, ShifterOperand(cls));
6143 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006144 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006145 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 __ CompareAndBranchIfNonZero(out, &loop);
6147 // If `out` is null, we use it for the result, and jump to `done`.
6148 __ b(&done);
6149 __ Bind(&success);
6150 __ LoadImmediate(out, 1);
6151 if (zero.IsLinked()) {
6152 __ b(&done);
6153 }
6154 break;
6155 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006158 // Do an exact check.
6159 Label exact_check;
6160 __ cmp(out, ShifterOperand(cls));
6161 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006162 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006163 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006164 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 // If `out` is null, we use it for the result, and jump to `done`.
6166 __ CompareAndBranchIfZero(out, &done);
6167 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6168 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6169 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006170 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 __ LoadImmediate(out, 1);
6172 __ b(&done);
6173 break;
6174 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 case TypeCheckKind::kArrayCheck: {
6177 __ cmp(out, ShifterOperand(cls));
6178 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006179 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6180 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 codegen_->AddSlowPath(slow_path);
6182 __ b(slow_path->GetEntryLabel(), NE);
6183 __ LoadImmediate(out, 1);
6184 if (zero.IsLinked()) {
6185 __ b(&done);
6186 }
6187 break;
6188 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006189
Calin Juravle98893e12015-10-02 21:05:03 +01006190 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006191 case TypeCheckKind::kInterfaceCheck: {
6192 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006193 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006194 // cases.
6195 //
6196 // We cannot directly call the InstanceofNonTrivial runtime
6197 // entry point without resorting to a type checking slow path
6198 // here (i.e. by calling InvokeRuntime directly), as it would
6199 // require to assign fixed registers for the inputs of this
6200 // HInstanceOf instruction (following the runtime calling
6201 // convention), which might be cluttered by the potential first
6202 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006203 //
6204 // TODO: Introduce a new runtime entry point taking the object
6205 // to test (instead of its class) as argument, and let it deal
6206 // with the read barrier issues. This will let us refactor this
6207 // case of the `switch` code as it was previously (with a direct
6208 // call to the runtime not using a type checking slow path).
6209 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006210 DCHECK(locations->OnlyCallsOnSlowPath());
6211 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6212 /* is_fatal */ false);
6213 codegen_->AddSlowPath(slow_path);
6214 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 if (zero.IsLinked()) {
6216 __ b(&done);
6217 }
6218 break;
6219 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006220 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006223 __ Bind(&zero);
6224 __ LoadImmediate(out, 0);
6225 }
6226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006227 if (done.IsLinked()) {
6228 __ Bind(&done);
6229 }
6230
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006231 if (slow_path != nullptr) {
6232 __ Bind(slow_path->GetExitLabel());
6233 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006234}
6235
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006236void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006237 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6238 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6239
Roland Levillain3b359c72015-11-17 19:35:12 +00006240 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6241 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 case TypeCheckKind::kExactCheck:
6243 case TypeCheckKind::kAbstractClassCheck:
6244 case TypeCheckKind::kClassHierarchyCheck:
6245 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006246 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6247 LocationSummary::kCallOnSlowPath :
6248 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006249 break;
6250 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006251 case TypeCheckKind::kUnresolvedCheck:
6252 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 call_kind = LocationSummary::kCallOnSlowPath;
6254 break;
6255 }
6256
Roland Levillain3b359c72015-11-17 19:35:12 +00006257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6258 locations->SetInAt(0, Location::RequiresRegister());
6259 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006260 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006261}
6262
6263void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006264 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006265 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006266 Location obj_loc = locations->InAt(0);
6267 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006268 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006269 Location temp_loc = locations->GetTemp(0);
6270 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006271 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6272 DCHECK_LE(num_temps, 3u);
6273 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6274 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6275 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6276 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6277 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6278 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6279 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6280 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6281 const uint32_t object_array_data_offset =
6282 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006283
Roland Levillain3b359c72015-11-17 19:35:12 +00006284 bool is_type_check_slow_path_fatal =
6285 (type_check_kind == TypeCheckKind::kExactCheck ||
6286 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6287 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6288 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6289 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006290 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006291 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6292 is_type_check_slow_path_fatal);
6293 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294
6295 Label done;
6296 // Avoid null check if we know obj is not null.
6297 if (instruction->MustDoNullCheck()) {
6298 __ CompareAndBranchIfZero(obj, &done);
6299 }
6300
Roland Levillain3b359c72015-11-17 19:35:12 +00006301 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 case TypeCheckKind::kExactCheck:
6303 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006304 // /* HeapReference<Class> */ temp = obj->klass_
6305 GenerateReferenceLoadTwoRegisters(instruction,
6306 temp_loc,
6307 obj_loc,
6308 class_offset,
6309 maybe_temp2_loc,
6310 kEmitCompilerReadBarrier);
6311
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 __ cmp(temp, ShifterOperand(cls));
6313 // Jump to slow path for throwing the exception or doing a
6314 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006315 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 break;
6317 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006318
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006320 // /* HeapReference<Class> */ temp = obj->klass_
6321 GenerateReferenceLoadTwoRegisters(instruction,
6322 temp_loc,
6323 obj_loc,
6324 class_offset,
6325 maybe_temp2_loc,
6326 kEmitCompilerReadBarrier);
6327
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 // If the class is abstract, we eagerly fetch the super class of the
6329 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006330 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006332 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006333 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006334
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006335 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6336 // exception.
6337 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006338
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006339 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 __ cmp(temp, ShifterOperand(cls));
6341 __ b(&loop, NE);
6342 break;
6343 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006346 // /* HeapReference<Class> */ temp = obj->klass_
6347 GenerateReferenceLoadTwoRegisters(instruction,
6348 temp_loc,
6349 obj_loc,
6350 class_offset,
6351 maybe_temp2_loc,
6352 kEmitCompilerReadBarrier);
6353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006355 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006356 __ Bind(&loop);
6357 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006358 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006359
Roland Levillain3b359c72015-11-17 19:35:12 +00006360 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006361 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006362
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006363 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6364 // exception.
6365 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6366 // Otherwise, jump to the beginning of the loop.
6367 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006368 break;
6369 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006372 // /* HeapReference<Class> */ temp = obj->klass_
6373 GenerateReferenceLoadTwoRegisters(instruction,
6374 temp_loc,
6375 obj_loc,
6376 class_offset,
6377 maybe_temp2_loc,
6378 kEmitCompilerReadBarrier);
6379
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006380 // Do an exact check.
6381 __ cmp(temp, ShifterOperand(cls));
6382 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006383
6384 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006385 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006386 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006387 // If the component type is null, jump to the slow path to throw the exception.
6388 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6389 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6390 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006392 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006393 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394 break;
6395 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006396
Calin Juravle98893e12015-10-02 21:05:03 +01006397 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006398 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006399 // We cannot directly call the CheckCast runtime entry point
6400 // without resorting to a type checking slow path here (i.e. by
6401 // calling InvokeRuntime directly), as it would require to
6402 // assign fixed registers for the inputs of this HInstanceOf
6403 // instruction (following the runtime calling convention), which
6404 // might be cluttered by the potential first read barrier
6405 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006406
Roland Levillain3b359c72015-11-17 19:35:12 +00006407 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006409
6410 case TypeCheckKind::kInterfaceCheck: {
6411 // Avoid read barriers to improve performance of the fast path. We can not get false
6412 // positives by doing this.
6413 // /* HeapReference<Class> */ temp = obj->klass_
6414 GenerateReferenceLoadTwoRegisters(instruction,
6415 temp_loc,
6416 obj_loc,
6417 class_offset,
6418 maybe_temp2_loc,
6419 /*emit_read_barrier*/ false);
6420
6421 // /* HeapReference<Class> */ temp = temp->iftable_
6422 GenerateReferenceLoadTwoRegisters(instruction,
6423 temp_loc,
6424 temp_loc,
6425 iftable_offset,
6426 maybe_temp2_loc,
6427 /*emit_read_barrier*/ false);
6428 Label is_null;
6429 // Null iftable means it is empty and will always fail the check.
6430 // Not cbz since the temp may not be a low register.
6431 __ CompareAndBranchIfZero(temp, &is_null);
6432
6433 // Loop through the iftable and check if any class matches.
6434 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6435
6436 Label start_loop;
6437 __ Bind(&start_loop);
6438 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6439 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
6440 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6441 __ b(&done, EQ); // Return if same class.
6442 // Go to next interface.
6443 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6444 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6445 maybe_temp2_loc.AsRegister<Register>(),
6446 ShifterOperand(2));
6447 // Not cbnz since the temp may not be a low register.
6448 __ CompareAndBranchIfNonZero(maybe_temp2_loc.AsRegister<Register>(), &start_loop);
6449 __ Bind(&is_null);
6450
6451 __ b(type_check_slow_path->GetEntryLabel());
6452 break;
6453 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 }
6455 __ Bind(&done);
6456
Roland Levillain3b359c72015-11-17 19:35:12 +00006457 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006458}
6459
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006460void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6461 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006462 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006463 InvokeRuntimeCallingConvention calling_convention;
6464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6465}
6466
6467void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006468 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6469 instruction,
6470 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006471 if (instruction->IsEnter()) {
6472 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6473 } else {
6474 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6475 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006476}
6477
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006478void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6479void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6480void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006481
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006482void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006483 LocationSummary* locations =
6484 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6485 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6486 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006487 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006488 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006489 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006491}
6492
6493void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6494 HandleBitwiseOperation(instruction);
6495}
6496
6497void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6498 HandleBitwiseOperation(instruction);
6499}
6500
6501void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6502 HandleBitwiseOperation(instruction);
6503}
6504
Artem Serov7fc63502016-02-09 17:15:29 +00006505
6506void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6507 LocationSummary* locations =
6508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6509 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6510 || instruction->GetResultType() == Primitive::kPrimLong);
6511
6512 locations->SetInAt(0, Location::RequiresRegister());
6513 locations->SetInAt(1, Location::RequiresRegister());
6514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6515}
6516
6517void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6518 LocationSummary* locations = instruction->GetLocations();
6519 Location first = locations->InAt(0);
6520 Location second = locations->InAt(1);
6521 Location out = locations->Out();
6522
6523 if (instruction->GetResultType() == Primitive::kPrimInt) {
6524 Register first_reg = first.AsRegister<Register>();
6525 ShifterOperand second_reg(second.AsRegister<Register>());
6526 Register out_reg = out.AsRegister<Register>();
6527
6528 switch (instruction->GetOpKind()) {
6529 case HInstruction::kAnd:
6530 __ bic(out_reg, first_reg, second_reg);
6531 break;
6532 case HInstruction::kOr:
6533 __ orn(out_reg, first_reg, second_reg);
6534 break;
6535 // There is no EON on arm.
6536 case HInstruction::kXor:
6537 default:
6538 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6539 UNREACHABLE();
6540 }
6541 return;
6542
6543 } else {
6544 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6545 Register first_low = first.AsRegisterPairLow<Register>();
6546 Register first_high = first.AsRegisterPairHigh<Register>();
6547 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6548 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6549 Register out_low = out.AsRegisterPairLow<Register>();
6550 Register out_high = out.AsRegisterPairHigh<Register>();
6551
6552 switch (instruction->GetOpKind()) {
6553 case HInstruction::kAnd:
6554 __ bic(out_low, first_low, second_low);
6555 __ bic(out_high, first_high, second_high);
6556 break;
6557 case HInstruction::kOr:
6558 __ orn(out_low, first_low, second_low);
6559 __ orn(out_high, first_high, second_high);
6560 break;
6561 // There is no EON on arm.
6562 case HInstruction::kXor:
6563 default:
6564 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6565 UNREACHABLE();
6566 }
6567 }
6568}
6569
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006570void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6571 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6572 if (value == 0xffffffffu) {
6573 if (out != first) {
6574 __ mov(out, ShifterOperand(first));
6575 }
6576 return;
6577 }
6578 if (value == 0u) {
6579 __ mov(out, ShifterOperand(0));
6580 return;
6581 }
6582 ShifterOperand so;
6583 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6584 __ and_(out, first, so);
6585 } else {
6586 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6587 __ bic(out, first, ShifterOperand(~value));
6588 }
6589}
6590
6591void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6592 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6593 if (value == 0u) {
6594 if (out != first) {
6595 __ mov(out, ShifterOperand(first));
6596 }
6597 return;
6598 }
6599 if (value == 0xffffffffu) {
6600 __ mvn(out, ShifterOperand(0));
6601 return;
6602 }
6603 ShifterOperand so;
6604 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6605 __ orr(out, first, so);
6606 } else {
6607 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6608 __ orn(out, first, ShifterOperand(~value));
6609 }
6610}
6611
6612void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6613 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6614 if (value == 0u) {
6615 if (out != first) {
6616 __ mov(out, ShifterOperand(first));
6617 }
6618 return;
6619 }
6620 __ eor(out, first, ShifterOperand(value));
6621}
6622
Vladimir Marko59751a72016-08-05 14:37:27 +01006623void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6624 Location first,
6625 uint64_t value) {
6626 Register out_low = out.AsRegisterPairLow<Register>();
6627 Register out_high = out.AsRegisterPairHigh<Register>();
6628 Register first_low = first.AsRegisterPairLow<Register>();
6629 Register first_high = first.AsRegisterPairHigh<Register>();
6630 uint32_t value_low = Low32Bits(value);
6631 uint32_t value_high = High32Bits(value);
6632 if (value_low == 0u) {
6633 if (out_low != first_low) {
6634 __ mov(out_low, ShifterOperand(first_low));
6635 }
6636 __ AddConstant(out_high, first_high, value_high);
6637 return;
6638 }
6639 __ AddConstantSetFlags(out_low, first_low, value_low);
6640 ShifterOperand so;
6641 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6642 __ adc(out_high, first_high, so);
6643 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6644 __ sbc(out_high, first_high, so);
6645 } else {
6646 LOG(FATAL) << "Unexpected constant " << value_high;
6647 UNREACHABLE();
6648 }
6649}
6650
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006651void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6652 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006653 Location first = locations->InAt(0);
6654 Location second = locations->InAt(1);
6655 Location out = locations->Out();
6656
6657 if (second.IsConstant()) {
6658 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6659 uint32_t value_low = Low32Bits(value);
6660 if (instruction->GetResultType() == Primitive::kPrimInt) {
6661 Register first_reg = first.AsRegister<Register>();
6662 Register out_reg = out.AsRegister<Register>();
6663 if (instruction->IsAnd()) {
6664 GenerateAndConst(out_reg, first_reg, value_low);
6665 } else if (instruction->IsOr()) {
6666 GenerateOrrConst(out_reg, first_reg, value_low);
6667 } else {
6668 DCHECK(instruction->IsXor());
6669 GenerateEorConst(out_reg, first_reg, value_low);
6670 }
6671 } else {
6672 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6673 uint32_t value_high = High32Bits(value);
6674 Register first_low = first.AsRegisterPairLow<Register>();
6675 Register first_high = first.AsRegisterPairHigh<Register>();
6676 Register out_low = out.AsRegisterPairLow<Register>();
6677 Register out_high = out.AsRegisterPairHigh<Register>();
6678 if (instruction->IsAnd()) {
6679 GenerateAndConst(out_low, first_low, value_low);
6680 GenerateAndConst(out_high, first_high, value_high);
6681 } else if (instruction->IsOr()) {
6682 GenerateOrrConst(out_low, first_low, value_low);
6683 GenerateOrrConst(out_high, first_high, value_high);
6684 } else {
6685 DCHECK(instruction->IsXor());
6686 GenerateEorConst(out_low, first_low, value_low);
6687 GenerateEorConst(out_high, first_high, value_high);
6688 }
6689 }
6690 return;
6691 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006692
6693 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006694 Register first_reg = first.AsRegister<Register>();
6695 ShifterOperand second_reg(second.AsRegister<Register>());
6696 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006697 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006698 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006699 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006700 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006701 } else {
6702 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006703 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006704 }
6705 } else {
6706 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006707 Register first_low = first.AsRegisterPairLow<Register>();
6708 Register first_high = first.AsRegisterPairHigh<Register>();
6709 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6710 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6711 Register out_low = out.AsRegisterPairLow<Register>();
6712 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006713 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006714 __ and_(out_low, first_low, second_low);
6715 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006716 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006717 __ orr(out_low, first_low, second_low);
6718 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006719 } else {
6720 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006721 __ eor(out_low, first_low, second_low);
6722 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006723 }
6724 }
6725}
6726
Roland Levillainc9285912015-12-18 10:38:42 +00006727void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6728 Location out,
6729 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006730 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006731 Register out_reg = out.AsRegister<Register>();
6732 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006733 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006734 if (kUseBakerReadBarrier) {
6735 // Load with fast path based Baker's read barrier.
6736 // /* HeapReference<Object> */ out = *(out + offset)
6737 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006738 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006739 } else {
6740 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006741 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006742 // in the following move operation, as we will need it for the
6743 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006744 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006745 // /* HeapReference<Object> */ out = *(out + offset)
6746 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006747 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006748 }
6749 } else {
6750 // Plain load with no read barrier.
6751 // /* HeapReference<Object> */ out = *(out + offset)
6752 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6753 __ MaybeUnpoisonHeapReference(out_reg);
6754 }
6755}
6756
6757void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6758 Location out,
6759 Location obj,
6760 uint32_t offset,
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006761 Location maybe_temp,
6762 bool emit_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006763 Register out_reg = out.AsRegister<Register>();
6764 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006765 if (emit_read_barrier) {
6766 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006767 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006768 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006769 // Load with fast path based Baker's read barrier.
6770 // /* HeapReference<Object> */ out = *(obj + offset)
6771 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006772 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006773 } else {
6774 // Load with slow path based read barrier.
6775 // /* HeapReference<Object> */ out = *(obj + offset)
6776 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6777 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6778 }
6779 } else {
6780 // Plain load with no read barrier.
6781 // /* HeapReference<Object> */ out = *(obj + offset)
6782 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6783 __ MaybeUnpoisonHeapReference(out_reg);
6784 }
6785}
6786
6787void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6788 Location root,
6789 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006790 uint32_t offset,
6791 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006792 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006793 if (requires_read_barrier) {
6794 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006795 if (kUseBakerReadBarrier) {
6796 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6797 // Baker's read barrier are used:
6798 //
6799 // root = obj.field;
6800 // if (Thread::Current()->GetIsGcMarking()) {
6801 // root = ReadBarrier::Mark(root)
6802 // }
6803
6804 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6805 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6806 static_assert(
6807 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6808 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6809 "have different sizes.");
6810 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6811 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6812 "have different sizes.");
6813
Vladimir Marko953437b2016-08-24 08:30:46 +00006814 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006815 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006816 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006817 codegen_->AddSlowPath(slow_path);
6818
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006819 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006820 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006821 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006822 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6823 __ Bind(slow_path->GetExitLabel());
6824 } else {
6825 // GC root loaded through a slow path for read barriers other
6826 // than Baker's.
6827 // /* GcRoot<mirror::Object>* */ root = obj + offset
6828 __ AddConstant(root_reg, obj, offset);
6829 // /* mirror::Object* */ root = root->Read()
6830 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6831 }
6832 } else {
6833 // Plain GC root load with no read barrier.
6834 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6835 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6836 // Note that GC roots are not affected by heap poisoning, thus we
6837 // do not have to unpoison `root_reg` here.
6838 }
6839}
6840
6841void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6842 Location ref,
6843 Register obj,
6844 uint32_t offset,
6845 Location temp,
6846 bool needs_null_check) {
6847 DCHECK(kEmitCompilerReadBarrier);
6848 DCHECK(kUseBakerReadBarrier);
6849
6850 // /* HeapReference<Object> */ ref = *(obj + offset)
6851 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006852 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006853 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006854 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006855}
6856
6857void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6858 Location ref,
6859 Register obj,
6860 uint32_t data_offset,
6861 Location index,
6862 Location temp,
6863 bool needs_null_check) {
6864 DCHECK(kEmitCompilerReadBarrier);
6865 DCHECK(kUseBakerReadBarrier);
6866
Roland Levillainbfea3352016-06-23 13:48:47 +01006867 static_assert(
6868 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6869 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006870 // /* HeapReference<Object> */ ref =
6871 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006872 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006873 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006874 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006875}
6876
6877void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6878 Location ref,
6879 Register obj,
6880 uint32_t offset,
6881 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006882 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006883 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006884 bool needs_null_check,
6885 bool always_update_field,
6886 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006887 DCHECK(kEmitCompilerReadBarrier);
6888 DCHECK(kUseBakerReadBarrier);
6889
6890 // In slow path based read barriers, the read barrier call is
6891 // inserted after the original load. However, in fast path based
6892 // Baker's read barriers, we need to perform the load of
6893 // mirror::Object::monitor_ *before* the original reference load.
6894 // This load-load ordering is required by the read barrier.
6895 // The fast path/slow path (for Baker's algorithm) should look like:
6896 //
6897 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6898 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6899 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006900 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006901 // if (is_gray) {
6902 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6903 // }
6904 //
6905 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006906 // slightly more complex as it performs additional checks that we do
6907 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006908
6909 Register ref_reg = ref.AsRegister<Register>();
6910 Register temp_reg = temp.AsRegister<Register>();
6911 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6912
6913 // /* int32_t */ monitor = obj->monitor_
6914 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6915 if (needs_null_check) {
6916 MaybeRecordImplicitNullCheck(instruction);
6917 }
6918 // /* LockWord */ lock_word = LockWord(monitor)
6919 static_assert(sizeof(LockWord) == sizeof(int32_t),
6920 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006921
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006922 // Introduce a dependency on the lock_word including the rb_state,
6923 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006924 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006925 // `obj` is unchanged by this operation, but its value now depends
6926 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006927 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006928
6929 // The actual reference load.
6930 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006931 // Load types involving an "index": ArrayGet,
6932 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6933 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006934 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006935 if (index.IsConstant()) {
6936 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006937 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006938 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6939 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006940 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006941 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6942 // intrinsics, which use a register pair as index ("long
6943 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01006944 Register index_reg = index.IsRegisterPair()
6945 ? index.AsRegisterPairLow<Register>()
6946 : index.AsRegister<Register>();
6947 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006948 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6949 }
6950 } else {
6951 // /* HeapReference<Object> */ ref = *(obj + offset)
6952 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6953 }
6954
6955 // Object* ref = ref_addr->AsMirrorPtr()
6956 __ MaybeUnpoisonHeapReference(ref_reg);
6957
Vladimir Marko953437b2016-08-24 08:30:46 +00006958 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006959 SlowPathCodeARM* slow_path;
6960 if (always_update_field) {
6961 DCHECK(temp2 != nullptr);
6962 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
6963 // of the form `obj + field_offset`, where `obj` is a register and
6964 // `field_offset` is a register pair (of which only the lower half
6965 // is used). Thus `offset` and `scale_factor` above are expected
6966 // to be null in this code path.
6967 DCHECK_EQ(offset, 0u);
6968 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6969 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
6970 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
6971 } else {
6972 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
6973 }
Roland Levillainc9285912015-12-18 10:38:42 +00006974 AddSlowPath(slow_path);
6975
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006976 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00006977 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006978 // Given the numeric representation, it's enough to check the low bit of the
6979 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6980 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006981 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6982 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006983 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6984 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006985 __ Bind(slow_path->GetExitLabel());
6986}
6987
6988void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6989 Location out,
6990 Location ref,
6991 Location obj,
6992 uint32_t offset,
6993 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006994 DCHECK(kEmitCompilerReadBarrier);
6995
Roland Levillainc9285912015-12-18 10:38:42 +00006996 // Insert a slow path based read barrier *after* the reference load.
6997 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006998 // If heap poisoning is enabled, the unpoisoning of the loaded
6999 // reference will be carried out by the runtime within the slow
7000 // path.
7001 //
7002 // Note that `ref` currently does not get unpoisoned (when heap
7003 // poisoning is enabled), which is alright as the `ref` argument is
7004 // not used by the artReadBarrierSlow entry point.
7005 //
7006 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007007 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007008 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7009 AddSlowPath(slow_path);
7010
Roland Levillain3b359c72015-11-17 19:35:12 +00007011 __ b(slow_path->GetEntryLabel());
7012 __ Bind(slow_path->GetExitLabel());
7013}
7014
Roland Levillainc9285912015-12-18 10:38:42 +00007015void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7016 Location out,
7017 Location ref,
7018 Location obj,
7019 uint32_t offset,
7020 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007021 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007022 // Baker's read barriers shall be handled by the fast path
7023 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7024 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007025 // If heap poisoning is enabled, unpoisoning will be taken care of
7026 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007027 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007028 } else if (kPoisonHeapReferences) {
7029 __ UnpoisonHeapReference(out.AsRegister<Register>());
7030 }
7031}
7032
Roland Levillainc9285912015-12-18 10:38:42 +00007033void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7034 Location out,
7035 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007036 DCHECK(kEmitCompilerReadBarrier);
7037
Roland Levillainc9285912015-12-18 10:38:42 +00007038 // Insert a slow path based read barrier *after* the GC root load.
7039 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007040 // Note that GC roots are not affected by heap poisoning, so we do
7041 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007042 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007043 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7044 AddSlowPath(slow_path);
7045
Roland Levillain3b359c72015-11-17 19:35:12 +00007046 __ b(slow_path->GetEntryLabel());
7047 __ Bind(slow_path->GetExitLabel());
7048}
7049
Vladimir Markodc151b22015-10-15 18:02:30 +01007050HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7051 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007052 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007053 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7054 // We disable pc-relative load when there is an irreducible loop, as the optimization
7055 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007056 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7057 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007058 if (GetGraph()->HasIrreducibleLoops() &&
7059 (dispatch_info.method_load_kind ==
7060 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7061 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7062 }
7063
7064 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007065 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007066 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007067 // Calls across dex files are more likely to exceed the available BL range,
7068 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7069 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7070 (desired_dispatch_info.method_load_kind ==
7071 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7072 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7073 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7074 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007075 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01007076 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007077 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01007078 0u
7079 };
7080 }
7081 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007082 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007083}
7084
Vladimir Markob4536b72015-11-24 13:45:23 +00007085Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7086 Register temp) {
7087 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7088 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7089 if (!invoke->GetLocations()->Intrinsified()) {
7090 return location.AsRegister<Register>();
7091 }
7092 // For intrinsics we allow any location, so it may be on the stack.
7093 if (!location.IsRegister()) {
7094 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7095 return temp;
7096 }
7097 // For register locations, check if the register was saved. If so, get it from the stack.
7098 // Note: There is a chance that the register was saved but not overwritten, so we could
7099 // save one load. However, since this is just an intrinsic slow path we prefer this
7100 // simple and more robust approach rather that trying to determine if that's the case.
7101 SlowPathCode* slow_path = GetCurrentSlowPath();
7102 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7103 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7104 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7105 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7106 return temp;
7107 }
7108 return location.AsRegister<Register>();
7109}
7110
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007111void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007112 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00007113 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00007114 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7115 // LR = code address from literal pool with link-time patch.
7116 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00007117 break;
7118 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7119 // LR = invoke->GetDirectCodePtr();
7120 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00007121 break;
7122 default:
7123 break;
7124 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007125
Vladimir Marko58155012015-08-19 12:49:41 +00007126 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7127 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007128 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7129 uint32_t offset =
7130 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007131 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007132 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007133 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007134 }
Vladimir Marko58155012015-08-19 12:49:41 +00007135 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007136 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007137 break;
7138 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7139 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7140 break;
7141 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
7142 __ LoadLiteral(temp.AsRegister<Register>(),
7143 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
7144 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007145 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7146 HArmDexCacheArraysBase* base =
7147 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7148 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7149 temp.AsRegister<Register>());
7150 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7151 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7152 break;
7153 }
Vladimir Marko58155012015-08-19 12:49:41 +00007154 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007155 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007156 Register method_reg;
7157 Register reg = temp.AsRegister<Register>();
7158 if (current_method.IsRegister()) {
7159 method_reg = current_method.AsRegister<Register>();
7160 } else {
7161 DCHECK(invoke->GetLocations()->Intrinsified());
7162 DCHECK(!current_method.IsValid());
7163 method_reg = reg;
7164 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7165 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007166 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7167 __ LoadFromOffset(kLoadWord,
7168 reg,
7169 method_reg,
7170 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007171 // temp = temp[index_in_cache];
7172 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7173 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007174 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7175 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007176 }
Vladimir Marko58155012015-08-19 12:49:41 +00007177 }
7178
7179 switch (invoke->GetCodePtrLocation()) {
7180 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7181 __ bl(GetFrameEntryLabel());
7182 break;
7183 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007184 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7185 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007186 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01007187 // Arbitrarily branch to the BL itself, override at link time.
7188 __ bl(&relative_call_patches_.back().label);
7189 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007190 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7191 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7192 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00007193 // LR()
7194 __ blx(LR);
7195 break;
7196 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7197 // LR = callee_method->entry_point_from_quick_compiled_code_
7198 __ LoadFromOffset(
7199 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007200 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007201 // LR()
7202 __ blx(LR);
7203 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007204 }
7205
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007206 DCHECK(!IsLeafMethod());
7207}
7208
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007209void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7210 Register temp = temp_location.AsRegister<Register>();
7211 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7212 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007213
7214 // Use the calling convention instead of the location of the receiver, as
7215 // intrinsics may have put the receiver in a different register. In the intrinsics
7216 // slow path, the arguments have been moved to the right place, so here we are
7217 // guaranteed that the receiver is the first register of the calling convention.
7218 InvokeDexCallingConvention calling_convention;
7219 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007220 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007221 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007222 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007223 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007224 // Instead of simply (possibly) unpoisoning `temp` here, we should
7225 // emit a read barrier for the previous class reference load.
7226 // However this is not required in practice, as this is an
7227 // intermediate/temporary reference and because the current
7228 // concurrent copying collector keeps the from-space memory
7229 // intact/accessible until the end of the marking phase (the
7230 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007231 __ MaybeUnpoisonHeapReference(temp);
7232 // temp = temp->GetMethodAt(method_offset);
7233 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007234 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007235 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7236 // LR = temp->GetEntryPoint();
7237 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7238 // LR();
7239 __ blx(LR);
7240}
7241
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007242CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7243 const DexFile& dex_file, uint32_t string_index) {
7244 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7245}
7246
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007247CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
7248 const DexFile& dex_file, uint32_t type_index) {
7249 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7250}
7251
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007252CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7253 const DexFile& dex_file, uint32_t element_offset) {
7254 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7255}
7256
7257CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7258 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7259 patches->emplace_back(dex_file, offset_or_index);
7260 return &patches->back();
7261}
7262
7263Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
7264 uint32_t string_index) {
7265 return boot_image_string_patches_.GetOrCreate(
7266 StringReference(&dex_file, string_index),
7267 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7268}
7269
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007270Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
7271 uint32_t type_index) {
7272 return boot_image_type_patches_.GetOrCreate(
7273 TypeReference(&dex_file, type_index),
7274 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7275}
7276
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007277Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7278 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7279 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7280 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7281}
7282
7283Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7284 return DeduplicateUint32Literal(address, &uint32_literals_);
7285}
7286
Vladimir Markoaad75c62016-10-03 08:46:48 +00007287template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7288inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7289 const ArenaDeque<PcRelativePatchInfo>& infos,
7290 ArenaVector<LinkerPatch>* linker_patches) {
7291 for (const PcRelativePatchInfo& info : infos) {
7292 const DexFile& dex_file = info.target_dex_file;
7293 size_t offset_or_index = info.offset_or_index;
7294 DCHECK(info.add_pc_label.IsBound());
7295 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7296 // Add MOVW patch.
7297 DCHECK(info.movw_label.IsBound());
7298 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7299 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7300 // Add MOVT patch.
7301 DCHECK(info.movt_label.IsBound());
7302 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7303 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7304 }
7305}
7306
Vladimir Marko58155012015-08-19 12:49:41 +00007307void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7308 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007309 size_t size =
7310 method_patches_.size() +
7311 call_patches_.size() +
7312 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007313 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007314 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007315 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007316 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007317 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007318 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007319 linker_patches->reserve(size);
7320 for (const auto& entry : method_patches_) {
7321 const MethodReference& target_method = entry.first;
7322 Literal* literal = entry.second;
7323 DCHECK(literal->GetLabel()->IsBound());
7324 uint32_t literal_offset = literal->GetLabel()->Position();
7325 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7326 target_method.dex_file,
7327 target_method.dex_method_index));
7328 }
7329 for (const auto& entry : call_patches_) {
7330 const MethodReference& target_method = entry.first;
7331 Literal* literal = entry.second;
7332 DCHECK(literal->GetLabel()->IsBound());
7333 uint32_t literal_offset = literal->GetLabel()->Position();
7334 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7335 target_method.dex_file,
7336 target_method.dex_method_index));
7337 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007338 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007339 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007340 linker_patches->push_back(
7341 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007342 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007343 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7344 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007345 for (const auto& entry : boot_image_string_patches_) {
7346 const StringReference& target_string = entry.first;
7347 Literal* literal = entry.second;
7348 DCHECK(literal->GetLabel()->IsBound());
7349 uint32_t literal_offset = literal->GetLabel()->Position();
7350 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7351 target_string.dex_file,
7352 target_string.string_index));
7353 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007354 if (!GetCompilerOptions().IsBootImage()) {
7355 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7356 linker_patches);
7357 } else {
7358 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7359 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007360 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007361 for (const auto& entry : boot_image_type_patches_) {
7362 const TypeReference& target_type = entry.first;
7363 Literal* literal = entry.second;
7364 DCHECK(literal->GetLabel()->IsBound());
7365 uint32_t literal_offset = literal->GetLabel()->Position();
7366 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7367 target_type.dex_file,
7368 target_type.type_index));
7369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007370 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7371 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007372 for (const auto& entry : boot_image_address_patches_) {
7373 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7374 Literal* literal = entry.second;
7375 DCHECK(literal->GetLabel()->IsBound());
7376 uint32_t literal_offset = literal->GetLabel()->Position();
7377 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7378 }
7379}
7380
7381Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7382 return map->GetOrCreate(
7383 value,
7384 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007385}
7386
7387Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7388 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007389 return map->GetOrCreate(
7390 target_method,
7391 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007392}
7393
7394Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7395 return DeduplicateMethodLiteral(target_method, &method_patches_);
7396}
7397
7398Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7399 return DeduplicateMethodLiteral(target_method, &call_patches_);
7400}
7401
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007402void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7403 LocationSummary* locations =
7404 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7405 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7406 Location::RequiresRegister());
7407 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7408 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7410}
7411
7412void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7413 LocationSummary* locations = instr->GetLocations();
7414 Register res = locations->Out().AsRegister<Register>();
7415 Register accumulator =
7416 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7417 Register mul_left =
7418 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7419 Register mul_right =
7420 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7421
7422 if (instr->GetOpKind() == HInstruction::kAdd) {
7423 __ mla(res, mul_left, mul_right, accumulator);
7424 } else {
7425 __ mls(res, mul_left, mul_right, accumulator);
7426 }
7427}
7428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007429void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007430 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007431 LOG(FATAL) << "Unreachable";
7432}
7433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007434void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007435 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007436 LOG(FATAL) << "Unreachable";
7437}
7438
Mark Mendellfe57faa2015-09-18 09:26:15 -04007439// Simple implementation of packed switch - generate cascaded compare/jumps.
7440void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7441 LocationSummary* locations =
7442 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7443 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007444 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007445 codegen_->GetAssembler()->IsThumb()) {
7446 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7447 if (switch_instr->GetStartValue() != 0) {
7448 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7449 }
7450 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007451}
7452
7453void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7454 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007455 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007456 LocationSummary* locations = switch_instr->GetLocations();
7457 Register value_reg = locations->InAt(0).AsRegister<Register>();
7458 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7459
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007460 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007461 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007462 Register temp_reg = IP;
7463 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7464 // the immediate, because IP is used as the destination register. For the other
7465 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7466 // and they can be encoded in the instruction without making use of IP register.
7467 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7468
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007469 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007470 // Jump to successors[0] if value == lower_bound.
7471 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7472 int32_t last_index = 0;
7473 for (; num_entries - last_index > 2; last_index += 2) {
7474 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7475 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7476 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7477 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7478 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7479 }
7480 if (num_entries - last_index == 2) {
7481 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007482 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007483 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007484 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007485
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007486 // And the default for any other value.
7487 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7488 __ b(codegen_->GetLabelOf(default_block));
7489 }
7490 } else {
7491 // Create a table lookup.
7492 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7493
7494 // Materialize a pointer to the switch table
7495 std::vector<Label*> labels(num_entries);
7496 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7497 for (uint32_t i = 0; i < num_entries; i++) {
7498 labels[i] = codegen_->GetLabelOf(successors[i]);
7499 }
7500 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7501
7502 // Remove the bias.
7503 Register key_reg;
7504 if (lower_bound != 0) {
7505 key_reg = locations->GetTemp(1).AsRegister<Register>();
7506 __ AddConstant(key_reg, value_reg, -lower_bound);
7507 } else {
7508 key_reg = value_reg;
7509 }
7510
7511 // Check whether the value is in the table, jump to default block if not.
7512 __ CmpConstant(key_reg, num_entries - 1);
7513 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7514
7515 // Load the displacement from the table.
7516 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7517
7518 // Dispatch is a direct add to the PC (for Thumb2).
7519 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007520 }
7521}
7522
Vladimir Markob4536b72015-11-24 13:45:23 +00007523void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7525 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007526}
7527
7528void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7529 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007530 CodeGeneratorARM::PcRelativePatchInfo* labels =
7531 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007532 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007533 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007534 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007535 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007536 __ BindTrackedLabel(&labels->add_pc_label);
7537 __ add(base_reg, base_reg, ShifterOperand(PC));
7538}
7539
Andreas Gampe85b62f22015-09-09 13:15:38 -07007540void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7541 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007542 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007543 return;
7544 }
7545
7546 DCHECK_NE(type, Primitive::kPrimVoid);
7547
7548 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7549 if (return_loc.Equals(trg)) {
7550 return;
7551 }
7552
7553 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7554 // with the last branch.
7555 if (type == Primitive::kPrimLong) {
7556 HParallelMove parallel_move(GetGraph()->GetArena());
7557 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7558 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7559 GetMoveResolver()->EmitNativeCode(&parallel_move);
7560 } else if (type == Primitive::kPrimDouble) {
7561 HParallelMove parallel_move(GetGraph()->GetArena());
7562 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7563 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7564 GetMoveResolver()->EmitNativeCode(&parallel_move);
7565 } else {
7566 // Let the parallel move resolver take care of all of this.
7567 HParallelMove parallel_move(GetGraph()->GetArena());
7568 parallel_move.AddMove(return_loc, trg, type, nullptr);
7569 GetMoveResolver()->EmitNativeCode(&parallel_move);
7570 }
7571}
7572
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007573void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7574 LocationSummary* locations =
7575 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7576 locations->SetInAt(0, Location::RequiresRegister());
7577 locations->SetOut(Location::RequiresRegister());
7578}
7579
7580void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7581 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007582 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007583 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007584 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007585 __ LoadFromOffset(kLoadWord,
7586 locations->Out().AsRegister<Register>(),
7587 locations->InAt(0).AsRegister<Register>(),
7588 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007589 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007590 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007591 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007592 __ LoadFromOffset(kLoadWord,
7593 locations->Out().AsRegister<Register>(),
7594 locations->InAt(0).AsRegister<Register>(),
7595 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7596 __ LoadFromOffset(kLoadWord,
7597 locations->Out().AsRegister<Register>(),
7598 locations->Out().AsRegister<Register>(),
7599 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007600 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007601}
7602
Roland Levillain4d027112015-07-01 15:41:14 +01007603#undef __
7604#undef QUICK_ENTRY_POINT
7605
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007606} // namespace arm
7607} // namespace art