blob: 541a1c5b8f6265477c7f316de14ead0fc08b0e0c [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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
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();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800433 const uint32_t string_index = load->GetStringIndex().index_;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100434 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();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000492 DCHECK(instruction_->IsCheckCast()
493 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000494
495 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
496 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000497
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000498 if (!is_fatal_) {
499 SaveLiveRegisters(codegen, locations);
500 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000501
502 // We're moving two locations to locations that could overlap, so we need a parallel
503 // move resolver.
504 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800505 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800506 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
507 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800508 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800509 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
510 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000511 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100512 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100513 instruction_,
514 instruction_->GetDexPc(),
515 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800516 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000517 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
518 } else {
519 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800520 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
521 instruction_,
522 instruction_->GetDexPc(),
523 this);
524 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000527 if (!is_fatal_) {
528 RestoreLiveRegisters(codegen, locations);
529 __ b(GetExitLabel());
530 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000531 }
532
Alexandre Rames9931f312015-06-19 14:47:01 +0100533 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 bool IsFatal() const OVERRIDE { return is_fatal_; }
536
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000537 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000538 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539
540 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
541};
542
Artem Serovf4d6aee2016-07-11 10:41:45 +0100543class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 public:
Aart Bik42249c32016-01-07 15:33:50 -0800545 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100546 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700547
548 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800549 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700550 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100551 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000552 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700553 }
554
Alexandre Rames9931f312015-06-19 14:47:01 +0100555 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
556
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700557 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
559};
560
Artem Serovf4d6aee2016-07-11 10:41:45 +0100561class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100562 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100563 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564
565 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
566 LocationSummary* locations = instruction_->GetLocations();
567 __ Bind(GetEntryLabel());
568 SaveLiveRegisters(codegen, locations);
569
570 InvokeRuntimeCallingConvention calling_convention;
571 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
572 parallel_move.AddMove(
573 locations->InAt(0),
574 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
575 Primitive::kPrimNot,
576 nullptr);
577 parallel_move.AddMove(
578 locations->InAt(1),
579 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
580 Primitive::kPrimInt,
581 nullptr);
582 parallel_move.AddMove(
583 locations->InAt(2),
584 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
585 Primitive::kPrimNot,
586 nullptr);
587 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
588
589 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100590 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000591 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100592 RestoreLiveRegisters(codegen, locations);
593 __ b(GetExitLabel());
594 }
595
596 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
597
598 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100599 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
600};
601
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100602// Slow path marking an object reference `ref` during a read
603// barrier. The field `obj.field` in the object `obj` holding this
604// reference does not get updated by this slow path after marking (see
605// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
606//
607// This means that after the execution of this slow path, `ref` will
608// always be up-to-date, but `obj.field` may not; i.e., after the
609// flip, `ref` will be a to-space reference, but `obj.field` will
610// probably still be a from-space reference (unless it gets updated by
611// another thread, or if another thread installed another object
612// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100613class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000614 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800615 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
616 Location ref,
617 Location entrypoint = Location::NoLocation())
618 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
622 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
623
624 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
625 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100626 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000627 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100628 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000629 DCHECK(instruction_->IsInstanceFieldGet() ||
630 instruction_->IsStaticFieldGet() ||
631 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100632 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000633 instruction_->IsLoadClass() ||
634 instruction_->IsLoadString() ||
635 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100636 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100637 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
638 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000639 << "Unexpected instruction in read barrier marking slow path: "
640 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000641 // The read barrier instrumentation of object ArrayGet
642 // instructions does not support the HIntermediateAddress
643 // instruction.
644 DCHECK(!(instruction_->IsArrayGet() &&
645 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000646
647 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000651 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 DCHECK_NE(ref_reg, SP);
653 DCHECK_NE(ref_reg, LR);
654 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100655 // IP is used internally by the ReadBarrierMarkRegX entry point
656 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 DCHECK_NE(ref_reg, IP);
658 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100659 // "Compact" slow path, saving two moves.
660 //
661 // Instead of using the standard runtime calling convention (input
662 // and output in R0):
663 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100664 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100665 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100667 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100668 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100669 // of a dedicated entrypoint:
670 //
671 // rX <- ReadBarrierMarkRegX(rX)
672 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800673 if (entrypoint_.IsValid()) {
674 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
675 __ blx(entrypoint_.AsRegister<Register>());
676 } else {
677 int32_t entry_point_offset =
678 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
679 // This runtime call does not require a stack map.
680 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
681 }
Roland Levillainc9285912015-12-18 10:38:42 +0000682 __ b(GetExitLabel());
683 }
684
685 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100686 // The location (register) of the marked object reference.
687 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000688
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800689 // The location of the entrypoint if already loaded.
690 const Location entrypoint_;
691
Roland Levillainc9285912015-12-18 10:38:42 +0000692 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
693};
694
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100695// Slow path marking an object reference `ref` during a read barrier,
696// and if needed, atomically updating the field `obj.field` in the
697// object `obj` holding this reference after marking (contrary to
698// ReadBarrierMarkSlowPathARM above, which never tries to update
699// `obj.field`).
700//
701// This means that after the execution of this slow path, both `ref`
702// and `obj.field` will be up-to-date; i.e., after the flip, both will
703// hold the same to-space reference (unless another thread installed
704// another object reference (different from `ref`) in `obj.field`).
705class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
706 public:
707 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
708 Location ref,
709 Register obj,
710 Location field_offset,
711 Register temp1,
712 Register temp2)
713 : SlowPathCodeARM(instruction),
714 ref_(ref),
715 obj_(obj),
716 field_offset_(field_offset),
717 temp1_(temp1),
718 temp2_(temp2) {
719 DCHECK(kEmitCompilerReadBarrier);
720 }
721
722 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
723
724 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
725 LocationSummary* locations = instruction_->GetLocations();
726 Register ref_reg = ref_.AsRegister<Register>();
727 DCHECK(locations->CanCall());
728 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
729 // This slow path is only used by the UnsafeCASObject intrinsic.
730 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
731 << "Unexpected instruction in read barrier marking and field updating slow path: "
732 << instruction_->DebugName();
733 DCHECK(instruction_->GetLocations()->Intrinsified());
734 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
735 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
736
737 __ Bind(GetEntryLabel());
738
739 // Save the old reference.
740 // Note that we cannot use IP to save the old reference, as IP is
741 // used internally by the ReadBarrierMarkRegX entry point, and we
742 // need the old reference after the call to that entry point.
743 DCHECK_NE(temp1_, IP);
744 __ Mov(temp1_, ref_reg);
745
746 // No need to save live registers; it's taken care of by the
747 // entrypoint. Also, there is no need to update the stack mask,
748 // as this runtime call will not trigger a garbage collection.
749 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
750 DCHECK_NE(ref_reg, SP);
751 DCHECK_NE(ref_reg, LR);
752 DCHECK_NE(ref_reg, PC);
753 // IP is used internally by the ReadBarrierMarkRegX entry point
754 // as a temporary, it cannot be the entry point's input/output.
755 DCHECK_NE(ref_reg, IP);
756 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
757 // "Compact" slow path, saving two moves.
758 //
759 // Instead of using the standard runtime calling convention (input
760 // and output in R0):
761 //
762 // R0 <- ref
763 // R0 <- ReadBarrierMark(R0)
764 // ref <- R0
765 //
766 // we just use rX (the register containing `ref`) as input and output
767 // of a dedicated entrypoint:
768 //
769 // rX <- ReadBarrierMarkRegX(rX)
770 //
771 int32_t entry_point_offset =
772 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
773 // This runtime call does not require a stack map.
774 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
775
776 // If the new reference is different from the old reference,
777 // update the field in the holder (`*(obj_ + field_offset_)`).
778 //
779 // Note that this field could also hold a different object, if
780 // another thread had concurrently changed it. In that case, the
781 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
782 // (CAS) operation below would abort the CAS, leaving the field
783 // as-is.
784 Label done;
785 __ cmp(temp1_, ShifterOperand(ref_reg));
786 __ b(&done, EQ);
787
788 // Update the the holder's field atomically. This may fail if
789 // mutator updates before us, but it's OK. This is achieved
790 // using a strong compare-and-set (CAS) operation with relaxed
791 // memory synchronization ordering, where the expected value is
792 // the old reference and the desired value is the new reference.
793
794 // Convenience aliases.
795 Register base = obj_;
796 // The UnsafeCASObject intrinsic uses a register pair as field
797 // offset ("long offset"), of which only the low part contains
798 // data.
799 Register offset = field_offset_.AsRegisterPairLow<Register>();
800 Register expected = temp1_;
801 Register value = ref_reg;
802 Register tmp_ptr = IP; // Pointer to actual memory.
803 Register tmp = temp2_; // Value in memory.
804
805 __ add(tmp_ptr, base, ShifterOperand(offset));
806
807 if (kPoisonHeapReferences) {
808 __ PoisonHeapReference(expected);
809 if (value == expected) {
810 // Do not poison `value`, as it is the same register as
811 // `expected`, which has just been poisoned.
812 } else {
813 __ PoisonHeapReference(value);
814 }
815 }
816
817 // do {
818 // tmp = [r_ptr] - expected;
819 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
820
Roland Levillain24a4d112016-10-26 13:10:46 +0100821 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100822 __ Bind(&loop_head);
823
824 __ ldrex(tmp, tmp_ptr);
825
826 __ subs(tmp, tmp, ShifterOperand(expected));
827
Roland Levillain24a4d112016-10-26 13:10:46 +0100828 __ it(NE);
829 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100830
Roland Levillain24a4d112016-10-26 13:10:46 +0100831 __ b(&exit_loop, NE);
832
833 __ strex(tmp, value, tmp_ptr);
834 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100835 __ b(&loop_head, EQ);
836
Roland Levillain24a4d112016-10-26 13:10:46 +0100837 __ Bind(&exit_loop);
838
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100839 if (kPoisonHeapReferences) {
840 __ UnpoisonHeapReference(expected);
841 if (value == expected) {
842 // Do not unpoison `value`, as it is the same register as
843 // `expected`, which has just been unpoisoned.
844 } else {
845 __ UnpoisonHeapReference(value);
846 }
847 }
848
849 __ Bind(&done);
850 __ b(GetExitLabel());
851 }
852
853 private:
854 // The location (register) of the marked object reference.
855 const Location ref_;
856 // The register containing the object holding the marked object reference field.
857 const Register obj_;
858 // The location of the offset of the marked reference field within `obj_`.
859 Location field_offset_;
860
861 const Register temp1_;
862 const Register temp2_;
863
864 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
865};
866
Roland Levillain3b359c72015-11-17 19:35:12 +0000867// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100868class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000869 public:
870 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
871 Location out,
872 Location ref,
873 Location obj,
874 uint32_t offset,
875 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100876 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000877 out_(out),
878 ref_(ref),
879 obj_(obj),
880 offset_(offset),
881 index_(index) {
882 DCHECK(kEmitCompilerReadBarrier);
883 // If `obj` is equal to `out` or `ref`, it means the initial object
884 // has been overwritten by (or after) the heap object reference load
885 // to be instrumented, e.g.:
886 //
887 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000888 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000889 //
890 // In that case, we have lost the information about the original
891 // object, and the emitted read barrier cannot work properly.
892 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
893 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
894 }
895
896 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
897 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
898 LocationSummary* locations = instruction_->GetLocations();
899 Register reg_out = out_.AsRegister<Register>();
900 DCHECK(locations->CanCall());
901 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100902 DCHECK(instruction_->IsInstanceFieldGet() ||
903 instruction_->IsStaticFieldGet() ||
904 instruction_->IsArrayGet() ||
905 instruction_->IsInstanceOf() ||
906 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100907 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000908 << "Unexpected instruction in read barrier for heap reference slow path: "
909 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000910 // The read barrier instrumentation of object ArrayGet
911 // instructions does not support the HIntermediateAddress
912 // instruction.
913 DCHECK(!(instruction_->IsArrayGet() &&
914 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000915
916 __ Bind(GetEntryLabel());
917 SaveLiveRegisters(codegen, locations);
918
919 // We may have to change the index's value, but as `index_` is a
920 // constant member (like other "inputs" of this slow path),
921 // introduce a copy of it, `index`.
922 Location index = index_;
923 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100924 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000925 if (instruction_->IsArrayGet()) {
926 // Compute the actual memory offset and store it in `index`.
927 Register index_reg = index_.AsRegister<Register>();
928 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
929 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
930 // We are about to change the value of `index_reg` (see the
931 // calls to art::arm::Thumb2Assembler::Lsl and
932 // art::arm::Thumb2Assembler::AddConstant below), but it has
933 // not been saved by the previous call to
934 // art::SlowPathCode::SaveLiveRegisters, as it is a
935 // callee-save register --
936 // art::SlowPathCode::SaveLiveRegisters does not consider
937 // callee-save registers, as it has been designed with the
938 // assumption that callee-save registers are supposed to be
939 // handled by the called function. So, as a callee-save
940 // register, `index_reg` _would_ eventually be saved onto
941 // the stack, but it would be too late: we would have
942 // changed its value earlier. Therefore, we manually save
943 // it here into another freely available register,
944 // `free_reg`, chosen of course among the caller-save
945 // registers (as a callee-save `free_reg` register would
946 // exhibit the same problem).
947 //
948 // Note we could have requested a temporary register from
949 // the register allocator instead; but we prefer not to, as
950 // this is a slow path, and we know we can find a
951 // caller-save register that is available.
952 Register free_reg = FindAvailableCallerSaveRegister(codegen);
953 __ Mov(free_reg, index_reg);
954 index_reg = free_reg;
955 index = Location::RegisterLocation(index_reg);
956 } else {
957 // The initial register stored in `index_` has already been
958 // saved in the call to art::SlowPathCode::SaveLiveRegisters
959 // (as it is not a callee-save register), so we can freely
960 // use it.
961 }
962 // Shifting the index value contained in `index_reg` by the scale
963 // factor (2) cannot overflow in practice, as the runtime is
964 // unable to allocate object arrays with a size larger than
965 // 2^26 - 1 (that is, 2^28 - 4 bytes).
966 __ Lsl(index_reg, index_reg, TIMES_4);
967 static_assert(
968 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
969 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
970 __ AddConstant(index_reg, index_reg, offset_);
971 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100972 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
973 // intrinsics, `index_` is not shifted by a scale factor of 2
974 // (as in the case of ArrayGet), as it is actually an offset
975 // to an object field within an object.
976 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000977 DCHECK(instruction_->GetLocations()->Intrinsified());
978 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
979 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
980 << instruction_->AsInvoke()->GetIntrinsic();
981 DCHECK_EQ(offset_, 0U);
982 DCHECK(index_.IsRegisterPair());
983 // UnsafeGet's offset location is a register pair, the low
984 // part contains the correct offset.
985 index = index_.ToLow();
986 }
987 }
988
989 // We're moving two or three locations to locations that could
990 // overlap, so we need a parallel move resolver.
991 InvokeRuntimeCallingConvention calling_convention;
992 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
993 parallel_move.AddMove(ref_,
994 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
995 Primitive::kPrimNot,
996 nullptr);
997 parallel_move.AddMove(obj_,
998 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
999 Primitive::kPrimNot,
1000 nullptr);
1001 if (index.IsValid()) {
1002 parallel_move.AddMove(index,
1003 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1004 Primitive::kPrimInt,
1005 nullptr);
1006 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1007 } else {
1008 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1009 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1010 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001011 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001012 CheckEntrypointTypes<
1013 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1014 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1015
1016 RestoreLiveRegisters(codegen, locations);
1017 __ b(GetExitLabel());
1018 }
1019
1020 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1021
1022 private:
1023 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1024 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1025 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1026 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1027 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1028 return static_cast<Register>(i);
1029 }
1030 }
1031 // We shall never fail to find a free caller-save register, as
1032 // there are more than two core caller-save registers on ARM
1033 // (meaning it is possible to find one which is different from
1034 // `ref` and `obj`).
1035 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1036 LOG(FATAL) << "Could not find a free caller-save register";
1037 UNREACHABLE();
1038 }
1039
Roland Levillain3b359c72015-11-17 19:35:12 +00001040 const Location out_;
1041 const Location ref_;
1042 const Location obj_;
1043 const uint32_t offset_;
1044 // An additional location containing an index to an array.
1045 // Only used for HArrayGet and the UnsafeGetObject &
1046 // UnsafeGetObjectVolatile intrinsics.
1047 const Location index_;
1048
1049 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1050};
1051
1052// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001053class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001054 public:
1055 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001056 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001057 DCHECK(kEmitCompilerReadBarrier);
1058 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001059
1060 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1061 LocationSummary* locations = instruction_->GetLocations();
1062 Register reg_out = out_.AsRegister<Register>();
1063 DCHECK(locations->CanCall());
1064 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001065 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1066 << "Unexpected instruction in read barrier for GC root slow path: "
1067 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001068
1069 __ Bind(GetEntryLabel());
1070 SaveLiveRegisters(codegen, locations);
1071
1072 InvokeRuntimeCallingConvention calling_convention;
1073 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1074 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001075 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001076 instruction_,
1077 instruction_->GetDexPc(),
1078 this);
1079 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1080 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1081
1082 RestoreLiveRegisters(codegen, locations);
1083 __ b(GetExitLabel());
1084 }
1085
1086 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1087
1088 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001089 const Location out_;
1090 const Location root_;
1091
1092 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1093};
1094
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001095#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001096// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1097#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001098
Aart Bike9f37602015-10-09 11:15:55 -07001099inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001100 switch (cond) {
1101 case kCondEQ: return EQ;
1102 case kCondNE: return NE;
1103 case kCondLT: return LT;
1104 case kCondLE: return LE;
1105 case kCondGT: return GT;
1106 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001107 case kCondB: return LO;
1108 case kCondBE: return LS;
1109 case kCondA: return HI;
1110 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001111 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001112 LOG(FATAL) << "Unreachable";
1113 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001114}
1115
Aart Bike9f37602015-10-09 11:15:55 -07001116// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001117inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001118 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001119 case kCondEQ: return EQ;
1120 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001121 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001122 case kCondLT: return LO;
1123 case kCondLE: return LS;
1124 case kCondGT: return HI;
1125 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001126 // Unsigned remain unchanged.
1127 case kCondB: return LO;
1128 case kCondBE: return LS;
1129 case kCondA: return HI;
1130 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001131 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001132 LOG(FATAL) << "Unreachable";
1133 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001134}
1135
Vladimir Markod6e069b2016-01-18 11:11:01 +00001136inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1137 // The ARM condition codes can express all the necessary branches, see the
1138 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1139 // There is no dex instruction or HIR that would need the missing conditions
1140 // "equal or unordered" or "not equal".
1141 switch (cond) {
1142 case kCondEQ: return EQ;
1143 case kCondNE: return NE /* unordered */;
1144 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1145 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1146 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1147 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1148 default:
1149 LOG(FATAL) << "UNREACHABLE";
1150 UNREACHABLE();
1151 }
1152}
1153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001155 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001156}
1157
1158void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001159 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001160}
1161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001162size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1163 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1164 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001165}
1166
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001167size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1168 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1169 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001170}
1171
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001172size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1173 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1174 return kArmWordSize;
1175}
1176
1177size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1178 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1179 return kArmWordSize;
1180}
1181
Calin Juravle34166012014-12-19 17:22:29 +00001182CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001183 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001184 const CompilerOptions& compiler_options,
1185 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001186 : CodeGenerator(graph,
1187 kNumberOfCoreRegisters,
1188 kNumberOfSRegisters,
1189 kNumberOfRegisterPairs,
1190 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1191 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001192 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1193 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001194 compiler_options,
1195 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001196 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001197 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001198 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001199 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001200 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001201 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001202 uint32_literals_(std::less<uint32_t>(),
1203 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001204 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1205 boot_image_string_patches_(StringReferenceValueComparator(),
1206 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1207 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001208 boot_image_type_patches_(TypeReferenceValueComparator(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1210 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001211 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001212 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1213 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001214 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1215 jit_class_patches_(TypeReferenceValueComparator(),
1216 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001217 // Always save the LR register to mimic Quick.
1218 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001219}
1220
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001221void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1222 // Ensure that we fix up branches and literal loads and emit the literal pool.
1223 __ FinalizeCode();
1224
1225 // Adjust native pc offsets in stack maps.
1226 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1227 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1228 uint32_t new_position = __ GetAdjustedPosition(old_position);
1229 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1230 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001231 // Adjust pc offsets for the disassembly information.
1232 if (disasm_info_ != nullptr) {
1233 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1234 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1235 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1236 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1237 it.second.start = __ GetAdjustedPosition(it.second.start);
1238 it.second.end = __ GetAdjustedPosition(it.second.end);
1239 }
1240 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1241 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1242 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1243 }
1244 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001245
1246 CodeGenerator::Finalize(allocator);
1247}
1248
David Brazdil58282f42016-01-14 12:45:10 +00001249void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001250 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001251 blocked_core_registers_[SP] = true;
1252 blocked_core_registers_[LR] = true;
1253 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001254
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001255 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001256 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001257
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001258 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001259 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001260
David Brazdil58282f42016-01-14 12:45:10 +00001261 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001262 // Stubs do not save callee-save floating point registers. If the graph
1263 // is debuggable, we need to deal with these registers differently. For
1264 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001265 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1266 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1267 }
1268 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001269}
1270
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001271InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001272 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001273 assembler_(codegen->GetAssembler()),
1274 codegen_(codegen) {}
1275
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001276void CodeGeneratorARM::ComputeSpillMask() {
1277 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1278 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001279 // There is no easy instruction to restore just the PC on thumb2. We spill and
1280 // restore another arbitrary register.
1281 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001282 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1283 // We use vpush and vpop for saving and restoring floating point registers, which take
1284 // a SRegister and the number of registers to save/restore after that SRegister. We
1285 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1286 // but in the range.
1287 if (fpu_spill_mask_ != 0) {
1288 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1289 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1290 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1291 fpu_spill_mask_ |= (1 << i);
1292 }
1293 }
1294}
1295
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001296static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001297 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001298}
1299
1300static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001301 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001302}
1303
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001304void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001305 bool skip_overflow_check =
1306 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001307 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001308 __ Bind(&frame_entry_label_);
1309
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001310 if (HasEmptyFrame()) {
1311 return;
1312 }
1313
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001314 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001315 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1316 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1317 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001318 }
1319
Andreas Gampe501fd632015-09-10 16:11:06 -07001320 __ PushList(core_spill_mask_);
1321 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1322 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001323 if (fpu_spill_mask_ != 0) {
1324 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1325 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001326 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001327 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001328 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001329
1330 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1331 // Initialize should_deoptimize flag to 0.
1332 __ mov(IP, ShifterOperand(0));
1333 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1334 }
1335
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001336 int adjust = GetFrameSize() - FrameEntrySpillSize();
1337 __ AddConstant(SP, -adjust);
1338 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001339
1340 // Save the current method if we need it. Note that we do not
1341 // do this in HCurrentMethod, as the instruction might have been removed
1342 // in the SSA graph.
1343 if (RequiresCurrentMethod()) {
1344 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1345 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001346}
1347
1348void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001349 if (HasEmptyFrame()) {
1350 __ bx(LR);
1351 return;
1352 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001353 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001354 int adjust = GetFrameSize() - FrameEntrySpillSize();
1355 __ AddConstant(SP, adjust);
1356 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001357 if (fpu_spill_mask_ != 0) {
1358 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1359 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001360 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001361 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001362 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001363 // Pop LR into PC to return.
1364 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1365 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1366 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001367 __ cfi().RestoreState();
1368 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369}
1370
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001371void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001372 Label* label = GetLabelOf(block);
1373 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001374}
1375
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001376Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001377 switch (type) {
1378 case Primitive::kPrimBoolean:
1379 case Primitive::kPrimByte:
1380 case Primitive::kPrimChar:
1381 case Primitive::kPrimShort:
1382 case Primitive::kPrimInt:
1383 case Primitive::kPrimNot: {
1384 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001385 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001386 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001388 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001389 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001390 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001391 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001392
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001393 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001394 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001395 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001396 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001397 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001398 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001399 if (calling_convention.GetRegisterAt(index) == R1) {
1400 // Skip R1, and use R2_R3 instead.
1401 gp_index_++;
1402 index++;
1403 }
1404 }
1405 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1406 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001407 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001408
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001409 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001410 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001411 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001412 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1413 }
1414 }
1415
1416 case Primitive::kPrimFloat: {
1417 uint32_t stack_index = stack_index_++;
1418 if (float_index_ % 2 == 0) {
1419 float_index_ = std::max(double_index_, float_index_);
1420 }
1421 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1422 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1423 } else {
1424 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1425 }
1426 }
1427
1428 case Primitive::kPrimDouble: {
1429 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1430 uint32_t stack_index = stack_index_;
1431 stack_index_ += 2;
1432 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1433 uint32_t index = double_index_;
1434 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001435 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001436 calling_convention.GetFpuRegisterAt(index),
1437 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001438 DCHECK(ExpectedPairLayout(result));
1439 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001440 } else {
1441 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001442 }
1443 }
1444
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001445 case Primitive::kPrimVoid:
1446 LOG(FATAL) << "Unexpected parameter type " << type;
1447 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001448 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001449 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001450}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001451
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001452Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001453 switch (type) {
1454 case Primitive::kPrimBoolean:
1455 case Primitive::kPrimByte:
1456 case Primitive::kPrimChar:
1457 case Primitive::kPrimShort:
1458 case Primitive::kPrimInt:
1459 case Primitive::kPrimNot: {
1460 return Location::RegisterLocation(R0);
1461 }
1462
1463 case Primitive::kPrimFloat: {
1464 return Location::FpuRegisterLocation(S0);
1465 }
1466
1467 case Primitive::kPrimLong: {
1468 return Location::RegisterPairLocation(R0, R1);
1469 }
1470
1471 case Primitive::kPrimDouble: {
1472 return Location::FpuRegisterPairLocation(S0, S1);
1473 }
1474
1475 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001476 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001477 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001478
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001479 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001480}
1481
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001482Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1483 return Location::RegisterLocation(kMethodRegisterArgument);
1484}
1485
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001486void CodeGeneratorARM::Move32(Location destination, Location source) {
1487 if (source.Equals(destination)) {
1488 return;
1489 }
1490 if (destination.IsRegister()) {
1491 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001496 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001498 } else if (destination.IsFpuRegister()) {
1499 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001500 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001501 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001504 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001505 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001507 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001509 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001510 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001511 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001512 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001513 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001514 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1515 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001516 }
1517 }
1518}
1519
1520void CodeGeneratorARM::Move64(Location destination, Location source) {
1521 if (source.Equals(destination)) {
1522 return;
1523 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001524 if (destination.IsRegisterPair()) {
1525 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001526 EmitParallelMoves(
1527 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1528 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001529 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001530 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001531 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1532 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001533 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001534 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001535 } else if (source.IsFpuRegisterPair()) {
1536 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1537 destination.AsRegisterPairHigh<Register>(),
1538 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 } else {
1540 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001541 DCHECK(ExpectedPairLayout(destination));
1542 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1543 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001545 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001546 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001547 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1548 SP,
1549 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001550 } else if (source.IsRegisterPair()) {
1551 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1552 source.AsRegisterPairLow<Register>(),
1553 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001554 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001555 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001556 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 } else {
1558 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001559 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001560 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001561 if (source.AsRegisterPairLow<Register>() == R1) {
1562 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001563 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1564 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001566 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567 SP, destination.GetStackIndex());
1568 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001569 } else if (source.IsFpuRegisterPair()) {
1570 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1571 SP,
1572 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 } else {
1574 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001575 EmitParallelMoves(
1576 Location::StackSlot(source.GetStackIndex()),
1577 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001578 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001579 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001580 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1581 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 }
1583 }
1584}
1585
Calin Juravle175dc732015-08-25 15:42:32 +01001586void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1587 DCHECK(location.IsRegister());
1588 __ LoadImmediate(location.AsRegister<Register>(), value);
1589}
1590
Calin Juravlee460d1d2015-09-29 04:52:17 +01001591void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001592 HParallelMove move(GetGraph()->GetArena());
1593 move.AddMove(src, dst, dst_type, nullptr);
1594 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001595}
1596
1597void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1598 if (location.IsRegister()) {
1599 locations->AddTemp(location);
1600 } else if (location.IsRegisterPair()) {
1601 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1602 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1603 } else {
1604 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1605 }
1606}
1607
Calin Juravle175dc732015-08-25 15:42:32 +01001608void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1609 HInstruction* instruction,
1610 uint32_t dex_pc,
1611 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001612 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001613 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001614 if (EntrypointRequiresStackMap(entrypoint)) {
1615 RecordPcInfo(instruction, dex_pc, slow_path);
1616 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001617}
1618
Roland Levillaindec8f632016-07-22 17:10:06 +01001619void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1620 HInstruction* instruction,
1621 SlowPathCode* slow_path) {
1622 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001623 GenerateInvokeRuntime(entry_point_offset);
1624}
1625
1626void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001627 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1628 __ blx(LR);
1629}
1630
David Brazdilfc6a86a2015-06-26 10:33:45 +00001631void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001632 DCHECK(!successor->IsExitBlock());
1633
1634 HBasicBlock* block = got->GetBlock();
1635 HInstruction* previous = got->GetPrevious();
1636
1637 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001638 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001639 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1640 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1641 return;
1642 }
1643
1644 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1645 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1646 }
1647 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001648 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001649 }
1650}
1651
David Brazdilfc6a86a2015-06-26 10:33:45 +00001652void LocationsBuilderARM::VisitGoto(HGoto* got) {
1653 got->SetLocations(nullptr);
1654}
1655
1656void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1657 HandleGoto(got, got->GetSuccessor());
1658}
1659
1660void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1661 try_boundary->SetLocations(nullptr);
1662}
1663
1664void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1665 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1666 if (!successor->IsExitBlock()) {
1667 HandleGoto(try_boundary, successor);
1668 }
1669}
1670
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001671void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001672 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001673}
1674
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001675void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001676}
1677
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001678void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1679 Primitive::Type type = instruction->InputAt(0)->GetType();
1680 Location lhs_loc = instruction->GetLocations()->InAt(0);
1681 Location rhs_loc = instruction->GetLocations()->InAt(1);
1682 if (rhs_loc.IsConstant()) {
1683 // 0.0 is the only immediate that can be encoded directly in
1684 // a VCMP instruction.
1685 //
1686 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1687 // specify that in a floating-point comparison, positive zero
1688 // and negative zero are considered equal, so we can use the
1689 // literal 0.0 for both cases here.
1690 //
1691 // Note however that some methods (Float.equal, Float.compare,
1692 // Float.compareTo, Double.equal, Double.compare,
1693 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1694 // StrictMath.min) consider 0.0 to be (strictly) greater than
1695 // -0.0. So if we ever translate calls to these methods into a
1696 // HCompare instruction, we must handle the -0.0 case with
1697 // care here.
1698 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1699 if (type == Primitive::kPrimFloat) {
1700 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1701 } else {
1702 DCHECK_EQ(type, Primitive::kPrimDouble);
1703 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1704 }
1705 } else {
1706 if (type == Primitive::kPrimFloat) {
1707 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1708 } else {
1709 DCHECK_EQ(type, Primitive::kPrimDouble);
1710 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1711 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1712 }
1713 }
1714}
1715
Roland Levillain4fa13f62015-07-06 18:11:54 +01001716void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1717 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001718 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001719 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001720 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721}
1722
1723void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1724 Label* true_label,
1725 Label* false_label) {
1726 LocationSummary* locations = cond->GetLocations();
1727 Location left = locations->InAt(0);
1728 Location right = locations->InAt(1);
1729 IfCondition if_cond = cond->GetCondition();
1730
1731 Register left_high = left.AsRegisterPairHigh<Register>();
1732 Register left_low = left.AsRegisterPairLow<Register>();
1733 IfCondition true_high_cond = if_cond;
1734 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001735 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001736
1737 // Set the conditions for the test, remembering that == needs to be
1738 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001739 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001740 switch (if_cond) {
1741 case kCondEQ:
1742 case kCondNE:
1743 // Nothing to do.
1744 break;
1745 case kCondLT:
1746 false_high_cond = kCondGT;
1747 break;
1748 case kCondLE:
1749 true_high_cond = kCondLT;
1750 break;
1751 case kCondGT:
1752 false_high_cond = kCondLT;
1753 break;
1754 case kCondGE:
1755 true_high_cond = kCondGT;
1756 break;
Aart Bike9f37602015-10-09 11:15:55 -07001757 case kCondB:
1758 false_high_cond = kCondA;
1759 break;
1760 case kCondBE:
1761 true_high_cond = kCondB;
1762 break;
1763 case kCondA:
1764 false_high_cond = kCondB;
1765 break;
1766 case kCondAE:
1767 true_high_cond = kCondA;
1768 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001769 }
1770 if (right.IsConstant()) {
1771 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1772 int32_t val_low = Low32Bits(value);
1773 int32_t val_high = High32Bits(value);
1774
Vladimir Markoac6ac102015-12-17 12:14:00 +00001775 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001776 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001777 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001779 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001780 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001781 __ b(true_label, ARMCondition(true_high_cond));
1782 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001783 }
1784 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001785 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001786 } else {
1787 Register right_high = right.AsRegisterPairHigh<Register>();
1788 Register right_low = right.AsRegisterPairLow<Register>();
1789
1790 __ cmp(left_high, ShifterOperand(right_high));
1791 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001792 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001793 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001794 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001795 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001796 __ b(true_label, ARMCondition(true_high_cond));
1797 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001798 }
1799 // Must be equal high, so compare the lows.
1800 __ cmp(left_low, ShifterOperand(right_low));
1801 }
1802 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001803 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001804 __ b(true_label, final_condition);
1805}
1806
David Brazdil0debae72015-11-12 18:37:00 +00001807void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1808 Label* true_target_in,
1809 Label* false_target_in) {
1810 // Generated branching requires both targets to be explicit. If either of the
1811 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1812 Label fallthrough_target;
1813 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1814 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1815
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 Primitive::Type type = condition->InputAt(0)->GetType();
1817 switch (type) {
1818 case Primitive::kPrimLong:
1819 GenerateLongComparesAndJumps(condition, true_target, false_target);
1820 break;
1821 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001822 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001823 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001824 GenerateFPJumps(condition, true_target, false_target);
1825 break;
1826 default:
1827 LOG(FATAL) << "Unexpected compare type " << type;
1828 }
1829
David Brazdil0debae72015-11-12 18:37:00 +00001830 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001831 __ b(false_target);
1832 }
David Brazdil0debae72015-11-12 18:37:00 +00001833
1834 if (fallthrough_target.IsLinked()) {
1835 __ Bind(&fallthrough_target);
1836 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001837}
1838
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001839void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001840 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001841 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001842 Label* false_target) {
1843 HInstruction* cond = instruction->InputAt(condition_input_index);
1844
1845 if (true_target == nullptr && false_target == nullptr) {
1846 // Nothing to do. The code always falls through.
1847 return;
1848 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001849 // Constant condition, statically compared against "true" (integer value 1).
1850 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001851 if (true_target != nullptr) {
1852 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001854 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001855 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001856 if (false_target != nullptr) {
1857 __ b(false_target);
1858 }
1859 }
1860 return;
1861 }
1862
1863 // The following code generates these patterns:
1864 // (1) true_target == nullptr && false_target != nullptr
1865 // - opposite condition true => branch to false_target
1866 // (2) true_target != nullptr && false_target == nullptr
1867 // - condition true => branch to true_target
1868 // (3) true_target != nullptr && false_target != nullptr
1869 // - condition true => branch to true_target
1870 // - branch to false_target
1871 if (IsBooleanValueOrMaterializedCondition(cond)) {
1872 // Condition has been materialized, compare the output to 0.
1873 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1874 DCHECK(cond_val.IsRegister());
1875 if (true_target == nullptr) {
1876 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1877 } else {
1878 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001879 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001880 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001881 // Condition has not been materialized. Use its inputs as the comparison and
1882 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001883 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001884
1885 // If this is a long or FP comparison that has been folded into
1886 // the HCondition, generate the comparison directly.
1887 Primitive::Type type = condition->InputAt(0)->GetType();
1888 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1889 GenerateCompareTestAndBranch(condition, true_target, false_target);
1890 return;
1891 }
1892
1893 LocationSummary* locations = cond->GetLocations();
1894 DCHECK(locations->InAt(0).IsRegister());
1895 Register left = locations->InAt(0).AsRegister<Register>();
1896 Location right = locations->InAt(1);
1897 if (right.IsRegister()) {
1898 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001899 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001900 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001901 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001902 }
1903 if (true_target == nullptr) {
1904 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1905 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001906 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001907 }
Dave Allison20dfc792014-06-16 20:44:29 -07001908 }
David Brazdil0debae72015-11-12 18:37:00 +00001909
1910 // If neither branch falls through (case 3), the conditional branch to `true_target`
1911 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1912 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001913 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914 }
1915}
1916
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001917void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1919 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001920 locations->SetInAt(0, Location::RequiresRegister());
1921 }
1922}
1923
1924void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001925 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1926 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1927 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1928 nullptr : codegen_->GetLabelOf(true_successor);
1929 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1930 nullptr : codegen_->GetLabelOf(false_successor);
1931 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001932}
1933
1934void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1935 LocationSummary* locations = new (GetGraph()->GetArena())
1936 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001937 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001938 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001939 locations->SetInAt(0, Location::RequiresRegister());
1940 }
1941}
1942
1943void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001944 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001945 GenerateTestAndBranch(deoptimize,
1946 /* condition_input_index */ 0,
1947 slow_path->GetEntryLabel(),
1948 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001949}
Dave Allison20dfc792014-06-16 20:44:29 -07001950
Mingyao Yang063fc772016-08-02 11:02:54 -07001951void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1952 LocationSummary* locations = new (GetGraph()->GetArena())
1953 LocationSummary(flag, LocationSummary::kNoCall);
1954 locations->SetOut(Location::RequiresRegister());
1955}
1956
1957void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1958 __ LoadFromOffset(kLoadWord,
1959 flag->GetLocations()->Out().AsRegister<Register>(),
1960 SP,
1961 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
1962}
1963
David Brazdil74eb1b22015-12-14 11:44:01 +00001964void LocationsBuilderARM::VisitSelect(HSelect* select) {
1965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1966 if (Primitive::IsFloatingPointType(select->GetType())) {
1967 locations->SetInAt(0, Location::RequiresFpuRegister());
1968 locations->SetInAt(1, Location::RequiresFpuRegister());
1969 } else {
1970 locations->SetInAt(0, Location::RequiresRegister());
1971 locations->SetInAt(1, Location::RequiresRegister());
1972 }
1973 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1974 locations->SetInAt(2, Location::RequiresRegister());
1975 }
1976 locations->SetOut(Location::SameAsFirstInput());
1977}
1978
1979void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1980 LocationSummary* locations = select->GetLocations();
1981 Label false_target;
1982 GenerateTestAndBranch(select,
1983 /* condition_input_index */ 2,
1984 /* true_target */ nullptr,
1985 &false_target);
1986 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1987 __ Bind(&false_target);
1988}
1989
David Srbecky0cf44932015-12-09 14:09:59 +00001990void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1991 new (GetGraph()->GetArena()) LocationSummary(info);
1992}
1993
David Srbeckyd28f4a02016-03-14 17:14:24 +00001994void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1995 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001996}
1997
1998void CodeGeneratorARM::GenerateNop() {
1999 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002000}
2001
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002003 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002004 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002005 // Handle the long/FP comparisons made in instruction simplification.
2006 switch (cond->InputAt(0)->GetType()) {
2007 case Primitive::kPrimLong:
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002010 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002011 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2012 }
2013 break;
2014
2015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble:
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002018 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002019 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2021 }
2022 break;
2023
2024 default:
2025 locations->SetInAt(0, Location::RequiresRegister());
2026 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002027 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2029 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002030 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002031}
2032
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002033void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002034 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002035 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002036 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002037
2038 LocationSummary* locations = cond->GetLocations();
2039 Location left = locations->InAt(0);
2040 Location right = locations->InAt(1);
2041 Register out = locations->Out().AsRegister<Register>();
2042 Label true_label, false_label;
2043
2044 switch (cond->InputAt(0)->GetType()) {
2045 default: {
2046 // Integer case.
2047 if (right.IsRegister()) {
2048 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2049 } else {
2050 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002051 __ CmpConstant(left.AsRegister<Register>(),
2052 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002053 }
Aart Bike9f37602015-10-09 11:15:55 -07002054 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002055 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002056 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002057 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002058 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002059 return;
2060 }
2061 case Primitive::kPrimLong:
2062 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2063 break;
2064 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002065 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002066 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002067 GenerateFPJumps(cond, &true_label, &false_label);
2068 break;
2069 }
2070
2071 // Convert the jumps into the result.
2072 Label done_label;
2073
2074 // False case: result = 0.
2075 __ Bind(&false_label);
2076 __ LoadImmediate(out, 0);
2077 __ b(&done_label);
2078
2079 // True case: result = 1.
2080 __ Bind(&true_label);
2081 __ LoadImmediate(out, 1);
2082 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002083}
2084
2085void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002086 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002087}
2088
2089void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002090 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002091}
2092
2093void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002094 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002095}
2096
2097void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002098 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002099}
2100
2101void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002103}
2104
2105void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002107}
2108
2109void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002110 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002111}
2112
2113void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002114 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002115}
2116
2117void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002118 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002119}
2120
2121void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002122 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002123}
2124
2125void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002126 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002127}
2128
2129void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002130 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131}
2132
Aart Bike9f37602015-10-09 11:15:55 -07002133void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002134 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002135}
2136
2137void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002138 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002139}
2140
2141void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002142 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002143}
2144
2145void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002147}
2148
2149void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002150 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002151}
2152
2153void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002154 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002155}
2156
2157void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002158 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002159}
2160
2161void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002162 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002163}
2164
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002165void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002166 LocationSummary* locations =
2167 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002168 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169}
2170
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002171void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002172 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002173}
2174
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002175void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2178 locations->SetOut(Location::ConstantLocation(constant));
2179}
2180
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002181void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002182 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002183}
2184
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002185void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002186 LocationSummary* locations =
2187 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002188 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002189}
2190
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002191void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002192 // Will be generated at use site.
2193}
2194
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002195void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2198 locations->SetOut(Location::ConstantLocation(constant));
2199}
2200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002201void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002202 // Will be generated at use site.
2203}
2204
2205void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2206 LocationSummary* locations =
2207 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2208 locations->SetOut(Location::ConstantLocation(constant));
2209}
2210
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002211void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002212 // Will be generated at use site.
2213}
2214
Calin Juravle27df7582015-04-17 19:12:31 +01002215void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2216 memory_barrier->SetLocations(nullptr);
2217}
2218
2219void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002220 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002221}
2222
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002223void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002224 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225}
2226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002227void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002228 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002229}
2230
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002231void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002232 LocationSummary* locations =
2233 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002234 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002235}
2236
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002237void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002238 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002239}
2240
Calin Juravle175dc732015-08-25 15:42:32 +01002241void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2242 // The trampoline uses the same calling convention as dex calling conventions,
2243 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2244 // the method_idx.
2245 HandleInvoke(invoke);
2246}
2247
2248void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2249 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2250}
2251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002252void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002253 // Explicit clinit checks triggered by static invokes must have been pruned by
2254 // art::PrepareForRegisterAllocation.
2255 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002256
Vladimir Marko68c981f2016-08-26 13:13:33 +01002257 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002258 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002259 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2260 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2261 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002262 return;
2263 }
2264
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002265 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002266
2267 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2268 if (invoke->HasPcRelativeDexCache()) {
2269 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2270 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002271}
2272
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002273static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2274 if (invoke->GetLocations()->Intrinsified()) {
2275 IntrinsicCodeGeneratorARM intrinsic(codegen);
2276 intrinsic.Dispatch(invoke);
2277 return true;
2278 }
2279 return false;
2280}
2281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002282void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002283 // Explicit clinit checks triggered by static invokes must have been pruned by
2284 // art::PrepareForRegisterAllocation.
2285 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002286
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002287 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2288 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002289 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002290
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002291 LocationSummary* locations = invoke->GetLocations();
2292 codegen_->GenerateStaticOrDirectCall(
2293 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002294 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002295}
2296
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002297void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002298 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002299 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002300}
2301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002302void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002303 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002304 if (intrinsic.TryDispatch(invoke)) {
2305 return;
2306 }
2307
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002308 HandleInvoke(invoke);
2309}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002310
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002311void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002312 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2313 return;
2314 }
2315
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002316 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002317 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002318 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002319}
2320
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002321void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2322 HandleInvoke(invoke);
2323 // Add the hidden argument.
2324 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2325}
2326
2327void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2328 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002329 LocationSummary* locations = invoke->GetLocations();
2330 Register temp = locations->GetTemp(0).AsRegister<Register>();
2331 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002332 Location receiver = locations->InAt(0);
2333 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2334
Roland Levillain3b359c72015-11-17 19:35:12 +00002335 // Set the hidden argument. This is safe to do this here, as R12
2336 // won't be modified thereafter, before the `blx` (call) instruction.
2337 DCHECK_EQ(R12, hidden_reg);
2338 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002339
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002340 if (receiver.IsStackSlot()) {
2341 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002342 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002343 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2344 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002345 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002347 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002348 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002349 // Instead of simply (possibly) unpoisoning `temp` here, we should
2350 // emit a read barrier for the previous class reference load.
2351 // However this is not required in practice, as this is an
2352 // intermediate/temporary reference and because the current
2353 // concurrent copying collector keeps the from-space memory
2354 // intact/accessible until the end of the marking phase (the
2355 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002356 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002357 __ LoadFromOffset(kLoadWord, temp, temp,
2358 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2359 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002360 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002361 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002362 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002363 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002364 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002365 // LR = temp->GetEntryPoint();
2366 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2367 // LR();
2368 __ blx(LR);
2369 DCHECK(!codegen_->IsLeafMethod());
2370 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2371}
2372
Roland Levillain88cb1752014-10-20 16:36:47 +01002373void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2374 LocationSummary* locations =
2375 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2376 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002377 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002378 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2380 break;
2381 }
2382 case Primitive::kPrimLong: {
2383 locations->SetInAt(0, Location::RequiresRegister());
2384 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002385 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002386 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002387
Roland Levillain88cb1752014-10-20 16:36:47 +01002388 case Primitive::kPrimFloat:
2389 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002390 locations->SetInAt(0, Location::RequiresFpuRegister());
2391 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002392 break;
2393
2394 default:
2395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2396 }
2397}
2398
2399void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2400 LocationSummary* locations = neg->GetLocations();
2401 Location out = locations->Out();
2402 Location in = locations->InAt(0);
2403 switch (neg->GetResultType()) {
2404 case Primitive::kPrimInt:
2405 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002407 break;
2408
2409 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002410 DCHECK(in.IsRegisterPair());
2411 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2412 __ rsbs(out.AsRegisterPairLow<Register>(),
2413 in.AsRegisterPairLow<Register>(),
2414 ShifterOperand(0));
2415 // We cannot emit an RSC (Reverse Subtract with Carry)
2416 // instruction here, as it does not exist in the Thumb-2
2417 // instruction set. We use the following approach
2418 // using SBC and SUB instead.
2419 //
2420 // out.hi = -C
2421 __ sbc(out.AsRegisterPairHigh<Register>(),
2422 out.AsRegisterPairHigh<Register>(),
2423 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2424 // out.hi = out.hi - in.hi
2425 __ sub(out.AsRegisterPairHigh<Register>(),
2426 out.AsRegisterPairHigh<Register>(),
2427 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2428 break;
2429
Roland Levillain88cb1752014-10-20 16:36:47 +01002430 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002431 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002432 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002433 break;
2434
Roland Levillain88cb1752014-10-20 16:36:47 +01002435 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002436 DCHECK(in.IsFpuRegisterPair());
2437 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2438 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002439 break;
2440
2441 default:
2442 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2443 }
2444}
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002447 Primitive::Type result_type = conversion->GetResultType();
2448 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002449 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002450
Roland Levillain5b3ee562015-04-14 16:02:41 +01002451 // The float-to-long, double-to-long and long-to-float type conversions
2452 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002453 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002454 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2455 && result_type == Primitive::kPrimLong)
2456 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002457 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002458 : LocationSummary::kNoCall;
2459 LocationSummary* locations =
2460 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2461
David Brazdilb2bd1c52015-03-25 11:17:37 +00002462 // The Java language does not allow treating boolean as an integral type but
2463 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002464
Roland Levillaindff1f282014-11-05 14:15:05 +00002465 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002466 case Primitive::kPrimByte:
2467 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002468 case Primitive::kPrimLong:
2469 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002472 case Primitive::kPrimShort:
2473 case Primitive::kPrimInt:
2474 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002475 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002476 locations->SetInAt(0, Location::RequiresRegister());
2477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2478 break;
2479
2480 default:
2481 LOG(FATAL) << "Unexpected type conversion from " << input_type
2482 << " to " << result_type;
2483 }
2484 break;
2485
Roland Levillain01a8d712014-11-14 16:27:39 +00002486 case Primitive::kPrimShort:
2487 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002488 case Primitive::kPrimLong:
2489 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimInt:
2494 case Primitive::kPrimChar:
2495 // Processing a Dex `int-to-short' instruction.
2496 locations->SetInAt(0, Location::RequiresRegister());
2497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillain946e1432014-11-11 17:35:19 +00002506 case Primitive::kPrimInt:
2507 switch (input_type) {
2508 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002509 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002510 locations->SetInAt(0, Location::Any());
2511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2512 break;
2513
2514 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002515 // Processing a Dex `float-to-int' instruction.
2516 locations->SetInAt(0, Location::RequiresFpuRegister());
2517 locations->SetOut(Location::RequiresRegister());
2518 locations->AddTemp(Location::RequiresFpuRegister());
2519 break;
2520
Roland Levillain946e1432014-11-11 17:35:19 +00002521 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002522 // Processing a Dex `double-to-int' instruction.
2523 locations->SetInAt(0, Location::RequiresFpuRegister());
2524 locations->SetOut(Location::RequiresRegister());
2525 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 case Primitive::kPrimLong:
2535 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002536 case Primitive::kPrimBoolean:
2537 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimByte:
2539 case Primitive::kPrimShort:
2540 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002541 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002542 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 locations->SetInAt(0, Location::RequiresRegister());
2544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2545 break;
2546
Roland Levillain624279f2014-12-04 11:54:28 +00002547 case Primitive::kPrimFloat: {
2548 // Processing a Dex `float-to-long' instruction.
2549 InvokeRuntimeCallingConvention calling_convention;
2550 locations->SetInAt(0, Location::FpuRegisterLocation(
2551 calling_convention.GetFpuRegisterAt(0)));
2552 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2553 break;
2554 }
2555
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002556 case Primitive::kPrimDouble: {
2557 // Processing a Dex `double-to-long' instruction.
2558 InvokeRuntimeCallingConvention calling_convention;
2559 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2560 calling_convention.GetFpuRegisterAt(0),
2561 calling_convention.GetFpuRegisterAt(1)));
2562 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002564 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002565
2566 default:
2567 LOG(FATAL) << "Unexpected type conversion from " << input_type
2568 << " to " << result_type;
2569 }
2570 break;
2571
Roland Levillain981e4542014-11-14 11:47:14 +00002572 case Primitive::kPrimChar:
2573 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002574 case Primitive::kPrimLong:
2575 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002578 case Primitive::kPrimByte:
2579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002581 // Processing a Dex `int-to-char' instruction.
2582 locations->SetInAt(0, Location::RequiresRegister());
2583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected type conversion from " << input_type
2588 << " to " << result_type;
2589 }
2590 break;
2591
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002593 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002594 case Primitive::kPrimBoolean:
2595 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002596 case Primitive::kPrimByte:
2597 case Primitive::kPrimShort:
2598 case Primitive::kPrimInt:
2599 case Primitive::kPrimChar:
2600 // Processing a Dex `int-to-float' instruction.
2601 locations->SetInAt(0, Location::RequiresRegister());
2602 locations->SetOut(Location::RequiresFpuRegister());
2603 break;
2604
Roland Levillain5b3ee562015-04-14 16:02:41 +01002605 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002606 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002607 InvokeRuntimeCallingConvention calling_convention;
2608 locations->SetInAt(0, Location::RegisterPairLocation(
2609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2610 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002611 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002612 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002613
Roland Levillaincff13742014-11-17 14:32:17 +00002614 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002615 // Processing a Dex `double-to-float' instruction.
2616 locations->SetInAt(0, Location::RequiresFpuRegister());
2617 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 };
2624 break;
2625
Roland Levillaindff1f282014-11-05 14:15:05 +00002626 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002628 case Primitive::kPrimBoolean:
2629 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002630 case Primitive::kPrimByte:
2631 case Primitive::kPrimShort:
2632 case Primitive::kPrimInt:
2633 case Primitive::kPrimChar:
2634 // Processing a Dex `int-to-double' instruction.
2635 locations->SetInAt(0, Location::RequiresRegister());
2636 locations->SetOut(Location::RequiresFpuRegister());
2637 break;
2638
2639 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002640 // Processing a Dex `long-to-double' instruction.
2641 locations->SetInAt(0, Location::RequiresRegister());
2642 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002643 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002644 locations->AddTemp(Location::RequiresFpuRegister());
2645 break;
2646
Roland Levillaincff13742014-11-17 14:32:17 +00002647 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002648 // Processing a Dex `float-to-double' instruction.
2649 locations->SetInAt(0, Location::RequiresFpuRegister());
2650 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663}
2664
2665void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2666 LocationSummary* locations = conversion->GetLocations();
2667 Location out = locations->Out();
2668 Location in = locations->InAt(0);
2669 Primitive::Type result_type = conversion->GetResultType();
2670 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002671 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002672 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002673 case Primitive::kPrimByte:
2674 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002675 case Primitive::kPrimLong:
2676 // Type conversion from long to byte is a result of code transformations.
2677 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2678 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002681 case Primitive::kPrimShort:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002684 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002686 break;
2687
2688 default:
2689 LOG(FATAL) << "Unexpected type conversion from " << input_type
2690 << " to " << result_type;
2691 }
2692 break;
2693
Roland Levillain01a8d712014-11-14 16:27:39 +00002694 case Primitive::kPrimShort:
2695 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002696 case Primitive::kPrimLong:
2697 // Type conversion from long to short is a result of code transformations.
2698 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2699 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002700 case Primitive::kPrimBoolean:
2701 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002702 case Primitive::kPrimByte:
2703 case Primitive::kPrimInt:
2704 case Primitive::kPrimChar:
2705 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002706 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002707 break;
2708
2709 default:
2710 LOG(FATAL) << "Unexpected type conversion from " << input_type
2711 << " to " << result_type;
2712 }
2713 break;
2714
Roland Levillain946e1432014-11-11 17:35:19 +00002715 case Primitive::kPrimInt:
2716 switch (input_type) {
2717 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002718 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002719 DCHECK(out.IsRegister());
2720 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002722 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002724 } else {
2725 DCHECK(in.IsConstant());
2726 DCHECK(in.GetConstant()->IsLongConstant());
2727 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002729 }
2730 break;
2731
Roland Levillain3f8f9362014-12-02 17:45:01 +00002732 case Primitive::kPrimFloat: {
2733 // Processing a Dex `float-to-int' instruction.
2734 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002735 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002736 __ vmovrs(out.AsRegister<Register>(), temp);
2737 break;
2738 }
2739
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002740 case Primitive::kPrimDouble: {
2741 // Processing a Dex `double-to-int' instruction.
2742 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002743 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002745 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746 }
Roland Levillain946e1432014-11-11 17:35:19 +00002747
2748 default:
2749 LOG(FATAL) << "Unexpected type conversion from " << input_type
2750 << " to " << result_type;
2751 }
2752 break;
2753
Roland Levillaindff1f282014-11-05 14:15:05 +00002754 case Primitive::kPrimLong:
2755 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002756 case Primitive::kPrimBoolean:
2757 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002758 case Primitive::kPrimByte:
2759 case Primitive::kPrimShort:
2760 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002761 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002762 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002763 DCHECK(out.IsRegisterPair());
2764 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002766 // Sign extension.
2767 __ Asr(out.AsRegisterPairHigh<Register>(),
2768 out.AsRegisterPairLow<Register>(),
2769 31);
2770 break;
2771
2772 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002773 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002774 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002775 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002776 break;
2777
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002779 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002780 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002781 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 break;
2783
2784 default:
2785 LOG(FATAL) << "Unexpected type conversion from " << input_type
2786 << " to " << result_type;
2787 }
2788 break;
2789
Roland Levillain981e4542014-11-14 11:47:14 +00002790 case Primitive::kPrimChar:
2791 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002792 case Primitive::kPrimLong:
2793 // Type conversion from long to char is a result of code transformations.
2794 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2795 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002796 case Primitive::kPrimBoolean:
2797 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002798 case Primitive::kPrimByte:
2799 case Primitive::kPrimShort:
2800 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002801 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002803 break;
2804
2805 default:
2806 LOG(FATAL) << "Unexpected type conversion from " << input_type
2807 << " to " << result_type;
2808 }
2809 break;
2810
Roland Levillaindff1f282014-11-05 14:15:05 +00002811 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002812 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002813 case Primitive::kPrimBoolean:
2814 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002815 case Primitive::kPrimByte:
2816 case Primitive::kPrimShort:
2817 case Primitive::kPrimInt:
2818 case Primitive::kPrimChar: {
2819 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2821 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002822 break;
2823 }
2824
Roland Levillain5b3ee562015-04-14 16:02:41 +01002825 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002826 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002827 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002828 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002829 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002830
Roland Levillaincff13742014-11-17 14:32:17 +00002831 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002832 // Processing a Dex `double-to-float' instruction.
2833 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2834 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002835 break;
2836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 };
2841 break;
2842
Roland Levillaindff1f282014-11-05 14:15:05 +00002843 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002844 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002845 case Primitive::kPrimBoolean:
2846 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002847 case Primitive::kPrimByte:
2848 case Primitive::kPrimShort:
2849 case Primitive::kPrimInt:
2850 case Primitive::kPrimChar: {
2851 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002853 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2854 out.AsFpuRegisterPairLow<SRegister>());
2855 break;
2856 }
2857
Roland Levillain647b9ed2014-11-27 12:06:00 +00002858 case Primitive::kPrimLong: {
2859 // Processing a Dex `long-to-double' instruction.
2860 Register low = in.AsRegisterPairLow<Register>();
2861 Register high = in.AsRegisterPairHigh<Register>();
2862 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2863 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002864 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002865 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002866 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2867 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002868
Roland Levillain682393c2015-04-14 15:57:52 +01002869 // temp_d = int-to-double(high)
2870 __ vmovsr(temp_s, high);
2871 __ vcvtdi(temp_d, temp_s);
2872 // constant_d = k2Pow32EncodingForDouble
2873 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2874 // out_d = unsigned-to-double(low)
2875 __ vmovsr(out_s, low);
2876 __ vcvtdu(out_d, out_s);
2877 // out_d += temp_d * constant_d
2878 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002879 break;
2880 }
2881
Roland Levillaincff13742014-11-17 14:32:17 +00002882 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002883 // Processing a Dex `float-to-double' instruction.
2884 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2885 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002886 break;
2887
2888 default:
2889 LOG(FATAL) << "Unexpected type conversion from " << input_type
2890 << " to " << result_type;
2891 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002892 break;
2893
2894 default:
2895 LOG(FATAL) << "Unexpected type conversion from " << input_type
2896 << " to " << result_type;
2897 }
2898}
2899
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002900void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002901 LocationSummary* locations =
2902 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002903 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002904 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002905 locations->SetInAt(0, Location::RequiresRegister());
2906 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2908 break;
2909 }
2910
2911 case Primitive::kPrimLong: {
2912 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002913 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002915 break;
2916 }
2917
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002918 case Primitive::kPrimFloat:
2919 case Primitive::kPrimDouble: {
2920 locations->SetInAt(0, Location::RequiresFpuRegister());
2921 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002922 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002923 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002927 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002929}
2930
2931void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2932 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002933 Location out = locations->Out();
2934 Location first = locations->InAt(0);
2935 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936 switch (add->GetResultType()) {
2937 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002938 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002939 __ add(out.AsRegister<Register>(),
2940 first.AsRegister<Register>(),
2941 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 __ AddConstant(out.AsRegister<Register>(),
2944 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002945 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002946 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002947 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002948
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002949 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002950 if (second.IsConstant()) {
2951 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2952 GenerateAddLongConst(out, first, value);
2953 } else {
2954 DCHECK(second.IsRegisterPair());
2955 __ adds(out.AsRegisterPairLow<Register>(),
2956 first.AsRegisterPairLow<Register>(),
2957 ShifterOperand(second.AsRegisterPairLow<Register>()));
2958 __ adc(out.AsRegisterPairHigh<Register>(),
2959 first.AsRegisterPairHigh<Register>(),
2960 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2961 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002962 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002963 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002966 __ vadds(out.AsFpuRegister<SRegister>(),
2967 first.AsFpuRegister<SRegister>(),
2968 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 break;
2970
2971 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002972 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2973 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2974 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002975 break;
2976
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002977 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979 }
2980}
2981
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002982void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002985 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002986 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002987 locations->SetInAt(0, Location::RequiresRegister());
2988 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2990 break;
2991 }
2992
2993 case Primitive::kPrimLong: {
2994 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002995 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002997 break;
2998 }
Calin Juravle11351682014-10-23 15:38:15 +01002999 case Primitive::kPrimFloat:
3000 case Primitive::kPrimDouble: {
3001 locations->SetInAt(0, Location::RequiresFpuRegister());
3002 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003003 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003004 break;
Calin Juravle11351682014-10-23 15:38:15 +01003005 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006 default:
Calin Juravle11351682014-10-23 15:38:15 +01003007 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003008 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003009}
3010
3011void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3012 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003013 Location out = locations->Out();
3014 Location first = locations->InAt(0);
3015 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003016 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003017 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003018 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003019 __ sub(out.AsRegister<Register>(),
3020 first.AsRegister<Register>(),
3021 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003022 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ AddConstant(out.AsRegister<Register>(),
3024 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003025 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003026 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003027 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029
Calin Juravle11351682014-10-23 15:38:15 +01003030 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003031 if (second.IsConstant()) {
3032 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3033 GenerateAddLongConst(out, first, -value);
3034 } else {
3035 DCHECK(second.IsRegisterPair());
3036 __ subs(out.AsRegisterPairLow<Register>(),
3037 first.AsRegisterPairLow<Register>(),
3038 ShifterOperand(second.AsRegisterPairLow<Register>()));
3039 __ sbc(out.AsRegisterPairHigh<Register>(),
3040 first.AsRegisterPairHigh<Register>(),
3041 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043 break;
Calin Juravle11351682014-10-23 15:38:15 +01003044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003045
Calin Juravle11351682014-10-23 15:38:15 +01003046 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003047 __ vsubs(out.AsFpuRegister<SRegister>(),
3048 first.AsFpuRegister<SRegister>(),
3049 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 break;
Calin Juravle11351682014-10-23 15:38:15 +01003051 }
3052
3053 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003054 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3055 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3056 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003057 break;
3058 }
3059
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003060
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003061 default:
Calin Juravle11351682014-10-23 15:38:15 +01003062 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003063 }
3064}
3065
Calin Juravle34bacdf2014-10-07 20:23:36 +01003066void LocationsBuilderARM::VisitMul(HMul* mul) {
3067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3069 switch (mul->GetResultType()) {
3070 case Primitive::kPrimInt:
3071 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003072 locations->SetInAt(0, Location::RequiresRegister());
3073 locations->SetInAt(1, Location::RequiresRegister());
3074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075 break;
3076 }
3077
Calin Juravleb5bfa962014-10-21 18:02:24 +01003078 case Primitive::kPrimFloat:
3079 case Primitive::kPrimDouble: {
3080 locations->SetInAt(0, Location::RequiresFpuRegister());
3081 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003083 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003084 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003085
3086 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003087 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088 }
3089}
3090
3091void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3092 LocationSummary* locations = mul->GetLocations();
3093 Location out = locations->Out();
3094 Location first = locations->InAt(0);
3095 Location second = locations->InAt(1);
3096 switch (mul->GetResultType()) {
3097 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003098 __ mul(out.AsRegister<Register>(),
3099 first.AsRegister<Register>(),
3100 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003101 break;
3102 }
3103 case Primitive::kPrimLong: {
3104 Register out_hi = out.AsRegisterPairHigh<Register>();
3105 Register out_lo = out.AsRegisterPairLow<Register>();
3106 Register in1_hi = first.AsRegisterPairHigh<Register>();
3107 Register in1_lo = first.AsRegisterPairLow<Register>();
3108 Register in2_hi = second.AsRegisterPairHigh<Register>();
3109 Register in2_lo = second.AsRegisterPairLow<Register>();
3110
3111 // Extra checks to protect caused by the existence of R1_R2.
3112 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3113 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3114 DCHECK_NE(out_hi, in1_lo);
3115 DCHECK_NE(out_hi, in2_lo);
3116
3117 // input: in1 - 64 bits, in2 - 64 bits
3118 // output: out
3119 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3120 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3121 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3122
3123 // IP <- in1.lo * in2.hi
3124 __ mul(IP, in1_lo, in2_hi);
3125 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3126 __ mla(out_hi, in1_hi, in2_lo, IP);
3127 // out.lo <- (in1.lo * in2.lo)[31:0];
3128 __ umull(out_lo, IP, in1_lo, in2_lo);
3129 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3130 __ add(out_hi, out_hi, ShifterOperand(IP));
3131 break;
3132 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133
3134 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003135 __ vmuls(out.AsFpuRegister<SRegister>(),
3136 first.AsFpuRegister<SRegister>(),
3137 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003139 }
3140
3141 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003142 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3143 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3144 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003145 break;
3146 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003147
3148 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150 }
3151}
3152
Zheng Xuc6667102015-05-15 16:08:45 +08003153void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3154 DCHECK(instruction->IsDiv() || instruction->IsRem());
3155 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3156
3157 LocationSummary* locations = instruction->GetLocations();
3158 Location second = locations->InAt(1);
3159 DCHECK(second.IsConstant());
3160
3161 Register out = locations->Out().AsRegister<Register>();
3162 Register dividend = locations->InAt(0).AsRegister<Register>();
3163 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3164 DCHECK(imm == 1 || imm == -1);
3165
3166 if (instruction->IsRem()) {
3167 __ LoadImmediate(out, 0);
3168 } else {
3169 if (imm == 1) {
3170 __ Mov(out, dividend);
3171 } else {
3172 __ rsb(out, dividend, ShifterOperand(0));
3173 }
3174 }
3175}
3176
3177void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3178 DCHECK(instruction->IsDiv() || instruction->IsRem());
3179 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3180
3181 LocationSummary* locations = instruction->GetLocations();
3182 Location second = locations->InAt(1);
3183 DCHECK(second.IsConstant());
3184
3185 Register out = locations->Out().AsRegister<Register>();
3186 Register dividend = locations->InAt(0).AsRegister<Register>();
3187 Register temp = locations->GetTemp(0).AsRegister<Register>();
3188 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003189 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003190 int ctz_imm = CTZ(abs_imm);
3191
3192 if (ctz_imm == 1) {
3193 __ Lsr(temp, dividend, 32 - ctz_imm);
3194 } else {
3195 __ Asr(temp, dividend, 31);
3196 __ Lsr(temp, temp, 32 - ctz_imm);
3197 }
3198 __ add(out, temp, ShifterOperand(dividend));
3199
3200 if (instruction->IsDiv()) {
3201 __ Asr(out, out, ctz_imm);
3202 if (imm < 0) {
3203 __ rsb(out, out, ShifterOperand(0));
3204 }
3205 } else {
3206 __ ubfx(out, out, 0, ctz_imm);
3207 __ sub(out, out, ShifterOperand(temp));
3208 }
3209}
3210
3211void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3212 DCHECK(instruction->IsDiv() || instruction->IsRem());
3213 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3214
3215 LocationSummary* locations = instruction->GetLocations();
3216 Location second = locations->InAt(1);
3217 DCHECK(second.IsConstant());
3218
3219 Register out = locations->Out().AsRegister<Register>();
3220 Register dividend = locations->InAt(0).AsRegister<Register>();
3221 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3222 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3223 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3224
3225 int64_t magic;
3226 int shift;
3227 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3228
3229 __ LoadImmediate(temp1, magic);
3230 __ smull(temp2, temp1, dividend, temp1);
3231
3232 if (imm > 0 && magic < 0) {
3233 __ add(temp1, temp1, ShifterOperand(dividend));
3234 } else if (imm < 0 && magic > 0) {
3235 __ sub(temp1, temp1, ShifterOperand(dividend));
3236 }
3237
3238 if (shift != 0) {
3239 __ Asr(temp1, temp1, shift);
3240 }
3241
3242 if (instruction->IsDiv()) {
3243 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3244 } else {
3245 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3246 // TODO: Strength reduction for mls.
3247 __ LoadImmediate(temp2, imm);
3248 __ mls(out, temp1, temp2, dividend);
3249 }
3250}
3251
3252void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3253 DCHECK(instruction->IsDiv() || instruction->IsRem());
3254 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3255
3256 LocationSummary* locations = instruction->GetLocations();
3257 Location second = locations->InAt(1);
3258 DCHECK(second.IsConstant());
3259
3260 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3261 if (imm == 0) {
3262 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3263 } else if (imm == 1 || imm == -1) {
3264 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003265 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003266 DivRemByPowerOfTwo(instruction);
3267 } else {
3268 DCHECK(imm <= -2 || imm >= 2);
3269 GenerateDivRemWithAnyConstant(instruction);
3270 }
3271}
3272
Calin Juravle7c4954d2014-10-28 16:57:40 +00003273void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003274 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3275 if (div->GetResultType() == Primitive::kPrimLong) {
3276 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003277 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003278 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3279 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003280 } else if (div->GetResultType() == Primitive::kPrimInt &&
3281 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3282 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003283 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003284 }
3285
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3287
Calin Juravle7c4954d2014-10-28 16:57:40 +00003288 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003289 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003290 if (div->InputAt(1)->IsConstant()) {
3291 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003292 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003294 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3295 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003296 // No temp register required.
3297 } else {
3298 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003299 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003300 locations->AddTemp(Location::RequiresRegister());
3301 }
3302 }
3303 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003304 locations->SetInAt(0, Location::RequiresRegister());
3305 locations->SetInAt(1, Location::RequiresRegister());
3306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3307 } else {
3308 InvokeRuntimeCallingConvention calling_convention;
3309 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3310 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3311 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3312 // we only need the former.
3313 locations->SetOut(Location::RegisterLocation(R0));
3314 }
Calin Juravled0d48522014-11-04 16:40:20 +00003315 break;
3316 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003317 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003318 InvokeRuntimeCallingConvention calling_convention;
3319 locations->SetInAt(0, Location::RegisterPairLocation(
3320 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3321 locations->SetInAt(1, Location::RegisterPairLocation(
3322 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003323 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003324 break;
3325 }
3326 case Primitive::kPrimFloat:
3327 case Primitive::kPrimDouble: {
3328 locations->SetInAt(0, Location::RequiresFpuRegister());
3329 locations->SetInAt(1, Location::RequiresFpuRegister());
3330 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3331 break;
3332 }
3333
3334 default:
3335 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3336 }
3337}
3338
3339void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3340 LocationSummary* locations = div->GetLocations();
3341 Location out = locations->Out();
3342 Location first = locations->InAt(0);
3343 Location second = locations->InAt(1);
3344
3345 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003346 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003347 if (second.IsConstant()) {
3348 GenerateDivRemConstantIntegral(div);
3349 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003350 __ sdiv(out.AsRegister<Register>(),
3351 first.AsRegister<Register>(),
3352 second.AsRegister<Register>());
3353 } else {
3354 InvokeRuntimeCallingConvention calling_convention;
3355 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3356 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3357 DCHECK_EQ(R0, out.AsRegister<Register>());
3358
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003359 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003360 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003361 }
Calin Juravled0d48522014-11-04 16:40:20 +00003362 break;
3363 }
3364
Calin Juravle7c4954d2014-10-28 16:57:40 +00003365 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003366 InvokeRuntimeCallingConvention calling_convention;
3367 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3368 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3369 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3370 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3371 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003372 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003373
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003374 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003375 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003376 break;
3377 }
3378
3379 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003380 __ vdivs(out.AsFpuRegister<SRegister>(),
3381 first.AsFpuRegister<SRegister>(),
3382 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003383 break;
3384 }
3385
3386 case Primitive::kPrimDouble: {
3387 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3388 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3389 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3390 break;
3391 }
3392
3393 default:
3394 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3395 }
3396}
3397
Calin Juravlebacfec32014-11-14 15:54:36 +00003398void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003399 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003400
3401 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003402 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003403 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3404 // sdiv will be replaced by other instruction sequence.
3405 call_kind = LocationSummary::kNoCall;
3406 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3407 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003408 // Have hardware divide instruction for int, do it with three instructions.
3409 call_kind = LocationSummary::kNoCall;
3410 }
3411
Calin Juravlebacfec32014-11-14 15:54:36 +00003412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3413
Calin Juravled2ec87d2014-12-08 14:24:46 +00003414 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003415 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003416 if (rem->InputAt(1)->IsConstant()) {
3417 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003418 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003420 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3421 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003422 // No temp register required.
3423 } else {
3424 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003425 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003426 locations->AddTemp(Location::RequiresRegister());
3427 }
3428 }
3429 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003430 locations->SetInAt(0, Location::RequiresRegister());
3431 locations->SetInAt(1, Location::RequiresRegister());
3432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3433 locations->AddTemp(Location::RequiresRegister());
3434 } else {
3435 InvokeRuntimeCallingConvention calling_convention;
3436 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3437 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3438 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3439 // we only need the latter.
3440 locations->SetOut(Location::RegisterLocation(R1));
3441 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003442 break;
3443 }
3444 case Primitive::kPrimLong: {
3445 InvokeRuntimeCallingConvention calling_convention;
3446 locations->SetInAt(0, Location::RegisterPairLocation(
3447 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3448 locations->SetInAt(1, Location::RegisterPairLocation(
3449 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3450 // The runtime helper puts the output in R2,R3.
3451 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3452 break;
3453 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003454 case Primitive::kPrimFloat: {
3455 InvokeRuntimeCallingConvention calling_convention;
3456 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3457 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3458 locations->SetOut(Location::FpuRegisterLocation(S0));
3459 break;
3460 }
3461
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003463 InvokeRuntimeCallingConvention calling_convention;
3464 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3465 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3466 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3467 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3468 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003469 break;
3470 }
3471
3472 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003473 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003474 }
3475}
3476
3477void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3478 LocationSummary* locations = rem->GetLocations();
3479 Location out = locations->Out();
3480 Location first = locations->InAt(0);
3481 Location second = locations->InAt(1);
3482
Calin Juravled2ec87d2014-12-08 14:24:46 +00003483 Primitive::Type type = rem->GetResultType();
3484 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003486 if (second.IsConstant()) {
3487 GenerateDivRemConstantIntegral(rem);
3488 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003489 Register reg1 = first.AsRegister<Register>();
3490 Register reg2 = second.AsRegister<Register>();
3491 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003492
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003493 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003494 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003495 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003496 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003497 } else {
3498 InvokeRuntimeCallingConvention calling_convention;
3499 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3500 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3501 DCHECK_EQ(R1, out.AsRegister<Register>());
3502
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003503 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003504 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003505 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003506 break;
3507 }
3508
3509 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003510 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003511 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003512 break;
3513 }
3514
Calin Juravled2ec87d2014-12-08 14:24:46 +00003515 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003516 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003517 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003518 break;
3519 }
3520
Calin Juravlebacfec32014-11-14 15:54:36 +00003521 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003522 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003523 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003524 break;
3525 }
3526
3527 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003528 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 }
3530}
3531
Calin Juravled0d48522014-11-04 16:40:20 +00003532void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003533 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003534 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003535}
3536
3537void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003538 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003539 codegen_->AddSlowPath(slow_path);
3540
3541 LocationSummary* locations = instruction->GetLocations();
3542 Location value = locations->InAt(0);
3543
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003544 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003545 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003546 case Primitive::kPrimByte:
3547 case Primitive::kPrimChar:
3548 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003549 case Primitive::kPrimInt: {
3550 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003551 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003552 } else {
3553 DCHECK(value.IsConstant()) << value;
3554 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3555 __ b(slow_path->GetEntryLabel());
3556 }
3557 }
3558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 if (value.IsRegisterPair()) {
3562 __ orrs(IP,
3563 value.AsRegisterPairLow<Register>(),
3564 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3565 __ b(slow_path->GetEntryLabel(), EQ);
3566 } else {
3567 DCHECK(value.IsConstant()) << value;
3568 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3569 __ b(slow_path->GetEntryLabel());
3570 }
3571 }
3572 break;
3573 default:
3574 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3575 }
3576 }
Calin Juravled0d48522014-11-04 16:40:20 +00003577}
3578
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003579void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3580 Register in = locations->InAt(0).AsRegister<Register>();
3581 Location rhs = locations->InAt(1);
3582 Register out = locations->Out().AsRegister<Register>();
3583
3584 if (rhs.IsConstant()) {
3585 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3586 // so map all rotations to a +ve. equivalent in that range.
3587 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3588 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3589 if (rot) {
3590 // Rotate, mapping left rotations to right equivalents if necessary.
3591 // (e.g. left by 2 bits == right by 30.)
3592 __ Ror(out, in, rot);
3593 } else if (out != in) {
3594 __ Mov(out, in);
3595 }
3596 } else {
3597 __ Ror(out, in, rhs.AsRegister<Register>());
3598 }
3599}
3600
3601// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3602// rotates by swapping input regs (effectively rotating by the first 32-bits of
3603// a larger rotation) or flipping direction (thus treating larger right/left
3604// rotations as sub-word sized rotations in the other direction) as appropriate.
3605void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3606 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3607 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3608 Location rhs = locations->InAt(1);
3609 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3610 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3611
3612 if (rhs.IsConstant()) {
3613 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3614 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003615 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003616 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3617 // logic below to a simple pair of binary orr.
3618 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3619 if (rot >= kArmBitsPerWord) {
3620 rot -= kArmBitsPerWord;
3621 std::swap(in_reg_hi, in_reg_lo);
3622 }
3623 // Rotate, or mov to out for zero or word size rotations.
3624 if (rot != 0u) {
3625 __ Lsr(out_reg_hi, in_reg_hi, rot);
3626 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3627 __ Lsr(out_reg_lo, in_reg_lo, rot);
3628 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3629 } else {
3630 __ Mov(out_reg_lo, in_reg_lo);
3631 __ Mov(out_reg_hi, in_reg_hi);
3632 }
3633 } else {
3634 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3635 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3636 Label end;
3637 Label shift_by_32_plus_shift_right;
3638
3639 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3640 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3641 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3642 __ b(&shift_by_32_plus_shift_right, CC);
3643
3644 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3645 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3646 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3647 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3648 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3649 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3650 __ Lsr(shift_left, in_reg_hi, shift_right);
3651 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3652 __ b(&end);
3653
3654 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3655 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3656 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3657 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3658 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3659 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3660 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3661 __ Lsl(shift_right, in_reg_hi, shift_left);
3662 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3663
3664 __ Bind(&end);
3665 }
3666}
Roland Levillain22c49222016-03-18 14:04:28 +00003667
3668void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003669 LocationSummary* locations =
3670 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3671 switch (ror->GetResultType()) {
3672 case Primitive::kPrimInt: {
3673 locations->SetInAt(0, Location::RequiresRegister());
3674 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3676 break;
3677 }
3678 case Primitive::kPrimLong: {
3679 locations->SetInAt(0, Location::RequiresRegister());
3680 if (ror->InputAt(1)->IsConstant()) {
3681 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3682 } else {
3683 locations->SetInAt(1, Location::RequiresRegister());
3684 locations->AddTemp(Location::RequiresRegister());
3685 locations->AddTemp(Location::RequiresRegister());
3686 }
3687 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3688 break;
3689 }
3690 default:
3691 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3692 }
3693}
3694
Roland Levillain22c49222016-03-18 14:04:28 +00003695void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003696 LocationSummary* locations = ror->GetLocations();
3697 Primitive::Type type = ror->GetResultType();
3698 switch (type) {
3699 case Primitive::kPrimInt: {
3700 HandleIntegerRotate(locations);
3701 break;
3702 }
3703 case Primitive::kPrimLong: {
3704 HandleLongRotate(locations);
3705 break;
3706 }
3707 default:
3708 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003709 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003710 }
3711}
3712
Calin Juravle9aec02f2014-11-18 23:06:35 +00003713void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3714 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3715
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003716 LocationSummary* locations =
3717 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718
3719 switch (op->GetResultType()) {
3720 case Primitive::kPrimInt: {
3721 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003722 if (op->InputAt(1)->IsConstant()) {
3723 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3725 } else {
3726 locations->SetInAt(1, Location::RequiresRegister());
3727 // Make the output overlap, as it will be used to hold the masked
3728 // second input.
3729 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3730 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003731 break;
3732 }
3733 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003734 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003735 if (op->InputAt(1)->IsConstant()) {
3736 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3737 // For simplicity, use kOutputOverlap even though we only require that low registers
3738 // don't clash with high registers which the register allocator currently guarantees.
3739 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3740 } else {
3741 locations->SetInAt(1, Location::RequiresRegister());
3742 locations->AddTemp(Location::RequiresRegister());
3743 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3744 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 break;
3746 }
3747 default:
3748 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3749 }
3750}
3751
3752void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3753 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3754
3755 LocationSummary* locations = op->GetLocations();
3756 Location out = locations->Out();
3757 Location first = locations->InAt(0);
3758 Location second = locations->InAt(1);
3759
3760 Primitive::Type type = op->GetResultType();
3761 switch (type) {
3762 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003763 Register out_reg = out.AsRegister<Register>();
3764 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003765 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003767 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003768 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003770 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003771 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003772 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003774 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775 }
3776 } else {
3777 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003778 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003779 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003780 __ Mov(out_reg, first_reg);
3781 } else if (op->IsShl()) {
3782 __ Lsl(out_reg, first_reg, shift_value);
3783 } else if (op->IsShr()) {
3784 __ Asr(out_reg, first_reg, shift_value);
3785 } else {
3786 __ Lsr(out_reg, first_reg, shift_value);
3787 }
3788 }
3789 break;
3790 }
3791 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003792 Register o_h = out.AsRegisterPairHigh<Register>();
3793 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003795 Register high = first.AsRegisterPairHigh<Register>();
3796 Register low = first.AsRegisterPairLow<Register>();
3797
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003798 if (second.IsRegister()) {
3799 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003800
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003801 Register second_reg = second.AsRegister<Register>();
3802
3803 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003804 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003805 // Shift the high part
3806 __ Lsl(o_h, high, o_l);
3807 // Shift the low part and `or` what overflew on the high part
3808 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3809 __ Lsr(temp, low, temp);
3810 __ orr(o_h, o_h, ShifterOperand(temp));
3811 // If the shift is > 32 bits, override the high part
3812 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3813 __ it(PL);
3814 __ Lsl(o_h, low, temp, PL);
3815 // Shift the low part
3816 __ Lsl(o_l, low, o_l);
3817 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003818 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003819 // Shift the low part
3820 __ Lsr(o_l, low, o_h);
3821 // Shift the high part and `or` what underflew on the low part
3822 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3823 __ Lsl(temp, high, temp);
3824 __ orr(o_l, o_l, ShifterOperand(temp));
3825 // If the shift is > 32 bits, override the low part
3826 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3827 __ it(PL);
3828 __ Asr(o_l, high, temp, PL);
3829 // Shift the high part
3830 __ Asr(o_h, high, o_h);
3831 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003832 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003833 // same as Shr except we use `Lsr`s and not `Asr`s
3834 __ Lsr(o_l, low, o_h);
3835 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3836 __ Lsl(temp, high, temp);
3837 __ orr(o_l, o_l, ShifterOperand(temp));
3838 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3839 __ it(PL);
3840 __ Lsr(o_l, high, temp, PL);
3841 __ Lsr(o_h, high, o_h);
3842 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003843 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003844 // Register allocator doesn't create partial overlap.
3845 DCHECK_NE(o_l, high);
3846 DCHECK_NE(o_h, low);
3847 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003848 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003849 if (shift_value > 32) {
3850 if (op->IsShl()) {
3851 __ Lsl(o_h, low, shift_value - 32);
3852 __ LoadImmediate(o_l, 0);
3853 } else if (op->IsShr()) {
3854 __ Asr(o_l, high, shift_value - 32);
3855 __ Asr(o_h, high, 31);
3856 } else {
3857 __ Lsr(o_l, high, shift_value - 32);
3858 __ LoadImmediate(o_h, 0);
3859 }
3860 } else if (shift_value == 32) {
3861 if (op->IsShl()) {
3862 __ mov(o_h, ShifterOperand(low));
3863 __ LoadImmediate(o_l, 0);
3864 } else if (op->IsShr()) {
3865 __ mov(o_l, ShifterOperand(high));
3866 __ Asr(o_h, high, 31);
3867 } else {
3868 __ mov(o_l, ShifterOperand(high));
3869 __ LoadImmediate(o_h, 0);
3870 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003871 } else if (shift_value == 1) {
3872 if (op->IsShl()) {
3873 __ Lsls(o_l, low, 1);
3874 __ adc(o_h, high, ShifterOperand(high));
3875 } else if (op->IsShr()) {
3876 __ Asrs(o_h, high, 1);
3877 __ Rrx(o_l, low);
3878 } else {
3879 __ Lsrs(o_h, high, 1);
3880 __ Rrx(o_l, low);
3881 }
3882 } else {
3883 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003884 if (op->IsShl()) {
3885 __ Lsl(o_h, high, shift_value);
3886 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3887 __ Lsl(o_l, low, shift_value);
3888 } else if (op->IsShr()) {
3889 __ Lsr(o_l, low, shift_value);
3890 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3891 __ Asr(o_h, high, shift_value);
3892 } else {
3893 __ Lsr(o_l, low, shift_value);
3894 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3895 __ Lsr(o_h, high, shift_value);
3896 }
3897 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899 break;
3900 }
3901 default:
3902 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003903 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 }
3905}
3906
3907void LocationsBuilderARM::VisitShl(HShl* shl) {
3908 HandleShift(shl);
3909}
3910
3911void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3912 HandleShift(shl);
3913}
3914
3915void LocationsBuilderARM::VisitShr(HShr* shr) {
3916 HandleShift(shr);
3917}
3918
3919void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3920 HandleShift(shr);
3921}
3922
3923void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3924 HandleShift(ushr);
3925}
3926
3927void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3928 HandleShift(ushr);
3929}
3930
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003931void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003932 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003934 if (instruction->IsStringAlloc()) {
3935 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3936 } else {
3937 InvokeRuntimeCallingConvention calling_convention;
3938 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003939 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003940 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003941}
3942
3943void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003944 // Note: if heap poisoning is enabled, the entry point takes cares
3945 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003946 if (instruction->IsStringAlloc()) {
3947 // String is allocated through StringFactory. Call NewEmptyString entry point.
3948 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003949 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003950 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3951 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3952 __ blx(LR);
3953 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3954 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003955 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003956 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003957 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003958}
3959
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003960void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3961 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003963 InvokeRuntimeCallingConvention calling_convention;
3964 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003965 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003966 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003967 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968}
3969
3970void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3971 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08003972 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01003973 // Note: if heap poisoning is enabled, the entry point takes cares
3974 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003975 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003976 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003977}
3978
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003979void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations =
3981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003982 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3983 if (location.IsStackSlot()) {
3984 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3985 } else if (location.IsDoubleStackSlot()) {
3986 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003987 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003988 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003989}
3990
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003991void InstructionCodeGeneratorARM::VisitParameterValue(
3992 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003993 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003994}
3995
3996void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3999 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4000}
4001
4002void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4003 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004004}
4005
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004006void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004007 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004008 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004009 locations->SetInAt(0, Location::RequiresRegister());
4010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004011}
4012
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004013void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4014 LocationSummary* locations = not_->GetLocations();
4015 Location out = locations->Out();
4016 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004017 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004018 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004020 break;
4021
4022 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004023 __ mvn(out.AsRegisterPairLow<Register>(),
4024 ShifterOperand(in.AsRegisterPairLow<Register>()));
4025 __ mvn(out.AsRegisterPairHigh<Register>(),
4026 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004027 break;
4028
4029 default:
4030 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4031 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004032}
4033
David Brazdil66d126e2015-04-03 16:02:44 +01004034void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4035 LocationSummary* locations =
4036 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4037 locations->SetInAt(0, Location::RequiresRegister());
4038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4039}
4040
4041void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004042 LocationSummary* locations = bool_not->GetLocations();
4043 Location out = locations->Out();
4044 Location in = locations->InAt(0);
4045 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4046}
4047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004048void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004049 LocationSummary* locations =
4050 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004051 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004052 case Primitive::kPrimBoolean:
4053 case Primitive::kPrimByte:
4054 case Primitive::kPrimShort:
4055 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004056 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004057 case Primitive::kPrimLong: {
4058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004060 // Output overlaps because it is written before doing the low comparison.
4061 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004062 break;
4063 }
4064 case Primitive::kPrimFloat:
4065 case Primitive::kPrimDouble: {
4066 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004067 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004068 locations->SetOut(Location::RequiresRegister());
4069 break;
4070 }
4071 default:
4072 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4073 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004074}
4075
4076void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004079 Location left = locations->InAt(0);
4080 Location right = locations->InAt(1);
4081
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004082 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004084 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004085 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004086 case Primitive::kPrimBoolean:
4087 case Primitive::kPrimByte:
4088 case Primitive::kPrimShort:
4089 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004090 case Primitive::kPrimInt: {
4091 __ LoadImmediate(out, 0);
4092 __ cmp(left.AsRegister<Register>(),
4093 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4094 less_cond = LT;
4095 break;
4096 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004097 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004098 __ cmp(left.AsRegisterPairHigh<Register>(),
4099 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100 __ b(&less, LT);
4101 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004102 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004104 __ cmp(left.AsRegisterPairLow<Register>(),
4105 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004106 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 break;
4108 }
4109 case Primitive::kPrimFloat:
4110 case Primitive::kPrimDouble: {
4111 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004112 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004113 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004114 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004115 break;
4116 }
4117 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004118 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004119 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120 }
Aart Bika19616e2016-02-01 18:57:58 -08004121
Calin Juravleddb7df22014-11-25 20:56:51 +00004122 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004123 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004124
4125 __ Bind(&greater);
4126 __ LoadImmediate(out, 1);
4127 __ b(&done);
4128
4129 __ Bind(&less);
4130 __ LoadImmediate(out, -1);
4131
4132 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133}
4134
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004135void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004136 LocationSummary* locations =
4137 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004138 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004139 locations->SetInAt(i, Location::Any());
4140 }
4141 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004142}
4143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004144void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004145 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004146}
4147
Roland Levillainc9285912015-12-18 10:38:42 +00004148void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4149 // TODO (ported from quick): revisit ARM barrier kinds.
4150 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004151 switch (kind) {
4152 case MemBarrierKind::kAnyStore:
4153 case MemBarrierKind::kLoadAny:
4154 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004155 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004156 break;
4157 }
4158 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004159 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004160 break;
4161 }
4162 default:
4163 LOG(FATAL) << "Unexpected memory barrier " << kind;
4164 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004165 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004166}
4167
4168void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4169 uint32_t offset,
4170 Register out_lo,
4171 Register out_hi) {
4172 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004173 // Ensure `out_lo` is different from `addr`, so that loading
4174 // `offset` into `out_lo` does not clutter `addr`.
4175 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004176 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004177 __ add(IP, addr, ShifterOperand(out_lo));
4178 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004179 }
4180 __ ldrexd(out_lo, out_hi, addr);
4181}
4182
4183void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4184 uint32_t offset,
4185 Register value_lo,
4186 Register value_hi,
4187 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004188 Register temp2,
4189 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004190 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004191 if (offset != 0) {
4192 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004193 __ add(IP, addr, ShifterOperand(temp1));
4194 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004195 }
4196 __ Bind(&fail);
4197 // We need a load followed by store. (The address used in a STREX instruction must
4198 // be the same as the address in the most recently executed LDREX instruction.)
4199 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004200 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004201 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004202 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004203}
4204
4205void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4206 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4207
Nicolas Geoffray39468442014-09-02 15:17:15 +01004208 LocationSummary* locations =
4209 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004210 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004211
Calin Juravle52c48962014-12-16 17:02:57 +00004212 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004213 if (Primitive::IsFloatingPointType(field_type)) {
4214 locations->SetInAt(1, Location::RequiresFpuRegister());
4215 } else {
4216 locations->SetInAt(1, Location::RequiresRegister());
4217 }
4218
Calin Juravle52c48962014-12-16 17:02:57 +00004219 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004220 bool generate_volatile = field_info.IsVolatile()
4221 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004222 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004223 bool needs_write_barrier =
4224 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004225 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004226 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004227 if (needs_write_barrier) {
4228 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004229 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004230 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004231 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004232 // - registers need to be consecutive
4233 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004234 // We don't test for ARM yet, and the assertion makes sure that we
4235 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004236 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4237
4238 locations->AddTemp(Location::RequiresRegister());
4239 locations->AddTemp(Location::RequiresRegister());
4240 if (field_type == Primitive::kPrimDouble) {
4241 // For doubles we need two more registers to copy the value.
4242 locations->AddTemp(Location::RegisterLocation(R2));
4243 locations->AddTemp(Location::RegisterLocation(R3));
4244 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246}
4247
Calin Juravle52c48962014-12-16 17:02:57 +00004248void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004249 const FieldInfo& field_info,
4250 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4252
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004254 Register base = locations->InAt(0).AsRegister<Register>();
4255 Location value = locations->InAt(1);
4256
4257 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004258 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004259 Primitive::Type field_type = field_info.GetFieldType();
4260 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004261 bool needs_write_barrier =
4262 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004263
4264 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004265 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267
4268 switch (field_type) {
4269 case Primitive::kPrimBoolean:
4270 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004271 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272 break;
4273 }
4274
4275 case Primitive::kPrimShort:
4276 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004277 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278 break;
4279 }
4280
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004283 if (kPoisonHeapReferences && needs_write_barrier) {
4284 // Note that in the case where `value` is a null reference,
4285 // we do not enter this block, as a null reference does not
4286 // need poisoning.
4287 DCHECK_EQ(field_type, Primitive::kPrimNot);
4288 Register temp = locations->GetTemp(0).AsRegister<Register>();
4289 __ Mov(temp, value.AsRegister<Register>());
4290 __ PoisonHeapReference(temp);
4291 __ StoreToOffset(kStoreWord, temp, base, offset);
4292 } else {
4293 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295 break;
4296 }
4297
4298 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004299 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004300 GenerateWideAtomicStore(base, offset,
4301 value.AsRegisterPairLow<Register>(),
4302 value.AsRegisterPairHigh<Register>(),
4303 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004304 locations->GetTemp(1).AsRegister<Register>(),
4305 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004306 } else {
4307 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004309 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 break;
4311 }
4312
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004313 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004314 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004315 break;
4316 }
4317
4318 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004319 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004320 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004321 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4322 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4323
4324 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4325
4326 GenerateWideAtomicStore(base, offset,
4327 value_reg_lo,
4328 value_reg_hi,
4329 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004330 locations->GetTemp(3).AsRegister<Register>(),
4331 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004332 } else {
4333 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004334 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004335 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004336 break;
4337 }
4338
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339 case Primitive::kPrimVoid:
4340 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004341 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342 }
Calin Juravle52c48962014-12-16 17:02:57 +00004343
Calin Juravle77520bc2015-01-12 18:45:46 +00004344 // Longs and doubles are handled in the switch.
4345 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4346 codegen_->MaybeRecordImplicitNullCheck(instruction);
4347 }
4348
4349 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4350 Register temp = locations->GetTemp(0).AsRegister<Register>();
4351 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004352 codegen_->MarkGCCard(
4353 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 }
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004357 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004358 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359}
4360
Calin Juravle52c48962014-12-16 17:02:57 +00004361void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4362 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004363
4364 bool object_field_get_with_read_barrier =
4365 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004366 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004367 new (GetGraph()->GetArena()) LocationSummary(instruction,
4368 object_field_get_with_read_barrier ?
4369 LocationSummary::kCallOnSlowPath :
4370 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004371 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004372 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004373 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004374 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004375
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004376 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004377 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004378 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004379 // The output overlaps in case of volatile long: we don't want the
4380 // code generated by GenerateWideAtomicLoad to overwrite the
4381 // object's location. Likewise, in the case of an object field get
4382 // with read barriers enabled, we do not want the load to overwrite
4383 // the object's location, as we need it to emit the read barrier.
4384 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4385 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004386
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004387 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4388 locations->SetOut(Location::RequiresFpuRegister());
4389 } else {
4390 locations->SetOut(Location::RequiresRegister(),
4391 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4392 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004393 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004394 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004395 // - registers need to be consecutive
4396 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004397 // We don't test for ARM yet, and the assertion makes sure that we
4398 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004399 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4400 locations->AddTemp(Location::RequiresRegister());
4401 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004402 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4403 // We need a temporary register for the read barrier marking slow
4404 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4405 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004406 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407}
4408
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004409Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4410 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4411 << input->GetType();
4412 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4413 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4414 return Location::ConstantLocation(input->AsConstant());
4415 } else {
4416 return Location::RequiresFpuRegister();
4417 }
4418}
4419
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004420Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4421 Opcode opcode) {
4422 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4423 if (constant->IsConstant() &&
4424 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4425 return Location::ConstantLocation(constant->AsConstant());
4426 }
4427 return Location::RequiresRegister();
4428}
4429
4430bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4431 Opcode opcode) {
4432 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4433 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004434 Opcode high_opcode = opcode;
4435 SetCc low_set_cc = kCcDontCare;
4436 switch (opcode) {
4437 case SUB:
4438 // Flip the operation to an ADD.
4439 value = -value;
4440 opcode = ADD;
4441 FALLTHROUGH_INTENDED;
4442 case ADD:
4443 if (Low32Bits(value) == 0u) {
4444 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4445 }
4446 high_opcode = ADC;
4447 low_set_cc = kCcSet;
4448 break;
4449 default:
4450 break;
4451 }
4452 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4453 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004454 } else {
4455 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4456 }
4457}
4458
Vladimir Marko59751a72016-08-05 14:37:27 +01004459bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4460 Opcode opcode,
4461 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004462 ShifterOperand so;
4463 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004464 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004465 return true;
4466 }
4467 Opcode neg_opcode = kNoOperand;
4468 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004469 case AND: neg_opcode = BIC; value = ~value; break;
4470 case ORR: neg_opcode = ORN; value = ~value; break;
4471 case ADD: neg_opcode = SUB; value = -value; break;
4472 case ADC: neg_opcode = SBC; value = ~value; break;
4473 case SUB: neg_opcode = ADD; value = -value; break;
4474 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004475 default:
4476 return false;
4477 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004478 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004479}
4480
Calin Juravle52c48962014-12-16 17:02:57 +00004481void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4482 const FieldInfo& field_info) {
4483 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
Calin Juravle52c48962014-12-16 17:02:57 +00004485 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004486 Location base_loc = locations->InAt(0);
4487 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004488 Location out = locations->Out();
4489 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004490 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004491 Primitive::Type field_type = field_info.GetFieldType();
4492 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4493
4494 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004495 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004496 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004497 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004498
Roland Levillainc9285912015-12-18 10:38:42 +00004499 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004500 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502
Roland Levillainc9285912015-12-18 10:38:42 +00004503 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004504 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506
Roland Levillainc9285912015-12-18 10:38:42 +00004507 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004508 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510
4511 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004512 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004514
4515 case Primitive::kPrimNot: {
4516 // /* HeapReference<Object> */ out = *(base + offset)
4517 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4518 Location temp_loc = locations->GetTemp(0);
4519 // Note that a potential implicit null check is handled in this
4520 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4521 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4522 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4523 if (is_volatile) {
4524 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4525 }
4526 } else {
4527 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4528 codegen_->MaybeRecordImplicitNullCheck(instruction);
4529 if (is_volatile) {
4530 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4531 }
4532 // If read barriers are enabled, emit read barriers other than
4533 // Baker's using a slow path (and also unpoison the loaded
4534 // reference, if heap poisoning is enabled).
4535 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4536 }
4537 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538 }
4539
Roland Levillainc9285912015-12-18 10:38:42 +00004540 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004541 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004542 GenerateWideAtomicLoad(base, offset,
4543 out.AsRegisterPairLow<Register>(),
4544 out.AsRegisterPairHigh<Register>());
4545 } else {
4546 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4547 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549
Roland Levillainc9285912015-12-18 10:38:42 +00004550 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004551 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004552 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004553
4554 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004555 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004556 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004557 Register lo = locations->GetTemp(0).AsRegister<Register>();
4558 Register hi = locations->GetTemp(1).AsRegister<Register>();
4559 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004561 __ vmovdrr(out_reg, lo, hi);
4562 } else {
4563 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004564 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004565 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004566 break;
4567 }
4568
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004570 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004571 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004572 }
Calin Juravle52c48962014-12-16 17:02:57 +00004573
Roland Levillainc9285912015-12-18 10:38:42 +00004574 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4575 // Potential implicit null checks, in the case of reference or
4576 // double fields, are handled in the previous switch statement.
4577 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004578 codegen_->MaybeRecordImplicitNullCheck(instruction);
4579 }
4580
Calin Juravle52c48962014-12-16 17:02:57 +00004581 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004582 if (field_type == Primitive::kPrimNot) {
4583 // Memory barriers, in the case of references, are also handled
4584 // in the previous switch statement.
4585 } else {
4586 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4587 }
Roland Levillain4d027112015-07-01 15:41:14 +01004588 }
Calin Juravle52c48962014-12-16 17:02:57 +00004589}
4590
4591void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4592 HandleFieldSet(instruction, instruction->GetFieldInfo());
4593}
4594
4595void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004596 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004597}
4598
4599void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4600 HandleFieldGet(instruction, instruction->GetFieldInfo());
4601}
4602
4603void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4604 HandleFieldGet(instruction, instruction->GetFieldInfo());
4605}
4606
4607void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4608 HandleFieldGet(instruction, instruction->GetFieldInfo());
4609}
4610
4611void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4612 HandleFieldGet(instruction, instruction->GetFieldInfo());
4613}
4614
4615void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4616 HandleFieldSet(instruction, instruction->GetFieldInfo());
4617}
4618
4619void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004620 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004621}
4622
Calin Juravlee460d1d2015-09-29 04:52:17 +01004623void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4624 HUnresolvedInstanceFieldGet* instruction) {
4625 FieldAccessCallingConventionARM calling_convention;
4626 codegen_->CreateUnresolvedFieldLocationSummary(
4627 instruction, instruction->GetFieldType(), calling_convention);
4628}
4629
4630void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4631 HUnresolvedInstanceFieldGet* instruction) {
4632 FieldAccessCallingConventionARM calling_convention;
4633 codegen_->GenerateUnresolvedFieldAccess(instruction,
4634 instruction->GetFieldType(),
4635 instruction->GetFieldIndex(),
4636 instruction->GetDexPc(),
4637 calling_convention);
4638}
4639
4640void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4641 HUnresolvedInstanceFieldSet* instruction) {
4642 FieldAccessCallingConventionARM calling_convention;
4643 codegen_->CreateUnresolvedFieldLocationSummary(
4644 instruction, instruction->GetFieldType(), calling_convention);
4645}
4646
4647void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4648 HUnresolvedInstanceFieldSet* instruction) {
4649 FieldAccessCallingConventionARM calling_convention;
4650 codegen_->GenerateUnresolvedFieldAccess(instruction,
4651 instruction->GetFieldType(),
4652 instruction->GetFieldIndex(),
4653 instruction->GetDexPc(),
4654 calling_convention);
4655}
4656
4657void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4658 HUnresolvedStaticFieldGet* instruction) {
4659 FieldAccessCallingConventionARM calling_convention;
4660 codegen_->CreateUnresolvedFieldLocationSummary(
4661 instruction, instruction->GetFieldType(), calling_convention);
4662}
4663
4664void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4665 HUnresolvedStaticFieldGet* instruction) {
4666 FieldAccessCallingConventionARM calling_convention;
4667 codegen_->GenerateUnresolvedFieldAccess(instruction,
4668 instruction->GetFieldType(),
4669 instruction->GetFieldIndex(),
4670 instruction->GetDexPc(),
4671 calling_convention);
4672}
4673
4674void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4675 HUnresolvedStaticFieldSet* instruction) {
4676 FieldAccessCallingConventionARM calling_convention;
4677 codegen_->CreateUnresolvedFieldLocationSummary(
4678 instruction, instruction->GetFieldType(), calling_convention);
4679}
4680
4681void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4682 HUnresolvedStaticFieldSet* instruction) {
4683 FieldAccessCallingConventionARM calling_convention;
4684 codegen_->GenerateUnresolvedFieldAccess(instruction,
4685 instruction->GetFieldType(),
4686 instruction->GetFieldIndex(),
4687 instruction->GetDexPc(),
4688 calling_convention);
4689}
4690
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004691void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004692 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4693 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694}
4695
Calin Juravle2ae48182016-03-16 14:05:09 +00004696void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4697 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 return;
4699 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004700 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004701
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004702 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004703 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004704}
4705
Calin Juravle2ae48182016-03-16 14:05:09 +00004706void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004707 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004708 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004709
4710 LocationSummary* locations = instruction->GetLocations();
4711 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004713 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004714}
4715
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004716void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004717 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004718}
4719
Artem Serov6c916792016-07-11 14:02:34 +01004720static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4721 switch (type) {
4722 case Primitive::kPrimNot:
4723 return kLoadWord;
4724 case Primitive::kPrimBoolean:
4725 return kLoadUnsignedByte;
4726 case Primitive::kPrimByte:
4727 return kLoadSignedByte;
4728 case Primitive::kPrimChar:
4729 return kLoadUnsignedHalfword;
4730 case Primitive::kPrimShort:
4731 return kLoadSignedHalfword;
4732 case Primitive::kPrimInt:
4733 return kLoadWord;
4734 case Primitive::kPrimLong:
4735 return kLoadWordPair;
4736 case Primitive::kPrimFloat:
4737 return kLoadSWord;
4738 case Primitive::kPrimDouble:
4739 return kLoadDWord;
4740 default:
4741 LOG(FATAL) << "Unreachable type " << type;
4742 UNREACHABLE();
4743 }
4744}
4745
4746static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4747 switch (type) {
4748 case Primitive::kPrimNot:
4749 return kStoreWord;
4750 case Primitive::kPrimBoolean:
4751 case Primitive::kPrimByte:
4752 return kStoreByte;
4753 case Primitive::kPrimChar:
4754 case Primitive::kPrimShort:
4755 return kStoreHalfword;
4756 case Primitive::kPrimInt:
4757 return kStoreWord;
4758 case Primitive::kPrimLong:
4759 return kStoreWordPair;
4760 case Primitive::kPrimFloat:
4761 return kStoreSWord;
4762 case Primitive::kPrimDouble:
4763 return kStoreDWord;
4764 default:
4765 LOG(FATAL) << "Unreachable type " << type;
4766 UNREACHABLE();
4767 }
4768}
4769
4770void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4771 Location out_loc,
4772 Register base,
4773 Register reg_offset,
4774 Condition cond) {
4775 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4776 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4777
4778 switch (type) {
4779 case Primitive::kPrimByte:
4780 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4781 break;
4782 case Primitive::kPrimBoolean:
4783 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4784 break;
4785 case Primitive::kPrimShort:
4786 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4787 break;
4788 case Primitive::kPrimChar:
4789 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4790 break;
4791 case Primitive::kPrimNot:
4792 case Primitive::kPrimInt:
4793 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4794 break;
4795 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4796 case Primitive::kPrimLong:
4797 case Primitive::kPrimFloat:
4798 case Primitive::kPrimDouble:
4799 default:
4800 LOG(FATAL) << "Unreachable type " << type;
4801 UNREACHABLE();
4802 }
4803}
4804
4805void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4806 Location loc,
4807 Register base,
4808 Register reg_offset,
4809 Condition cond) {
4810 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4811 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4812
4813 switch (type) {
4814 case Primitive::kPrimByte:
4815 case Primitive::kPrimBoolean:
4816 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4817 break;
4818 case Primitive::kPrimShort:
4819 case Primitive::kPrimChar:
4820 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4821 break;
4822 case Primitive::kPrimNot:
4823 case Primitive::kPrimInt:
4824 __ str(loc.AsRegister<Register>(), mem_address, cond);
4825 break;
4826 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4827 case Primitive::kPrimLong:
4828 case Primitive::kPrimFloat:
4829 case Primitive::kPrimDouble:
4830 default:
4831 LOG(FATAL) << "Unreachable type " << type;
4832 UNREACHABLE();
4833 }
4834}
4835
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004837 bool object_array_get_with_read_barrier =
4838 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004839 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004840 new (GetGraph()->GetArena()) LocationSummary(instruction,
4841 object_array_get_with_read_barrier ?
4842 LocationSummary::kCallOnSlowPath :
4843 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004844 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004845 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004846 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004847 locations->SetInAt(0, Location::RequiresRegister());
4848 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004849 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4850 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4851 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004852 // The output overlaps in the case of an object array get with
4853 // read barriers enabled: we do not want the move to overwrite the
4854 // array's location, as we need it to emit the read barrier.
4855 locations->SetOut(
4856 Location::RequiresRegister(),
4857 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004858 }
Roland Levillainc9285912015-12-18 10:38:42 +00004859 // We need a temporary register for the read barrier marking slow
4860 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004861 // Also need for String compression feature.
4862 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4863 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004864 locations->AddTemp(Location::RequiresRegister());
4865 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866}
4867
4868void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4869 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004870 Location obj_loc = locations->InAt(0);
4871 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004872 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004873 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004874 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004875 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004876 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4877 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004878 HInstruction* array_instr = instruction->GetArray();
4879 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004880
Roland Levillain4d027112015-07-01 15:41:14 +01004881 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004882 case Primitive::kPrimBoolean:
4883 case Primitive::kPrimByte:
4884 case Primitive::kPrimShort:
4885 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004886 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004887 Register length;
4888 if (maybe_compressed_char_at) {
4889 length = locations->GetTemp(0).AsRegister<Register>();
4890 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4891 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4892 codegen_->MaybeRecordImplicitNullCheck(instruction);
4893 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004895 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004896 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004897 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004898 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4899 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4900 "Expecting 0=compressed, 1=uncompressed");
4901 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004902 __ LoadFromOffset(kLoadUnsignedByte,
4903 out_loc.AsRegister<Register>(),
4904 obj,
4905 data_offset + const_index);
4906 __ b(&done);
4907 __ Bind(&uncompressed_load);
4908 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4909 out_loc.AsRegister<Register>(),
4910 obj,
4911 data_offset + (const_index << 1));
4912 __ Bind(&done);
4913 } else {
4914 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004915
jessicahandojo05765752016-09-09 19:01:32 -07004916 LoadOperandType load_type = GetLoadOperandType(type);
4917 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4918 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004920 Register temp = IP;
4921
4922 if (has_intermediate_address) {
4923 // We do not need to compute the intermediate address from the array: the
4924 // input instruction has done it already. See the comment in
4925 // `TryExtractArrayAccessAddress()`.
4926 if (kIsDebugBuild) {
4927 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4928 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4929 }
4930 temp = obj;
4931 } else {
4932 __ add(temp, obj, ShifterOperand(data_offset));
4933 }
jessicahandojo05765752016-09-09 19:01:32 -07004934 if (maybe_compressed_char_at) {
4935 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004936 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4937 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4938 "Expecting 0=compressed, 1=uncompressed");
4939 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004940 __ ldrb(out_loc.AsRegister<Register>(),
4941 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4942 __ b(&done);
4943 __ Bind(&uncompressed_load);
4944 __ ldrh(out_loc.AsRegister<Register>(),
4945 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4946 __ Bind(&done);
4947 } else {
4948 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4949 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950 }
4951 break;
4952 }
4953
Roland Levillainc9285912015-12-18 10:38:42 +00004954 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004955 // The read barrier instrumentation of object ArrayGet
4956 // instructions does not support the HIntermediateAddress
4957 // instruction.
4958 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4959
Roland Levillainc9285912015-12-18 10:38:42 +00004960 static_assert(
4961 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4962 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004963 // /* HeapReference<Object> */ out =
4964 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4965 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4966 Location temp = locations->GetTemp(0);
4967 // Note that a potential implicit null check is handled in this
4968 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4969 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4970 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4971 } else {
4972 Register out = out_loc.AsRegister<Register>();
4973 if (index.IsConstant()) {
4974 size_t offset =
4975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4976 __ LoadFromOffset(kLoadWord, out, obj, offset);
4977 codegen_->MaybeRecordImplicitNullCheck(instruction);
4978 // If read barriers are enabled, emit read barriers other than
4979 // Baker's using a slow path (and also unpoison the loaded
4980 // reference, if heap poisoning is enabled).
4981 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4982 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004983 Register temp = IP;
4984
4985 if (has_intermediate_address) {
4986 // We do not need to compute the intermediate address from the array: the
4987 // input instruction has done it already. See the comment in
4988 // `TryExtractArrayAccessAddress()`.
4989 if (kIsDebugBuild) {
4990 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4991 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4992 }
4993 temp = obj;
4994 } else {
4995 __ add(temp, obj, ShifterOperand(data_offset));
4996 }
4997 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004998
Roland Levillainc9285912015-12-18 10:38:42 +00004999 codegen_->MaybeRecordImplicitNullCheck(instruction);
5000 // If read barriers are enabled, emit read barriers other than
5001 // Baker's using a slow path (and also unpoison the loaded
5002 // reference, if heap poisoning is enabled).
5003 codegen_->MaybeGenerateReadBarrierSlow(
5004 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5005 }
5006 }
5007 break;
5008 }
5009
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005012 size_t offset =
5013 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005014 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005016 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005017 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018 }
5019 break;
5020 }
5021
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005022 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005023 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005024 if (index.IsConstant()) {
5025 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005026 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005027 } else {
5028 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005029 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005030 }
5031 break;
5032 }
5033
5034 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005035 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005036 if (index.IsConstant()) {
5037 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005038 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005039 } else {
5040 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005041 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005042 }
5043 break;
5044 }
5045
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005047 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005048 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 }
Roland Levillain4d027112015-07-01 15:41:14 +01005050
5051 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005052 // Potential implicit null checks, in the case of reference
5053 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005054 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005056 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057}
5058
5059void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005061
5062 bool needs_write_barrier =
5063 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005064 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005065
Nicolas Geoffray39468442014-09-02 15:17:15 +01005066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005067 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005068 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005069 LocationSummary::kCallOnSlowPath :
5070 LocationSummary::kNoCall);
5071
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005072 locations->SetInAt(0, Location::RequiresRegister());
5073 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5074 if (Primitive::IsFloatingPointType(value_type)) {
5075 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005077 locations->SetInAt(2, Location::RequiresRegister());
5078 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005079 if (needs_write_barrier) {
5080 // Temporary registers for the write barrier.
5081 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005082 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084}
5085
5086void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5087 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005088 Location array_loc = locations->InAt(0);
5089 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005091 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005092 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005093 bool needs_write_barrier =
5094 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005095 uint32_t data_offset =
5096 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5097 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005098 HInstruction* array_instr = instruction->GetArray();
5099 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100
5101 switch (value_type) {
5102 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005103 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005105 case Primitive::kPrimChar:
5106 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005108 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5109 uint32_t full_offset =
5110 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5111 StoreOperandType store_type = GetStoreOperandType(value_type);
5112 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005114 Register temp = IP;
5115
5116 if (has_intermediate_address) {
5117 // We do not need to compute the intermediate address from the array: the
5118 // input instruction has done it already. See the comment in
5119 // `TryExtractArrayAccessAddress()`.
5120 if (kIsDebugBuild) {
5121 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5122 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5123 }
5124 temp = array;
5125 } else {
5126 __ add(temp, array, ShifterOperand(data_offset));
5127 }
Artem Serov6c916792016-07-11 14:02:34 +01005128 codegen_->StoreToShiftedRegOffset(value_type,
5129 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005130 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005131 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
5133 break;
5134 }
5135
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005137 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005138 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5139 // See the comment in instruction_simplifier_shared.cc.
5140 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005141
5142 if (instruction->InputAt(2)->IsNullConstant()) {
5143 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005145 size_t offset =
5146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005147 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148 } else {
5149 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005150 __ add(IP, array, ShifterOperand(data_offset));
5151 codegen_->StoreToShiftedRegOffset(value_type,
5152 value_loc,
5153 IP,
5154 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005155 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005156 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005157 DCHECK(!needs_write_barrier);
5158 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161
5162 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005163 Location temp1_loc = locations->GetTemp(0);
5164 Register temp1 = temp1_loc.AsRegister<Register>();
5165 Location temp2_loc = locations->GetTemp(1);
5166 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005167 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5168 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5169 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5170 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005171 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172
Roland Levillain3b359c72015-11-17 19:35:12 +00005173 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005174 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5175 codegen_->AddSlowPath(slow_path);
5176 if (instruction->GetValueCanBeNull()) {
5177 Label non_zero;
5178 __ CompareAndBranchIfNonZero(value, &non_zero);
5179 if (index.IsConstant()) {
5180 size_t offset =
5181 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5182 __ StoreToOffset(kStoreWord, value, array, offset);
5183 } else {
5184 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005185 __ add(IP, array, ShifterOperand(data_offset));
5186 codegen_->StoreToShiftedRegOffset(value_type,
5187 value_loc,
5188 IP,
5189 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005190 }
5191 codegen_->MaybeRecordImplicitNullCheck(instruction);
5192 __ b(&done);
5193 __ Bind(&non_zero);
5194 }
5195
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005196 // Note that when read barriers are enabled, the type checks
5197 // are performed without read barriers. This is fine, even in
5198 // the case where a class object is in the from-space after
5199 // the flip, as a comparison involving such a type would not
5200 // produce a false positive; it may of course produce a false
5201 // negative, in which case we would take the ArraySet slow
5202 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005203
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005204 // /* HeapReference<Class> */ temp1 = array->klass_
5205 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5206 codegen_->MaybeRecordImplicitNullCheck(instruction);
5207 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005208
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005209 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5210 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5211 // /* HeapReference<Class> */ temp2 = value->klass_
5212 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5213 // If heap poisoning is enabled, no need to unpoison `temp1`
5214 // nor `temp2`, as we are comparing two poisoned references.
5215 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005216
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005217 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5218 Label do_put;
5219 __ b(&do_put, EQ);
5220 // If heap poisoning is enabled, the `temp1` reference has
5221 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005222 __ MaybeUnpoisonHeapReference(temp1);
5223
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005224 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5225 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5226 // If heap poisoning is enabled, no need to unpoison
5227 // `temp1`, as we are comparing against null below.
5228 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5229 __ Bind(&do_put);
5230 } else {
5231 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 }
5233 }
5234
Artem Serov6c916792016-07-11 14:02:34 +01005235 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 if (kPoisonHeapReferences) {
5237 // Note that in the case where `value` is a null reference,
5238 // we do not enter this block, as a null reference does not
5239 // need poisoning.
5240 DCHECK_EQ(value_type, Primitive::kPrimNot);
5241 __ Mov(temp1, value);
5242 __ PoisonHeapReference(temp1);
5243 source = temp1;
5244 }
5245
5246 if (index.IsConstant()) {
5247 size_t offset =
5248 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5249 __ StoreToOffset(kStoreWord, source, array, offset);
5250 } else {
5251 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005252
5253 __ add(IP, array, ShifterOperand(data_offset));
5254 codegen_->StoreToShiftedRegOffset(value_type,
5255 Location::RegisterLocation(source),
5256 IP,
5257 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 }
5259
Roland Levillain3b359c72015-11-17 19:35:12 +00005260 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005261 codegen_->MaybeRecordImplicitNullCheck(instruction);
5262 }
5263
5264 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5265
5266 if (done.IsLinked()) {
5267 __ Bind(&done);
5268 }
5269
5270 if (slow_path != nullptr) {
5271 __ Bind(slow_path->GetExitLabel());
5272 }
5273
5274 break;
5275 }
5276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005278 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005280 size_t offset =
5281 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005285 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 }
5287 break;
5288 }
5289
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005290 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005291 Location value = locations->InAt(2);
5292 DCHECK(value.IsFpuRegister());
5293 if (index.IsConstant()) {
5294 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005296 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005298 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5299 }
5300 break;
5301 }
5302
5303 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005304 Location value = locations->InAt(2);
5305 DCHECK(value.IsFpuRegisterPair());
5306 if (index.IsConstant()) {
5307 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005309 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005311 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5312 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005313
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005314 break;
5315 }
5316
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005318 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005319 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005321
Roland Levillain80e67092016-01-08 16:04:55 +00005322 // Objects are handled in the switch.
5323 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 codegen_->MaybeRecordImplicitNullCheck(instruction);
5325 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326}
5327
5328void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005329 LocationSummary* locations =
5330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005331 locations->SetInAt(0, Location::RequiresRegister());
5332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333}
5334
5335void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5336 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005337 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005338 Register obj = locations->InAt(0).AsRegister<Register>();
5339 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005341 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005342 // Mask out compression flag from String's array length.
5343 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005344 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005345 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346}
5347
Artem Serov328429f2016-07-06 16:23:04 +01005348void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005349 LocationSummary* locations =
5350 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5351
5352 locations->SetInAt(0, Location::RequiresRegister());
5353 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5355}
5356
5357void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5358 LocationSummary* locations = instruction->GetLocations();
5359 Location out = locations->Out();
5360 Location first = locations->InAt(0);
5361 Location second = locations->InAt(1);
5362
Artem Serov328429f2016-07-06 16:23:04 +01005363 if (second.IsRegister()) {
5364 __ add(out.AsRegister<Register>(),
5365 first.AsRegister<Register>(),
5366 ShifterOperand(second.AsRegister<Register>()));
5367 } else {
5368 __ AddConstant(out.AsRegister<Register>(),
5369 first.AsRegister<Register>(),
5370 second.GetConstant()->AsIntConstant()->GetValue());
5371 }
5372}
5373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005375 RegisterSet caller_saves = RegisterSet::Empty();
5376 InvokeRuntimeCallingConvention calling_convention;
5377 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5378 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5379 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380 locations->SetInAt(0, Location::RequiresRegister());
5381 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382}
5383
5384void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5385 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005386 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005387 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 codegen_->AddSlowPath(slow_path);
5389
Roland Levillain271ab9c2014-11-27 15:23:57 +00005390 Register index = locations->InAt(0).AsRegister<Register>();
5391 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392
5393 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005394 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395}
5396
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005397void CodeGeneratorARM::MarkGCCard(Register temp,
5398 Register card,
5399 Register object,
5400 Register value,
5401 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005402 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005403 if (can_be_null) {
5404 __ CompareAndBranchIfZero(value, &is_null);
5405 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005406 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5408 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005409 if (can_be_null) {
5410 __ Bind(&is_null);
5411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412}
5413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005414void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005415 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005416}
5417
5418void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005419 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5420}
5421
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005422void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005423 LocationSummary* locations =
5424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005425 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005426}
5427
5428void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005429 HBasicBlock* block = instruction->GetBlock();
5430 if (block->GetLoopInformation() != nullptr) {
5431 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5432 // The back edge will generate the suspend check.
5433 return;
5434 }
5435 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5436 // The goto will generate the suspend check.
5437 return;
5438 }
5439 GenerateSuspendCheck(instruction, nullptr);
5440}
5441
5442void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5443 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005445 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5446 if (slow_path == nullptr) {
5447 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5448 instruction->SetSlowPath(slow_path);
5449 codegen_->AddSlowPath(slow_path);
5450 if (successor != nullptr) {
5451 DCHECK(successor->IsLoopHeader());
5452 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5453 }
5454 } else {
5455 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5456 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005457
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005458 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005459 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005460 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005461 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005462 __ Bind(slow_path->GetReturnLabel());
5463 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005464 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005465 __ b(slow_path->GetEntryLabel());
5466 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005467}
5468
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005469ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5470 return codegen_->GetAssembler();
5471}
5472
5473void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005474 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005475 Location source = move->GetSource();
5476 Location destination = move->GetDestination();
5477
5478 if (source.IsRegister()) {
5479 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005480 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005481 } else if (destination.IsFpuRegister()) {
5482 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005483 } else {
5484 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005485 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005486 SP, destination.GetStackIndex());
5487 }
5488 } else if (source.IsStackSlot()) {
5489 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005490 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005491 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005492 } else if (destination.IsFpuRegister()) {
5493 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005494 } else {
5495 DCHECK(destination.IsStackSlot());
5496 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5497 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5498 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005499 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005500 if (destination.IsRegister()) {
5501 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5502 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005503 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005504 } else {
5505 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005506 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5507 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005508 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005509 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005510 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5511 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005512 } else if (destination.IsRegisterPair()) {
5513 DCHECK(ExpectedPairLayout(destination));
5514 __ LoadFromOffset(
5515 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5516 } else {
5517 DCHECK(destination.IsFpuRegisterPair()) << destination;
5518 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5519 SP,
5520 source.GetStackIndex());
5521 }
5522 } else if (source.IsRegisterPair()) {
5523 if (destination.IsRegisterPair()) {
5524 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5525 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005526 } else if (destination.IsFpuRegisterPair()) {
5527 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5528 source.AsRegisterPairLow<Register>(),
5529 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005530 } else {
5531 DCHECK(destination.IsDoubleStackSlot()) << destination;
5532 DCHECK(ExpectedPairLayout(source));
5533 __ StoreToOffset(
5534 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5535 }
5536 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005537 if (destination.IsRegisterPair()) {
5538 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5539 destination.AsRegisterPairHigh<Register>(),
5540 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5541 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005542 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5543 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5544 } else {
5545 DCHECK(destination.IsDoubleStackSlot()) << destination;
5546 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5547 SP,
5548 destination.GetStackIndex());
5549 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005550 } else {
5551 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005552 HConstant* constant = source.GetConstant();
5553 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5554 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005555 if (destination.IsRegister()) {
5556 __ LoadImmediate(destination.AsRegister<Register>(), value);
5557 } else {
5558 DCHECK(destination.IsStackSlot());
5559 __ LoadImmediate(IP, value);
5560 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5561 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005562 } else if (constant->IsLongConstant()) {
5563 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005564 if (destination.IsRegisterPair()) {
5565 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5566 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005567 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005568 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005569 __ LoadImmediate(IP, Low32Bits(value));
5570 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5571 __ LoadImmediate(IP, High32Bits(value));
5572 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5573 }
5574 } else if (constant->IsDoubleConstant()) {
5575 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005576 if (destination.IsFpuRegisterPair()) {
5577 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005578 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005579 DCHECK(destination.IsDoubleStackSlot()) << destination;
5580 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005581 __ LoadImmediate(IP, Low32Bits(int_value));
5582 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5583 __ LoadImmediate(IP, High32Bits(int_value));
5584 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5585 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005586 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005587 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005588 float value = constant->AsFloatConstant()->GetValue();
5589 if (destination.IsFpuRegister()) {
5590 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5591 } else {
5592 DCHECK(destination.IsStackSlot());
5593 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5594 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5595 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005596 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005597 }
5598}
5599
5600void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5601 __ Mov(IP, reg);
5602 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5603 __ StoreToOffset(kStoreWord, IP, SP, mem);
5604}
5605
5606void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5607 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5608 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5609 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5610 SP, mem1 + stack_offset);
5611 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5612 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5613 SP, mem2 + stack_offset);
5614 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5615}
5616
5617void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005618 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005619 Location source = move->GetSource();
5620 Location destination = move->GetDestination();
5621
5622 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005623 DCHECK_NE(source.AsRegister<Register>(), IP);
5624 DCHECK_NE(destination.AsRegister<Register>(), IP);
5625 __ Mov(IP, source.AsRegister<Register>());
5626 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5627 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005628 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005629 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005630 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005631 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005632 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5633 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005634 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005635 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005636 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005637 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005638 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005639 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005640 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005641 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005642 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5643 destination.AsRegisterPairHigh<Register>(),
5644 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005645 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005646 Register low_reg = source.IsRegisterPair()
5647 ? source.AsRegisterPairLow<Register>()
5648 : destination.AsRegisterPairLow<Register>();
5649 int mem = source.IsRegisterPair()
5650 ? destination.GetStackIndex()
5651 : source.GetStackIndex();
5652 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005653 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005654 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005655 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005656 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005657 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5658 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005659 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005660 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005661 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005662 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5663 DRegister reg = source.IsFpuRegisterPair()
5664 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5665 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5666 int mem = source.IsFpuRegisterPair()
5667 ? destination.GetStackIndex()
5668 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005669 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005670 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005671 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005672 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5673 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5674 : destination.AsFpuRegister<SRegister>();
5675 int mem = source.IsFpuRegister()
5676 ? destination.GetStackIndex()
5677 : source.GetStackIndex();
5678
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005679 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005680 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005681 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005682 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005683 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5684 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005685 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005686 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005687 }
5688}
5689
5690void ParallelMoveResolverARM::SpillScratch(int reg) {
5691 __ Push(static_cast<Register>(reg));
5692}
5693
5694void ParallelMoveResolverARM::RestoreScratch(int reg) {
5695 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005696}
5697
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005698HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5699 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 switch (desired_class_load_kind) {
5701 case HLoadClass::LoadKind::kReferrersClass:
5702 break;
5703 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5704 DCHECK(!GetCompilerOptions().GetCompilePic());
5705 break;
5706 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5707 DCHECK(GetCompilerOptions().GetCompilePic());
5708 break;
5709 case HLoadClass::LoadKind::kBootImageAddress:
5710 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005711 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005712 break;
5713 case HLoadClass::LoadKind::kDexCachePcRelative:
5714 DCHECK(!Runtime::Current()->UseJitCompilation());
5715 // We disable pc-relative load when there is an irreducible loop, as the optimization
5716 // is incompatible with it.
5717 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5718 // with irreducible loops.
5719 if (GetGraph()->HasIrreducibleLoops()) {
5720 return HLoadClass::LoadKind::kDexCacheViaMethod;
5721 }
5722 break;
5723 case HLoadClass::LoadKind::kDexCacheViaMethod:
5724 break;
5725 }
5726 return desired_class_load_kind;
5727}
5728
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005729void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005730 if (cls->NeedsAccessCheck()) {
5731 InvokeRuntimeCallingConvention calling_convention;
5732 CodeGenerator::CreateLoadClassLocationSummary(
5733 cls,
5734 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5735 Location::RegisterLocation(R0),
5736 /* code_generator_supports_read_barrier */ true);
5737 return;
5738 }
5739
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005740 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5741 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005742 ? LocationSummary::kCallOnSlowPath
5743 : LocationSummary::kNoCall;
5744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005745 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005746 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005747 }
5748
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5750 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5751 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5752 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5753 locations->SetInAt(0, Location::RequiresRegister());
5754 }
5755 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756}
5757
5758void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005759 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005760 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005761 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005762 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005763 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005764 return;
5765 }
5766
Roland Levillain3b359c72015-11-17 19:35:12 +00005767 Location out_loc = locations->Out();
5768 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005769
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005770 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5771 ? kWithoutReadBarrier
5772 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005773 bool generate_null_check = false;
5774 switch (cls->GetLoadKind()) {
5775 case HLoadClass::LoadKind::kReferrersClass: {
5776 DCHECK(!cls->CanCallRuntime());
5777 DCHECK(!cls->MustGenerateClinitCheck());
5778 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5779 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005780 GenerateGcRootFieldLoad(cls,
5781 out_loc,
5782 current_method,
5783 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005784 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005785 break;
5786 }
5787 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005788 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5790 cls->GetTypeIndex()));
5791 break;
5792 }
5793 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005794 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005795 CodeGeneratorARM::PcRelativePatchInfo* labels =
5796 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5797 __ BindTrackedLabel(&labels->movw_label);
5798 __ movw(out, /* placeholder */ 0u);
5799 __ BindTrackedLabel(&labels->movt_label);
5800 __ movt(out, /* placeholder */ 0u);
5801 __ BindTrackedLabel(&labels->add_pc_label);
5802 __ add(out, out, ShifterOperand(PC));
5803 break;
5804 }
5805 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005806 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005807 DCHECK_NE(cls->GetAddress(), 0u);
5808 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5809 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5810 break;
5811 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005812 case HLoadClass::LoadKind::kJitTableAddress: {
5813 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5814 cls->GetTypeIndex(),
5815 cls->GetAddress()));
5816 // /* GcRoot<mirror::Class> */ out = *out
5817 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 break;
5819 }
5820 case HLoadClass::LoadKind::kDexCachePcRelative: {
5821 Register base_reg = locations->InAt(0).AsRegister<Register>();
5822 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5823 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5824 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005825 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826 generate_null_check = !cls->IsInDexCache();
5827 break;
5828 }
5829 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5830 // /* GcRoot<mirror::Class>[] */ out =
5831 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5832 Register current_method = locations->InAt(0).AsRegister<Register>();
5833 __ LoadFromOffset(kLoadWord,
5834 out,
5835 current_method,
5836 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5837 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005838 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005839 GenerateGcRootFieldLoad(cls, out_loc, out, offset, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005840 generate_null_check = !cls->IsInDexCache();
5841 }
5842 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005843
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005844 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5845 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005846 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005847 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5848 codegen_->AddSlowPath(slow_path);
5849 if (generate_null_check) {
5850 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5851 }
5852 if (cls->MustGenerateClinitCheck()) {
5853 GenerateClassInitializationCheck(slow_path, out);
5854 } else {
5855 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005856 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005857 }
5858}
5859
5860void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5861 LocationSummary* locations =
5862 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5863 locations->SetInAt(0, Location::RequiresRegister());
5864 if (check->HasUses()) {
5865 locations->SetOut(Location::SameAsFirstInput());
5866 }
5867}
5868
5869void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005870 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005871 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005872 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005874 GenerateClassInitializationCheck(slow_path,
5875 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005876}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005878void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005879 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5881 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5882 __ b(slow_path->GetEntryLabel(), LT);
5883 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5884 // properly. Therefore, we do a memory fence.
5885 __ dmb(ISH);
5886 __ Bind(slow_path->GetExitLabel());
5887}
5888
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5890 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 switch (desired_string_load_kind) {
5892 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5893 DCHECK(!GetCompilerOptions().GetCompilePic());
5894 break;
5895 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5896 DCHECK(GetCompilerOptions().GetCompilePic());
5897 break;
5898 case HLoadString::LoadKind::kBootImageAddress:
5899 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005900 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005901 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005903 case HLoadString::LoadKind::kJitTableAddress:
5904 DCHECK(Runtime::Current()->UseJitCompilation());
5905 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906 case HLoadString::LoadKind::kDexCacheViaMethod:
5907 break;
5908 }
5909 return desired_string_load_kind;
5910}
5911
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005913 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005915 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005916 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005917 locations->SetOut(Location::RegisterLocation(R0));
5918 } else {
5919 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005920 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5921 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5922 // Rely on the pResolveString and/or marking to save everything, including temps.
5923 // Note that IP may theoretically be clobbered by saving/restoring the live register
5924 // (only one thanks to the custom calling convention), so we request a different temp.
5925 locations->AddTemp(Location::RequiresRegister());
5926 RegisterSet caller_saves = RegisterSet::Empty();
5927 InvokeRuntimeCallingConvention calling_convention;
5928 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5929 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5930 // that the the kPrimNot result register is the same as the first argument register.
5931 locations->SetCustomSlowPathCallerSaves(caller_saves);
5932 } else {
5933 // For non-Baker read barrier we have a temp-clobbering call.
5934 }
5935 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005936 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005937}
5938
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005939// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5940// move.
5941void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005942 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005943 Location out_loc = locations->Out();
5944 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005945 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005946
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005947 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005948 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5950 load->GetStringIndex()));
5951 return; // No dex cache slow path.
5952 }
5953 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005954 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005955 CodeGeneratorARM::PcRelativePatchInfo* labels =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005956 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 __ BindTrackedLabel(&labels->movw_label);
5958 __ movw(out, /* placeholder */ 0u);
5959 __ BindTrackedLabel(&labels->movt_label);
5960 __ movt(out, /* placeholder */ 0u);
5961 __ BindTrackedLabel(&labels->add_pc_label);
5962 __ add(out, out, ShifterOperand(PC));
5963 return; // No dex cache slow path.
5964 }
5965 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005966 uint32_t address = dchecked_integral_cast<uint32_t>(
5967 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5968 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5970 return; // No dex cache slow path.
5971 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005972 case HLoadString::LoadKind::kBssEntry: {
5973 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005974 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005975 CodeGeneratorARM::PcRelativePatchInfo* labels =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005976 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005977 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005978 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005979 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005980 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005981 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005982 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005983 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005984 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5985 codegen_->AddSlowPath(slow_path);
5986 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5987 __ Bind(slow_path->GetExitLabel());
5988 return;
5989 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005990 case HLoadString::LoadKind::kJitTableAddress: {
5991 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005992 load->GetStringIndex(),
5993 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005994 // /* GcRoot<mirror::String> */ out = *out
5995 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
5996 return;
5997 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005999 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006000 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006001
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006002 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6003 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6004 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006005 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006006 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006007 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6008 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006009}
6010
David Brazdilcb1c0552015-08-04 16:22:25 +01006011static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006012 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006013}
6014
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006015void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6016 LocationSummary* locations =
6017 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6018 locations->SetOut(Location::RequiresRegister());
6019}
6020
6021void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006022 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006023 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6024}
6025
6026void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6027 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6028}
6029
6030void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006031 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006032 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006033}
6034
6035void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6036 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006037 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006038 InvokeRuntimeCallingConvention calling_convention;
6039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6040}
6041
6042void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006043 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006044 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006045}
6046
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006047// Temp is used for read barrier.
6048static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6049 if (kEmitCompilerReadBarrier &&
6050 (kUseBakerReadBarrier ||
6051 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6052 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6053 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6054 return 1;
6055 }
6056 return 0;
6057}
6058
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006059// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006060// interface pointer, one for loading the current interface.
6061// The other checks have one temp for loading the object's class.
6062static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6063 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6064 return 3;
6065 }
6066 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006067}
6068
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006069void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006072 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006073 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 case TypeCheckKind::kExactCheck:
6075 case TypeCheckKind::kAbstractClassCheck:
6076 case TypeCheckKind::kClassHierarchyCheck:
6077 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006078 call_kind =
6079 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006080 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 break;
6082 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006083 case TypeCheckKind::kUnresolvedCheck:
6084 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 call_kind = LocationSummary::kCallOnSlowPath;
6086 break;
6087 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006088
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006090 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006091 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006092 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006093 locations->SetInAt(0, Location::RequiresRegister());
6094 locations->SetInAt(1, Location::RequiresRegister());
6095 // The "out" register is used as a temporary, so it overlaps with the inputs.
6096 // Note that TypeCheckSlowPathARM uses this register too.
6097 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006098 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006099}
6100
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006101void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006102 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006103 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006104 Location obj_loc = locations->InAt(0);
6105 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006106 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006107 Location out_loc = locations->Out();
6108 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006109 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6110 DCHECK_LE(num_temps, 1u);
6111 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006112 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6114 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6115 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006116 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006117 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006118
6119 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006120 // avoid null check if we know obj is not null.
6121 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006122 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006123 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124
Roland Levillainc9285912015-12-18 10:38:42 +00006125 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006127 // /* HeapReference<Class> */ out = obj->klass_
6128 GenerateReferenceLoadTwoRegisters(instruction,
6129 out_loc,
6130 obj_loc,
6131 class_offset,
6132 maybe_temp_loc,
6133 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006134 __ cmp(out, ShifterOperand(cls));
6135 // Classes must be equal for the instanceof to succeed.
6136 __ b(&zero, NE);
6137 __ LoadImmediate(out, 1);
6138 __ b(&done);
6139 break;
6140 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006143 // /* HeapReference<Class> */ out = obj->klass_
6144 GenerateReferenceLoadTwoRegisters(instruction,
6145 out_loc,
6146 obj_loc,
6147 class_offset,
6148 maybe_temp_loc,
6149 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 // If the class is abstract, we eagerly fetch the super class of the
6151 // object to avoid doing a comparison we know will fail.
6152 Label loop;
6153 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006154 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006155 GenerateReferenceLoadOneRegister(instruction,
6156 out_loc,
6157 super_offset,
6158 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006159 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160 // If `out` is null, we use it for the result, and jump to `done`.
6161 __ CompareAndBranchIfZero(out, &done);
6162 __ cmp(out, ShifterOperand(cls));
6163 __ b(&loop, NE);
6164 __ LoadImmediate(out, 1);
6165 if (zero.IsLinked()) {
6166 __ b(&done);
6167 }
6168 break;
6169 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006172 // /* HeapReference<Class> */ out = obj->klass_
6173 GenerateReferenceLoadTwoRegisters(instruction,
6174 out_loc,
6175 obj_loc,
6176 class_offset,
6177 maybe_temp_loc,
6178 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 // Walk over the class hierarchy to find a match.
6180 Label loop, success;
6181 __ Bind(&loop);
6182 __ cmp(out, ShifterOperand(cls));
6183 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006184 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006185 GenerateReferenceLoadOneRegister(instruction,
6186 out_loc,
6187 super_offset,
6188 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006189 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 __ CompareAndBranchIfNonZero(out, &loop);
6191 // If `out` is null, we use it for the result, and jump to `done`.
6192 __ b(&done);
6193 __ Bind(&success);
6194 __ LoadImmediate(out, 1);
6195 if (zero.IsLinked()) {
6196 __ b(&done);
6197 }
6198 break;
6199 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006202 // /* HeapReference<Class> */ out = obj->klass_
6203 GenerateReferenceLoadTwoRegisters(instruction,
6204 out_loc,
6205 obj_loc,
6206 class_offset,
6207 maybe_temp_loc,
6208 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006209 // Do an exact check.
6210 Label exact_check;
6211 __ cmp(out, ShifterOperand(cls));
6212 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006213 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006214 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006215 GenerateReferenceLoadOneRegister(instruction,
6216 out_loc,
6217 component_offset,
6218 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006219 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 // If `out` is null, we use it for the result, and jump to `done`.
6221 __ CompareAndBranchIfZero(out, &done);
6222 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6223 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6224 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006225 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226 __ LoadImmediate(out, 1);
6227 __ b(&done);
6228 break;
6229 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006231 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006232 // No read barrier since the slow path will retry upon failure.
6233 // /* HeapReference<Class> */ out = obj->klass_
6234 GenerateReferenceLoadTwoRegisters(instruction,
6235 out_loc,
6236 obj_loc,
6237 class_offset,
6238 maybe_temp_loc,
6239 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006240 __ cmp(out, ShifterOperand(cls));
6241 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006242 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6243 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 codegen_->AddSlowPath(slow_path);
6245 __ b(slow_path->GetEntryLabel(), NE);
6246 __ LoadImmediate(out, 1);
6247 if (zero.IsLinked()) {
6248 __ b(&done);
6249 }
6250 break;
6251 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006252
Calin Juravle98893e12015-10-02 21:05:03 +01006253 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006254 case TypeCheckKind::kInterfaceCheck: {
6255 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006256 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006257 // cases.
6258 //
6259 // We cannot directly call the InstanceofNonTrivial runtime
6260 // entry point without resorting to a type checking slow path
6261 // here (i.e. by calling InvokeRuntime directly), as it would
6262 // require to assign fixed registers for the inputs of this
6263 // HInstanceOf instruction (following the runtime calling
6264 // convention), which might be cluttered by the potential first
6265 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006266 //
6267 // TODO: Introduce a new runtime entry point taking the object
6268 // to test (instead of its class) as argument, and let it deal
6269 // with the read barrier issues. This will let us refactor this
6270 // case of the `switch` code as it was previously (with a direct
6271 // call to the runtime not using a type checking slow path).
6272 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006273 DCHECK(locations->OnlyCallsOnSlowPath());
6274 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6275 /* is_fatal */ false);
6276 codegen_->AddSlowPath(slow_path);
6277 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006278 if (zero.IsLinked()) {
6279 __ b(&done);
6280 }
6281 break;
6282 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006283 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006284
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006286 __ Bind(&zero);
6287 __ LoadImmediate(out, 0);
6288 }
6289
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006290 if (done.IsLinked()) {
6291 __ Bind(&done);
6292 }
6293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006294 if (slow_path != nullptr) {
6295 __ Bind(slow_path->GetExitLabel());
6296 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006297}
6298
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6301 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6302
Roland Levillain3b359c72015-11-17 19:35:12 +00006303 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6304 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 case TypeCheckKind::kExactCheck:
6306 case TypeCheckKind::kAbstractClassCheck:
6307 case TypeCheckKind::kClassHierarchyCheck:
6308 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006309 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6310 LocationSummary::kCallOnSlowPath :
6311 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 break;
6313 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006314 case TypeCheckKind::kUnresolvedCheck:
6315 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 call_kind = LocationSummary::kCallOnSlowPath;
6317 break;
6318 }
6319
Roland Levillain3b359c72015-11-17 19:35:12 +00006320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6321 locations->SetInAt(0, Location::RequiresRegister());
6322 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006323 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324}
6325
6326void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006327 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006328 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006329 Location obj_loc = locations->InAt(0);
6330 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006331 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006332 Location temp_loc = locations->GetTemp(0);
6333 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006334 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6335 DCHECK_LE(num_temps, 3u);
6336 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6337 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6338 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6339 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6340 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6341 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6342 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6343 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6344 const uint32_t object_array_data_offset =
6345 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006346
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006347 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6348 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6349 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006350 bool is_type_check_slow_path_fatal = false;
6351 if (!kEmitCompilerReadBarrier) {
6352 is_type_check_slow_path_fatal =
6353 (type_check_kind == TypeCheckKind::kExactCheck ||
6354 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6355 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6356 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6357 !instruction->CanThrowIntoCatchBlock();
6358 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006359 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006360 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6361 is_type_check_slow_path_fatal);
6362 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363
6364 Label done;
6365 // Avoid null check if we know obj is not null.
6366 if (instruction->MustDoNullCheck()) {
6367 __ CompareAndBranchIfZero(obj, &done);
6368 }
6369
Roland Levillain3b359c72015-11-17 19:35:12 +00006370 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 case TypeCheckKind::kExactCheck:
6372 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006373 // /* HeapReference<Class> */ temp = obj->klass_
6374 GenerateReferenceLoadTwoRegisters(instruction,
6375 temp_loc,
6376 obj_loc,
6377 class_offset,
6378 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006379 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006380
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381 __ cmp(temp, ShifterOperand(cls));
6382 // Jump to slow path for throwing the exception or doing a
6383 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006384 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 break;
6386 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006389 // /* HeapReference<Class> */ temp = obj->klass_
6390 GenerateReferenceLoadTwoRegisters(instruction,
6391 temp_loc,
6392 obj_loc,
6393 class_offset,
6394 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006395 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 // If the class is abstract, we eagerly fetch the super class of the
6398 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006399 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006401 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006402 GenerateReferenceLoadOneRegister(instruction,
6403 temp_loc,
6404 super_offset,
6405 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006406 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006407
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006408 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6409 // exception.
6410 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006411
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006412 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 __ cmp(temp, ShifterOperand(cls));
6414 __ b(&loop, NE);
6415 break;
6416 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006417
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006419 // /* HeapReference<Class> */ temp = obj->klass_
6420 GenerateReferenceLoadTwoRegisters(instruction,
6421 temp_loc,
6422 obj_loc,
6423 class_offset,
6424 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006425 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006426
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006428 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 __ Bind(&loop);
6430 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006431 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006432
Roland Levillain3b359c72015-11-17 19:35:12 +00006433 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006434 GenerateReferenceLoadOneRegister(instruction,
6435 temp_loc,
6436 super_offset,
6437 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006438 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006439
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006440 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6441 // exception.
6442 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6443 // Otherwise, jump to the beginning of the loop.
6444 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 break;
6446 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006449 // /* HeapReference<Class> */ temp = obj->klass_
6450 GenerateReferenceLoadTwoRegisters(instruction,
6451 temp_loc,
6452 obj_loc,
6453 class_offset,
6454 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006455 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006456
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006457 // Do an exact check.
6458 __ cmp(temp, ShifterOperand(cls));
6459 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006460
6461 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006462 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006463 GenerateReferenceLoadOneRegister(instruction,
6464 temp_loc,
6465 component_offset,
6466 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006467 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006468 // If the component type is null, jump to the slow path to throw the exception.
6469 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6470 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6471 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006473 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006474 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 break;
6476 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006477
Calin Juravle98893e12015-10-02 21:05:03 +01006478 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006479 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006480 // We cannot directly call the CheckCast runtime entry point
6481 // without resorting to a type checking slow path here (i.e. by
6482 // calling InvokeRuntime directly), as it would require to
6483 // assign fixed registers for the inputs of this HInstanceOf
6484 // instruction (following the runtime calling convention), which
6485 // might be cluttered by the potential first read barrier
6486 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006487
Roland Levillain3b359c72015-11-17 19:35:12 +00006488 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006490
6491 case TypeCheckKind::kInterfaceCheck: {
6492 // Avoid read barriers to improve performance of the fast path. We can not get false
6493 // positives by doing this.
6494 // /* HeapReference<Class> */ temp = obj->klass_
6495 GenerateReferenceLoadTwoRegisters(instruction,
6496 temp_loc,
6497 obj_loc,
6498 class_offset,
6499 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006500 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006501
6502 // /* HeapReference<Class> */ temp = temp->iftable_
6503 GenerateReferenceLoadTwoRegisters(instruction,
6504 temp_loc,
6505 temp_loc,
6506 iftable_offset,
6507 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006508 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006509 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006510 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006511 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006512 Label start_loop;
6513 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006514 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6515 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006516 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6517 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006518 // Go to next interface.
6519 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6520 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6521 maybe_temp2_loc.AsRegister<Register>(),
6522 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006523 // Compare the classes and continue the loop if they do not match.
6524 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6525 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006526 break;
6527 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006528 }
6529 __ Bind(&done);
6530
Roland Levillain3b359c72015-11-17 19:35:12 +00006531 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006532}
6533
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006534void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6535 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006537 InvokeRuntimeCallingConvention calling_convention;
6538 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6539}
6540
6541void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006542 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6543 instruction,
6544 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006545 if (instruction->IsEnter()) {
6546 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6547 } else {
6548 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6549 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006550}
6551
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006552void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6553void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6554void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006555
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006556void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006557 LocationSummary* locations =
6558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6559 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6560 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006561 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006562 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006563 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006565}
6566
6567void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6568 HandleBitwiseOperation(instruction);
6569}
6570
6571void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6572 HandleBitwiseOperation(instruction);
6573}
6574
6575void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6576 HandleBitwiseOperation(instruction);
6577}
6578
Artem Serov7fc63502016-02-09 17:15:29 +00006579
6580void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6581 LocationSummary* locations =
6582 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6583 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6584 || instruction->GetResultType() == Primitive::kPrimLong);
6585
6586 locations->SetInAt(0, Location::RequiresRegister());
6587 locations->SetInAt(1, Location::RequiresRegister());
6588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6589}
6590
6591void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6592 LocationSummary* locations = instruction->GetLocations();
6593 Location first = locations->InAt(0);
6594 Location second = locations->InAt(1);
6595 Location out = locations->Out();
6596
6597 if (instruction->GetResultType() == Primitive::kPrimInt) {
6598 Register first_reg = first.AsRegister<Register>();
6599 ShifterOperand second_reg(second.AsRegister<Register>());
6600 Register out_reg = out.AsRegister<Register>();
6601
6602 switch (instruction->GetOpKind()) {
6603 case HInstruction::kAnd:
6604 __ bic(out_reg, first_reg, second_reg);
6605 break;
6606 case HInstruction::kOr:
6607 __ orn(out_reg, first_reg, second_reg);
6608 break;
6609 // There is no EON on arm.
6610 case HInstruction::kXor:
6611 default:
6612 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6613 UNREACHABLE();
6614 }
6615 return;
6616
6617 } else {
6618 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6619 Register first_low = first.AsRegisterPairLow<Register>();
6620 Register first_high = first.AsRegisterPairHigh<Register>();
6621 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6622 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6623 Register out_low = out.AsRegisterPairLow<Register>();
6624 Register out_high = out.AsRegisterPairHigh<Register>();
6625
6626 switch (instruction->GetOpKind()) {
6627 case HInstruction::kAnd:
6628 __ bic(out_low, first_low, second_low);
6629 __ bic(out_high, first_high, second_high);
6630 break;
6631 case HInstruction::kOr:
6632 __ orn(out_low, first_low, second_low);
6633 __ orn(out_high, first_high, second_high);
6634 break;
6635 // There is no EON on arm.
6636 case HInstruction::kXor:
6637 default:
6638 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6639 UNREACHABLE();
6640 }
6641 }
6642}
6643
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006644void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6645 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6646 if (value == 0xffffffffu) {
6647 if (out != first) {
6648 __ mov(out, ShifterOperand(first));
6649 }
6650 return;
6651 }
6652 if (value == 0u) {
6653 __ mov(out, ShifterOperand(0));
6654 return;
6655 }
6656 ShifterOperand so;
6657 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6658 __ and_(out, first, so);
6659 } else {
6660 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6661 __ bic(out, first, ShifterOperand(~value));
6662 }
6663}
6664
6665void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6666 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6667 if (value == 0u) {
6668 if (out != first) {
6669 __ mov(out, ShifterOperand(first));
6670 }
6671 return;
6672 }
6673 if (value == 0xffffffffu) {
6674 __ mvn(out, ShifterOperand(0));
6675 return;
6676 }
6677 ShifterOperand so;
6678 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6679 __ orr(out, first, so);
6680 } else {
6681 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6682 __ orn(out, first, ShifterOperand(~value));
6683 }
6684}
6685
6686void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6687 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6688 if (value == 0u) {
6689 if (out != first) {
6690 __ mov(out, ShifterOperand(first));
6691 }
6692 return;
6693 }
6694 __ eor(out, first, ShifterOperand(value));
6695}
6696
Vladimir Marko59751a72016-08-05 14:37:27 +01006697void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6698 Location first,
6699 uint64_t value) {
6700 Register out_low = out.AsRegisterPairLow<Register>();
6701 Register out_high = out.AsRegisterPairHigh<Register>();
6702 Register first_low = first.AsRegisterPairLow<Register>();
6703 Register first_high = first.AsRegisterPairHigh<Register>();
6704 uint32_t value_low = Low32Bits(value);
6705 uint32_t value_high = High32Bits(value);
6706 if (value_low == 0u) {
6707 if (out_low != first_low) {
6708 __ mov(out_low, ShifterOperand(first_low));
6709 }
6710 __ AddConstant(out_high, first_high, value_high);
6711 return;
6712 }
6713 __ AddConstantSetFlags(out_low, first_low, value_low);
6714 ShifterOperand so;
6715 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6716 __ adc(out_high, first_high, so);
6717 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6718 __ sbc(out_high, first_high, so);
6719 } else {
6720 LOG(FATAL) << "Unexpected constant " << value_high;
6721 UNREACHABLE();
6722 }
6723}
6724
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006725void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6726 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006727 Location first = locations->InAt(0);
6728 Location second = locations->InAt(1);
6729 Location out = locations->Out();
6730
6731 if (second.IsConstant()) {
6732 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6733 uint32_t value_low = Low32Bits(value);
6734 if (instruction->GetResultType() == Primitive::kPrimInt) {
6735 Register first_reg = first.AsRegister<Register>();
6736 Register out_reg = out.AsRegister<Register>();
6737 if (instruction->IsAnd()) {
6738 GenerateAndConst(out_reg, first_reg, value_low);
6739 } else if (instruction->IsOr()) {
6740 GenerateOrrConst(out_reg, first_reg, value_low);
6741 } else {
6742 DCHECK(instruction->IsXor());
6743 GenerateEorConst(out_reg, first_reg, value_low);
6744 }
6745 } else {
6746 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6747 uint32_t value_high = High32Bits(value);
6748 Register first_low = first.AsRegisterPairLow<Register>();
6749 Register first_high = first.AsRegisterPairHigh<Register>();
6750 Register out_low = out.AsRegisterPairLow<Register>();
6751 Register out_high = out.AsRegisterPairHigh<Register>();
6752 if (instruction->IsAnd()) {
6753 GenerateAndConst(out_low, first_low, value_low);
6754 GenerateAndConst(out_high, first_high, value_high);
6755 } else if (instruction->IsOr()) {
6756 GenerateOrrConst(out_low, first_low, value_low);
6757 GenerateOrrConst(out_high, first_high, value_high);
6758 } else {
6759 DCHECK(instruction->IsXor());
6760 GenerateEorConst(out_low, first_low, value_low);
6761 GenerateEorConst(out_high, first_high, value_high);
6762 }
6763 }
6764 return;
6765 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006766
6767 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006768 Register first_reg = first.AsRegister<Register>();
6769 ShifterOperand second_reg(second.AsRegister<Register>());
6770 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006771 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006772 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006773 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006774 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006775 } else {
6776 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006777 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006778 }
6779 } else {
6780 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006781 Register first_low = first.AsRegisterPairLow<Register>();
6782 Register first_high = first.AsRegisterPairHigh<Register>();
6783 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6784 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6785 Register out_low = out.AsRegisterPairLow<Register>();
6786 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006787 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006788 __ and_(out_low, first_low, second_low);
6789 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006790 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006791 __ orr(out_low, first_low, second_low);
6792 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006793 } else {
6794 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006795 __ eor(out_low, first_low, second_low);
6796 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006797 }
6798 }
6799}
6800
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006801void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6802 HInstruction* instruction,
6803 Location out,
6804 uint32_t offset,
6805 Location maybe_temp,
6806 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006807 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006809 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006810 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006811 if (kUseBakerReadBarrier) {
6812 // Load with fast path based Baker's read barrier.
6813 // /* HeapReference<Object> */ out = *(out + offset)
6814 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006815 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006816 } else {
6817 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006818 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006819 // in the following move operation, as we will need it for the
6820 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006821 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006822 // /* HeapReference<Object> */ out = *(out + offset)
6823 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006824 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006825 }
6826 } else {
6827 // Plain load with no read barrier.
6828 // /* HeapReference<Object> */ out = *(out + offset)
6829 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6830 __ MaybeUnpoisonHeapReference(out_reg);
6831 }
6832}
6833
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006834void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6835 HInstruction* instruction,
6836 Location out,
6837 Location obj,
6838 uint32_t offset,
6839 Location maybe_temp,
6840 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006841 Register out_reg = out.AsRegister<Register>();
6842 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006843 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006844 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006845 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006846 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006847 // Load with fast path based Baker's read barrier.
6848 // /* HeapReference<Object> */ out = *(obj + offset)
6849 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006850 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006851 } else {
6852 // Load with slow path based read barrier.
6853 // /* HeapReference<Object> */ out = *(obj + offset)
6854 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6855 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6856 }
6857 } else {
6858 // Plain load with no read barrier.
6859 // /* HeapReference<Object> */ out = *(obj + offset)
6860 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6861 __ MaybeUnpoisonHeapReference(out_reg);
6862 }
6863}
6864
6865void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6866 Location root,
6867 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006868 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006869 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006870 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006871 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006872 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006873 if (kUseBakerReadBarrier) {
6874 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6875 // Baker's read barrier are used:
6876 //
6877 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006878 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6879 // if (temp != null) {
6880 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006881 // }
6882
6883 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6884 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6885 static_assert(
6886 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6887 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6888 "have different sizes.");
6889 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6890 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6891 "have different sizes.");
6892
Vladimir Marko953437b2016-08-24 08:30:46 +00006893 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006894 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006895 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006896 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6897 instruction,
6898 root,
6899 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006900 codegen_->AddSlowPath(slow_path);
6901
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006902 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6903 const int32_t entry_point_offset =
6904 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6905 // Loading the entrypoint does not require a load acquire since it is only changed when
6906 // threads are suspended or running a checkpoint.
6907 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6908 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6909 // checking GetIsGcMarking.
6910 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006911 __ Bind(slow_path->GetExitLabel());
6912 } else {
6913 // GC root loaded through a slow path for read barriers other
6914 // than Baker's.
6915 // /* GcRoot<mirror::Object>* */ root = obj + offset
6916 __ AddConstant(root_reg, obj, offset);
6917 // /* mirror::Object* */ root = root->Read()
6918 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6919 }
6920 } else {
6921 // Plain GC root load with no read barrier.
6922 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6923 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6924 // Note that GC roots are not affected by heap poisoning, thus we
6925 // do not have to unpoison `root_reg` here.
6926 }
6927}
6928
6929void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6930 Location ref,
6931 Register obj,
6932 uint32_t offset,
6933 Location temp,
6934 bool needs_null_check) {
6935 DCHECK(kEmitCompilerReadBarrier);
6936 DCHECK(kUseBakerReadBarrier);
6937
6938 // /* HeapReference<Object> */ ref = *(obj + offset)
6939 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006940 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006941 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006942 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006943}
6944
6945void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6946 Location ref,
6947 Register obj,
6948 uint32_t data_offset,
6949 Location index,
6950 Location temp,
6951 bool needs_null_check) {
6952 DCHECK(kEmitCompilerReadBarrier);
6953 DCHECK(kUseBakerReadBarrier);
6954
Roland Levillainbfea3352016-06-23 13:48:47 +01006955 static_assert(
6956 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6957 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006958 // /* HeapReference<Object> */ ref =
6959 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006960 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006961 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006962 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006963}
6964
6965void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6966 Location ref,
6967 Register obj,
6968 uint32_t offset,
6969 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006970 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006971 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006972 bool needs_null_check,
6973 bool always_update_field,
6974 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006975 DCHECK(kEmitCompilerReadBarrier);
6976 DCHECK(kUseBakerReadBarrier);
6977
6978 // In slow path based read barriers, the read barrier call is
6979 // inserted after the original load. However, in fast path based
6980 // Baker's read barriers, we need to perform the load of
6981 // mirror::Object::monitor_ *before* the original reference load.
6982 // This load-load ordering is required by the read barrier.
6983 // The fast path/slow path (for Baker's algorithm) should look like:
6984 //
6985 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6986 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6987 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006988 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006989 // if (is_gray) {
6990 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6991 // }
6992 //
6993 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006994 // slightly more complex as it performs additional checks that we do
6995 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006996
6997 Register ref_reg = ref.AsRegister<Register>();
6998 Register temp_reg = temp.AsRegister<Register>();
6999 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7000
7001 // /* int32_t */ monitor = obj->monitor_
7002 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7003 if (needs_null_check) {
7004 MaybeRecordImplicitNullCheck(instruction);
7005 }
7006 // /* LockWord */ lock_word = LockWord(monitor)
7007 static_assert(sizeof(LockWord) == sizeof(int32_t),
7008 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007009
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007010 // Introduce a dependency on the lock_word including the rb_state,
7011 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007012 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007013 // `obj` is unchanged by this operation, but its value now depends
7014 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007015 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007016
7017 // The actual reference load.
7018 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007019 // Load types involving an "index": ArrayGet,
7020 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7021 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007022 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007023 if (index.IsConstant()) {
7024 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007025 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007026 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7027 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007028 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007029 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7030 // intrinsics, which use a register pair as index ("long
7031 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007032 Register index_reg = index.IsRegisterPair()
7033 ? index.AsRegisterPairLow<Register>()
7034 : index.AsRegister<Register>();
7035 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007036 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7037 }
7038 } else {
7039 // /* HeapReference<Object> */ ref = *(obj + offset)
7040 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7041 }
7042
7043 // Object* ref = ref_addr->AsMirrorPtr()
7044 __ MaybeUnpoisonHeapReference(ref_reg);
7045
Vladimir Marko953437b2016-08-24 08:30:46 +00007046 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007047 SlowPathCodeARM* slow_path;
7048 if (always_update_field) {
7049 DCHECK(temp2 != nullptr);
7050 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7051 // of the form `obj + field_offset`, where `obj` is a register and
7052 // `field_offset` is a register pair (of which only the lower half
7053 // is used). Thus `offset` and `scale_factor` above are expected
7054 // to be null in this code path.
7055 DCHECK_EQ(offset, 0u);
7056 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7057 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7058 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7059 } else {
7060 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7061 }
Roland Levillainc9285912015-12-18 10:38:42 +00007062 AddSlowPath(slow_path);
7063
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007064 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007065 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007066 // Given the numeric representation, it's enough to check the low bit of the
7067 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7068 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007069 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7070 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007071 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7072 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007073 __ Bind(slow_path->GetExitLabel());
7074}
7075
7076void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7077 Location out,
7078 Location ref,
7079 Location obj,
7080 uint32_t offset,
7081 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007082 DCHECK(kEmitCompilerReadBarrier);
7083
Roland Levillainc9285912015-12-18 10:38:42 +00007084 // Insert a slow path based read barrier *after* the reference load.
7085 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007086 // If heap poisoning is enabled, the unpoisoning of the loaded
7087 // reference will be carried out by the runtime within the slow
7088 // path.
7089 //
7090 // Note that `ref` currently does not get unpoisoned (when heap
7091 // poisoning is enabled), which is alright as the `ref` argument is
7092 // not used by the artReadBarrierSlow entry point.
7093 //
7094 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007095 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007096 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7097 AddSlowPath(slow_path);
7098
Roland Levillain3b359c72015-11-17 19:35:12 +00007099 __ b(slow_path->GetEntryLabel());
7100 __ Bind(slow_path->GetExitLabel());
7101}
7102
Roland Levillainc9285912015-12-18 10:38:42 +00007103void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7104 Location out,
7105 Location ref,
7106 Location obj,
7107 uint32_t offset,
7108 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007109 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007110 // Baker's read barriers shall be handled by the fast path
7111 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7112 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007113 // If heap poisoning is enabled, unpoisoning will be taken care of
7114 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007115 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007116 } else if (kPoisonHeapReferences) {
7117 __ UnpoisonHeapReference(out.AsRegister<Register>());
7118 }
7119}
7120
Roland Levillainc9285912015-12-18 10:38:42 +00007121void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7122 Location out,
7123 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007124 DCHECK(kEmitCompilerReadBarrier);
7125
Roland Levillainc9285912015-12-18 10:38:42 +00007126 // Insert a slow path based read barrier *after* the GC root load.
7127 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007128 // Note that GC roots are not affected by heap poisoning, so we do
7129 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007130 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007131 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7132 AddSlowPath(slow_path);
7133
Roland Levillain3b359c72015-11-17 19:35:12 +00007134 __ b(slow_path->GetEntryLabel());
7135 __ Bind(slow_path->GetExitLabel());
7136}
7137
Vladimir Markodc151b22015-10-15 18:02:30 +01007138HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7139 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007140 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007141 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7142 // We disable pc-relative load when there is an irreducible loop, as the optimization
7143 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007144 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7145 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007146 if (GetGraph()->HasIrreducibleLoops() &&
7147 (dispatch_info.method_load_kind ==
7148 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7149 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7150 }
7151
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007152 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007153}
7154
Vladimir Markob4536b72015-11-24 13:45:23 +00007155Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7156 Register temp) {
7157 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7158 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7159 if (!invoke->GetLocations()->Intrinsified()) {
7160 return location.AsRegister<Register>();
7161 }
7162 // For intrinsics we allow any location, so it may be on the stack.
7163 if (!location.IsRegister()) {
7164 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7165 return temp;
7166 }
7167 // For register locations, check if the register was saved. If so, get it from the stack.
7168 // Note: There is a chance that the register was saved but not overwritten, so we could
7169 // save one load. However, since this is just an intrinsic slow path we prefer this
7170 // simple and more robust approach rather that trying to determine if that's the case.
7171 SlowPathCode* slow_path = GetCurrentSlowPath();
7172 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7173 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7174 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7175 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7176 return temp;
7177 }
7178 return location.AsRegister<Register>();
7179}
7180
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007181void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007182 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7183 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007184 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7185 uint32_t offset =
7186 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007187 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007188 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007189 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007190 }
Vladimir Marko58155012015-08-19 12:49:41 +00007191 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007192 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007193 break;
7194 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7195 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7196 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007197 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7198 HArmDexCacheArraysBase* base =
7199 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7200 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7201 temp.AsRegister<Register>());
7202 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7203 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7204 break;
7205 }
Vladimir Marko58155012015-08-19 12:49:41 +00007206 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007207 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007208 Register method_reg;
7209 Register reg = temp.AsRegister<Register>();
7210 if (current_method.IsRegister()) {
7211 method_reg = current_method.AsRegister<Register>();
7212 } else {
7213 DCHECK(invoke->GetLocations()->Intrinsified());
7214 DCHECK(!current_method.IsValid());
7215 method_reg = reg;
7216 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7217 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007218 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7219 __ LoadFromOffset(kLoadWord,
7220 reg,
7221 method_reg,
7222 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007223 // temp = temp[index_in_cache];
7224 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7225 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007226 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7227 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007228 }
Vladimir Marko58155012015-08-19 12:49:41 +00007229 }
7230
7231 switch (invoke->GetCodePtrLocation()) {
7232 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7233 __ bl(GetFrameEntryLabel());
7234 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007235 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7236 // LR = callee_method->entry_point_from_quick_compiled_code_
7237 __ LoadFromOffset(
7238 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007239 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007240 // LR()
7241 __ blx(LR);
7242 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007243 }
7244
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007245 DCHECK(!IsLeafMethod());
7246}
7247
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007248void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7249 Register temp = temp_location.AsRegister<Register>();
7250 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7251 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007252
7253 // Use the calling convention instead of the location of the receiver, as
7254 // intrinsics may have put the receiver in a different register. In the intrinsics
7255 // slow path, the arguments have been moved to the right place, so here we are
7256 // guaranteed that the receiver is the first register of the calling convention.
7257 InvokeDexCallingConvention calling_convention;
7258 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007259 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007260 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007261 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007262 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007263 // Instead of simply (possibly) unpoisoning `temp` here, we should
7264 // emit a read barrier for the previous class reference load.
7265 // However this is not required in practice, as this is an
7266 // intermediate/temporary reference and because the current
7267 // concurrent copying collector keeps the from-space memory
7268 // intact/accessible until the end of the marking phase (the
7269 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007270 __ MaybeUnpoisonHeapReference(temp);
7271 // temp = temp->GetMethodAt(method_offset);
7272 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007273 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007274 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7275 // LR = temp->GetEntryPoint();
7276 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7277 // LR();
7278 __ blx(LR);
7279}
7280
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007281CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7282 const DexFile& dex_file, uint32_t string_index) {
7283 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7284}
7285
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007286CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007287 const DexFile& dex_file, dex::TypeIndex type_index) {
7288 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007289}
7290
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007291CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7292 const DexFile& dex_file, uint32_t element_offset) {
7293 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7294}
7295
7296CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7297 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7298 patches->emplace_back(dex_file, offset_or_index);
7299 return &patches->back();
7300}
7301
7302Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007303 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007304 return boot_image_string_patches_.GetOrCreate(
7305 StringReference(&dex_file, string_index),
7306 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7307}
7308
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007309Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007310 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007311 return boot_image_type_patches_.GetOrCreate(
7312 TypeReference(&dex_file, type_index),
7313 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7314}
7315
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007316Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7317 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7318 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7319 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7320}
7321
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007322Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007323 dex::StringIndex string_index,
7324 Handle<mirror::String> handle) {
7325 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7326 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007327 return jit_string_patches_.GetOrCreate(
7328 StringReference(&dex_file, string_index),
7329 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7330}
7331
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007332Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7333 dex::TypeIndex type_index,
7334 uint64_t address) {
7335 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
7336 return jit_class_patches_.GetOrCreate(
7337 TypeReference(&dex_file, type_index),
7338 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7339}
7340
Vladimir Markoaad75c62016-10-03 08:46:48 +00007341template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7342inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7343 const ArenaDeque<PcRelativePatchInfo>& infos,
7344 ArenaVector<LinkerPatch>* linker_patches) {
7345 for (const PcRelativePatchInfo& info : infos) {
7346 const DexFile& dex_file = info.target_dex_file;
7347 size_t offset_or_index = info.offset_or_index;
7348 DCHECK(info.add_pc_label.IsBound());
7349 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7350 // Add MOVW patch.
7351 DCHECK(info.movw_label.IsBound());
7352 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7353 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7354 // Add MOVT patch.
7355 DCHECK(info.movt_label.IsBound());
7356 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7357 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7358 }
7359}
7360
Vladimir Marko58155012015-08-19 12:49:41 +00007361void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7362 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007363 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007364 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007365 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007366 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007367 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007368 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007369 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007370 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007371 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7372 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007373 for (const auto& entry : boot_image_string_patches_) {
7374 const StringReference& target_string = entry.first;
7375 Literal* literal = entry.second;
7376 DCHECK(literal->GetLabel()->IsBound());
7377 uint32_t literal_offset = literal->GetLabel()->Position();
7378 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7379 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007380 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007381 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007382 if (!GetCompilerOptions().IsBootImage()) {
7383 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7384 linker_patches);
7385 } else {
7386 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7387 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007388 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007389 for (const auto& entry : boot_image_type_patches_) {
7390 const TypeReference& target_type = entry.first;
7391 Literal* literal = entry.second;
7392 DCHECK(literal->GetLabel()->IsBound());
7393 uint32_t literal_offset = literal->GetLabel()->Position();
7394 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7395 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007396 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007397 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007398 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7399 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007400 for (const auto& entry : boot_image_address_patches_) {
7401 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7402 Literal* literal = entry.second;
7403 DCHECK(literal->GetLabel()->IsBound());
7404 uint32_t literal_offset = literal->GetLabel()->Position();
7405 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7406 }
7407}
7408
7409Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7410 return map->GetOrCreate(
7411 value,
7412 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007413}
7414
7415Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7416 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007417 return map->GetOrCreate(
7418 target_method,
7419 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007420}
7421
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007422void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7423 LocationSummary* locations =
7424 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7425 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7426 Location::RequiresRegister());
7427 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7428 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7430}
7431
7432void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7433 LocationSummary* locations = instr->GetLocations();
7434 Register res = locations->Out().AsRegister<Register>();
7435 Register accumulator =
7436 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7437 Register mul_left =
7438 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7439 Register mul_right =
7440 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7441
7442 if (instr->GetOpKind() == HInstruction::kAdd) {
7443 __ mla(res, mul_left, mul_right, accumulator);
7444 } else {
7445 __ mls(res, mul_left, mul_right, accumulator);
7446 }
7447}
7448
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007449void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007450 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007451 LOG(FATAL) << "Unreachable";
7452}
7453
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007454void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007455 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007456 LOG(FATAL) << "Unreachable";
7457}
7458
Mark Mendellfe57faa2015-09-18 09:26:15 -04007459// Simple implementation of packed switch - generate cascaded compare/jumps.
7460void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7461 LocationSummary* locations =
7462 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7463 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007464 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007465 codegen_->GetAssembler()->IsThumb()) {
7466 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7467 if (switch_instr->GetStartValue() != 0) {
7468 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7469 }
7470 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007471}
7472
7473void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7474 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007475 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007476 LocationSummary* locations = switch_instr->GetLocations();
7477 Register value_reg = locations->InAt(0).AsRegister<Register>();
7478 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7479
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007480 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007481 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007482 Register temp_reg = IP;
7483 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7484 // the immediate, because IP is used as the destination register. For the other
7485 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7486 // and they can be encoded in the instruction without making use of IP register.
7487 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7488
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007489 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007490 // Jump to successors[0] if value == lower_bound.
7491 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7492 int32_t last_index = 0;
7493 for (; num_entries - last_index > 2; last_index += 2) {
7494 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7495 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7496 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7497 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7498 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7499 }
7500 if (num_entries - last_index == 2) {
7501 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007502 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007503 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007504 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007505
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007506 // And the default for any other value.
7507 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7508 __ b(codegen_->GetLabelOf(default_block));
7509 }
7510 } else {
7511 // Create a table lookup.
7512 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7513
7514 // Materialize a pointer to the switch table
7515 std::vector<Label*> labels(num_entries);
7516 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7517 for (uint32_t i = 0; i < num_entries; i++) {
7518 labels[i] = codegen_->GetLabelOf(successors[i]);
7519 }
7520 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7521
7522 // Remove the bias.
7523 Register key_reg;
7524 if (lower_bound != 0) {
7525 key_reg = locations->GetTemp(1).AsRegister<Register>();
7526 __ AddConstant(key_reg, value_reg, -lower_bound);
7527 } else {
7528 key_reg = value_reg;
7529 }
7530
7531 // Check whether the value is in the table, jump to default block if not.
7532 __ CmpConstant(key_reg, num_entries - 1);
7533 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7534
7535 // Load the displacement from the table.
7536 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7537
7538 // Dispatch is a direct add to the PC (for Thumb2).
7539 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007540 }
7541}
7542
Vladimir Markob4536b72015-11-24 13:45:23 +00007543void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7545 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007546}
7547
7548void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7549 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007550 CodeGeneratorARM::PcRelativePatchInfo* labels =
7551 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007552 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007553 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007554 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007555 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007556 __ BindTrackedLabel(&labels->add_pc_label);
7557 __ add(base_reg, base_reg, ShifterOperand(PC));
7558}
7559
Andreas Gampe85b62f22015-09-09 13:15:38 -07007560void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7561 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007562 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007563 return;
7564 }
7565
7566 DCHECK_NE(type, Primitive::kPrimVoid);
7567
7568 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7569 if (return_loc.Equals(trg)) {
7570 return;
7571 }
7572
7573 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7574 // with the last branch.
7575 if (type == Primitive::kPrimLong) {
7576 HParallelMove parallel_move(GetGraph()->GetArena());
7577 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7578 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7579 GetMoveResolver()->EmitNativeCode(&parallel_move);
7580 } else if (type == Primitive::kPrimDouble) {
7581 HParallelMove parallel_move(GetGraph()->GetArena());
7582 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7583 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7584 GetMoveResolver()->EmitNativeCode(&parallel_move);
7585 } else {
7586 // Let the parallel move resolver take care of all of this.
7587 HParallelMove parallel_move(GetGraph()->GetArena());
7588 parallel_move.AddMove(return_loc, trg, type, nullptr);
7589 GetMoveResolver()->EmitNativeCode(&parallel_move);
7590 }
7591}
7592
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007593void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7594 LocationSummary* locations =
7595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7596 locations->SetInAt(0, Location::RequiresRegister());
7597 locations->SetOut(Location::RequiresRegister());
7598}
7599
7600void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7601 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007602 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007603 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007604 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007605 __ LoadFromOffset(kLoadWord,
7606 locations->Out().AsRegister<Register>(),
7607 locations->InAt(0).AsRegister<Register>(),
7608 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007609 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007610 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007611 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007612 __ LoadFromOffset(kLoadWord,
7613 locations->Out().AsRegister<Register>(),
7614 locations->InAt(0).AsRegister<Register>(),
7615 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7616 __ LoadFromOffset(kLoadWord,
7617 locations->Out().AsRegister<Register>(),
7618 locations->Out().AsRegister<Register>(),
7619 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007620 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007621}
7622
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007623static void PatchJitRootUse(uint8_t* code,
7624 const uint8_t* roots_data,
7625 Literal* literal,
7626 uint64_t index_in_table) {
7627 DCHECK(literal->GetLabel()->IsBound());
7628 uint32_t literal_offset = literal->GetLabel()->Position();
7629 uintptr_t address =
7630 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7631 uint8_t* data = code + literal_offset;
7632 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7633}
7634
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007635void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7636 for (const auto& entry : jit_string_patches_) {
7637 const auto& it = jit_string_roots_.find(entry.first);
7638 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007639 PatchJitRootUse(code, roots_data, entry.second, it->second);
7640 }
7641 for (const auto& entry : jit_class_patches_) {
7642 const auto& it = jit_class_roots_.find(entry.first);
7643 DCHECK(it != jit_class_roots_.end());
7644 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007645 }
7646}
7647
Roland Levillain4d027112015-07-01 15:41:14 +01007648#undef __
7649#undef QUICK_ENTRY_POINT
7650
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007651} // namespace arm
7652} // namespace art