blob: 32642e186816203c1a8c63e7ec660b47df4f38ee [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100110 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100374 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000375 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100377
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 LocationSummary* locations = at_->GetLocations();
380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100387 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
388 : kQuickInitializeType;
389 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000390 if (do_clinit_) {
391 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
392 } else {
393 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000395
396 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000397 Location out = locations->Out();
398 if (out.IsValid()) {
399 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000400 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
401 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 __ b(GetExitLabel());
404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 // The class this slow path will load.
410 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412 // The instruction where this slow path is happening.
413 // (Might be the load class or an initialization check).
414 HInstruction* const at_;
415
416 // The dex PC of `at_`.
417 const uint32_t dex_pc_;
418
419 // Whether to initialize the class.
420 const bool do_clinit_;
421
422 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423};
424
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425class LoadStringSlowPathARM : public SlowPathCodeARM {
426 public:
427 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
428
429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
430 LocationSummary* locations = instruction_->GetLocations();
431 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 HLoadString* load = instruction_->AsLoadString();
433 const uint32_t string_index = load->GetStringIndex();
434 Register out = locations->Out().AsRegister<Register>();
435 Register temp = locations->GetTemp(0).AsRegister<Register>();
436 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000437
438 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
439 __ Bind(GetEntryLabel());
440 SaveLiveRegisters(codegen, locations);
441
442 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100443 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
444 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
445 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
446 Register entry_address = temp_is_r0 ? out : temp;
447 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
448 if (call_saves_everything_except_r0 && temp_is_r0) {
449 __ mov(entry_address, ShifterOperand(temp));
450 }
451
Vladimir Markoaad75c62016-10-03 08:46:48 +0000452 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
453 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100455
456 // Store the resolved String to the .bss entry.
457 if (call_saves_everything_except_r0) {
458 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
459 __ str(R0, Address(entry_address));
460 } else {
461 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
462 CodeGeneratorARM::PcRelativePatchInfo* labels =
463 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
464 __ BindTrackedLabel(&labels->movw_label);
465 __ movw(entry_address, /* placeholder */ 0u);
466 __ BindTrackedLabel(&labels->movt_label);
467 __ movt(entry_address, /* placeholder */ 0u);
468 __ BindTrackedLabel(&labels->add_pc_label);
469 __ add(entry_address, entry_address, ShifterOperand(PC));
470 __ str(R0, Address(entry_address));
471 }
472
Vladimir Markoaad75c62016-10-03 08:46:48 +0000473 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000474 RestoreLiveRegisters(codegen, locations);
475
Vladimir Markoaad75c62016-10-03 08:46:48 +0000476 __ b(GetExitLabel());
477 }
478
479 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
480
481 private:
482 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
483};
484
Artem Serovf4d6aee2016-07-11 10:41:45 +0100485class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000486 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100488 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000489
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000491 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 Location arg0, arg1;
493 if (instruction_->IsInstanceOf()) {
494 arg0 = locations->InAt(1);
495 arg1 = locations->Out();
496 } else {
497 arg0 = locations->InAt(0);
498 arg1 = locations->InAt(1);
499 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000500 DCHECK(instruction_->IsCheckCast()
501 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000502
503 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
504 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 if (!is_fatal_) {
507 SaveLiveRegisters(codegen, locations);
508 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000509
510 // We're moving two locations to locations that could overlap, so we need a parallel
511 // move resolver.
512 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800513 codegen->EmitParallelMoves(arg0,
514 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 arg1,
517 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
518 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000519 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100520 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100521 instruction_,
522 instruction_->GetDexPc(),
523 this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800524 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
526 } else {
527 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800528 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
529 instruction_,
530 instruction_->GetDexPc(),
531 this);
532 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000533 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 RestoreLiveRegisters(codegen, locations);
537 __ b(GetExitLabel());
538 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 }
540
Alexandre Rames9931f312015-06-19 14:47:01 +0100541 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543 bool IsFatal() const OVERRIDE { return is_fatal_; }
544
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000545 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000546 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000547
548 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
549};
550
Artem Serovf4d6aee2016-07-11 10:41:45 +0100551class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 public:
Aart Bik42249c32016-01-07 15:33:50 -0800553 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100554 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555
556 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800557 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100559 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000560 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561 }
562
Alexandre Rames9931f312015-06-19 14:47:01 +0100563 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
564
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700565 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700566 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
567};
568
Artem Serovf4d6aee2016-07-11 10:41:45 +0100569class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100571 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 __ Bind(GetEntryLabel());
576 SaveLiveRegisters(codegen, locations);
577
578 InvokeRuntimeCallingConvention calling_convention;
579 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
580 parallel_move.AddMove(
581 locations->InAt(0),
582 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
583 Primitive::kPrimNot,
584 nullptr);
585 parallel_move.AddMove(
586 locations->InAt(1),
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimInt,
589 nullptr);
590 parallel_move.AddMove(
591 locations->InAt(2),
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimNot,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596
597 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100598 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000599 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600 RestoreLiveRegisters(codegen, locations);
601 __ b(GetExitLabel());
602 }
603
604 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
605
606 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
608};
609
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100610// Slow path marking an object reference `ref` during a read
611// barrier. The field `obj.field` in the object `obj` holding this
612// reference does not get updated by this slow path after marking (see
613// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
614//
615// This means that after the execution of this slow path, `ref` will
616// always be up-to-date, but `obj.field` may not; i.e., after the
617// flip, `ref` will be a to-space reference, but `obj.field` will
618// probably still be a from-space reference (unless it gets updated by
619// another thread, or if another thread installed another object
620// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100621class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000622 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100623 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location ref)
624 : SlowPathCodeARM(instruction), ref_(ref) {
Roland Levillainc9285912015-12-18 10:38:42 +0000625 DCHECK(kEmitCompilerReadBarrier);
626 }
627
628 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
629
630 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
631 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100632 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000633 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100634 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000635 DCHECK(instruction_->IsInstanceFieldGet() ||
636 instruction_->IsStaticFieldGet() ||
637 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100638 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000639 instruction_->IsLoadClass() ||
640 instruction_->IsLoadString() ||
641 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100642 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100643 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
644 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000645 << "Unexpected instruction in read barrier marking slow path: "
646 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000647 // The read barrier instrumentation of object ArrayGet
648 // instructions does not support the HIntermediateAddress
649 // instruction.
650 DCHECK(!(instruction_->IsArrayGet() &&
651 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000652
653 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100654 // No need to save live registers; it's taken care of by the
655 // entrypoint. Also, there is no need to update the stack mask,
656 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000657 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100658 DCHECK_NE(ref_reg, SP);
659 DCHECK_NE(ref_reg, LR);
660 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100661 // IP is used internally by the ReadBarrierMarkRegX entry point
662 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 DCHECK_NE(ref_reg, IP);
664 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100665 // "Compact" slow path, saving two moves.
666 //
667 // Instead of using the standard runtime calling convention (input
668 // and output in R0):
669 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100671 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100672 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100673 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100674 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100675 // of a dedicated entrypoint:
676 //
677 // rX <- ReadBarrierMarkRegX(rX)
678 //
679 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100681 // This runtime call does not require a stack map.
682 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000683 __ b(GetExitLabel());
684 }
685
686 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687 // The location (register) of the marked object reference.
688 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
691};
692
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693// Slow path marking an object reference `ref` during a read barrier,
694// and if needed, atomically updating the field `obj.field` in the
695// object `obj` holding this reference after marking (contrary to
696// ReadBarrierMarkSlowPathARM above, which never tries to update
697// `obj.field`).
698//
699// This means that after the execution of this slow path, both `ref`
700// and `obj.field` will be up-to-date; i.e., after the flip, both will
701// hold the same to-space reference (unless another thread installed
702// another object reference (different from `ref`) in `obj.field`).
703class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
704 public:
705 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
706 Location ref,
707 Register obj,
708 Location field_offset,
709 Register temp1,
710 Register temp2)
711 : SlowPathCodeARM(instruction),
712 ref_(ref),
713 obj_(obj),
714 field_offset_(field_offset),
715 temp1_(temp1),
716 temp2_(temp2) {
717 DCHECK(kEmitCompilerReadBarrier);
718 }
719
720 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 LocationSummary* locations = instruction_->GetLocations();
724 Register ref_reg = ref_.AsRegister<Register>();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
727 // This slow path is only used by the UnsafeCASObject intrinsic.
728 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
729 << "Unexpected instruction in read barrier marking and field updating slow path: "
730 << instruction_->DebugName();
731 DCHECK(instruction_->GetLocations()->Intrinsified());
732 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
733 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
734
735 __ Bind(GetEntryLabel());
736
737 // Save the old reference.
738 // Note that we cannot use IP to save the old reference, as IP is
739 // used internally by the ReadBarrierMarkRegX entry point, and we
740 // need the old reference after the call to that entry point.
741 DCHECK_NE(temp1_, IP);
742 __ Mov(temp1_, ref_reg);
743
744 // No need to save live registers; it's taken care of by the
745 // entrypoint. Also, there is no need to update the stack mask,
746 // as this runtime call will not trigger a garbage collection.
747 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
748 DCHECK_NE(ref_reg, SP);
749 DCHECK_NE(ref_reg, LR);
750 DCHECK_NE(ref_reg, PC);
751 // IP is used internally by the ReadBarrierMarkRegX entry point
752 // as a temporary, it cannot be the entry point's input/output.
753 DCHECK_NE(ref_reg, IP);
754 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
755 // "Compact" slow path, saving two moves.
756 //
757 // Instead of using the standard runtime calling convention (input
758 // and output in R0):
759 //
760 // R0 <- ref
761 // R0 <- ReadBarrierMark(R0)
762 // ref <- R0
763 //
764 // we just use rX (the register containing `ref`) as input and output
765 // of a dedicated entrypoint:
766 //
767 // rX <- ReadBarrierMarkRegX(rX)
768 //
769 int32_t entry_point_offset =
770 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
771 // This runtime call does not require a stack map.
772 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
773
774 // If the new reference is different from the old reference,
775 // update the field in the holder (`*(obj_ + field_offset_)`).
776 //
777 // Note that this field could also hold a different object, if
778 // another thread had concurrently changed it. In that case, the
779 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
780 // (CAS) operation below would abort the CAS, leaving the field
781 // as-is.
782 Label done;
783 __ cmp(temp1_, ShifterOperand(ref_reg));
784 __ b(&done, EQ);
785
786 // Update the the holder's field atomically. This may fail if
787 // mutator updates before us, but it's OK. This is achieved
788 // using a strong compare-and-set (CAS) operation with relaxed
789 // memory synchronization ordering, where the expected value is
790 // the old reference and the desired value is the new reference.
791
792 // Convenience aliases.
793 Register base = obj_;
794 // The UnsafeCASObject intrinsic uses a register pair as field
795 // offset ("long offset"), of which only the low part contains
796 // data.
797 Register offset = field_offset_.AsRegisterPairLow<Register>();
798 Register expected = temp1_;
799 Register value = ref_reg;
800 Register tmp_ptr = IP; // Pointer to actual memory.
801 Register tmp = temp2_; // Value in memory.
802
803 __ add(tmp_ptr, base, ShifterOperand(offset));
804
805 if (kPoisonHeapReferences) {
806 __ PoisonHeapReference(expected);
807 if (value == expected) {
808 // Do not poison `value`, as it is the same register as
809 // `expected`, which has just been poisoned.
810 } else {
811 __ PoisonHeapReference(value);
812 }
813 }
814
815 // do {
816 // tmp = [r_ptr] - expected;
817 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
818
Roland Levillain24a4d112016-10-26 13:10:46 +0100819 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100820 __ Bind(&loop_head);
821
822 __ ldrex(tmp, tmp_ptr);
823
824 __ subs(tmp, tmp, ShifterOperand(expected));
825
Roland Levillain24a4d112016-10-26 13:10:46 +0100826 __ it(NE);
827 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828
Roland Levillain24a4d112016-10-26 13:10:46 +0100829 __ b(&exit_loop, NE);
830
831 __ strex(tmp, value, tmp_ptr);
832 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100833 __ b(&loop_head, EQ);
834
Roland Levillain24a4d112016-10-26 13:10:46 +0100835 __ Bind(&exit_loop);
836
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 if (kPoisonHeapReferences) {
838 __ UnpoisonHeapReference(expected);
839 if (value == expected) {
840 // Do not unpoison `value`, as it is the same register as
841 // `expected`, which has just been unpoisoned.
842 } else {
843 __ UnpoisonHeapReference(value);
844 }
845 }
846
847 __ Bind(&done);
848 __ b(GetExitLabel());
849 }
850
851 private:
852 // The location (register) of the marked object reference.
853 const Location ref_;
854 // The register containing the object holding the marked object reference field.
855 const Register obj_;
856 // The location of the offset of the marked reference field within `obj_`.
857 Location field_offset_;
858
859 const Register temp1_;
860 const Register temp2_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
863};
864
Roland Levillain3b359c72015-11-17 19:35:12 +0000865// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100866class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000867 public:
868 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
869 Location out,
870 Location ref,
871 Location obj,
872 uint32_t offset,
873 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100874 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000875 out_(out),
876 ref_(ref),
877 obj_(obj),
878 offset_(offset),
879 index_(index) {
880 DCHECK(kEmitCompilerReadBarrier);
881 // If `obj` is equal to `out` or `ref`, it means the initial object
882 // has been overwritten by (or after) the heap object reference load
883 // to be instrumented, e.g.:
884 //
885 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000886 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000887 //
888 // In that case, we have lost the information about the original
889 // object, and the emitted read barrier cannot work properly.
890 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
891 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
892 }
893
894 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
895 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
896 LocationSummary* locations = instruction_->GetLocations();
897 Register reg_out = out_.AsRegister<Register>();
898 DCHECK(locations->CanCall());
899 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100900 DCHECK(instruction_->IsInstanceFieldGet() ||
901 instruction_->IsStaticFieldGet() ||
902 instruction_->IsArrayGet() ||
903 instruction_->IsInstanceOf() ||
904 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100905 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000906 << "Unexpected instruction in read barrier for heap reference slow path: "
907 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000908 // The read barrier instrumentation of object ArrayGet
909 // instructions does not support the HIntermediateAddress
910 // instruction.
911 DCHECK(!(instruction_->IsArrayGet() &&
912 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000913
914 __ Bind(GetEntryLabel());
915 SaveLiveRegisters(codegen, locations);
916
917 // We may have to change the index's value, but as `index_` is a
918 // constant member (like other "inputs" of this slow path),
919 // introduce a copy of it, `index`.
920 Location index = index_;
921 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100922 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000923 if (instruction_->IsArrayGet()) {
924 // Compute the actual memory offset and store it in `index`.
925 Register index_reg = index_.AsRegister<Register>();
926 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
927 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
928 // We are about to change the value of `index_reg` (see the
929 // calls to art::arm::Thumb2Assembler::Lsl and
930 // art::arm::Thumb2Assembler::AddConstant below), but it has
931 // not been saved by the previous call to
932 // art::SlowPathCode::SaveLiveRegisters, as it is a
933 // callee-save register --
934 // art::SlowPathCode::SaveLiveRegisters does not consider
935 // callee-save registers, as it has been designed with the
936 // assumption that callee-save registers are supposed to be
937 // handled by the called function. So, as a callee-save
938 // register, `index_reg` _would_ eventually be saved onto
939 // the stack, but it would be too late: we would have
940 // changed its value earlier. Therefore, we manually save
941 // it here into another freely available register,
942 // `free_reg`, chosen of course among the caller-save
943 // registers (as a callee-save `free_reg` register would
944 // exhibit the same problem).
945 //
946 // Note we could have requested a temporary register from
947 // the register allocator instead; but we prefer not to, as
948 // this is a slow path, and we know we can find a
949 // caller-save register that is available.
950 Register free_reg = FindAvailableCallerSaveRegister(codegen);
951 __ Mov(free_reg, index_reg);
952 index_reg = free_reg;
953 index = Location::RegisterLocation(index_reg);
954 } else {
955 // The initial register stored in `index_` has already been
956 // saved in the call to art::SlowPathCode::SaveLiveRegisters
957 // (as it is not a callee-save register), so we can freely
958 // use it.
959 }
960 // Shifting the index value contained in `index_reg` by the scale
961 // factor (2) cannot overflow in practice, as the runtime is
962 // unable to allocate object arrays with a size larger than
963 // 2^26 - 1 (that is, 2^28 - 4 bytes).
964 __ Lsl(index_reg, index_reg, TIMES_4);
965 static_assert(
966 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
967 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
968 __ AddConstant(index_reg, index_reg, offset_);
969 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100970 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
971 // intrinsics, `index_` is not shifted by a scale factor of 2
972 // (as in the case of ArrayGet), as it is actually an offset
973 // to an object field within an object.
974 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000975 DCHECK(instruction_->GetLocations()->Intrinsified());
976 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
977 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
978 << instruction_->AsInvoke()->GetIntrinsic();
979 DCHECK_EQ(offset_, 0U);
980 DCHECK(index_.IsRegisterPair());
981 // UnsafeGet's offset location is a register pair, the low
982 // part contains the correct offset.
983 index = index_.ToLow();
984 }
985 }
986
987 // We're moving two or three locations to locations that could
988 // overlap, so we need a parallel move resolver.
989 InvokeRuntimeCallingConvention calling_convention;
990 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
991 parallel_move.AddMove(ref_,
992 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
993 Primitive::kPrimNot,
994 nullptr);
995 parallel_move.AddMove(obj_,
996 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
997 Primitive::kPrimNot,
998 nullptr);
999 if (index.IsValid()) {
1000 parallel_move.AddMove(index,
1001 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1002 Primitive::kPrimInt,
1003 nullptr);
1004 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1005 } else {
1006 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1007 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1008 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001009 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001010 CheckEntrypointTypes<
1011 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1012 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1013
1014 RestoreLiveRegisters(codegen, locations);
1015 __ b(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1019
1020 private:
1021 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1022 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1023 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1024 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1025 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1026 return static_cast<Register>(i);
1027 }
1028 }
1029 // We shall never fail to find a free caller-save register, as
1030 // there are more than two core caller-save registers on ARM
1031 // (meaning it is possible to find one which is different from
1032 // `ref` and `obj`).
1033 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1034 LOG(FATAL) << "Could not find a free caller-save register";
1035 UNREACHABLE();
1036 }
1037
Roland Levillain3b359c72015-11-17 19:35:12 +00001038 const Location out_;
1039 const Location ref_;
1040 const Location obj_;
1041 const uint32_t offset_;
1042 // An additional location containing an index to an array.
1043 // Only used for HArrayGet and the UnsafeGetObject &
1044 // UnsafeGetObjectVolatile intrinsics.
1045 const Location index_;
1046
1047 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1048};
1049
1050// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001051class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001052 public:
1053 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001054 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001055 DCHECK(kEmitCompilerReadBarrier);
1056 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001057
1058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1059 LocationSummary* locations = instruction_->GetLocations();
1060 Register reg_out = out_.AsRegister<Register>();
1061 DCHECK(locations->CanCall());
1062 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001063 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1064 << "Unexpected instruction in read barrier for GC root slow path: "
1065 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001066
1067 __ Bind(GetEntryLabel());
1068 SaveLiveRegisters(codegen, locations);
1069
1070 InvokeRuntimeCallingConvention calling_convention;
1071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1072 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001073 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001074 instruction_,
1075 instruction_->GetDexPc(),
1076 this);
1077 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1078 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1079
1080 RestoreLiveRegisters(codegen, locations);
1081 __ b(GetExitLabel());
1082 }
1083
1084 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1085
1086 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001087 const Location out_;
1088 const Location root_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1091};
1092
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001093#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001094// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1095#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Aart Bike9f37602015-10-09 11:15:55 -07001097inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001098 switch (cond) {
1099 case kCondEQ: return EQ;
1100 case kCondNE: return NE;
1101 case kCondLT: return LT;
1102 case kCondLE: return LE;
1103 case kCondGT: return GT;
1104 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001105 case kCondB: return LO;
1106 case kCondBE: return LS;
1107 case kCondA: return HI;
1108 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001109 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 LOG(FATAL) << "Unreachable";
1111 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001112}
1113
Aart Bike9f37602015-10-09 11:15:55 -07001114// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001115inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001116 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001117 case kCondEQ: return EQ;
1118 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001119 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 case kCondLT: return LO;
1121 case kCondLE: return LS;
1122 case kCondGT: return HI;
1123 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001124 // Unsigned remain unchanged.
1125 case kCondB: return LO;
1126 case kCondBE: return LS;
1127 case kCondA: return HI;
1128 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001129 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001130 LOG(FATAL) << "Unreachable";
1131 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001132}
1133
Vladimir Markod6e069b2016-01-18 11:11:01 +00001134inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1135 // The ARM condition codes can express all the necessary branches, see the
1136 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1137 // There is no dex instruction or HIR that would need the missing conditions
1138 // "equal or unordered" or "not equal".
1139 switch (cond) {
1140 case kCondEQ: return EQ;
1141 case kCondNE: return NE /* unordered */;
1142 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1143 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1144 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1145 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1146 default:
1147 LOG(FATAL) << "UNREACHABLE";
1148 UNREACHABLE();
1149 }
1150}
1151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001152void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001153 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154}
1155
1156void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001157 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001158}
1159
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001160size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1161 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1162 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001163}
1164
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001165size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1166 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1167 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001168}
1169
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001170size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1171 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1172 return kArmWordSize;
1173}
1174
1175size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1176 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1177 return kArmWordSize;
1178}
1179
Calin Juravle34166012014-12-19 17:22:29 +00001180CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001181 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001182 const CompilerOptions& compiler_options,
1183 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001184 : CodeGenerator(graph,
1185 kNumberOfCoreRegisters,
1186 kNumberOfSRegisters,
1187 kNumberOfRegisterPairs,
1188 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1189 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001190 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1191 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001192 compiler_options,
1193 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001194 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001195 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001196 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001197 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001198 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001199 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001200 uint32_literals_(std::less<uint32_t>(),
1201 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001202 method_patches_(MethodReferenceComparator(),
1203 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1204 call_patches_(MethodReferenceComparator(),
1205 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001206 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001207 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1208 boot_image_string_patches_(StringReferenceValueComparator(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1210 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001211 boot_image_type_patches_(TypeReferenceValueComparator(),
1212 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1213 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001214 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray07c919f2016-11-09 17:29:03 +00001215 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001216 // Always save the LR register to mimic Quick.
1217 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001218}
1219
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001220void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1221 // Ensure that we fix up branches and literal loads and emit the literal pool.
1222 __ FinalizeCode();
1223
1224 // Adjust native pc offsets in stack maps.
1225 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1226 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1227 uint32_t new_position = __ GetAdjustedPosition(old_position);
1228 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1229 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001230 // Adjust pc offsets for the disassembly information.
1231 if (disasm_info_ != nullptr) {
1232 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1233 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1234 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1235 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1236 it.second.start = __ GetAdjustedPosition(it.second.start);
1237 it.second.end = __ GetAdjustedPosition(it.second.end);
1238 }
1239 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1240 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1241 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1242 }
1243 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001244
1245 CodeGenerator::Finalize(allocator);
1246}
1247
David Brazdil58282f42016-01-14 12:45:10 +00001248void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001249 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001250 blocked_core_registers_[SP] = true;
1251 blocked_core_registers_[LR] = true;
1252 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001253
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001254 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001255 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001256
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001257 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001258 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001259
David Brazdil58282f42016-01-14 12:45:10 +00001260 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001261 // Stubs do not save callee-save floating point registers. If the graph
1262 // is debuggable, we need to deal with these registers differently. For
1263 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001264 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1265 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1266 }
1267 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001268}
1269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001270InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001271 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001272 assembler_(codegen->GetAssembler()),
1273 codegen_(codegen) {}
1274
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001275void CodeGeneratorARM::ComputeSpillMask() {
1276 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1277 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001278 // There is no easy instruction to restore just the PC on thumb2. We spill and
1279 // restore another arbitrary register.
1280 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001281 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1282 // We use vpush and vpop for saving and restoring floating point registers, which take
1283 // a SRegister and the number of registers to save/restore after that SRegister. We
1284 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1285 // but in the range.
1286 if (fpu_spill_mask_ != 0) {
1287 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1288 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1289 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1290 fpu_spill_mask_ |= (1 << i);
1291 }
1292 }
1293}
1294
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001296 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297}
1298
1299static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001300 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001301}
1302
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001303void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001304 bool skip_overflow_check =
1305 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001307 __ Bind(&frame_entry_label_);
1308
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001309 if (HasEmptyFrame()) {
1310 return;
1311 }
1312
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001313 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001314 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1315 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1316 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001317 }
1318
Andreas Gampe501fd632015-09-10 16:11:06 -07001319 __ PushList(core_spill_mask_);
1320 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1321 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001322 if (fpu_spill_mask_ != 0) {
1323 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1324 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001325 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001326 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001327 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001328 int adjust = GetFrameSize() - FrameEntrySpillSize();
1329 __ AddConstant(SP, -adjust);
1330 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001331
1332 // Save the current method if we need it. Note that we do not
1333 // do this in HCurrentMethod, as the instruction might have been removed
1334 // in the SSA graph.
1335 if (RequiresCurrentMethod()) {
1336 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1337 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001338}
1339
1340void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001341 if (HasEmptyFrame()) {
1342 __ bx(LR);
1343 return;
1344 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001345 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001346 int adjust = GetFrameSize() - FrameEntrySpillSize();
1347 __ AddConstant(SP, adjust);
1348 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001349 if (fpu_spill_mask_ != 0) {
1350 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1351 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001352 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001353 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001354 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001355 // Pop LR into PC to return.
1356 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1357 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1358 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001359 __ cfi().RestoreState();
1360 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001361}
1362
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001363void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001364 Label* label = GetLabelOf(block);
1365 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001366}
1367
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001368Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001369 switch (type) {
1370 case Primitive::kPrimBoolean:
1371 case Primitive::kPrimByte:
1372 case Primitive::kPrimChar:
1373 case Primitive::kPrimShort:
1374 case Primitive::kPrimInt:
1375 case Primitive::kPrimNot: {
1376 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001377 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001378 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001379 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001380 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001381 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001382 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001383 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001384
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001385 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001386 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001387 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001388 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001389 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001390 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001391 if (calling_convention.GetRegisterAt(index) == R1) {
1392 // Skip R1, and use R2_R3 instead.
1393 gp_index_++;
1394 index++;
1395 }
1396 }
1397 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1398 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001399 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001400
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001401 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001402 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001403 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001404 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1405 }
1406 }
1407
1408 case Primitive::kPrimFloat: {
1409 uint32_t stack_index = stack_index_++;
1410 if (float_index_ % 2 == 0) {
1411 float_index_ = std::max(double_index_, float_index_);
1412 }
1413 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1414 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1415 } else {
1416 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1417 }
1418 }
1419
1420 case Primitive::kPrimDouble: {
1421 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1422 uint32_t stack_index = stack_index_;
1423 stack_index_ += 2;
1424 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1425 uint32_t index = double_index_;
1426 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001427 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001428 calling_convention.GetFpuRegisterAt(index),
1429 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001430 DCHECK(ExpectedPairLayout(result));
1431 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001432 } else {
1433 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001434 }
1435 }
1436
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001437 case Primitive::kPrimVoid:
1438 LOG(FATAL) << "Unexpected parameter type " << type;
1439 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001440 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001441 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001442}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001443
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001444Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001445 switch (type) {
1446 case Primitive::kPrimBoolean:
1447 case Primitive::kPrimByte:
1448 case Primitive::kPrimChar:
1449 case Primitive::kPrimShort:
1450 case Primitive::kPrimInt:
1451 case Primitive::kPrimNot: {
1452 return Location::RegisterLocation(R0);
1453 }
1454
1455 case Primitive::kPrimFloat: {
1456 return Location::FpuRegisterLocation(S0);
1457 }
1458
1459 case Primitive::kPrimLong: {
1460 return Location::RegisterPairLocation(R0, R1);
1461 }
1462
1463 case Primitive::kPrimDouble: {
1464 return Location::FpuRegisterPairLocation(S0, S1);
1465 }
1466
1467 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001468 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001469 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001470
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001471 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001472}
1473
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001474Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1475 return Location::RegisterLocation(kMethodRegisterArgument);
1476}
1477
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478void CodeGeneratorARM::Move32(Location destination, Location source) {
1479 if (source.Equals(destination)) {
1480 return;
1481 }
1482 if (destination.IsRegister()) {
1483 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001484 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 } else if (destination.IsFpuRegister()) {
1491 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001496 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001499 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001501 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001502 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001503 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001504 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001505 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001506 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1507 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508 }
1509 }
1510}
1511
1512void CodeGeneratorARM::Move64(Location destination, Location source) {
1513 if (source.Equals(destination)) {
1514 return;
1515 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001516 if (destination.IsRegisterPair()) {
1517 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001518 EmitParallelMoves(
1519 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1520 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001521 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001522 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001523 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1524 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001525 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001526 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 } else if (source.IsFpuRegisterPair()) {
1528 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1529 destination.AsRegisterPairHigh<Register>(),
1530 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531 } else {
1532 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001533 DCHECK(ExpectedPairLayout(destination));
1534 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1535 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001537 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001538 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001539 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1540 SP,
1541 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001542 } else if (source.IsRegisterPair()) {
1543 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1544 source.AsRegisterPairLow<Register>(),
1545 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001546 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001547 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001548 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001549 } else {
1550 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001552 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 if (source.AsRegisterPairLow<Register>() == R1) {
1554 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001555 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1556 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001558 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 SP, destination.GetStackIndex());
1560 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001561 } else if (source.IsFpuRegisterPair()) {
1562 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1563 SP,
1564 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 } else {
1566 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001567 EmitParallelMoves(
1568 Location::StackSlot(source.GetStackIndex()),
1569 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001570 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001571 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001572 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1573 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 }
1575 }
1576}
1577
Calin Juravle175dc732015-08-25 15:42:32 +01001578void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1579 DCHECK(location.IsRegister());
1580 __ LoadImmediate(location.AsRegister<Register>(), value);
1581}
1582
Calin Juravlee460d1d2015-09-29 04:52:17 +01001583void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001584 HParallelMove move(GetGraph()->GetArena());
1585 move.AddMove(src, dst, dst_type, nullptr);
1586 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001587}
1588
1589void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1590 if (location.IsRegister()) {
1591 locations->AddTemp(location);
1592 } else if (location.IsRegisterPair()) {
1593 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1594 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1595 } else {
1596 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1597 }
1598}
1599
Calin Juravle175dc732015-08-25 15:42:32 +01001600void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1601 HInstruction* instruction,
1602 uint32_t dex_pc,
1603 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001604 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001605 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001606 if (EntrypointRequiresStackMap(entrypoint)) {
1607 RecordPcInfo(instruction, dex_pc, slow_path);
1608 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001609}
1610
Roland Levillaindec8f632016-07-22 17:10:06 +01001611void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1612 HInstruction* instruction,
1613 SlowPathCode* slow_path) {
1614 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001615 GenerateInvokeRuntime(entry_point_offset);
1616}
1617
1618void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001619 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1620 __ blx(LR);
1621}
1622
David Brazdilfc6a86a2015-06-26 10:33:45 +00001623void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001624 DCHECK(!successor->IsExitBlock());
1625
1626 HBasicBlock* block = got->GetBlock();
1627 HInstruction* previous = got->GetPrevious();
1628
1629 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001630 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001631 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1632 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1633 return;
1634 }
1635
1636 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1637 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1638 }
1639 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001640 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001641 }
1642}
1643
David Brazdilfc6a86a2015-06-26 10:33:45 +00001644void LocationsBuilderARM::VisitGoto(HGoto* got) {
1645 got->SetLocations(nullptr);
1646}
1647
1648void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1649 HandleGoto(got, got->GetSuccessor());
1650}
1651
1652void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1653 try_boundary->SetLocations(nullptr);
1654}
1655
1656void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1657 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1658 if (!successor->IsExitBlock()) {
1659 HandleGoto(try_boundary, successor);
1660 }
1661}
1662
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001663void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001664 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665}
1666
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001667void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001668}
1669
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001670void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1671 Primitive::Type type = instruction->InputAt(0)->GetType();
1672 Location lhs_loc = instruction->GetLocations()->InAt(0);
1673 Location rhs_loc = instruction->GetLocations()->InAt(1);
1674 if (rhs_loc.IsConstant()) {
1675 // 0.0 is the only immediate that can be encoded directly in
1676 // a VCMP instruction.
1677 //
1678 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1679 // specify that in a floating-point comparison, positive zero
1680 // and negative zero are considered equal, so we can use the
1681 // literal 0.0 for both cases here.
1682 //
1683 // Note however that some methods (Float.equal, Float.compare,
1684 // Float.compareTo, Double.equal, Double.compare,
1685 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1686 // StrictMath.min) consider 0.0 to be (strictly) greater than
1687 // -0.0. So if we ever translate calls to these methods into a
1688 // HCompare instruction, we must handle the -0.0 case with
1689 // care here.
1690 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1691 if (type == Primitive::kPrimFloat) {
1692 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1693 } else {
1694 DCHECK_EQ(type, Primitive::kPrimDouble);
1695 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1696 }
1697 } else {
1698 if (type == Primitive::kPrimFloat) {
1699 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1700 } else {
1701 DCHECK_EQ(type, Primitive::kPrimDouble);
1702 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1703 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1704 }
1705 }
1706}
1707
Roland Levillain4fa13f62015-07-06 18:11:54 +01001708void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1709 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001710 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001712 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713}
1714
1715void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1716 Label* true_label,
1717 Label* false_label) {
1718 LocationSummary* locations = cond->GetLocations();
1719 Location left = locations->InAt(0);
1720 Location right = locations->InAt(1);
1721 IfCondition if_cond = cond->GetCondition();
1722
1723 Register left_high = left.AsRegisterPairHigh<Register>();
1724 Register left_low = left.AsRegisterPairLow<Register>();
1725 IfCondition true_high_cond = if_cond;
1726 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001727 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001728
1729 // Set the conditions for the test, remembering that == needs to be
1730 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001731 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001732 switch (if_cond) {
1733 case kCondEQ:
1734 case kCondNE:
1735 // Nothing to do.
1736 break;
1737 case kCondLT:
1738 false_high_cond = kCondGT;
1739 break;
1740 case kCondLE:
1741 true_high_cond = kCondLT;
1742 break;
1743 case kCondGT:
1744 false_high_cond = kCondLT;
1745 break;
1746 case kCondGE:
1747 true_high_cond = kCondGT;
1748 break;
Aart Bike9f37602015-10-09 11:15:55 -07001749 case kCondB:
1750 false_high_cond = kCondA;
1751 break;
1752 case kCondBE:
1753 true_high_cond = kCondB;
1754 break;
1755 case kCondA:
1756 false_high_cond = kCondB;
1757 break;
1758 case kCondAE:
1759 true_high_cond = kCondA;
1760 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 }
1762 if (right.IsConstant()) {
1763 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1764 int32_t val_low = Low32Bits(value);
1765 int32_t val_high = High32Bits(value);
1766
Vladimir Markoac6ac102015-12-17 12:14:00 +00001767 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001768 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001769 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001770 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001771 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001773 __ b(true_label, ARMCondition(true_high_cond));
1774 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001775 }
1776 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001777 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 } else {
1779 Register right_high = right.AsRegisterPairHigh<Register>();
1780 Register right_low = right.AsRegisterPairLow<Register>();
1781
1782 __ cmp(left_high, ShifterOperand(right_high));
1783 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001784 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001786 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001787 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001788 __ b(true_label, ARMCondition(true_high_cond));
1789 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001790 }
1791 // Must be equal high, so compare the lows.
1792 __ cmp(left_low, ShifterOperand(right_low));
1793 }
1794 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001795 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001796 __ b(true_label, final_condition);
1797}
1798
David Brazdil0debae72015-11-12 18:37:00 +00001799void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1800 Label* true_target_in,
1801 Label* false_target_in) {
1802 // Generated branching requires both targets to be explicit. If either of the
1803 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1804 Label fallthrough_target;
1805 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1806 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1807
Roland Levillain4fa13f62015-07-06 18:11:54 +01001808 Primitive::Type type = condition->InputAt(0)->GetType();
1809 switch (type) {
1810 case Primitive::kPrimLong:
1811 GenerateLongComparesAndJumps(condition, true_target, false_target);
1812 break;
1813 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001814 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001815 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 GenerateFPJumps(condition, true_target, false_target);
1817 break;
1818 default:
1819 LOG(FATAL) << "Unexpected compare type " << type;
1820 }
1821
David Brazdil0debae72015-11-12 18:37:00 +00001822 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001823 __ b(false_target);
1824 }
David Brazdil0debae72015-11-12 18:37:00 +00001825
1826 if (fallthrough_target.IsLinked()) {
1827 __ Bind(&fallthrough_target);
1828 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001829}
1830
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001831void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001832 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001833 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001834 Label* false_target) {
1835 HInstruction* cond = instruction->InputAt(condition_input_index);
1836
1837 if (true_target == nullptr && false_target == nullptr) {
1838 // Nothing to do. The code always falls through.
1839 return;
1840 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001841 // Constant condition, statically compared against "true" (integer value 1).
1842 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001843 if (true_target != nullptr) {
1844 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001845 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001846 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001847 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001848 if (false_target != nullptr) {
1849 __ b(false_target);
1850 }
1851 }
1852 return;
1853 }
1854
1855 // The following code generates these patterns:
1856 // (1) true_target == nullptr && false_target != nullptr
1857 // - opposite condition true => branch to false_target
1858 // (2) true_target != nullptr && false_target == nullptr
1859 // - condition true => branch to true_target
1860 // (3) true_target != nullptr && false_target != nullptr
1861 // - condition true => branch to true_target
1862 // - branch to false_target
1863 if (IsBooleanValueOrMaterializedCondition(cond)) {
1864 // Condition has been materialized, compare the output to 0.
1865 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1866 DCHECK(cond_val.IsRegister());
1867 if (true_target == nullptr) {
1868 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1869 } else {
1870 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001872 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001873 // Condition has not been materialized. Use its inputs as the comparison and
1874 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001875 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001876
1877 // If this is a long or FP comparison that has been folded into
1878 // the HCondition, generate the comparison directly.
1879 Primitive::Type type = condition->InputAt(0)->GetType();
1880 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1881 GenerateCompareTestAndBranch(condition, true_target, false_target);
1882 return;
1883 }
1884
1885 LocationSummary* locations = cond->GetLocations();
1886 DCHECK(locations->InAt(0).IsRegister());
1887 Register left = locations->InAt(0).AsRegister<Register>();
1888 Location right = locations->InAt(1);
1889 if (right.IsRegister()) {
1890 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001891 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001892 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001893 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001894 }
1895 if (true_target == nullptr) {
1896 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1897 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001898 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001899 }
Dave Allison20dfc792014-06-16 20:44:29 -07001900 }
David Brazdil0debae72015-11-12 18:37:00 +00001901
1902 // If neither branch falls through (case 3), the conditional branch to `true_target`
1903 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1904 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001905 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906 }
1907}
1908
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001909void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1911 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001912 locations->SetInAt(0, Location::RequiresRegister());
1913 }
1914}
1915
1916void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001917 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1918 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1919 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1920 nullptr : codegen_->GetLabelOf(true_successor);
1921 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1922 nullptr : codegen_->GetLabelOf(false_successor);
1923 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001924}
1925
1926void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1927 LocationSummary* locations = new (GetGraph()->GetArena())
1928 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001929 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001930 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001931 locations->SetInAt(0, Location::RequiresRegister());
1932 }
1933}
1934
1935void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001936 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001937 GenerateTestAndBranch(deoptimize,
1938 /* condition_input_index */ 0,
1939 slow_path->GetEntryLabel(),
1940 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001941}
Dave Allison20dfc792014-06-16 20:44:29 -07001942
David Brazdil74eb1b22015-12-14 11:44:01 +00001943void LocationsBuilderARM::VisitSelect(HSelect* select) {
1944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1945 if (Primitive::IsFloatingPointType(select->GetType())) {
1946 locations->SetInAt(0, Location::RequiresFpuRegister());
1947 locations->SetInAt(1, Location::RequiresFpuRegister());
1948 } else {
1949 locations->SetInAt(0, Location::RequiresRegister());
1950 locations->SetInAt(1, Location::RequiresRegister());
1951 }
1952 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1953 locations->SetInAt(2, Location::RequiresRegister());
1954 }
1955 locations->SetOut(Location::SameAsFirstInput());
1956}
1957
1958void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1959 LocationSummary* locations = select->GetLocations();
1960 Label false_target;
1961 GenerateTestAndBranch(select,
1962 /* condition_input_index */ 2,
1963 /* true_target */ nullptr,
1964 &false_target);
1965 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1966 __ Bind(&false_target);
1967}
1968
David Srbecky0cf44932015-12-09 14:09:59 +00001969void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1970 new (GetGraph()->GetArena()) LocationSummary(info);
1971}
1972
David Srbeckyd28f4a02016-03-14 17:14:24 +00001973void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1974 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001975}
1976
1977void CodeGeneratorARM::GenerateNop() {
1978 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001979}
1980
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001982 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001983 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001984 // Handle the long/FP comparisons made in instruction simplification.
1985 switch (cond->InputAt(0)->GetType()) {
1986 case Primitive::kPrimLong:
1987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001989 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001990 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1991 }
1992 break;
1993
1994 case Primitive::kPrimFloat:
1995 case Primitive::kPrimDouble:
1996 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001997 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001998 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2000 }
2001 break;
2002
2003 default:
2004 locations->SetInAt(0, Location::RequiresRegister());
2005 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002006 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2008 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010}
2011
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002013 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002014 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002015 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002016
2017 LocationSummary* locations = cond->GetLocations();
2018 Location left = locations->InAt(0);
2019 Location right = locations->InAt(1);
2020 Register out = locations->Out().AsRegister<Register>();
2021 Label true_label, false_label;
2022
2023 switch (cond->InputAt(0)->GetType()) {
2024 default: {
2025 // Integer case.
2026 if (right.IsRegister()) {
2027 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2028 } else {
2029 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002030 __ CmpConstant(left.AsRegister<Register>(),
2031 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002032 }
Aart Bike9f37602015-10-09 11:15:55 -07002033 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002034 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002035 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002036 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002037 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002038 return;
2039 }
2040 case Primitive::kPrimLong:
2041 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2042 break;
2043 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002044 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002045 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002046 GenerateFPJumps(cond, &true_label, &false_label);
2047 break;
2048 }
2049
2050 // Convert the jumps into the result.
2051 Label done_label;
2052
2053 // False case: result = 0.
2054 __ Bind(&false_label);
2055 __ LoadImmediate(out, 0);
2056 __ b(&done_label);
2057
2058 // True case: result = 1.
2059 __ Bind(&true_label);
2060 __ LoadImmediate(out, 1);
2061 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002062}
2063
2064void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002066}
2067
2068void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002069 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002070}
2071
2072void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002073 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002074}
2075
2076void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002078}
2079
2080void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002081 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002082}
2083
2084void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002086}
2087
2088void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002089 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002090}
2091
2092void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002094}
2095
2096void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002097 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002098}
2099
2100void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002101 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002102}
2103
2104void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002106}
2107
2108void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002109 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002110}
2111
Aart Bike9f37602015-10-09 11:15:55 -07002112void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002114}
2115
2116void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002118}
2119
2120void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002122}
2123
2124void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002125 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002126}
2127
2128void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002129 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002130}
2131
2132void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002133 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002134}
2135
2136void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002137 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002138}
2139
2140void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002141 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002142}
2143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002145 LocationSummary* locations =
2146 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002147 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148}
2149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002150void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002151 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152}
2153
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2155 LocationSummary* locations =
2156 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2157 locations->SetOut(Location::ConstantLocation(constant));
2158}
2159
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002160void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002162}
2163
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002165 LocationSummary* locations =
2166 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002167 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168}
2169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002170void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 // Will be generated at use site.
2172}
2173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002174void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2175 LocationSummary* locations =
2176 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2177 locations->SetOut(Location::ConstantLocation(constant));
2178}
2179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002180void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181 // Will be generated at use site.
2182}
2183
2184void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2187 locations->SetOut(Location::ConstantLocation(constant));
2188}
2189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002190void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002191 // Will be generated at use site.
2192}
2193
Calin Juravle27df7582015-04-17 19:12:31 +01002194void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2195 memory_barrier->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002199 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002200}
2201
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002203 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002204}
2205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002206void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002207 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002208}
2209
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002213 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002214}
2215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002216void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002217 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218}
2219
Calin Juravle175dc732015-08-25 15:42:32 +01002220void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2221 // The trampoline uses the same calling convention as dex calling conventions,
2222 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2223 // the method_idx.
2224 HandleInvoke(invoke);
2225}
2226
2227void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2228 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2229}
2230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002231void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002232 // Explicit clinit checks triggered by static invokes must have been pruned by
2233 // art::PrepareForRegisterAllocation.
2234 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002235
Vladimir Marko68c981f2016-08-26 13:13:33 +01002236 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002237 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002238 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2239 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2240 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002241 return;
2242 }
2243
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002244 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002245
2246 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2247 if (invoke->HasPcRelativeDexCache()) {
2248 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2249 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002250}
2251
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002252static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2253 if (invoke->GetLocations()->Intrinsified()) {
2254 IntrinsicCodeGeneratorARM intrinsic(codegen);
2255 intrinsic.Dispatch(invoke);
2256 return true;
2257 }
2258 return false;
2259}
2260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002261void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002262 // Explicit clinit checks triggered by static invokes must have been pruned by
2263 // art::PrepareForRegisterAllocation.
2264 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002265
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002266 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2267 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002268 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002269
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002270 LocationSummary* locations = invoke->GetLocations();
2271 codegen_->GenerateStaticOrDirectCall(
2272 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002273 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002274}
2275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002277 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002278 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002279}
2280
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002282 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002283 if (intrinsic.TryDispatch(invoke)) {
2284 return;
2285 }
2286
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 HandleInvoke(invoke);
2288}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002289
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002290void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002291 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2292 return;
2293 }
2294
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002295 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002296 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002297 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002298}
2299
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2301 HandleInvoke(invoke);
2302 // Add the hidden argument.
2303 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2304}
2305
2306void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2307 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002308 LocationSummary* locations = invoke->GetLocations();
2309 Register temp = locations->GetTemp(0).AsRegister<Register>();
2310 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002311 Location receiver = locations->InAt(0);
2312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2313
Roland Levillain3b359c72015-11-17 19:35:12 +00002314 // Set the hidden argument. This is safe to do this here, as R12
2315 // won't be modified thereafter, before the `blx` (call) instruction.
2316 DCHECK_EQ(R12, hidden_reg);
2317 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002318
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002319 if (receiver.IsStackSlot()) {
2320 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002321 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002322 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2323 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002324 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002327 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002328 // Instead of simply (possibly) unpoisoning `temp` here, we should
2329 // emit a read barrier for the previous class reference load.
2330 // However this is not required in practice, as this is an
2331 // intermediate/temporary reference and because the current
2332 // concurrent copying collector keeps the from-space memory
2333 // intact/accessible until the end of the marking phase (the
2334 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002335 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002336 __ LoadFromOffset(kLoadWord, temp, temp,
2337 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2338 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002339 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002340 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002341 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002342 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002343 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002344 // LR = temp->GetEntryPoint();
2345 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2346 // LR();
2347 __ blx(LR);
2348 DCHECK(!codegen_->IsLeafMethod());
2349 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2350}
2351
Roland Levillain88cb1752014-10-20 16:36:47 +01002352void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2355 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002356 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002357 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2359 break;
2360 }
2361 case Primitive::kPrimLong: {
2362 locations->SetInAt(0, Location::RequiresRegister());
2363 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002364 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002365 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002366
Roland Levillain88cb1752014-10-20 16:36:47 +01002367 case Primitive::kPrimFloat:
2368 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2375 }
2376}
2377
2378void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2379 LocationSummary* locations = neg->GetLocations();
2380 Location out = locations->Out();
2381 Location in = locations->InAt(0);
2382 switch (neg->GetResultType()) {
2383 case Primitive::kPrimInt:
2384 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002386 break;
2387
2388 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002389 DCHECK(in.IsRegisterPair());
2390 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2391 __ rsbs(out.AsRegisterPairLow<Register>(),
2392 in.AsRegisterPairLow<Register>(),
2393 ShifterOperand(0));
2394 // We cannot emit an RSC (Reverse Subtract with Carry)
2395 // instruction here, as it does not exist in the Thumb-2
2396 // instruction set. We use the following approach
2397 // using SBC and SUB instead.
2398 //
2399 // out.hi = -C
2400 __ sbc(out.AsRegisterPairHigh<Register>(),
2401 out.AsRegisterPairHigh<Register>(),
2402 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2403 // out.hi = out.hi - in.hi
2404 __ sub(out.AsRegisterPairHigh<Register>(),
2405 out.AsRegisterPairHigh<Register>(),
2406 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2407 break;
2408
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002410 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002412 break;
2413
Roland Levillain88cb1752014-10-20 16:36:47 +01002414 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002415 DCHECK(in.IsFpuRegisterPair());
2416 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2417 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2422 }
2423}
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 Primitive::Type result_type = conversion->GetResultType();
2427 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002428 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002429
Roland Levillain5b3ee562015-04-14 16:02:41 +01002430 // The float-to-long, double-to-long and long-to-float type conversions
2431 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002432 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002433 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2434 && result_type == Primitive::kPrimLong)
2435 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002436 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002437 : LocationSummary::kNoCall;
2438 LocationSummary* locations =
2439 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2440
David Brazdilb2bd1c52015-03-25 11:17:37 +00002441 // The Java language does not allow treating boolean as an integral type but
2442 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002445 case Primitive::kPrimByte:
2446 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002447 case Primitive::kPrimLong:
2448 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002449 case Primitive::kPrimBoolean:
2450 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002451 case Primitive::kPrimShort:
2452 case Primitive::kPrimInt:
2453 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002454 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2457 break;
2458
2459 default:
2460 LOG(FATAL) << "Unexpected type conversion from " << input_type
2461 << " to " << result_type;
2462 }
2463 break;
2464
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 case Primitive::kPrimShort:
2466 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002467 case Primitive::kPrimLong:
2468 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002469 case Primitive::kPrimBoolean:
2470 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002471 case Primitive::kPrimByte:
2472 case Primitive::kPrimInt:
2473 case Primitive::kPrimChar:
2474 // Processing a Dex `int-to-short' instruction.
2475 locations->SetInAt(0, Location::RequiresRegister());
2476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2477 break;
2478
2479 default:
2480 LOG(FATAL) << "Unexpected type conversion from " << input_type
2481 << " to " << result_type;
2482 }
2483 break;
2484
Roland Levillain946e1432014-11-11 17:35:19 +00002485 case Primitive::kPrimInt:
2486 switch (input_type) {
2487 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002488 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002489 locations->SetInAt(0, Location::Any());
2490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2491 break;
2492
2493 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002494 // Processing a Dex `float-to-int' instruction.
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresRegister());
2497 locations->AddTemp(Location::RequiresFpuRegister());
2498 break;
2499
Roland Levillain946e1432014-11-11 17:35:19 +00002500 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002501 // Processing a Dex `double-to-int' instruction.
2502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresRegister());
2504 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511 break;
2512
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 case Primitive::kPrimLong:
2514 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002515 case Primitive::kPrimBoolean:
2516 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 case Primitive::kPrimByte:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002520 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002521 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 locations->SetInAt(0, Location::RequiresRegister());
2523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 break;
2525
Roland Levillain624279f2014-12-04 11:54:28 +00002526 case Primitive::kPrimFloat: {
2527 // Processing a Dex `float-to-long' instruction.
2528 InvokeRuntimeCallingConvention calling_convention;
2529 locations->SetInAt(0, Location::FpuRegisterLocation(
2530 calling_convention.GetFpuRegisterAt(0)));
2531 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2532 break;
2533 }
2534
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002535 case Primitive::kPrimDouble: {
2536 // Processing a Dex `double-to-long' instruction.
2537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2539 calling_convention.GetFpuRegisterAt(0),
2540 calling_convention.GetFpuRegisterAt(1)));
2541 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002542 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002543 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 }
2549 break;
2550
Roland Levillain981e4542014-11-14 11:47:14 +00002551 case Primitive::kPrimChar:
2552 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002553 case Primitive::kPrimLong:
2554 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002555 case Primitive::kPrimBoolean:
2556 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002557 case Primitive::kPrimByte:
2558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002560 // Processing a Dex `int-to-char' instruction.
2561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
Roland Levillaindff1f282014-11-05 14:15:05 +00002571 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002572 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002573 case Primitive::kPrimBoolean:
2574 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002575 case Primitive::kPrimByte:
2576 case Primitive::kPrimShort:
2577 case Primitive::kPrimInt:
2578 case Primitive::kPrimChar:
2579 // Processing a Dex `int-to-float' instruction.
2580 locations->SetInAt(0, Location::RequiresRegister());
2581 locations->SetOut(Location::RequiresFpuRegister());
2582 break;
2583
Roland Levillain5b3ee562015-04-14 16:02:41 +01002584 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002585 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002586 InvokeRuntimeCallingConvention calling_convention;
2587 locations->SetInAt(0, Location::RegisterPairLocation(
2588 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2589 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002590 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002591 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002592
Roland Levillaincff13742014-11-17 14:32:17 +00002593 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002594 // Processing a Dex `double-to-float' instruction.
2595 locations->SetInAt(0, Location::RequiresFpuRegister());
2596 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 };
2603 break;
2604
Roland Levillaindff1f282014-11-05 14:15:05 +00002605 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002606 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002607 case Primitive::kPrimBoolean:
2608 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002609 case Primitive::kPrimByte:
2610 case Primitive::kPrimShort:
2611 case Primitive::kPrimInt:
2612 case Primitive::kPrimChar:
2613 // Processing a Dex `int-to-double' instruction.
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetOut(Location::RequiresFpuRegister());
2616 break;
2617
2618 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002619 // Processing a Dex `long-to-double' instruction.
2620 locations->SetInAt(0, Location::RequiresRegister());
2621 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002622 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002623 locations->AddTemp(Location::RequiresFpuRegister());
2624 break;
2625
Roland Levillaincff13742014-11-17 14:32:17 +00002626 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002627 // Processing a Dex `float-to-double' instruction.
2628 locations->SetInAt(0, Location::RequiresFpuRegister());
2629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002636 break;
2637
2638 default:
2639 LOG(FATAL) << "Unexpected type conversion from " << input_type
2640 << " to " << result_type;
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2645 LocationSummary* locations = conversion->GetLocations();
2646 Location out = locations->Out();
2647 Location in = locations->InAt(0);
2648 Primitive::Type result_type = conversion->GetResultType();
2649 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002650 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002651 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002652 case Primitive::kPrimByte:
2653 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002654 case Primitive::kPrimLong:
2655 // Type conversion from long to byte is a result of code transformations.
2656 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2657 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002658 case Primitive::kPrimBoolean:
2659 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
2662 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002663 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002665 break;
2666
2667 default:
2668 LOG(FATAL) << "Unexpected type conversion from " << input_type
2669 << " to " << result_type;
2670 }
2671 break;
2672
Roland Levillain01a8d712014-11-14 16:27:39 +00002673 case Primitive::kPrimShort:
2674 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002675 case Primitive::kPrimLong:
2676 // Type conversion from long to short is a result of code transformations.
2677 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2678 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
2684 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 break;
2687
2688 default:
2689 LOG(FATAL) << "Unexpected type conversion from " << input_type
2690 << " to " << result_type;
2691 }
2692 break;
2693
Roland Levillain946e1432014-11-11 17:35:19 +00002694 case Primitive::kPrimInt:
2695 switch (input_type) {
2696 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002697 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002698 DCHECK(out.IsRegister());
2699 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002701 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002703 } else {
2704 DCHECK(in.IsConstant());
2705 DCHECK(in.GetConstant()->IsLongConstant());
2706 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002708 }
2709 break;
2710
Roland Levillain3f8f9362014-12-02 17:45:01 +00002711 case Primitive::kPrimFloat: {
2712 // Processing a Dex `float-to-int' instruction.
2713 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002714 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002715 __ vmovrs(out.AsRegister<Register>(), temp);
2716 break;
2717 }
2718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002719 case Primitive::kPrimDouble: {
2720 // Processing a Dex `double-to-int' instruction.
2721 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002722 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002723 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002724 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002725 }
Roland Levillain946e1432014-11-11 17:35:19 +00002726
2727 default:
2728 LOG(FATAL) << "Unexpected type conversion from " << input_type
2729 << " to " << result_type;
2730 }
2731 break;
2732
Roland Levillaindff1f282014-11-05 14:15:05 +00002733 case Primitive::kPrimLong:
2734 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002735 case Primitive::kPrimBoolean:
2736 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002737 case Primitive::kPrimByte:
2738 case Primitive::kPrimShort:
2739 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002740 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002741 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002742 DCHECK(out.IsRegisterPair());
2743 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 // Sign extension.
2746 __ Asr(out.AsRegisterPairHigh<Register>(),
2747 out.AsRegisterPairLow<Register>(),
2748 31);
2749 break;
2750
2751 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002752 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002753 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002754 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002755 break;
2756
Roland Levillaindff1f282014-11-05 14:15:05 +00002757 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002758 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002759 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002760 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002761 break;
2762
2763 default:
2764 LOG(FATAL) << "Unexpected type conversion from " << input_type
2765 << " to " << result_type;
2766 }
2767 break;
2768
Roland Levillain981e4542014-11-14 11:47:14 +00002769 case Primitive::kPrimChar:
2770 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 case Primitive::kPrimLong:
2772 // Type conversion from long to char is a result of code transformations.
2773 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2774 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002775 case Primitive::kPrimBoolean:
2776 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002777 case Primitive::kPrimByte:
2778 case Primitive::kPrimShort:
2779 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002780 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002782 break;
2783
2784 default:
2785 LOG(FATAL) << "Unexpected type conversion from " << input_type
2786 << " to " << result_type;
2787 }
2788 break;
2789
Roland Levillaindff1f282014-11-05 14:15:05 +00002790 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002791 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002792 case Primitive::kPrimBoolean:
2793 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002794 case Primitive::kPrimByte:
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimInt:
2797 case Primitive::kPrimChar: {
2798 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2800 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002801 break;
2802 }
2803
Roland Levillain5b3ee562015-04-14 16:02:41 +01002804 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002805 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002806 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002807 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002808 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002809
Roland Levillaincff13742014-11-17 14:32:17 +00002810 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002811 // Processing a Dex `double-to-float' instruction.
2812 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2813 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 };
2820 break;
2821
Roland Levillaindff1f282014-11-05 14:15:05 +00002822 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002823 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002824 case Primitive::kPrimBoolean:
2825 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002826 case Primitive::kPrimByte:
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimInt:
2829 case Primitive::kPrimChar: {
2830 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002832 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2833 out.AsFpuRegisterPairLow<SRegister>());
2834 break;
2835 }
2836
Roland Levillain647b9ed2014-11-27 12:06:00 +00002837 case Primitive::kPrimLong: {
2838 // Processing a Dex `long-to-double' instruction.
2839 Register low = in.AsRegisterPairLow<Register>();
2840 Register high = in.AsRegisterPairHigh<Register>();
2841 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2842 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002843 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002844 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002845 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2846 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002847
Roland Levillain682393c2015-04-14 15:57:52 +01002848 // temp_d = int-to-double(high)
2849 __ vmovsr(temp_s, high);
2850 __ vcvtdi(temp_d, temp_s);
2851 // constant_d = k2Pow32EncodingForDouble
2852 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2853 // out_d = unsigned-to-double(low)
2854 __ vmovsr(out_s, low);
2855 __ vcvtdu(out_d, out_s);
2856 // out_d += temp_d * constant_d
2857 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002858 break;
2859 }
2860
Roland Levillaincff13742014-11-17 14:32:17 +00002861 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002862 // Processing a Dex `float-to-double' instruction.
2863 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2864 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002865 break;
2866
2867 default:
2868 LOG(FATAL) << "Unexpected type conversion from " << input_type
2869 << " to " << result_type;
2870 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
2876 }
2877}
2878
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002879void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002882 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002883 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002884 locations->SetInAt(0, Location::RequiresRegister());
2885 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2887 break;
2888 }
2889
2890 case Primitive::kPrimLong: {
2891 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002892 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002894 break;
2895 }
2896
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002897 case Primitive::kPrimFloat:
2898 case Primitive::kPrimDouble: {
2899 locations->SetInAt(0, Location::RequiresFpuRegister());
2900 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002902 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002903 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002905 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002907 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002908}
2909
2910void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2911 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002912 Location out = locations->Out();
2913 Location first = locations->InAt(0);
2914 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002915 switch (add->GetResultType()) {
2916 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002917 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002918 __ add(out.AsRegister<Register>(),
2919 first.AsRegister<Register>(),
2920 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002922 __ AddConstant(out.AsRegister<Register>(),
2923 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002924 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002925 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002928 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002929 if (second.IsConstant()) {
2930 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2931 GenerateAddLongConst(out, first, value);
2932 } else {
2933 DCHECK(second.IsRegisterPair());
2934 __ adds(out.AsRegisterPairLow<Register>(),
2935 first.AsRegisterPairLow<Register>(),
2936 ShifterOperand(second.AsRegisterPairLow<Register>()));
2937 __ adc(out.AsRegisterPairHigh<Register>(),
2938 first.AsRegisterPairHigh<Register>(),
2939 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002945 __ vadds(out.AsFpuRegister<SRegister>(),
2946 first.AsFpuRegister<SRegister>(),
2947 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948 break;
2949
2950 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002951 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2952 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2953 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002954 break;
2955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 }
2959}
2960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002961void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002964 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002965 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2969 break;
2970 }
2971
2972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002974 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002976 break;
2977 }
Calin Juravle11351682014-10-23 15:38:15 +01002978 case Primitive::kPrimFloat:
2979 case Primitive::kPrimDouble: {
2980 locations->SetInAt(0, Location::RequiresFpuRegister());
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002983 break;
Calin Juravle11351682014-10-23 15:38:15 +01002984 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002985 default:
Calin Juravle11351682014-10-23 15:38:15 +01002986 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002987 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002988}
2989
2990void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2991 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002992 Location out = locations->Out();
2993 Location first = locations->InAt(0);
2994 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002995 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002996 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002997 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002998 __ sub(out.AsRegister<Register>(),
2999 first.AsRegister<Register>(),
3000 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003001 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ AddConstant(out.AsRegister<Register>(),
3003 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003004 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003005 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008
Calin Juravle11351682014-10-23 15:38:15 +01003009 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003010 if (second.IsConstant()) {
3011 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3012 GenerateAddLongConst(out, first, -value);
3013 } else {
3014 DCHECK(second.IsRegisterPair());
3015 __ subs(out.AsRegisterPairLow<Register>(),
3016 first.AsRegisterPairLow<Register>(),
3017 ShifterOperand(second.AsRegisterPairLow<Register>()));
3018 __ sbc(out.AsRegisterPairHigh<Register>(),
3019 first.AsRegisterPairHigh<Register>(),
3020 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003022 break;
Calin Juravle11351682014-10-23 15:38:15 +01003023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024
Calin Juravle11351682014-10-23 15:38:15 +01003025 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003026 __ vsubs(out.AsFpuRegister<SRegister>(),
3027 first.AsFpuRegister<SRegister>(),
3028 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029 break;
Calin Juravle11351682014-10-23 15:38:15 +01003030 }
3031
3032 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003033 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3034 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3035 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003036 break;
3037 }
3038
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 default:
Calin Juravle11351682014-10-23 15:38:15 +01003041 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003042 }
3043}
3044
Calin Juravle34bacdf2014-10-07 20:23:36 +01003045void LocationsBuilderARM::VisitMul(HMul* mul) {
3046 LocationSummary* locations =
3047 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3048 switch (mul->GetResultType()) {
3049 case Primitive::kPrimInt:
3050 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003051 locations->SetInAt(0, Location::RequiresRegister());
3052 locations->SetInAt(1, Location::RequiresRegister());
3053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003054 break;
3055 }
3056
Calin Juravleb5bfa962014-10-21 18:02:24 +01003057 case Primitive::kPrimFloat:
3058 case Primitive::kPrimDouble: {
3059 locations->SetInAt(0, Location::RequiresFpuRegister());
3060 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003061 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003062 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003063 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064
3065 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003066 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003067 }
3068}
3069
3070void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3071 LocationSummary* locations = mul->GetLocations();
3072 Location out = locations->Out();
3073 Location first = locations->InAt(0);
3074 Location second = locations->InAt(1);
3075 switch (mul->GetResultType()) {
3076 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003077 __ mul(out.AsRegister<Register>(),
3078 first.AsRegister<Register>(),
3079 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080 break;
3081 }
3082 case Primitive::kPrimLong: {
3083 Register out_hi = out.AsRegisterPairHigh<Register>();
3084 Register out_lo = out.AsRegisterPairLow<Register>();
3085 Register in1_hi = first.AsRegisterPairHigh<Register>();
3086 Register in1_lo = first.AsRegisterPairLow<Register>();
3087 Register in2_hi = second.AsRegisterPairHigh<Register>();
3088 Register in2_lo = second.AsRegisterPairLow<Register>();
3089
3090 // Extra checks to protect caused by the existence of R1_R2.
3091 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3092 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3093 DCHECK_NE(out_hi, in1_lo);
3094 DCHECK_NE(out_hi, in2_lo);
3095
3096 // input: in1 - 64 bits, in2 - 64 bits
3097 // output: out
3098 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3099 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3100 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3101
3102 // IP <- in1.lo * in2.hi
3103 __ mul(IP, in1_lo, in2_hi);
3104 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3105 __ mla(out_hi, in1_hi, in2_lo, IP);
3106 // out.lo <- (in1.lo * in2.lo)[31:0];
3107 __ umull(out_lo, IP, in1_lo, in2_lo);
3108 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3109 __ add(out_hi, out_hi, ShifterOperand(IP));
3110 break;
3111 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112
3113 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003114 __ vmuls(out.AsFpuRegister<SRegister>(),
3115 first.AsFpuRegister<SRegister>(),
3116 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003117 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 }
3119
3120 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003121 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3122 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3123 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 break;
3125 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126
3127 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 }
3130}
3131
Zheng Xuc6667102015-05-15 16:08:45 +08003132void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3133 DCHECK(instruction->IsDiv() || instruction->IsRem());
3134 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3135
3136 LocationSummary* locations = instruction->GetLocations();
3137 Location second = locations->InAt(1);
3138 DCHECK(second.IsConstant());
3139
3140 Register out = locations->Out().AsRegister<Register>();
3141 Register dividend = locations->InAt(0).AsRegister<Register>();
3142 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3143 DCHECK(imm == 1 || imm == -1);
3144
3145 if (instruction->IsRem()) {
3146 __ LoadImmediate(out, 0);
3147 } else {
3148 if (imm == 1) {
3149 __ Mov(out, dividend);
3150 } else {
3151 __ rsb(out, dividend, ShifterOperand(0));
3152 }
3153 }
3154}
3155
3156void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3157 DCHECK(instruction->IsDiv() || instruction->IsRem());
3158 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3159
3160 LocationSummary* locations = instruction->GetLocations();
3161 Location second = locations->InAt(1);
3162 DCHECK(second.IsConstant());
3163
3164 Register out = locations->Out().AsRegister<Register>();
3165 Register dividend = locations->InAt(0).AsRegister<Register>();
3166 Register temp = locations->GetTemp(0).AsRegister<Register>();
3167 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003168 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003169 int ctz_imm = CTZ(abs_imm);
3170
3171 if (ctz_imm == 1) {
3172 __ Lsr(temp, dividend, 32 - ctz_imm);
3173 } else {
3174 __ Asr(temp, dividend, 31);
3175 __ Lsr(temp, temp, 32 - ctz_imm);
3176 }
3177 __ add(out, temp, ShifterOperand(dividend));
3178
3179 if (instruction->IsDiv()) {
3180 __ Asr(out, out, ctz_imm);
3181 if (imm < 0) {
3182 __ rsb(out, out, ShifterOperand(0));
3183 }
3184 } else {
3185 __ ubfx(out, out, 0, ctz_imm);
3186 __ sub(out, out, ShifterOperand(temp));
3187 }
3188}
3189
3190void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3191 DCHECK(instruction->IsDiv() || instruction->IsRem());
3192 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3193
3194 LocationSummary* locations = instruction->GetLocations();
3195 Location second = locations->InAt(1);
3196 DCHECK(second.IsConstant());
3197
3198 Register out = locations->Out().AsRegister<Register>();
3199 Register dividend = locations->InAt(0).AsRegister<Register>();
3200 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3201 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3202 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3203
3204 int64_t magic;
3205 int shift;
3206 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3207
3208 __ LoadImmediate(temp1, magic);
3209 __ smull(temp2, temp1, dividend, temp1);
3210
3211 if (imm > 0 && magic < 0) {
3212 __ add(temp1, temp1, ShifterOperand(dividend));
3213 } else if (imm < 0 && magic > 0) {
3214 __ sub(temp1, temp1, ShifterOperand(dividend));
3215 }
3216
3217 if (shift != 0) {
3218 __ Asr(temp1, temp1, shift);
3219 }
3220
3221 if (instruction->IsDiv()) {
3222 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3223 } else {
3224 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3225 // TODO: Strength reduction for mls.
3226 __ LoadImmediate(temp2, imm);
3227 __ mls(out, temp1, temp2, dividend);
3228 }
3229}
3230
3231void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3232 DCHECK(instruction->IsDiv() || instruction->IsRem());
3233 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 Location second = locations->InAt(1);
3237 DCHECK(second.IsConstant());
3238
3239 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3240 if (imm == 0) {
3241 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3242 } else if (imm == 1 || imm == -1) {
3243 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003244 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003245 DivRemByPowerOfTwo(instruction);
3246 } else {
3247 DCHECK(imm <= -2 || imm >= 2);
3248 GenerateDivRemWithAnyConstant(instruction);
3249 }
3250}
3251
Calin Juravle7c4954d2014-10-28 16:57:40 +00003252void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003253 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3254 if (div->GetResultType() == Primitive::kPrimLong) {
3255 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003256 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003257 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3258 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003259 } else if (div->GetResultType() == Primitive::kPrimInt &&
3260 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3261 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003262 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003263 }
3264
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3266
Calin Juravle7c4954d2014-10-28 16:57:40 +00003267 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003268 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003269 if (div->InputAt(1)->IsConstant()) {
3270 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003271 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003273 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3274 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003275 // No temp register required.
3276 } else {
3277 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003278 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003279 locations->AddTemp(Location::RequiresRegister());
3280 }
3281 }
3282 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::RequiresRegister());
3285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3286 } else {
3287 InvokeRuntimeCallingConvention calling_convention;
3288 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3289 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3290 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3291 // we only need the former.
3292 locations->SetOut(Location::RegisterLocation(R0));
3293 }
Calin Juravled0d48522014-11-04 16:40:20 +00003294 break;
3295 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003296 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003297 InvokeRuntimeCallingConvention calling_convention;
3298 locations->SetInAt(0, Location::RegisterPairLocation(
3299 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3300 locations->SetInAt(1, Location::RegisterPairLocation(
3301 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003302 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003303 break;
3304 }
3305 case Primitive::kPrimFloat:
3306 case Primitive::kPrimDouble: {
3307 locations->SetInAt(0, Location::RequiresFpuRegister());
3308 locations->SetInAt(1, Location::RequiresFpuRegister());
3309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3310 break;
3311 }
3312
3313 default:
3314 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3315 }
3316}
3317
3318void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3319 LocationSummary* locations = div->GetLocations();
3320 Location out = locations->Out();
3321 Location first = locations->InAt(0);
3322 Location second = locations->InAt(1);
3323
3324 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003325 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003326 if (second.IsConstant()) {
3327 GenerateDivRemConstantIntegral(div);
3328 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003329 __ sdiv(out.AsRegister<Register>(),
3330 first.AsRegister<Register>(),
3331 second.AsRegister<Register>());
3332 } else {
3333 InvokeRuntimeCallingConvention calling_convention;
3334 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3335 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3336 DCHECK_EQ(R0, out.AsRegister<Register>());
3337
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003338 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003339 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003340 }
Calin Juravled0d48522014-11-04 16:40:20 +00003341 break;
3342 }
3343
Calin Juravle7c4954d2014-10-28 16:57:40 +00003344 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003345 InvokeRuntimeCallingConvention calling_convention;
3346 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3347 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3348 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3349 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3350 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003351 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003352
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003353 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003354 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003355 break;
3356 }
3357
3358 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003359 __ vdivs(out.AsFpuRegister<SRegister>(),
3360 first.AsFpuRegister<SRegister>(),
3361 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003362 break;
3363 }
3364
3365 case Primitive::kPrimDouble: {
3366 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3367 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3368 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3369 break;
3370 }
3371
3372 default:
3373 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3374 }
3375}
3376
Calin Juravlebacfec32014-11-14 15:54:36 +00003377void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003378 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003379
3380 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003381 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003382 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3383 // sdiv will be replaced by other instruction sequence.
3384 call_kind = LocationSummary::kNoCall;
3385 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3386 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003387 // Have hardware divide instruction for int, do it with three instructions.
3388 call_kind = LocationSummary::kNoCall;
3389 }
3390
Calin Juravlebacfec32014-11-14 15:54:36 +00003391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3392
Calin Juravled2ec87d2014-12-08 14:24:46 +00003393 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003394 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003395 if (rem->InputAt(1)->IsConstant()) {
3396 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003397 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003399 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3400 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003401 // No temp register required.
3402 } else {
3403 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003404 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003405 locations->AddTemp(Location::RequiresRegister());
3406 }
3407 }
3408 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003409 locations->SetInAt(0, Location::RequiresRegister());
3410 locations->SetInAt(1, Location::RequiresRegister());
3411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3412 locations->AddTemp(Location::RequiresRegister());
3413 } else {
3414 InvokeRuntimeCallingConvention calling_convention;
3415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3416 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3417 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3418 // we only need the latter.
3419 locations->SetOut(Location::RegisterLocation(R1));
3420 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 break;
3422 }
3423 case Primitive::kPrimLong: {
3424 InvokeRuntimeCallingConvention calling_convention;
3425 locations->SetInAt(0, Location::RegisterPairLocation(
3426 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3427 locations->SetInAt(1, Location::RegisterPairLocation(
3428 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3429 // The runtime helper puts the output in R2,R3.
3430 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3431 break;
3432 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003433 case Primitive::kPrimFloat: {
3434 InvokeRuntimeCallingConvention calling_convention;
3435 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3436 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3437 locations->SetOut(Location::FpuRegisterLocation(S0));
3438 break;
3439 }
3440
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003442 InvokeRuntimeCallingConvention calling_convention;
3443 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3444 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3445 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3446 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3447 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 break;
3449 }
3450
3451 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003452 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003453 }
3454}
3455
3456void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3457 LocationSummary* locations = rem->GetLocations();
3458 Location out = locations->Out();
3459 Location first = locations->InAt(0);
3460 Location second = locations->InAt(1);
3461
Calin Juravled2ec87d2014-12-08 14:24:46 +00003462 Primitive::Type type = rem->GetResultType();
3463 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003465 if (second.IsConstant()) {
3466 GenerateDivRemConstantIntegral(rem);
3467 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003468 Register reg1 = first.AsRegister<Register>();
3469 Register reg2 = second.AsRegister<Register>();
3470 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003471
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003472 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003473 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003474 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003475 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003476 } else {
3477 InvokeRuntimeCallingConvention calling_convention;
3478 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3479 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3480 DCHECK_EQ(R1, out.AsRegister<Register>());
3481
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003482 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003483 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003484 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 break;
3486 }
3487
3488 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003489 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003490 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003491 break;
3492 }
3493
Calin Juravled2ec87d2014-12-08 14:24:46 +00003494 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003495 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003496 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003497 break;
3498 }
3499
Calin Juravlebacfec32014-11-14 15:54:36 +00003500 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003501 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003502 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003503 break;
3504 }
3505
3506 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003507 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 }
3509}
3510
Calin Juravled0d48522014-11-04 16:40:20 +00003511void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003512 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003513 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003514}
3515
3516void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003517 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003518 codegen_->AddSlowPath(slow_path);
3519
3520 LocationSummary* locations = instruction->GetLocations();
3521 Location value = locations->InAt(0);
3522
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003523 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003524 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003525 case Primitive::kPrimByte:
3526 case Primitive::kPrimChar:
3527 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003528 case Primitive::kPrimInt: {
3529 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003530 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003531 } else {
3532 DCHECK(value.IsConstant()) << value;
3533 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3534 __ b(slow_path->GetEntryLabel());
3535 }
3536 }
3537 break;
3538 }
3539 case Primitive::kPrimLong: {
3540 if (value.IsRegisterPair()) {
3541 __ orrs(IP,
3542 value.AsRegisterPairLow<Register>(),
3543 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3544 __ b(slow_path->GetEntryLabel(), EQ);
3545 } else {
3546 DCHECK(value.IsConstant()) << value;
3547 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3548 __ b(slow_path->GetEntryLabel());
3549 }
3550 }
3551 break;
3552 default:
3553 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3554 }
3555 }
Calin Juravled0d48522014-11-04 16:40:20 +00003556}
3557
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003558void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3559 Register in = locations->InAt(0).AsRegister<Register>();
3560 Location rhs = locations->InAt(1);
3561 Register out = locations->Out().AsRegister<Register>();
3562
3563 if (rhs.IsConstant()) {
3564 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3565 // so map all rotations to a +ve. equivalent in that range.
3566 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3567 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3568 if (rot) {
3569 // Rotate, mapping left rotations to right equivalents if necessary.
3570 // (e.g. left by 2 bits == right by 30.)
3571 __ Ror(out, in, rot);
3572 } else if (out != in) {
3573 __ Mov(out, in);
3574 }
3575 } else {
3576 __ Ror(out, in, rhs.AsRegister<Register>());
3577 }
3578}
3579
3580// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3581// rotates by swapping input regs (effectively rotating by the first 32-bits of
3582// a larger rotation) or flipping direction (thus treating larger right/left
3583// rotations as sub-word sized rotations in the other direction) as appropriate.
3584void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3585 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3586 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3587 Location rhs = locations->InAt(1);
3588 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3589 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3590
3591 if (rhs.IsConstant()) {
3592 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3593 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003594 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003595 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3596 // logic below to a simple pair of binary orr.
3597 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3598 if (rot >= kArmBitsPerWord) {
3599 rot -= kArmBitsPerWord;
3600 std::swap(in_reg_hi, in_reg_lo);
3601 }
3602 // Rotate, or mov to out for zero or word size rotations.
3603 if (rot != 0u) {
3604 __ Lsr(out_reg_hi, in_reg_hi, rot);
3605 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3606 __ Lsr(out_reg_lo, in_reg_lo, rot);
3607 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3608 } else {
3609 __ Mov(out_reg_lo, in_reg_lo);
3610 __ Mov(out_reg_hi, in_reg_hi);
3611 }
3612 } else {
3613 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3614 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3615 Label end;
3616 Label shift_by_32_plus_shift_right;
3617
3618 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3619 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3620 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3621 __ b(&shift_by_32_plus_shift_right, CC);
3622
3623 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3624 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3625 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3626 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3627 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3628 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3629 __ Lsr(shift_left, in_reg_hi, shift_right);
3630 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3631 __ b(&end);
3632
3633 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3634 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3635 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3636 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3637 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3638 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3639 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3640 __ Lsl(shift_right, in_reg_hi, shift_left);
3641 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3642
3643 __ Bind(&end);
3644 }
3645}
Roland Levillain22c49222016-03-18 14:04:28 +00003646
3647void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3650 switch (ror->GetResultType()) {
3651 case Primitive::kPrimInt: {
3652 locations->SetInAt(0, Location::RequiresRegister());
3653 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3655 break;
3656 }
3657 case Primitive::kPrimLong: {
3658 locations->SetInAt(0, Location::RequiresRegister());
3659 if (ror->InputAt(1)->IsConstant()) {
3660 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3661 } else {
3662 locations->SetInAt(1, Location::RequiresRegister());
3663 locations->AddTemp(Location::RequiresRegister());
3664 locations->AddTemp(Location::RequiresRegister());
3665 }
3666 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3667 break;
3668 }
3669 default:
3670 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3671 }
3672}
3673
Roland Levillain22c49222016-03-18 14:04:28 +00003674void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003675 LocationSummary* locations = ror->GetLocations();
3676 Primitive::Type type = ror->GetResultType();
3677 switch (type) {
3678 case Primitive::kPrimInt: {
3679 HandleIntegerRotate(locations);
3680 break;
3681 }
3682 case Primitive::kPrimLong: {
3683 HandleLongRotate(locations);
3684 break;
3685 }
3686 default:
3687 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003688 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003689 }
3690}
3691
Calin Juravle9aec02f2014-11-18 23:06:35 +00003692void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3693 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3694
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003695 LocationSummary* locations =
3696 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697
3698 switch (op->GetResultType()) {
3699 case Primitive::kPrimInt: {
3700 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003701 if (op->InputAt(1)->IsConstant()) {
3702 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3704 } else {
3705 locations->SetInAt(1, Location::RequiresRegister());
3706 // Make the output overlap, as it will be used to hold the masked
3707 // second input.
3708 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3709 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 break;
3711 }
3712 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003713 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003714 if (op->InputAt(1)->IsConstant()) {
3715 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3716 // For simplicity, use kOutputOverlap even though we only require that low registers
3717 // don't clash with high registers which the register allocator currently guarantees.
3718 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3719 } else {
3720 locations->SetInAt(1, Location::RequiresRegister());
3721 locations->AddTemp(Location::RequiresRegister());
3722 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3723 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 break;
3725 }
3726 default:
3727 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3728 }
3729}
3730
3731void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3732 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3733
3734 LocationSummary* locations = op->GetLocations();
3735 Location out = locations->Out();
3736 Location first = locations->InAt(0);
3737 Location second = locations->InAt(1);
3738
3739 Primitive::Type type = op->GetResultType();
3740 switch (type) {
3741 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 Register out_reg = out.AsRegister<Register>();
3743 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003746 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003747 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003749 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003751 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003753 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 }
3755 } else {
3756 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003757 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003758 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 __ Mov(out_reg, first_reg);
3760 } else if (op->IsShl()) {
3761 __ Lsl(out_reg, first_reg, shift_value);
3762 } else if (op->IsShr()) {
3763 __ Asr(out_reg, first_reg, shift_value);
3764 } else {
3765 __ Lsr(out_reg, first_reg, shift_value);
3766 }
3767 }
3768 break;
3769 }
3770 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003771 Register o_h = out.AsRegisterPairHigh<Register>();
3772 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003774 Register high = first.AsRegisterPairHigh<Register>();
3775 Register low = first.AsRegisterPairLow<Register>();
3776
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003777 if (second.IsRegister()) {
3778 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003779
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003780 Register second_reg = second.AsRegister<Register>();
3781
3782 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003783 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003784 // Shift the high part
3785 __ Lsl(o_h, high, o_l);
3786 // Shift the low part and `or` what overflew on the high part
3787 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3788 __ Lsr(temp, low, temp);
3789 __ orr(o_h, o_h, ShifterOperand(temp));
3790 // If the shift is > 32 bits, override the high part
3791 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3792 __ it(PL);
3793 __ Lsl(o_h, low, temp, PL);
3794 // Shift the low part
3795 __ Lsl(o_l, low, o_l);
3796 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003797 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003798 // Shift the low part
3799 __ Lsr(o_l, low, o_h);
3800 // Shift the high part and `or` what underflew on the low part
3801 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3802 __ Lsl(temp, high, temp);
3803 __ orr(o_l, o_l, ShifterOperand(temp));
3804 // If the shift is > 32 bits, override the low part
3805 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3806 __ it(PL);
3807 __ Asr(o_l, high, temp, PL);
3808 // Shift the high part
3809 __ Asr(o_h, high, o_h);
3810 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003811 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003812 // same as Shr except we use `Lsr`s and not `Asr`s
3813 __ Lsr(o_l, low, o_h);
3814 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3815 __ Lsl(temp, high, temp);
3816 __ orr(o_l, o_l, ShifterOperand(temp));
3817 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3818 __ it(PL);
3819 __ Lsr(o_l, high, temp, PL);
3820 __ Lsr(o_h, high, o_h);
3821 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003823 // Register allocator doesn't create partial overlap.
3824 DCHECK_NE(o_l, high);
3825 DCHECK_NE(o_h, low);
3826 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003827 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003828 if (shift_value > 32) {
3829 if (op->IsShl()) {
3830 __ Lsl(o_h, low, shift_value - 32);
3831 __ LoadImmediate(o_l, 0);
3832 } else if (op->IsShr()) {
3833 __ Asr(o_l, high, shift_value - 32);
3834 __ Asr(o_h, high, 31);
3835 } else {
3836 __ Lsr(o_l, high, shift_value - 32);
3837 __ LoadImmediate(o_h, 0);
3838 }
3839 } else if (shift_value == 32) {
3840 if (op->IsShl()) {
3841 __ mov(o_h, ShifterOperand(low));
3842 __ LoadImmediate(o_l, 0);
3843 } else if (op->IsShr()) {
3844 __ mov(o_l, ShifterOperand(high));
3845 __ Asr(o_h, high, 31);
3846 } else {
3847 __ mov(o_l, ShifterOperand(high));
3848 __ LoadImmediate(o_h, 0);
3849 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003850 } else if (shift_value == 1) {
3851 if (op->IsShl()) {
3852 __ Lsls(o_l, low, 1);
3853 __ adc(o_h, high, ShifterOperand(high));
3854 } else if (op->IsShr()) {
3855 __ Asrs(o_h, high, 1);
3856 __ Rrx(o_l, low);
3857 } else {
3858 __ Lsrs(o_h, high, 1);
3859 __ Rrx(o_l, low);
3860 }
3861 } else {
3862 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003863 if (op->IsShl()) {
3864 __ Lsl(o_h, high, shift_value);
3865 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3866 __ Lsl(o_l, low, shift_value);
3867 } else if (op->IsShr()) {
3868 __ Lsr(o_l, low, shift_value);
3869 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3870 __ Asr(o_h, high, shift_value);
3871 } else {
3872 __ Lsr(o_l, low, shift_value);
3873 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3874 __ Lsr(o_h, high, shift_value);
3875 }
3876 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 break;
3879 }
3880 default:
3881 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003882 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883 }
3884}
3885
3886void LocationsBuilderARM::VisitShl(HShl* shl) {
3887 HandleShift(shl);
3888}
3889
3890void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void LocationsBuilderARM::VisitShr(HShr* shr) {
3895 HandleShift(shr);
3896}
3897
3898void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3903 HandleShift(ushr);
3904}
3905
3906void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003910void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003911 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003913 if (instruction->IsStringAlloc()) {
3914 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3915 } else {
3916 InvokeRuntimeCallingConvention calling_convention;
3917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3918 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3919 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003920 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003921}
3922
3923void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003924 // Note: if heap poisoning is enabled, the entry point takes cares
3925 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003926 if (instruction->IsStringAlloc()) {
3927 // String is allocated through StringFactory. Call NewEmptyString entry point.
3928 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003929 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003930 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3931 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3932 __ blx(LR);
3933 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3934 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003935 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003936 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3937 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003938}
3939
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003940void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3941 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003942 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003943 InvokeRuntimeCallingConvention calling_convention;
3944 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003945 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003946 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003947 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003948}
3949
3950void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3951 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003953 // Note: if heap poisoning is enabled, the entry point takes cares
3954 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003955 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003956 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957}
3958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003959void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003960 LocationSummary* locations =
3961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003962 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3963 if (location.IsStackSlot()) {
3964 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3965 } else if (location.IsDoubleStackSlot()) {
3966 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003967 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003968 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003969}
3970
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003971void InstructionCodeGeneratorARM::VisitParameterValue(
3972 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003973 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003974}
3975
3976void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3977 LocationSummary* locations =
3978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3979 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3980}
3981
3982void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3983 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003984}
3985
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003986void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003989 locations->SetInAt(0, Location::RequiresRegister());
3990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003991}
3992
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3994 LocationSummary* locations = not_->GetLocations();
3995 Location out = locations->Out();
3996 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003997 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 break;
4001
4002 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004003 __ mvn(out.AsRegisterPairLow<Register>(),
4004 ShifterOperand(in.AsRegisterPairLow<Register>()));
4005 __ mvn(out.AsRegisterPairHigh<Register>(),
4006 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004007 break;
4008
4009 default:
4010 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4011 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004012}
4013
David Brazdil66d126e2015-04-03 16:02:44 +01004014void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4015 LocationSummary* locations =
4016 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4017 locations->SetInAt(0, Location::RequiresRegister());
4018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4019}
4020
4021void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004022 LocationSummary* locations = bool_not->GetLocations();
4023 Location out = locations->Out();
4024 Location in = locations->InAt(0);
4025 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4026}
4027
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004028void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004031 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004032 case Primitive::kPrimBoolean:
4033 case Primitive::kPrimByte:
4034 case Primitive::kPrimShort:
4035 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004036 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004037 case Primitive::kPrimLong: {
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004040 // Output overlaps because it is written before doing the low comparison.
4041 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004042 break;
4043 }
4044 case Primitive::kPrimFloat:
4045 case Primitive::kPrimDouble: {
4046 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004047 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004048 locations->SetOut(Location::RequiresRegister());
4049 break;
4050 }
4051 default:
4052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004054}
4055
4056void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004058 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004059 Location left = locations->InAt(0);
4060 Location right = locations->InAt(1);
4061
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004062 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004063 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004064 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004065 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004066 case Primitive::kPrimBoolean:
4067 case Primitive::kPrimByte:
4068 case Primitive::kPrimShort:
4069 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004070 case Primitive::kPrimInt: {
4071 __ LoadImmediate(out, 0);
4072 __ cmp(left.AsRegister<Register>(),
4073 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4074 less_cond = LT;
4075 break;
4076 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004078 __ cmp(left.AsRegisterPairHigh<Register>(),
4079 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080 __ b(&less, LT);
4081 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004082 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004084 __ cmp(left.AsRegisterPairLow<Register>(),
4085 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004086 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004087 break;
4088 }
4089 case Primitive::kPrimFloat:
4090 case Primitive::kPrimDouble: {
4091 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004092 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004093 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004094 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004095 break;
4096 }
4097 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004098 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004099 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100 }
Aart Bika19616e2016-02-01 18:57:58 -08004101
Calin Juravleddb7df22014-11-25 20:56:51 +00004102 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004103 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004104
4105 __ Bind(&greater);
4106 __ LoadImmediate(out, 1);
4107 __ b(&done);
4108
4109 __ Bind(&less);
4110 __ LoadImmediate(out, -1);
4111
4112 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004113}
4114
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004115void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004116 LocationSummary* locations =
4117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004118 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004119 locations->SetInAt(i, Location::Any());
4120 }
4121 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004122}
4123
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004124void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004125 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004126}
4127
Roland Levillainc9285912015-12-18 10:38:42 +00004128void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4129 // TODO (ported from quick): revisit ARM barrier kinds.
4130 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004131 switch (kind) {
4132 case MemBarrierKind::kAnyStore:
4133 case MemBarrierKind::kLoadAny:
4134 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004135 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004136 break;
4137 }
4138 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004139 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004140 break;
4141 }
4142 default:
4143 LOG(FATAL) << "Unexpected memory barrier " << kind;
4144 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004145 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004146}
4147
4148void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4149 uint32_t offset,
4150 Register out_lo,
4151 Register out_hi) {
4152 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004153 // Ensure `out_lo` is different from `addr`, so that loading
4154 // `offset` into `out_lo` does not clutter `addr`.
4155 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004156 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004157 __ add(IP, addr, ShifterOperand(out_lo));
4158 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004159 }
4160 __ ldrexd(out_lo, out_hi, addr);
4161}
4162
4163void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4164 uint32_t offset,
4165 Register value_lo,
4166 Register value_hi,
4167 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 Register temp2,
4169 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004170 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004171 if (offset != 0) {
4172 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004173 __ add(IP, addr, ShifterOperand(temp1));
4174 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004175 }
4176 __ Bind(&fail);
4177 // We need a load followed by store. (The address used in a STREX instruction must
4178 // be the same as the address in the most recently executed LDREX instruction.)
4179 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004181 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004182 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004183}
4184
4185void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4186 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4187
Nicolas Geoffray39468442014-09-02 15:17:15 +01004188 LocationSummary* locations =
4189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004190 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004191
Calin Juravle52c48962014-12-16 17:02:57 +00004192 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004193 if (Primitive::IsFloatingPointType(field_type)) {
4194 locations->SetInAt(1, Location::RequiresFpuRegister());
4195 } else {
4196 locations->SetInAt(1, Location::RequiresRegister());
4197 }
4198
Calin Juravle52c48962014-12-16 17:02:57 +00004199 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004200 bool generate_volatile = field_info.IsVolatile()
4201 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004202 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004203 bool needs_write_barrier =
4204 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004205 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004206 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004207 if (needs_write_barrier) {
4208 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004209 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004210 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004211 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004212 // - registers need to be consecutive
4213 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004214 // We don't test for ARM yet, and the assertion makes sure that we
4215 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004216 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4217
4218 locations->AddTemp(Location::RequiresRegister());
4219 locations->AddTemp(Location::RequiresRegister());
4220 if (field_type == Primitive::kPrimDouble) {
4221 // For doubles we need two more registers to copy the value.
4222 locations->AddTemp(Location::RegisterLocation(R2));
4223 locations->AddTemp(Location::RegisterLocation(R3));
4224 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004225 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004226}
4227
Calin Juravle52c48962014-12-16 17:02:57 +00004228void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 const FieldInfo& field_info,
4230 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004231 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004234 Register base = locations->InAt(0).AsRegister<Register>();
4235 Location value = locations->InAt(1);
4236
4237 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004238 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004239 Primitive::Type field_type = field_info.GetFieldType();
4240 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004241 bool needs_write_barrier =
4242 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004243
4244 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004245 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247
4248 switch (field_type) {
4249 case Primitive::kPrimBoolean:
4250 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252 break;
4253 }
4254
4255 case Primitive::kPrimShort:
4256 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004257 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004258 break;
4259 }
4260
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004261 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004263 if (kPoisonHeapReferences && needs_write_barrier) {
4264 // Note that in the case where `value` is a null reference,
4265 // we do not enter this block, as a null reference does not
4266 // need poisoning.
4267 DCHECK_EQ(field_type, Primitive::kPrimNot);
4268 Register temp = locations->GetTemp(0).AsRegister<Register>();
4269 __ Mov(temp, value.AsRegister<Register>());
4270 __ PoisonHeapReference(temp);
4271 __ StoreToOffset(kStoreWord, temp, base, offset);
4272 } else {
4273 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275 break;
4276 }
4277
4278 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004279 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004280 GenerateWideAtomicStore(base, offset,
4281 value.AsRegisterPairLow<Register>(),
4282 value.AsRegisterPairHigh<Register>(),
4283 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004284 locations->GetTemp(1).AsRegister<Register>(),
4285 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004286 } else {
4287 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004289 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290 break;
4291 }
4292
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004293 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004294 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004295 break;
4296 }
4297
4298 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004299 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004300 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4302 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4303
4304 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4305
4306 GenerateWideAtomicStore(base, offset,
4307 value_reg_lo,
4308 value_reg_hi,
4309 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004310 locations->GetTemp(3).AsRegister<Register>(),
4311 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004312 } else {
4313 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004315 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004316 break;
4317 }
4318
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319 case Primitive::kPrimVoid:
4320 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004321 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 }
Calin Juravle52c48962014-12-16 17:02:57 +00004323
Calin Juravle77520bc2015-01-12 18:45:46 +00004324 // Longs and doubles are handled in the switch.
4325 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4326 codegen_->MaybeRecordImplicitNullCheck(instruction);
4327 }
4328
4329 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4330 Register temp = locations->GetTemp(0).AsRegister<Register>();
4331 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004332 codegen_->MarkGCCard(
4333 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004334 }
4335
Calin Juravle52c48962014-12-16 17:02:57 +00004336 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004337 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004338 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339}
4340
Calin Juravle52c48962014-12-16 17:02:57 +00004341void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4342 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004343
4344 bool object_field_get_with_read_barrier =
4345 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004346 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004347 new (GetGraph()->GetArena()) LocationSummary(instruction,
4348 object_field_get_with_read_barrier ?
4349 LocationSummary::kCallOnSlowPath :
4350 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004351 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004352 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004353 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004354 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004355
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004356 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004357 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004358 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004359 // The output overlaps in case of volatile long: we don't want the
4360 // code generated by GenerateWideAtomicLoad to overwrite the
4361 // object's location. Likewise, in the case of an object field get
4362 // with read barriers enabled, we do not want the load to overwrite
4363 // the object's location, as we need it to emit the read barrier.
4364 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4365 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004366
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004367 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4368 locations->SetOut(Location::RequiresFpuRegister());
4369 } else {
4370 locations->SetOut(Location::RequiresRegister(),
4371 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4372 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004373 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004374 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004375 // - registers need to be consecutive
4376 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004377 // We don't test for ARM yet, and the assertion makes sure that we
4378 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004379 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4380 locations->AddTemp(Location::RequiresRegister());
4381 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004382 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4383 // We need a temporary register for the read barrier marking slow
4384 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4385 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004386 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387}
4388
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004389Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4390 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4391 << input->GetType();
4392 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4393 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4394 return Location::ConstantLocation(input->AsConstant());
4395 } else {
4396 return Location::RequiresFpuRegister();
4397 }
4398}
4399
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004400Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4401 Opcode opcode) {
4402 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4403 if (constant->IsConstant() &&
4404 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4405 return Location::ConstantLocation(constant->AsConstant());
4406 }
4407 return Location::RequiresRegister();
4408}
4409
4410bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4411 Opcode opcode) {
4412 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4413 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004414 Opcode high_opcode = opcode;
4415 SetCc low_set_cc = kCcDontCare;
4416 switch (opcode) {
4417 case SUB:
4418 // Flip the operation to an ADD.
4419 value = -value;
4420 opcode = ADD;
4421 FALLTHROUGH_INTENDED;
4422 case ADD:
4423 if (Low32Bits(value) == 0u) {
4424 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4425 }
4426 high_opcode = ADC;
4427 low_set_cc = kCcSet;
4428 break;
4429 default:
4430 break;
4431 }
4432 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4433 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004434 } else {
4435 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4436 }
4437}
4438
Vladimir Marko59751a72016-08-05 14:37:27 +01004439bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4440 Opcode opcode,
4441 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004442 ShifterOperand so;
4443 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004444 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004445 return true;
4446 }
4447 Opcode neg_opcode = kNoOperand;
4448 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004449 case AND: neg_opcode = BIC; value = ~value; break;
4450 case ORR: neg_opcode = ORN; value = ~value; break;
4451 case ADD: neg_opcode = SUB; value = -value; break;
4452 case ADC: neg_opcode = SBC; value = ~value; break;
4453 case SUB: neg_opcode = ADD; value = -value; break;
4454 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004455 default:
4456 return false;
4457 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004458 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004459}
4460
Calin Juravle52c48962014-12-16 17:02:57 +00004461void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4462 const FieldInfo& field_info) {
4463 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464
Calin Juravle52c48962014-12-16 17:02:57 +00004465 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004466 Location base_loc = locations->InAt(0);
4467 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004468 Location out = locations->Out();
4469 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004470 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004471 Primitive::Type field_type = field_info.GetFieldType();
4472 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4473
4474 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004475 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004476 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004478
Roland Levillainc9285912015-12-18 10:38:42 +00004479 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004480 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482
Roland Levillainc9285912015-12-18 10:38:42 +00004483 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004484 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486
Roland Levillainc9285912015-12-18 10:38:42 +00004487 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004488 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490
4491 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004492 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004494
4495 case Primitive::kPrimNot: {
4496 // /* HeapReference<Object> */ out = *(base + offset)
4497 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4498 Location temp_loc = locations->GetTemp(0);
4499 // Note that a potential implicit null check is handled in this
4500 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4501 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4502 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4503 if (is_volatile) {
4504 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4505 }
4506 } else {
4507 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4508 codegen_->MaybeRecordImplicitNullCheck(instruction);
4509 if (is_volatile) {
4510 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4511 }
4512 // If read barriers are enabled, emit read barriers other than
4513 // Baker's using a slow path (and also unpoison the loaded
4514 // reference, if heap poisoning is enabled).
4515 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4516 }
4517 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518 }
4519
Roland Levillainc9285912015-12-18 10:38:42 +00004520 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004521 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004522 GenerateWideAtomicLoad(base, offset,
4523 out.AsRegisterPairLow<Register>(),
4524 out.AsRegisterPairHigh<Register>());
4525 } else {
4526 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004529
Roland Levillainc9285912015-12-18 10:38:42 +00004530 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004531 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004532 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004533
4534 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004535 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004536 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 Register lo = locations->GetTemp(0).AsRegister<Register>();
4538 Register hi = locations->GetTemp(1).AsRegister<Register>();
4539 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004540 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004541 __ vmovdrr(out_reg, lo, hi);
4542 } else {
4543 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004545 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004546 break;
4547 }
4548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004550 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004551 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552 }
Calin Juravle52c48962014-12-16 17:02:57 +00004553
Roland Levillainc9285912015-12-18 10:38:42 +00004554 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4555 // Potential implicit null checks, in the case of reference or
4556 // double fields, are handled in the previous switch statement.
4557 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004558 codegen_->MaybeRecordImplicitNullCheck(instruction);
4559 }
4560
Calin Juravle52c48962014-12-16 17:02:57 +00004561 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004562 if (field_type == Primitive::kPrimNot) {
4563 // Memory barriers, in the case of references, are also handled
4564 // in the previous switch statement.
4565 } else {
4566 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4567 }
Roland Levillain4d027112015-07-01 15:41:14 +01004568 }
Calin Juravle52c48962014-12-16 17:02:57 +00004569}
4570
4571void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4572 HandleFieldSet(instruction, instruction->GetFieldInfo());
4573}
4574
4575void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004576 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004577}
4578
4579void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4580 HandleFieldGet(instruction, instruction->GetFieldInfo());
4581}
4582
4583void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4584 HandleFieldGet(instruction, instruction->GetFieldInfo());
4585}
4586
4587void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4588 HandleFieldGet(instruction, instruction->GetFieldInfo());
4589}
4590
4591void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4592 HandleFieldGet(instruction, instruction->GetFieldInfo());
4593}
4594
4595void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4596 HandleFieldSet(instruction, instruction->GetFieldInfo());
4597}
4598
4599void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004600 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601}
4602
Calin Juravlee460d1d2015-09-29 04:52:17 +01004603void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4604 HUnresolvedInstanceFieldGet* instruction) {
4605 FieldAccessCallingConventionARM calling_convention;
4606 codegen_->CreateUnresolvedFieldLocationSummary(
4607 instruction, instruction->GetFieldType(), calling_convention);
4608}
4609
4610void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4611 HUnresolvedInstanceFieldGet* instruction) {
4612 FieldAccessCallingConventionARM calling_convention;
4613 codegen_->GenerateUnresolvedFieldAccess(instruction,
4614 instruction->GetFieldType(),
4615 instruction->GetFieldIndex(),
4616 instruction->GetDexPc(),
4617 calling_convention);
4618}
4619
4620void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4621 HUnresolvedInstanceFieldSet* instruction) {
4622 FieldAccessCallingConventionARM calling_convention;
4623 codegen_->CreateUnresolvedFieldLocationSummary(
4624 instruction, instruction->GetFieldType(), calling_convention);
4625}
4626
4627void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4628 HUnresolvedInstanceFieldSet* instruction) {
4629 FieldAccessCallingConventionARM calling_convention;
4630 codegen_->GenerateUnresolvedFieldAccess(instruction,
4631 instruction->GetFieldType(),
4632 instruction->GetFieldIndex(),
4633 instruction->GetDexPc(),
4634 calling_convention);
4635}
4636
4637void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4638 HUnresolvedStaticFieldGet* instruction) {
4639 FieldAccessCallingConventionARM calling_convention;
4640 codegen_->CreateUnresolvedFieldLocationSummary(
4641 instruction, instruction->GetFieldType(), calling_convention);
4642}
4643
4644void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4645 HUnresolvedStaticFieldGet* instruction) {
4646 FieldAccessCallingConventionARM calling_convention;
4647 codegen_->GenerateUnresolvedFieldAccess(instruction,
4648 instruction->GetFieldType(),
4649 instruction->GetFieldIndex(),
4650 instruction->GetDexPc(),
4651 calling_convention);
4652}
4653
4654void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4655 HUnresolvedStaticFieldSet* instruction) {
4656 FieldAccessCallingConventionARM calling_convention;
4657 codegen_->CreateUnresolvedFieldLocationSummary(
4658 instruction, instruction->GetFieldType(), calling_convention);
4659}
4660
4661void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4662 HUnresolvedStaticFieldSet* instruction) {
4663 FieldAccessCallingConventionARM calling_convention;
4664 codegen_->GenerateUnresolvedFieldAccess(instruction,
4665 instruction->GetFieldType(),
4666 instruction->GetFieldIndex(),
4667 instruction->GetDexPc(),
4668 calling_convention);
4669}
4670
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004672 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4673 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674}
4675
Calin Juravle2ae48182016-03-16 14:05:09 +00004676void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4677 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004678 return;
4679 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004680 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004681
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004682 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004683 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004684}
4685
Calin Juravle2ae48182016-03-16 14:05:09 +00004686void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004687 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004688 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004689
4690 LocationSummary* locations = instruction->GetLocations();
4691 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004693 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694}
4695
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004696void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004697 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004698}
4699
Artem Serov6c916792016-07-11 14:02:34 +01004700static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4701 switch (type) {
4702 case Primitive::kPrimNot:
4703 return kLoadWord;
4704 case Primitive::kPrimBoolean:
4705 return kLoadUnsignedByte;
4706 case Primitive::kPrimByte:
4707 return kLoadSignedByte;
4708 case Primitive::kPrimChar:
4709 return kLoadUnsignedHalfword;
4710 case Primitive::kPrimShort:
4711 return kLoadSignedHalfword;
4712 case Primitive::kPrimInt:
4713 return kLoadWord;
4714 case Primitive::kPrimLong:
4715 return kLoadWordPair;
4716 case Primitive::kPrimFloat:
4717 return kLoadSWord;
4718 case Primitive::kPrimDouble:
4719 return kLoadDWord;
4720 default:
4721 LOG(FATAL) << "Unreachable type " << type;
4722 UNREACHABLE();
4723 }
4724}
4725
4726static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4727 switch (type) {
4728 case Primitive::kPrimNot:
4729 return kStoreWord;
4730 case Primitive::kPrimBoolean:
4731 case Primitive::kPrimByte:
4732 return kStoreByte;
4733 case Primitive::kPrimChar:
4734 case Primitive::kPrimShort:
4735 return kStoreHalfword;
4736 case Primitive::kPrimInt:
4737 return kStoreWord;
4738 case Primitive::kPrimLong:
4739 return kStoreWordPair;
4740 case Primitive::kPrimFloat:
4741 return kStoreSWord;
4742 case Primitive::kPrimDouble:
4743 return kStoreDWord;
4744 default:
4745 LOG(FATAL) << "Unreachable type " << type;
4746 UNREACHABLE();
4747 }
4748}
4749
4750void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4751 Location out_loc,
4752 Register base,
4753 Register reg_offset,
4754 Condition cond) {
4755 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4756 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4757
4758 switch (type) {
4759 case Primitive::kPrimByte:
4760 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4761 break;
4762 case Primitive::kPrimBoolean:
4763 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4764 break;
4765 case Primitive::kPrimShort:
4766 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4767 break;
4768 case Primitive::kPrimChar:
4769 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4770 break;
4771 case Primitive::kPrimNot:
4772 case Primitive::kPrimInt:
4773 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4774 break;
4775 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4776 case Primitive::kPrimLong:
4777 case Primitive::kPrimFloat:
4778 case Primitive::kPrimDouble:
4779 default:
4780 LOG(FATAL) << "Unreachable type " << type;
4781 UNREACHABLE();
4782 }
4783}
4784
4785void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4786 Location loc,
4787 Register base,
4788 Register reg_offset,
4789 Condition cond) {
4790 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4791 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4792
4793 switch (type) {
4794 case Primitive::kPrimByte:
4795 case Primitive::kPrimBoolean:
4796 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4797 break;
4798 case Primitive::kPrimShort:
4799 case Primitive::kPrimChar:
4800 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4801 break;
4802 case Primitive::kPrimNot:
4803 case Primitive::kPrimInt:
4804 __ str(loc.AsRegister<Register>(), mem_address, cond);
4805 break;
4806 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4807 case Primitive::kPrimLong:
4808 case Primitive::kPrimFloat:
4809 case Primitive::kPrimDouble:
4810 default:
4811 LOG(FATAL) << "Unreachable type " << type;
4812 UNREACHABLE();
4813 }
4814}
4815
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004817 bool object_array_get_with_read_barrier =
4818 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004819 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004820 new (GetGraph()->GetArena()) LocationSummary(instruction,
4821 object_array_get_with_read_barrier ?
4822 LocationSummary::kCallOnSlowPath :
4823 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004824 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004825 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004826 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004827 locations->SetInAt(0, Location::RequiresRegister());
4828 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004829 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4831 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004832 // The output overlaps in the case of an object array get with
4833 // read barriers enabled: we do not want the move to overwrite the
4834 // array's location, as we need it to emit the read barrier.
4835 locations->SetOut(
4836 Location::RequiresRegister(),
4837 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004838 }
Roland Levillainc9285912015-12-18 10:38:42 +00004839 // We need a temporary register for the read barrier marking slow
4840 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004841 // Also need for String compression feature.
4842 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4843 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004844 locations->AddTemp(Location::RequiresRegister());
4845 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004846}
4847
4848void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4849 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004850 Location obj_loc = locations->InAt(0);
4851 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004853 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004854 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004855 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004856 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4857 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004858 HInstruction* array_instr = instruction->GetArray();
4859 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004860
Roland Levillain4d027112015-07-01 15:41:14 +01004861 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004862 case Primitive::kPrimBoolean:
4863 case Primitive::kPrimByte:
4864 case Primitive::kPrimShort:
4865 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004866 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004867 Register length;
4868 if (maybe_compressed_char_at) {
4869 length = locations->GetTemp(0).AsRegister<Register>();
4870 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4871 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4872 codegen_->MaybeRecordImplicitNullCheck(instruction);
4873 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004875 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004876 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004877 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004878 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4879 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4880 "Expecting 0=compressed, 1=uncompressed");
4881 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004882 __ LoadFromOffset(kLoadUnsignedByte,
4883 out_loc.AsRegister<Register>(),
4884 obj,
4885 data_offset + const_index);
4886 __ b(&done);
4887 __ Bind(&uncompressed_load);
4888 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4889 out_loc.AsRegister<Register>(),
4890 obj,
4891 data_offset + (const_index << 1));
4892 __ Bind(&done);
4893 } else {
4894 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004895
jessicahandojo05765752016-09-09 19:01:32 -07004896 LoadOperandType load_type = GetLoadOperandType(type);
4897 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4898 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004900 Register temp = IP;
4901
4902 if (has_intermediate_address) {
4903 // We do not need to compute the intermediate address from the array: the
4904 // input instruction has done it already. See the comment in
4905 // `TryExtractArrayAccessAddress()`.
4906 if (kIsDebugBuild) {
4907 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4908 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4909 }
4910 temp = obj;
4911 } else {
4912 __ add(temp, obj, ShifterOperand(data_offset));
4913 }
jessicahandojo05765752016-09-09 19:01:32 -07004914 if (maybe_compressed_char_at) {
4915 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004916 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4917 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4918 "Expecting 0=compressed, 1=uncompressed");
4919 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004920 __ ldrb(out_loc.AsRegister<Register>(),
4921 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4922 __ b(&done);
4923 __ Bind(&uncompressed_load);
4924 __ ldrh(out_loc.AsRegister<Register>(),
4925 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4926 __ Bind(&done);
4927 } else {
4928 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4929 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004930 }
4931 break;
4932 }
4933
Roland Levillainc9285912015-12-18 10:38:42 +00004934 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004935 // The read barrier instrumentation of object ArrayGet
4936 // instructions does not support the HIntermediateAddress
4937 // instruction.
4938 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4939
Roland Levillainc9285912015-12-18 10:38:42 +00004940 static_assert(
4941 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4942 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004943 // /* HeapReference<Object> */ out =
4944 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4945 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4946 Location temp = locations->GetTemp(0);
4947 // Note that a potential implicit null check is handled in this
4948 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4949 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4950 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4951 } else {
4952 Register out = out_loc.AsRegister<Register>();
4953 if (index.IsConstant()) {
4954 size_t offset =
4955 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4956 __ LoadFromOffset(kLoadWord, out, obj, offset);
4957 codegen_->MaybeRecordImplicitNullCheck(instruction);
4958 // If read barriers are enabled, emit read barriers other than
4959 // Baker's using a slow path (and also unpoison the loaded
4960 // reference, if heap poisoning is enabled).
4961 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4962 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004963 Register temp = IP;
4964
4965 if (has_intermediate_address) {
4966 // We do not need to compute the intermediate address from the array: the
4967 // input instruction has done it already. See the comment in
4968 // `TryExtractArrayAccessAddress()`.
4969 if (kIsDebugBuild) {
4970 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4971 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4972 }
4973 temp = obj;
4974 } else {
4975 __ add(temp, obj, ShifterOperand(data_offset));
4976 }
4977 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004978
Roland Levillainc9285912015-12-18 10:38:42 +00004979 codegen_->MaybeRecordImplicitNullCheck(instruction);
4980 // If read barriers are enabled, emit read barriers other than
4981 // Baker's using a slow path (and also unpoison the loaded
4982 // reference, if heap poisoning is enabled).
4983 codegen_->MaybeGenerateReadBarrierSlow(
4984 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4985 }
4986 }
4987 break;
4988 }
4989
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004992 size_t offset =
4993 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004994 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004996 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004997 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998 }
4999 break;
5000 }
5001
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005002 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005003 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005004 if (index.IsConstant()) {
5005 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005006 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005007 } else {
5008 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005009 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005010 }
5011 break;
5012 }
5013
5014 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005015 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005016 if (index.IsConstant()) {
5017 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005018 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005019 } else {
5020 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005021 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005022 }
5023 break;
5024 }
5025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005027 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005028 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029 }
Roland Levillain4d027112015-07-01 15:41:14 +01005030
5031 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005032 // Potential implicit null checks, in the case of reference
5033 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005034 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005035 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005036 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037}
5038
5039void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005040 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005041
5042 bool needs_write_barrier =
5043 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005044 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005045
Nicolas Geoffray39468442014-09-02 15:17:15 +01005046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005047 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005048 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005049 LocationSummary::kCallOnSlowPath :
5050 LocationSummary::kNoCall);
5051
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005052 locations->SetInAt(0, Location::RequiresRegister());
5053 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5054 if (Primitive::IsFloatingPointType(value_type)) {
5055 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005057 locations->SetInAt(2, Location::RequiresRegister());
5058 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005059 if (needs_write_barrier) {
5060 // Temporary registers for the write barrier.
5061 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005062 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064}
5065
5066void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5067 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005068 Location array_loc = locations->InAt(0);
5069 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005071 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005072 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005073 bool needs_write_barrier =
5074 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005075 uint32_t data_offset =
5076 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5077 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005078 HInstruction* array_instr = instruction->GetArray();
5079 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080
5081 switch (value_type) {
5082 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005083 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005085 case Primitive::kPrimChar:
5086 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005088 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5089 uint32_t full_offset =
5090 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5091 StoreOperandType store_type = GetStoreOperandType(value_type);
5092 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005094 Register temp = IP;
5095
5096 if (has_intermediate_address) {
5097 // We do not need to compute the intermediate address from the array: the
5098 // input instruction has done it already. See the comment in
5099 // `TryExtractArrayAccessAddress()`.
5100 if (kIsDebugBuild) {
5101 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5102 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5103 }
5104 temp = array;
5105 } else {
5106 __ add(temp, array, ShifterOperand(data_offset));
5107 }
Artem Serov6c916792016-07-11 14:02:34 +01005108 codegen_->StoreToShiftedRegOffset(value_type,
5109 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005110 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005111 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 }
5113 break;
5114 }
5115
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005117 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005118 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5119 // See the comment in instruction_simplifier_shared.cc.
5120 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005121
5122 if (instruction->InputAt(2)->IsNullConstant()) {
5123 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005124 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005125 size_t offset =
5126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005127 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005128 } else {
5129 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005130 __ add(IP, array, ShifterOperand(data_offset));
5131 codegen_->StoreToShiftedRegOffset(value_type,
5132 value_loc,
5133 IP,
5134 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005135 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005136 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005137 DCHECK(!needs_write_barrier);
5138 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005139 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005140 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005141
5142 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005143 Location temp1_loc = locations->GetTemp(0);
5144 Register temp1 = temp1_loc.AsRegister<Register>();
5145 Location temp2_loc = locations->GetTemp(1);
5146 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005147 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5148 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5149 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5150 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005151 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005152
Roland Levillain3b359c72015-11-17 19:35:12 +00005153 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5155 codegen_->AddSlowPath(slow_path);
5156 if (instruction->GetValueCanBeNull()) {
5157 Label non_zero;
5158 __ CompareAndBranchIfNonZero(value, &non_zero);
5159 if (index.IsConstant()) {
5160 size_t offset =
5161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5162 __ StoreToOffset(kStoreWord, value, array, offset);
5163 } else {
5164 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005165 __ add(IP, array, ShifterOperand(data_offset));
5166 codegen_->StoreToShiftedRegOffset(value_type,
5167 value_loc,
5168 IP,
5169 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005170 }
5171 codegen_->MaybeRecordImplicitNullCheck(instruction);
5172 __ b(&done);
5173 __ Bind(&non_zero);
5174 }
5175
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005176 // Note that when read barriers are enabled, the type checks
5177 // are performed without read barriers. This is fine, even in
5178 // the case where a class object is in the from-space after
5179 // the flip, as a comparison involving such a type would not
5180 // produce a false positive; it may of course produce a false
5181 // negative, in which case we would take the ArraySet slow
5182 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005183
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005184 // /* HeapReference<Class> */ temp1 = array->klass_
5185 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5186 codegen_->MaybeRecordImplicitNullCheck(instruction);
5187 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005188
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005189 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5190 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5191 // /* HeapReference<Class> */ temp2 = value->klass_
5192 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5193 // If heap poisoning is enabled, no need to unpoison `temp1`
5194 // nor `temp2`, as we are comparing two poisoned references.
5195 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005196
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005197 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5198 Label do_put;
5199 __ b(&do_put, EQ);
5200 // If heap poisoning is enabled, the `temp1` reference has
5201 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005202 __ MaybeUnpoisonHeapReference(temp1);
5203
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005204 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5205 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5206 // If heap poisoning is enabled, no need to unpoison
5207 // `temp1`, as we are comparing against null below.
5208 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5209 __ Bind(&do_put);
5210 } else {
5211 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 }
5213 }
5214
Artem Serov6c916792016-07-11 14:02:34 +01005215 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005216 if (kPoisonHeapReferences) {
5217 // Note that in the case where `value` is a null reference,
5218 // we do not enter this block, as a null reference does not
5219 // need poisoning.
5220 DCHECK_EQ(value_type, Primitive::kPrimNot);
5221 __ Mov(temp1, value);
5222 __ PoisonHeapReference(temp1);
5223 source = temp1;
5224 }
5225
5226 if (index.IsConstant()) {
5227 size_t offset =
5228 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5229 __ StoreToOffset(kStoreWord, source, array, offset);
5230 } else {
5231 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005232
5233 __ add(IP, array, ShifterOperand(data_offset));
5234 codegen_->StoreToShiftedRegOffset(value_type,
5235 Location::RegisterLocation(source),
5236 IP,
5237 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005238 }
5239
Roland Levillain3b359c72015-11-17 19:35:12 +00005240 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005241 codegen_->MaybeRecordImplicitNullCheck(instruction);
5242 }
5243
5244 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5245
5246 if (done.IsLinked()) {
5247 __ Bind(&done);
5248 }
5249
5250 if (slow_path != nullptr) {
5251 __ Bind(slow_path->GetExitLabel());
5252 }
5253
5254 break;
5255 }
5256
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005258 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005260 size_t offset =
5261 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005265 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 }
5267 break;
5268 }
5269
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005270 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005271 Location value = locations->InAt(2);
5272 DCHECK(value.IsFpuRegister());
5273 if (index.IsConstant()) {
5274 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005276 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005277 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005278 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5279 }
5280 break;
5281 }
5282
5283 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005284 Location value = locations->InAt(2);
5285 DCHECK(value.IsFpuRegisterPair());
5286 if (index.IsConstant()) {
5287 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005289 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005291 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5292 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005293
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005294 break;
5295 }
5296
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005298 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005299 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005301
Roland Levillain80e67092016-01-08 16:04:55 +00005302 // Objects are handled in the switch.
5303 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 codegen_->MaybeRecordImplicitNullCheck(instruction);
5305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306}
5307
5308void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005309 LocationSummary* locations =
5310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005311 locations->SetInAt(0, Location::RequiresRegister());
5312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313}
5314
5315void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5316 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005317 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005318 Register obj = locations->InAt(0).AsRegister<Register>();
5319 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005321 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005322 // Mask out compression flag from String's array length.
5323 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005324 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005325 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326}
5327
Artem Serov328429f2016-07-06 16:23:04 +01005328void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005329 LocationSummary* locations =
5330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5331
5332 locations->SetInAt(0, Location::RequiresRegister());
5333 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5335}
5336
5337void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5338 LocationSummary* locations = instruction->GetLocations();
5339 Location out = locations->Out();
5340 Location first = locations->InAt(0);
5341 Location second = locations->InAt(1);
5342
Artem Serov328429f2016-07-06 16:23:04 +01005343 if (second.IsRegister()) {
5344 __ add(out.AsRegister<Register>(),
5345 first.AsRegister<Register>(),
5346 ShifterOperand(second.AsRegister<Register>()));
5347 } else {
5348 __ AddConstant(out.AsRegister<Register>(),
5349 first.AsRegister<Register>(),
5350 second.GetConstant()->AsIntConstant()->GetValue());
5351 }
5352}
5353
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005355 RegisterSet caller_saves = RegisterSet::Empty();
5356 InvokeRuntimeCallingConvention calling_convention;
5357 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5358 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5359 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 locations->SetInAt(0, Location::RequiresRegister());
5361 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362}
5363
5364void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5365 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005366 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005367 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 codegen_->AddSlowPath(slow_path);
5369
Roland Levillain271ab9c2014-11-27 15:23:57 +00005370 Register index = locations->InAt(0).AsRegister<Register>();
5371 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372
5373 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005374 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375}
5376
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005377void CodeGeneratorARM::MarkGCCard(Register temp,
5378 Register card,
5379 Register object,
5380 Register value,
5381 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005382 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005383 if (can_be_null) {
5384 __ CompareAndBranchIfZero(value, &is_null);
5385 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005386 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5388 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005389 if (can_be_null) {
5390 __ Bind(&is_null);
5391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392}
5393
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005394void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005395 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005396}
5397
5398void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005399 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5400}
5401
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005402void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005403 LocationSummary* locations =
5404 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005405 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005406}
5407
5408void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005409 HBasicBlock* block = instruction->GetBlock();
5410 if (block->GetLoopInformation() != nullptr) {
5411 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5412 // The back edge will generate the suspend check.
5413 return;
5414 }
5415 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5416 // The goto will generate the suspend check.
5417 return;
5418 }
5419 GenerateSuspendCheck(instruction, nullptr);
5420}
5421
5422void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5423 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005424 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005425 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5426 if (slow_path == nullptr) {
5427 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5428 instruction->SetSlowPath(slow_path);
5429 codegen_->AddSlowPath(slow_path);
5430 if (successor != nullptr) {
5431 DCHECK(successor->IsLoopHeader());
5432 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5433 }
5434 } else {
5435 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5436 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005437
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005438 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005439 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005440 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005441 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005442 __ Bind(slow_path->GetReturnLabel());
5443 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005444 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005445 __ b(slow_path->GetEntryLabel());
5446 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005447}
5448
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005449ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5450 return codegen_->GetAssembler();
5451}
5452
5453void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005454 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005455 Location source = move->GetSource();
5456 Location destination = move->GetDestination();
5457
5458 if (source.IsRegister()) {
5459 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005460 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005461 } else if (destination.IsFpuRegister()) {
5462 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005463 } else {
5464 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005465 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005466 SP, destination.GetStackIndex());
5467 }
5468 } else if (source.IsStackSlot()) {
5469 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005470 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005471 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005472 } else if (destination.IsFpuRegister()) {
5473 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005474 } else {
5475 DCHECK(destination.IsStackSlot());
5476 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5477 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5478 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005479 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005480 if (destination.IsRegister()) {
5481 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5482 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005483 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005484 } else {
5485 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005486 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5487 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005488 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005489 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005490 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5491 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005492 } else if (destination.IsRegisterPair()) {
5493 DCHECK(ExpectedPairLayout(destination));
5494 __ LoadFromOffset(
5495 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5496 } else {
5497 DCHECK(destination.IsFpuRegisterPair()) << destination;
5498 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5499 SP,
5500 source.GetStackIndex());
5501 }
5502 } else if (source.IsRegisterPair()) {
5503 if (destination.IsRegisterPair()) {
5504 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5505 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005506 } else if (destination.IsFpuRegisterPair()) {
5507 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5508 source.AsRegisterPairLow<Register>(),
5509 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005510 } else {
5511 DCHECK(destination.IsDoubleStackSlot()) << destination;
5512 DCHECK(ExpectedPairLayout(source));
5513 __ StoreToOffset(
5514 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5515 }
5516 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005517 if (destination.IsRegisterPair()) {
5518 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5519 destination.AsRegisterPairHigh<Register>(),
5520 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5521 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005522 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5523 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5524 } else {
5525 DCHECK(destination.IsDoubleStackSlot()) << destination;
5526 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5527 SP,
5528 destination.GetStackIndex());
5529 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005530 } else {
5531 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005532 HConstant* constant = source.GetConstant();
5533 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5534 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005535 if (destination.IsRegister()) {
5536 __ LoadImmediate(destination.AsRegister<Register>(), value);
5537 } else {
5538 DCHECK(destination.IsStackSlot());
5539 __ LoadImmediate(IP, value);
5540 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5541 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005542 } else if (constant->IsLongConstant()) {
5543 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005544 if (destination.IsRegisterPair()) {
5545 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5546 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005547 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005548 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005549 __ LoadImmediate(IP, Low32Bits(value));
5550 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5551 __ LoadImmediate(IP, High32Bits(value));
5552 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5553 }
5554 } else if (constant->IsDoubleConstant()) {
5555 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005556 if (destination.IsFpuRegisterPair()) {
5557 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005558 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005559 DCHECK(destination.IsDoubleStackSlot()) << destination;
5560 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005561 __ LoadImmediate(IP, Low32Bits(int_value));
5562 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5563 __ LoadImmediate(IP, High32Bits(int_value));
5564 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5565 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005566 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005567 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005568 float value = constant->AsFloatConstant()->GetValue();
5569 if (destination.IsFpuRegister()) {
5570 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5571 } else {
5572 DCHECK(destination.IsStackSlot());
5573 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5574 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5575 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005576 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005577 }
5578}
5579
5580void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5581 __ Mov(IP, reg);
5582 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5583 __ StoreToOffset(kStoreWord, IP, SP, mem);
5584}
5585
5586void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5587 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5588 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5589 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5590 SP, mem1 + stack_offset);
5591 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5592 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5593 SP, mem2 + stack_offset);
5594 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5595}
5596
5597void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005598 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005599 Location source = move->GetSource();
5600 Location destination = move->GetDestination();
5601
5602 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005603 DCHECK_NE(source.AsRegister<Register>(), IP);
5604 DCHECK_NE(destination.AsRegister<Register>(), IP);
5605 __ Mov(IP, source.AsRegister<Register>());
5606 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5607 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005608 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005609 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005610 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005611 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005612 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5613 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005614 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005615 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005616 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005617 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005618 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005619 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005620 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005621 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005622 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5623 destination.AsRegisterPairHigh<Register>(),
5624 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005625 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005626 Register low_reg = source.IsRegisterPair()
5627 ? source.AsRegisterPairLow<Register>()
5628 : destination.AsRegisterPairLow<Register>();
5629 int mem = source.IsRegisterPair()
5630 ? destination.GetStackIndex()
5631 : source.GetStackIndex();
5632 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005633 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005634 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005635 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005636 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005637 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5638 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005639 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005640 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005641 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005642 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5643 DRegister reg = source.IsFpuRegisterPair()
5644 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5645 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5646 int mem = source.IsFpuRegisterPair()
5647 ? destination.GetStackIndex()
5648 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005649 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005650 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005651 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005652 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5653 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5654 : destination.AsFpuRegister<SRegister>();
5655 int mem = source.IsFpuRegister()
5656 ? destination.GetStackIndex()
5657 : source.GetStackIndex();
5658
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005659 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005660 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005661 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005662 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005663 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5664 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005665 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005666 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005667 }
5668}
5669
5670void ParallelMoveResolverARM::SpillScratch(int reg) {
5671 __ Push(static_cast<Register>(reg));
5672}
5673
5674void ParallelMoveResolverARM::RestoreScratch(int reg) {
5675 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005676}
5677
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005678HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5679 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005680 switch (desired_class_load_kind) {
5681 case HLoadClass::LoadKind::kReferrersClass:
5682 break;
5683 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5684 DCHECK(!GetCompilerOptions().GetCompilePic());
5685 break;
5686 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5687 DCHECK(GetCompilerOptions().GetCompilePic());
5688 break;
5689 case HLoadClass::LoadKind::kBootImageAddress:
5690 break;
5691 case HLoadClass::LoadKind::kDexCacheAddress:
5692 DCHECK(Runtime::Current()->UseJitCompilation());
5693 break;
5694 case HLoadClass::LoadKind::kDexCachePcRelative:
5695 DCHECK(!Runtime::Current()->UseJitCompilation());
5696 // We disable pc-relative load when there is an irreducible loop, as the optimization
5697 // is incompatible with it.
5698 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5699 // with irreducible loops.
5700 if (GetGraph()->HasIrreducibleLoops()) {
5701 return HLoadClass::LoadKind::kDexCacheViaMethod;
5702 }
5703 break;
5704 case HLoadClass::LoadKind::kDexCacheViaMethod:
5705 break;
5706 }
5707 return desired_class_load_kind;
5708}
5709
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005710void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711 if (cls->NeedsAccessCheck()) {
5712 InvokeRuntimeCallingConvention calling_convention;
5713 CodeGenerator::CreateLoadClassLocationSummary(
5714 cls,
5715 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5716 Location::RegisterLocation(R0),
5717 /* code_generator_supports_read_barrier */ true);
5718 return;
5719 }
5720
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005721 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5722 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723 ? LocationSummary::kCallOnSlowPath
5724 : LocationSummary::kNoCall;
5725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005726 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005727 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005728 }
5729
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005730 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5731 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5732 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5733 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5734 locations->SetInAt(0, Location::RequiresRegister());
5735 }
5736 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005737}
5738
5739void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005740 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005741 if (cls->NeedsAccessCheck()) {
5742 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005743 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005744 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005745 return;
5746 }
5747
Roland Levillain3b359c72015-11-17 19:35:12 +00005748 Location out_loc = locations->Out();
5749 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005750
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005751 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5752 ? kWithoutReadBarrier
5753 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005754 bool generate_null_check = false;
5755 switch (cls->GetLoadKind()) {
5756 case HLoadClass::LoadKind::kReferrersClass: {
5757 DCHECK(!cls->CanCallRuntime());
5758 DCHECK(!cls->MustGenerateClinitCheck());
5759 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5760 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005761 GenerateGcRootFieldLoad(cls,
5762 out_loc,
5763 current_method,
5764 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005765 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005766 break;
5767 }
5768 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005769 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005770 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5771 cls->GetTypeIndex()));
5772 break;
5773 }
5774 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005775 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005776 CodeGeneratorARM::PcRelativePatchInfo* labels =
5777 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5778 __ BindTrackedLabel(&labels->movw_label);
5779 __ movw(out, /* placeholder */ 0u);
5780 __ BindTrackedLabel(&labels->movt_label);
5781 __ movt(out, /* placeholder */ 0u);
5782 __ BindTrackedLabel(&labels->add_pc_label);
5783 __ add(out, out, ShifterOperand(PC));
5784 break;
5785 }
5786 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005787 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788 DCHECK_NE(cls->GetAddress(), 0u);
5789 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5790 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5791 break;
5792 }
5793 case HLoadClass::LoadKind::kDexCacheAddress: {
5794 DCHECK_NE(cls->GetAddress(), 0u);
5795 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5796 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5797 // a 128B range. To try and reduce the number of literals if we load multiple types,
5798 // simply split the dex cache address to a 128B aligned base loaded from a literal
5799 // and the remaining offset embedded in the load.
5800 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5801 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5802 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5803 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5804 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5805 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5806 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005807 GenerateGcRootFieldLoad(cls, out_loc, out, offset, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 generate_null_check = !cls->IsInDexCache();
5809 break;
5810 }
5811 case HLoadClass::LoadKind::kDexCachePcRelative: {
5812 Register base_reg = locations->InAt(0).AsRegister<Register>();
5813 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5814 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5815 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005816 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005817 generate_null_check = !cls->IsInDexCache();
5818 break;
5819 }
5820 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5821 // /* GcRoot<mirror::Class>[] */ out =
5822 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5823 Register current_method = locations->InAt(0).AsRegister<Register>();
5824 __ LoadFromOffset(kLoadWord,
5825 out,
5826 current_method,
5827 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5828 // /* GcRoot<mirror::Class> */ out = out[type_index]
5829 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005830 GenerateGcRootFieldLoad(cls, out_loc, out, offset, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831 generate_null_check = !cls->IsInDexCache();
5832 }
5833 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005834
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5836 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005837 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005838 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5839 codegen_->AddSlowPath(slow_path);
5840 if (generate_null_check) {
5841 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5842 }
5843 if (cls->MustGenerateClinitCheck()) {
5844 GenerateClassInitializationCheck(slow_path, out);
5845 } else {
5846 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005847 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005848 }
5849}
5850
5851void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5852 LocationSummary* locations =
5853 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5854 locations->SetInAt(0, Location::RequiresRegister());
5855 if (check->HasUses()) {
5856 locations->SetOut(Location::SameAsFirstInput());
5857 }
5858}
5859
5860void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005861 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005862 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005863 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005864 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005865 GenerateClassInitializationCheck(slow_path,
5866 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005867}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005869void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005870 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005871 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5872 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5873 __ b(slow_path->GetEntryLabel(), LT);
5874 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5875 // properly. Therefore, we do a memory fence.
5876 __ dmb(ISH);
5877 __ Bind(slow_path->GetExitLabel());
5878}
5879
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5881 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 switch (desired_string_load_kind) {
5883 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5884 DCHECK(!GetCompilerOptions().GetCompilePic());
5885 break;
5886 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5887 DCHECK(GetCompilerOptions().GetCompilePic());
5888 break;
5889 case HLoadString::LoadKind::kBootImageAddress:
5890 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005891 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005892 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 break;
5894 case HLoadString::LoadKind::kDexCacheViaMethod:
5895 break;
5896 }
5897 return desired_string_load_kind;
5898}
5899
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005900void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005901 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005902 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5903 ? LocationSummary::kCallOnMainOnly
5904 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005905 : LocationSummary::kNoCall;
5906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005907
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005909 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005910 locations->SetOut(Location::RegisterLocation(R0));
5911 } else {
5912 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005913 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5914 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5915 // Rely on the pResolveString and/or marking to save everything, including temps.
5916 // Note that IP may theoretically be clobbered by saving/restoring the live register
5917 // (only one thanks to the custom calling convention), so we request a different temp.
5918 locations->AddTemp(Location::RequiresRegister());
5919 RegisterSet caller_saves = RegisterSet::Empty();
5920 InvokeRuntimeCallingConvention calling_convention;
5921 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5922 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5923 // that the the kPrimNot result register is the same as the first argument register.
5924 locations->SetCustomSlowPathCallerSaves(caller_saves);
5925 } else {
5926 // For non-Baker read barrier we have a temp-clobbering call.
5927 }
5928 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005930}
5931
5932void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005933 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005934 Location out_loc = locations->Out();
5935 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005936 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005937
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005938 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005939 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005940 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5941 load->GetStringIndex()));
5942 return; // No dex cache slow path.
5943 }
5944 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005945 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005946 CodeGeneratorARM::PcRelativePatchInfo* labels =
5947 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5948 __ BindTrackedLabel(&labels->movw_label);
5949 __ movw(out, /* placeholder */ 0u);
5950 __ BindTrackedLabel(&labels->movt_label);
5951 __ movt(out, /* placeholder */ 0u);
5952 __ BindTrackedLabel(&labels->add_pc_label);
5953 __ add(out, out, ShifterOperand(PC));
5954 return; // No dex cache slow path.
5955 }
5956 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 DCHECK_NE(load->GetAddress(), 0u);
5958 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5959 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5960 return; // No dex cache slow path.
5961 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005962 case HLoadString::LoadKind::kBssEntry: {
5963 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005964 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005965 CodeGeneratorARM::PcRelativePatchInfo* labels =
5966 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5967 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005968 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005969 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005970 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005971 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005972 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005973 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005974 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5975 codegen_->AddSlowPath(slow_path);
5976 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5977 __ Bind(slow_path->GetExitLabel());
5978 return;
5979 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005981 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005983
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005984 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5985 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5986 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005987 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005988 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5989 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5990 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005991}
5992
David Brazdilcb1c0552015-08-04 16:22:25 +01005993static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005994 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005995}
5996
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005997void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5998 LocationSummary* locations =
5999 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6000 locations->SetOut(Location::RequiresRegister());
6001}
6002
6003void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006004 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006005 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6006}
6007
6008void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6009 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6010}
6011
6012void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006013 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006014 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006015}
6016
6017void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6018 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006020 InvokeRuntimeCallingConvention calling_convention;
6021 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6022}
6023
6024void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006025 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006026 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006027}
6028
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006029// Temp is used for read barrier.
6030static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6031 if (kEmitCompilerReadBarrier &&
6032 (kUseBakerReadBarrier ||
6033 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6034 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6035 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6036 return 1;
6037 }
6038 return 0;
6039}
6040
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006041// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006042// interface pointer, one for loading the current interface.
6043// The other checks have one temp for loading the object's class.
6044static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6045 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6046 return 3;
6047 }
6048 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006049}
6050
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006051void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006053 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006054 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006055 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 case TypeCheckKind::kExactCheck:
6057 case TypeCheckKind::kAbstractClassCheck:
6058 case TypeCheckKind::kClassHierarchyCheck:
6059 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006060 call_kind =
6061 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006062 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063 break;
6064 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006065 case TypeCheckKind::kUnresolvedCheck:
6066 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 call_kind = LocationSummary::kCallOnSlowPath;
6068 break;
6069 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006070
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006071 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006072 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006073 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006074 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006075 locations->SetInAt(0, Location::RequiresRegister());
6076 locations->SetInAt(1, Location::RequiresRegister());
6077 // The "out" register is used as a temporary, so it overlaps with the inputs.
6078 // Note that TypeCheckSlowPathARM uses this register too.
6079 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006080 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006081}
6082
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006083void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 Location obj_loc = locations->InAt(0);
6087 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006088 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006089 Location out_loc = locations->Out();
6090 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006091 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6092 DCHECK_LE(num_temps, 1u);
6093 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006094 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6096 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6097 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006098 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006099 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006100
6101 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006102 // avoid null check if we know obj is not null.
6103 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006104 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006105 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006106
Roland Levillain3b359c72015-11-17 19:35:12 +00006107 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006108 GenerateReferenceLoadTwoRegisters(instruction,
6109 out_loc,
6110 obj_loc,
6111 class_offset,
6112 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006113 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006114
Roland Levillainc9285912015-12-18 10:38:42 +00006115 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006116 case TypeCheckKind::kExactCheck: {
6117 __ cmp(out, ShifterOperand(cls));
6118 // Classes must be equal for the instanceof to succeed.
6119 __ b(&zero, NE);
6120 __ LoadImmediate(out, 1);
6121 __ b(&done);
6122 break;
6123 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006124
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 case TypeCheckKind::kAbstractClassCheck: {
6126 // If the class is abstract, we eagerly fetch the super class of the
6127 // object to avoid doing a comparison we know will fail.
6128 Label loop;
6129 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006130 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006131 GenerateReferenceLoadOneRegister(instruction,
6132 out_loc,
6133 super_offset,
6134 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006135 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136 // If `out` is null, we use it for the result, and jump to `done`.
6137 __ CompareAndBranchIfZero(out, &done);
6138 __ cmp(out, ShifterOperand(cls));
6139 __ b(&loop, NE);
6140 __ LoadImmediate(out, 1);
6141 if (zero.IsLinked()) {
6142 __ b(&done);
6143 }
6144 break;
6145 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006146
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147 case TypeCheckKind::kClassHierarchyCheck: {
6148 // Walk over the class hierarchy to find a match.
6149 Label loop, success;
6150 __ Bind(&loop);
6151 __ cmp(out, ShifterOperand(cls));
6152 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006153 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006154 GenerateReferenceLoadOneRegister(instruction,
6155 out_loc,
6156 super_offset,
6157 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006158 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 __ CompareAndBranchIfNonZero(out, &loop);
6160 // If `out` is null, we use it for the result, and jump to `done`.
6161 __ b(&done);
6162 __ Bind(&success);
6163 __ LoadImmediate(out, 1);
6164 if (zero.IsLinked()) {
6165 __ b(&done);
6166 }
6167 break;
6168 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006169
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006171 // Do an exact check.
6172 Label exact_check;
6173 __ cmp(out, ShifterOperand(cls));
6174 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006175 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006177 GenerateReferenceLoadOneRegister(instruction,
6178 out_loc,
6179 component_offset,
6180 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006181 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 // If `out` is null, we use it for the result, and jump to `done`.
6183 __ CompareAndBranchIfZero(out, &done);
6184 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6185 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6186 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006187 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 __ LoadImmediate(out, 1);
6189 __ b(&done);
6190 break;
6191 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 case TypeCheckKind::kArrayCheck: {
6194 __ cmp(out, ShifterOperand(cls));
6195 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006196 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6197 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 codegen_->AddSlowPath(slow_path);
6199 __ b(slow_path->GetEntryLabel(), NE);
6200 __ LoadImmediate(out, 1);
6201 if (zero.IsLinked()) {
6202 __ b(&done);
6203 }
6204 break;
6205 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006206
Calin Juravle98893e12015-10-02 21:05:03 +01006207 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006208 case TypeCheckKind::kInterfaceCheck: {
6209 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006210 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006211 // cases.
6212 //
6213 // We cannot directly call the InstanceofNonTrivial runtime
6214 // entry point without resorting to a type checking slow path
6215 // here (i.e. by calling InvokeRuntime directly), as it would
6216 // require to assign fixed registers for the inputs of this
6217 // HInstanceOf instruction (following the runtime calling
6218 // convention), which might be cluttered by the potential first
6219 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006220 //
6221 // TODO: Introduce a new runtime entry point taking the object
6222 // to test (instead of its class) as argument, and let it deal
6223 // with the read barrier issues. This will let us refactor this
6224 // case of the `switch` code as it was previously (with a direct
6225 // call to the runtime not using a type checking slow path).
6226 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 DCHECK(locations->OnlyCallsOnSlowPath());
6228 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6229 /* is_fatal */ false);
6230 codegen_->AddSlowPath(slow_path);
6231 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 if (zero.IsLinked()) {
6233 __ b(&done);
6234 }
6235 break;
6236 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006237 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006240 __ Bind(&zero);
6241 __ LoadImmediate(out, 0);
6242 }
6243
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 if (done.IsLinked()) {
6245 __ Bind(&done);
6246 }
6247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006248 if (slow_path != nullptr) {
6249 __ Bind(slow_path->GetExitLabel());
6250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006251}
6252
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006253void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006254 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6255 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6256
Roland Levillain3b359c72015-11-17 19:35:12 +00006257 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6258 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006259 case TypeCheckKind::kExactCheck:
6260 case TypeCheckKind::kAbstractClassCheck:
6261 case TypeCheckKind::kClassHierarchyCheck:
6262 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006263 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6264 LocationSummary::kCallOnSlowPath :
6265 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 break;
6267 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006268 case TypeCheckKind::kUnresolvedCheck:
6269 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 call_kind = LocationSummary::kCallOnSlowPath;
6271 break;
6272 }
6273
Roland Levillain3b359c72015-11-17 19:35:12 +00006274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6275 locations->SetInAt(0, Location::RequiresRegister());
6276 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006277 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006278}
6279
6280void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006281 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006282 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006283 Location obj_loc = locations->InAt(0);
6284 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006285 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006286 Location temp_loc = locations->GetTemp(0);
6287 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006288 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6289 DCHECK_LE(num_temps, 3u);
6290 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6291 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6292 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6293 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6294 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6295 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6296 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6297 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6298 const uint32_t object_array_data_offset =
6299 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006300
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006301 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6302 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6303 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006304 bool is_type_check_slow_path_fatal = false;
6305 if (!kEmitCompilerReadBarrier) {
6306 is_type_check_slow_path_fatal =
6307 (type_check_kind == TypeCheckKind::kExactCheck ||
6308 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6309 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6310 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6311 !instruction->CanThrowIntoCatchBlock();
6312 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006313 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006314 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6315 is_type_check_slow_path_fatal);
6316 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317
6318 Label done;
6319 // Avoid null check if we know obj is not null.
6320 if (instruction->MustDoNullCheck()) {
6321 __ CompareAndBranchIfZero(obj, &done);
6322 }
6323
Roland Levillain3b359c72015-11-17 19:35:12 +00006324 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 case TypeCheckKind::kExactCheck:
6326 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006327 // /* HeapReference<Class> */ temp = obj->klass_
6328 GenerateReferenceLoadTwoRegisters(instruction,
6329 temp_loc,
6330 obj_loc,
6331 class_offset,
6332 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006333 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 __ cmp(temp, ShifterOperand(cls));
6336 // Jump to slow path for throwing the exception or doing a
6337 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006338 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339 break;
6340 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006343 // /* HeapReference<Class> */ temp = obj->klass_
6344 GenerateReferenceLoadTwoRegisters(instruction,
6345 temp_loc,
6346 obj_loc,
6347 class_offset,
6348 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006349 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 // If the class is abstract, we eagerly fetch the super class of the
6352 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006353 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006355 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006356 GenerateReferenceLoadOneRegister(instruction,
6357 temp_loc,
6358 super_offset,
6359 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006360 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006361
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006362 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6363 // exception.
6364 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006365
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006366 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 __ cmp(temp, ShifterOperand(cls));
6368 __ b(&loop, NE);
6369 break;
6370 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 case TypeCheckKind::kClassHierarchyCheck: {
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 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006382 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 __ Bind(&loop);
6384 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006385 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006386
Roland Levillain3b359c72015-11-17 19:35:12 +00006387 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006388 GenerateReferenceLoadOneRegister(instruction,
6389 temp_loc,
6390 super_offset,
6391 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006392 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006393
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006394 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6395 // exception.
6396 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6397 // Otherwise, jump to the beginning of the loop.
6398 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399 break;
6400 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006403 // /* HeapReference<Class> */ temp = obj->klass_
6404 GenerateReferenceLoadTwoRegisters(instruction,
6405 temp_loc,
6406 obj_loc,
6407 class_offset,
6408 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006409 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006410
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006411 // Do an exact check.
6412 __ cmp(temp, ShifterOperand(cls));
6413 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006414
6415 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006416 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006417 GenerateReferenceLoadOneRegister(instruction,
6418 temp_loc,
6419 component_offset,
6420 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006421 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006422 // If the component type is null, jump to the slow path to throw the exception.
6423 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6424 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6425 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006427 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006428 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 break;
6430 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006431
Calin Juravle98893e12015-10-02 21:05:03 +01006432 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006433 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006434 // We cannot directly call the CheckCast runtime entry point
6435 // without resorting to a type checking slow path here (i.e. by
6436 // calling InvokeRuntime directly), as it would require to
6437 // assign fixed registers for the inputs of this HInstanceOf
6438 // instruction (following the runtime calling convention), which
6439 // might be cluttered by the potential first read barrier
6440 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006441
Roland Levillain3b359c72015-11-17 19:35:12 +00006442 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006444
6445 case TypeCheckKind::kInterfaceCheck: {
6446 // Avoid read barriers to improve performance of the fast path. We can not get false
6447 // positives by doing this.
6448 // /* HeapReference<Class> */ temp = obj->klass_
6449 GenerateReferenceLoadTwoRegisters(instruction,
6450 temp_loc,
6451 obj_loc,
6452 class_offset,
6453 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006454 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006455
6456 // /* HeapReference<Class> */ temp = temp->iftable_
6457 GenerateReferenceLoadTwoRegisters(instruction,
6458 temp_loc,
6459 temp_loc,
6460 iftable_offset,
6461 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006462 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006463 Label is_null;
6464 // Null iftable means it is empty and will always fail the check.
6465 // Not cbz since the temp may not be a low register.
6466 __ CompareAndBranchIfZero(temp, &is_null);
6467
6468 // Loop through the iftable and check if any class matches.
6469 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6470
6471 Label start_loop;
6472 __ Bind(&start_loop);
6473 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6474 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
6475 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6476 __ b(&done, EQ); // Return if same class.
6477 // Go to next interface.
6478 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6479 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6480 maybe_temp2_loc.AsRegister<Register>(),
6481 ShifterOperand(2));
6482 // Not cbnz since the temp may not be a low register.
6483 __ CompareAndBranchIfNonZero(maybe_temp2_loc.AsRegister<Register>(), &start_loop);
6484 __ Bind(&is_null);
6485
6486 __ b(type_check_slow_path->GetEntryLabel());
6487 break;
6488 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 }
6490 __ Bind(&done);
6491
Roland Levillain3b359c72015-11-17 19:35:12 +00006492 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006493}
6494
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006495void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6496 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006498 InvokeRuntimeCallingConvention calling_convention;
6499 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6500}
6501
6502void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006503 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6504 instruction,
6505 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006506 if (instruction->IsEnter()) {
6507 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6508 } else {
6509 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6510 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006511}
6512
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006513void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6514void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6515void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006516
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006517void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006518 LocationSummary* locations =
6519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6520 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6521 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006522 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006523 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006524 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006526}
6527
6528void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6529 HandleBitwiseOperation(instruction);
6530}
6531
6532void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6533 HandleBitwiseOperation(instruction);
6534}
6535
6536void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6537 HandleBitwiseOperation(instruction);
6538}
6539
Artem Serov7fc63502016-02-09 17:15:29 +00006540
6541void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6542 LocationSummary* locations =
6543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6544 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6545 || instruction->GetResultType() == Primitive::kPrimLong);
6546
6547 locations->SetInAt(0, Location::RequiresRegister());
6548 locations->SetInAt(1, Location::RequiresRegister());
6549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6550}
6551
6552void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6553 LocationSummary* locations = instruction->GetLocations();
6554 Location first = locations->InAt(0);
6555 Location second = locations->InAt(1);
6556 Location out = locations->Out();
6557
6558 if (instruction->GetResultType() == Primitive::kPrimInt) {
6559 Register first_reg = first.AsRegister<Register>();
6560 ShifterOperand second_reg(second.AsRegister<Register>());
6561 Register out_reg = out.AsRegister<Register>();
6562
6563 switch (instruction->GetOpKind()) {
6564 case HInstruction::kAnd:
6565 __ bic(out_reg, first_reg, second_reg);
6566 break;
6567 case HInstruction::kOr:
6568 __ orn(out_reg, first_reg, second_reg);
6569 break;
6570 // There is no EON on arm.
6571 case HInstruction::kXor:
6572 default:
6573 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6574 UNREACHABLE();
6575 }
6576 return;
6577
6578 } else {
6579 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6580 Register first_low = first.AsRegisterPairLow<Register>();
6581 Register first_high = first.AsRegisterPairHigh<Register>();
6582 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6583 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6584 Register out_low = out.AsRegisterPairLow<Register>();
6585 Register out_high = out.AsRegisterPairHigh<Register>();
6586
6587 switch (instruction->GetOpKind()) {
6588 case HInstruction::kAnd:
6589 __ bic(out_low, first_low, second_low);
6590 __ bic(out_high, first_high, second_high);
6591 break;
6592 case HInstruction::kOr:
6593 __ orn(out_low, first_low, second_low);
6594 __ orn(out_high, first_high, second_high);
6595 break;
6596 // There is no EON on arm.
6597 case HInstruction::kXor:
6598 default:
6599 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6600 UNREACHABLE();
6601 }
6602 }
6603}
6604
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006605void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6606 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6607 if (value == 0xffffffffu) {
6608 if (out != first) {
6609 __ mov(out, ShifterOperand(first));
6610 }
6611 return;
6612 }
6613 if (value == 0u) {
6614 __ mov(out, ShifterOperand(0));
6615 return;
6616 }
6617 ShifterOperand so;
6618 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6619 __ and_(out, first, so);
6620 } else {
6621 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6622 __ bic(out, first, ShifterOperand(~value));
6623 }
6624}
6625
6626void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6627 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6628 if (value == 0u) {
6629 if (out != first) {
6630 __ mov(out, ShifterOperand(first));
6631 }
6632 return;
6633 }
6634 if (value == 0xffffffffu) {
6635 __ mvn(out, ShifterOperand(0));
6636 return;
6637 }
6638 ShifterOperand so;
6639 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6640 __ orr(out, first, so);
6641 } else {
6642 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6643 __ orn(out, first, ShifterOperand(~value));
6644 }
6645}
6646
6647void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6648 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6649 if (value == 0u) {
6650 if (out != first) {
6651 __ mov(out, ShifterOperand(first));
6652 }
6653 return;
6654 }
6655 __ eor(out, first, ShifterOperand(value));
6656}
6657
Vladimir Marko59751a72016-08-05 14:37:27 +01006658void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6659 Location first,
6660 uint64_t value) {
6661 Register out_low = out.AsRegisterPairLow<Register>();
6662 Register out_high = out.AsRegisterPairHigh<Register>();
6663 Register first_low = first.AsRegisterPairLow<Register>();
6664 Register first_high = first.AsRegisterPairHigh<Register>();
6665 uint32_t value_low = Low32Bits(value);
6666 uint32_t value_high = High32Bits(value);
6667 if (value_low == 0u) {
6668 if (out_low != first_low) {
6669 __ mov(out_low, ShifterOperand(first_low));
6670 }
6671 __ AddConstant(out_high, first_high, value_high);
6672 return;
6673 }
6674 __ AddConstantSetFlags(out_low, first_low, value_low);
6675 ShifterOperand so;
6676 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6677 __ adc(out_high, first_high, so);
6678 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6679 __ sbc(out_high, first_high, so);
6680 } else {
6681 LOG(FATAL) << "Unexpected constant " << value_high;
6682 UNREACHABLE();
6683 }
6684}
6685
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006686void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6687 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006688 Location first = locations->InAt(0);
6689 Location second = locations->InAt(1);
6690 Location out = locations->Out();
6691
6692 if (second.IsConstant()) {
6693 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6694 uint32_t value_low = Low32Bits(value);
6695 if (instruction->GetResultType() == Primitive::kPrimInt) {
6696 Register first_reg = first.AsRegister<Register>();
6697 Register out_reg = out.AsRegister<Register>();
6698 if (instruction->IsAnd()) {
6699 GenerateAndConst(out_reg, first_reg, value_low);
6700 } else if (instruction->IsOr()) {
6701 GenerateOrrConst(out_reg, first_reg, value_low);
6702 } else {
6703 DCHECK(instruction->IsXor());
6704 GenerateEorConst(out_reg, first_reg, value_low);
6705 }
6706 } else {
6707 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6708 uint32_t value_high = High32Bits(value);
6709 Register first_low = first.AsRegisterPairLow<Register>();
6710 Register first_high = first.AsRegisterPairHigh<Register>();
6711 Register out_low = out.AsRegisterPairLow<Register>();
6712 Register out_high = out.AsRegisterPairHigh<Register>();
6713 if (instruction->IsAnd()) {
6714 GenerateAndConst(out_low, first_low, value_low);
6715 GenerateAndConst(out_high, first_high, value_high);
6716 } else if (instruction->IsOr()) {
6717 GenerateOrrConst(out_low, first_low, value_low);
6718 GenerateOrrConst(out_high, first_high, value_high);
6719 } else {
6720 DCHECK(instruction->IsXor());
6721 GenerateEorConst(out_low, first_low, value_low);
6722 GenerateEorConst(out_high, first_high, value_high);
6723 }
6724 }
6725 return;
6726 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006727
6728 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006729 Register first_reg = first.AsRegister<Register>();
6730 ShifterOperand second_reg(second.AsRegister<Register>());
6731 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006732 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006733 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006734 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006735 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006736 } else {
6737 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006738 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006739 }
6740 } else {
6741 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006742 Register first_low = first.AsRegisterPairLow<Register>();
6743 Register first_high = first.AsRegisterPairHigh<Register>();
6744 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6745 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6746 Register out_low = out.AsRegisterPairLow<Register>();
6747 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006748 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006749 __ and_(out_low, first_low, second_low);
6750 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006751 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006752 __ orr(out_low, first_low, second_low);
6753 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006754 } else {
6755 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006756 __ eor(out_low, first_low, second_low);
6757 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006758 }
6759 }
6760}
6761
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006762void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6763 HInstruction* instruction,
6764 Location out,
6765 uint32_t offset,
6766 Location maybe_temp,
6767 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006768 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006769 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006770 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006771 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006772 if (kUseBakerReadBarrier) {
6773 // Load with fast path based Baker's read barrier.
6774 // /* HeapReference<Object> */ out = *(out + offset)
6775 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006776 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006777 } else {
6778 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006779 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006780 // in the following move operation, as we will need it for the
6781 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006782 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006783 // /* HeapReference<Object> */ out = *(out + offset)
6784 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006785 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006786 }
6787 } else {
6788 // Plain load with no read barrier.
6789 // /* HeapReference<Object> */ out = *(out + offset)
6790 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6791 __ MaybeUnpoisonHeapReference(out_reg);
6792 }
6793}
6794
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006795void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6796 HInstruction* instruction,
6797 Location out,
6798 Location obj,
6799 uint32_t offset,
6800 Location maybe_temp,
6801 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006802 Register out_reg = out.AsRegister<Register>();
6803 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006804 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006805 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006806 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006807 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006808 // Load with fast path based Baker's read barrier.
6809 // /* HeapReference<Object> */ out = *(obj + offset)
6810 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006811 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006812 } else {
6813 // Load with slow path based read barrier.
6814 // /* HeapReference<Object> */ out = *(obj + offset)
6815 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6816 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6817 }
6818 } else {
6819 // Plain load with no read barrier.
6820 // /* HeapReference<Object> */ out = *(obj + offset)
6821 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6822 __ MaybeUnpoisonHeapReference(out_reg);
6823 }
6824}
6825
6826void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6827 Location root,
6828 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006829 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006830 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006831 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006832 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006833 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006834 if (kUseBakerReadBarrier) {
6835 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6836 // Baker's read barrier are used:
6837 //
6838 // root = obj.field;
6839 // if (Thread::Current()->GetIsGcMarking()) {
6840 // root = ReadBarrier::Mark(root)
6841 // }
6842
6843 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6844 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6845 static_assert(
6846 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6847 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6848 "have different sizes.");
6849 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6850 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6851 "have different sizes.");
6852
Vladimir Marko953437b2016-08-24 08:30:46 +00006853 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006854 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006855 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006856 codegen_->AddSlowPath(slow_path);
6857
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006858 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006859 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006860 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006861 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6862 __ Bind(slow_path->GetExitLabel());
6863 } else {
6864 // GC root loaded through a slow path for read barriers other
6865 // than Baker's.
6866 // /* GcRoot<mirror::Object>* */ root = obj + offset
6867 __ AddConstant(root_reg, obj, offset);
6868 // /* mirror::Object* */ root = root->Read()
6869 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6870 }
6871 } else {
6872 // Plain GC root load with no read barrier.
6873 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6874 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6875 // Note that GC roots are not affected by heap poisoning, thus we
6876 // do not have to unpoison `root_reg` here.
6877 }
6878}
6879
6880void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6881 Location ref,
6882 Register obj,
6883 uint32_t offset,
6884 Location temp,
6885 bool needs_null_check) {
6886 DCHECK(kEmitCompilerReadBarrier);
6887 DCHECK(kUseBakerReadBarrier);
6888
6889 // /* HeapReference<Object> */ ref = *(obj + offset)
6890 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006891 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006892 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006893 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006894}
6895
6896void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6897 Location ref,
6898 Register obj,
6899 uint32_t data_offset,
6900 Location index,
6901 Location temp,
6902 bool needs_null_check) {
6903 DCHECK(kEmitCompilerReadBarrier);
6904 DCHECK(kUseBakerReadBarrier);
6905
Roland Levillainbfea3352016-06-23 13:48:47 +01006906 static_assert(
6907 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6908 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006909 // /* HeapReference<Object> */ ref =
6910 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006911 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006912 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006913 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006914}
6915
6916void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6917 Location ref,
6918 Register obj,
6919 uint32_t offset,
6920 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006921 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006922 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006923 bool needs_null_check,
6924 bool always_update_field,
6925 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006926 DCHECK(kEmitCompilerReadBarrier);
6927 DCHECK(kUseBakerReadBarrier);
6928
6929 // In slow path based read barriers, the read barrier call is
6930 // inserted after the original load. However, in fast path based
6931 // Baker's read barriers, we need to perform the load of
6932 // mirror::Object::monitor_ *before* the original reference load.
6933 // This load-load ordering is required by the read barrier.
6934 // The fast path/slow path (for Baker's algorithm) should look like:
6935 //
6936 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6937 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6938 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006939 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006940 // if (is_gray) {
6941 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6942 // }
6943 //
6944 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006945 // slightly more complex as it performs additional checks that we do
6946 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006947
6948 Register ref_reg = ref.AsRegister<Register>();
6949 Register temp_reg = temp.AsRegister<Register>();
6950 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6951
6952 // /* int32_t */ monitor = obj->monitor_
6953 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6954 if (needs_null_check) {
6955 MaybeRecordImplicitNullCheck(instruction);
6956 }
6957 // /* LockWord */ lock_word = LockWord(monitor)
6958 static_assert(sizeof(LockWord) == sizeof(int32_t),
6959 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006960
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006961 // Introduce a dependency on the lock_word including the rb_state,
6962 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006963 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006964 // `obj` is unchanged by this operation, but its value now depends
6965 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006966 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006967
6968 // The actual reference load.
6969 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006970 // Load types involving an "index": ArrayGet,
6971 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6972 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006973 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006974 if (index.IsConstant()) {
6975 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006976 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006977 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6978 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006979 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006980 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6981 // intrinsics, which use a register pair as index ("long
6982 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01006983 Register index_reg = index.IsRegisterPair()
6984 ? index.AsRegisterPairLow<Register>()
6985 : index.AsRegister<Register>();
6986 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006987 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6988 }
6989 } else {
6990 // /* HeapReference<Object> */ ref = *(obj + offset)
6991 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6992 }
6993
6994 // Object* ref = ref_addr->AsMirrorPtr()
6995 __ MaybeUnpoisonHeapReference(ref_reg);
6996
Vladimir Marko953437b2016-08-24 08:30:46 +00006997 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006998 SlowPathCodeARM* slow_path;
6999 if (always_update_field) {
7000 DCHECK(temp2 != nullptr);
7001 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7002 // of the form `obj + field_offset`, where `obj` is a register and
7003 // `field_offset` is a register pair (of which only the lower half
7004 // is used). Thus `offset` and `scale_factor` above are expected
7005 // to be null in this code path.
7006 DCHECK_EQ(offset, 0u);
7007 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7008 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7009 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7010 } else {
7011 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7012 }
Roland Levillainc9285912015-12-18 10:38:42 +00007013 AddSlowPath(slow_path);
7014
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007015 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007016 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007017 // Given the numeric representation, it's enough to check the low bit of the
7018 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7019 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007020 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7021 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007022 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7023 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007024 __ Bind(slow_path->GetExitLabel());
7025}
7026
7027void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7028 Location out,
7029 Location ref,
7030 Location obj,
7031 uint32_t offset,
7032 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007033 DCHECK(kEmitCompilerReadBarrier);
7034
Roland Levillainc9285912015-12-18 10:38:42 +00007035 // Insert a slow path based read barrier *after* the reference load.
7036 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007037 // If heap poisoning is enabled, the unpoisoning of the loaded
7038 // reference will be carried out by the runtime within the slow
7039 // path.
7040 //
7041 // Note that `ref` currently does not get unpoisoned (when heap
7042 // poisoning is enabled), which is alright as the `ref` argument is
7043 // not used by the artReadBarrierSlow entry point.
7044 //
7045 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007046 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007047 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7048 AddSlowPath(slow_path);
7049
Roland Levillain3b359c72015-11-17 19:35:12 +00007050 __ b(slow_path->GetEntryLabel());
7051 __ Bind(slow_path->GetExitLabel());
7052}
7053
Roland Levillainc9285912015-12-18 10:38:42 +00007054void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7055 Location out,
7056 Location ref,
7057 Location obj,
7058 uint32_t offset,
7059 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007060 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007061 // Baker's read barriers shall be handled by the fast path
7062 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7063 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007064 // If heap poisoning is enabled, unpoisoning will be taken care of
7065 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007066 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007067 } else if (kPoisonHeapReferences) {
7068 __ UnpoisonHeapReference(out.AsRegister<Register>());
7069 }
7070}
7071
Roland Levillainc9285912015-12-18 10:38:42 +00007072void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7073 Location out,
7074 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007075 DCHECK(kEmitCompilerReadBarrier);
7076
Roland Levillainc9285912015-12-18 10:38:42 +00007077 // Insert a slow path based read barrier *after* the GC root load.
7078 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007079 // Note that GC roots are not affected by heap poisoning, so we do
7080 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007081 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007082 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7083 AddSlowPath(slow_path);
7084
Roland Levillain3b359c72015-11-17 19:35:12 +00007085 __ b(slow_path->GetEntryLabel());
7086 __ Bind(slow_path->GetExitLabel());
7087}
7088
Vladimir Markodc151b22015-10-15 18:02:30 +01007089HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7090 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007091 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007092 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7093 // We disable pc-relative load when there is an irreducible loop, as the optimization
7094 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007095 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7096 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007097 if (GetGraph()->HasIrreducibleLoops() &&
7098 (dispatch_info.method_load_kind ==
7099 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7100 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7101 }
7102
7103 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007104 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007105 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007106 // Calls across dex files are more likely to exceed the available BL range,
7107 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7108 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7109 (desired_dispatch_info.method_load_kind ==
7110 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7111 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7112 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7113 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007114 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01007115 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007116 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01007117 0u
7118 };
7119 }
7120 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007121 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007122}
7123
Vladimir Markob4536b72015-11-24 13:45:23 +00007124Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7125 Register temp) {
7126 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7127 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7128 if (!invoke->GetLocations()->Intrinsified()) {
7129 return location.AsRegister<Register>();
7130 }
7131 // For intrinsics we allow any location, so it may be on the stack.
7132 if (!location.IsRegister()) {
7133 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7134 return temp;
7135 }
7136 // For register locations, check if the register was saved. If so, get it from the stack.
7137 // Note: There is a chance that the register was saved but not overwritten, so we could
7138 // save one load. However, since this is just an intrinsic slow path we prefer this
7139 // simple and more robust approach rather that trying to determine if that's the case.
7140 SlowPathCode* slow_path = GetCurrentSlowPath();
7141 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7142 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7143 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7144 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7145 return temp;
7146 }
7147 return location.AsRegister<Register>();
7148}
7149
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007150void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007151 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00007152 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00007153 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7154 // LR = code address from literal pool with link-time patch.
7155 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00007156 break;
7157 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7158 // LR = invoke->GetDirectCodePtr();
7159 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00007160 break;
7161 default:
7162 break;
7163 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007164
Vladimir Marko58155012015-08-19 12:49:41 +00007165 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7166 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007167 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7168 uint32_t offset =
7169 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007170 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007171 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007172 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007173 }
Vladimir Marko58155012015-08-19 12:49:41 +00007174 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007175 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007176 break;
7177 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7178 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7179 break;
7180 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
7181 __ LoadLiteral(temp.AsRegister<Register>(),
7182 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
7183 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007184 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7185 HArmDexCacheArraysBase* base =
7186 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7187 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7188 temp.AsRegister<Register>());
7189 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7190 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7191 break;
7192 }
Vladimir Marko58155012015-08-19 12:49:41 +00007193 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007194 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007195 Register method_reg;
7196 Register reg = temp.AsRegister<Register>();
7197 if (current_method.IsRegister()) {
7198 method_reg = current_method.AsRegister<Register>();
7199 } else {
7200 DCHECK(invoke->GetLocations()->Intrinsified());
7201 DCHECK(!current_method.IsValid());
7202 method_reg = reg;
7203 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7204 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007205 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7206 __ LoadFromOffset(kLoadWord,
7207 reg,
7208 method_reg,
7209 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007210 // temp = temp[index_in_cache];
7211 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7212 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007213 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7214 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007215 }
Vladimir Marko58155012015-08-19 12:49:41 +00007216 }
7217
7218 switch (invoke->GetCodePtrLocation()) {
7219 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7220 __ bl(GetFrameEntryLabel());
7221 break;
7222 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007223 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7224 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007225 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01007226 // Arbitrarily branch to the BL itself, override at link time.
7227 __ bl(&relative_call_patches_.back().label);
7228 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007229 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7230 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7231 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00007232 // LR()
7233 __ blx(LR);
7234 break;
7235 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(
7287 const DexFile& dex_file, uint32_t type_index) {
7288 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7289}
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,
7303 uint32_t string_index) {
7304 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,
7310 uint32_t type_index) {
7311 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
7322Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7323 return DeduplicateUint32Literal(address, &uint32_literals_);
7324}
7325
Vladimir Markoaad75c62016-10-03 08:46:48 +00007326template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7327inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7328 const ArenaDeque<PcRelativePatchInfo>& infos,
7329 ArenaVector<LinkerPatch>* linker_patches) {
7330 for (const PcRelativePatchInfo& info : infos) {
7331 const DexFile& dex_file = info.target_dex_file;
7332 size_t offset_or_index = info.offset_or_index;
7333 DCHECK(info.add_pc_label.IsBound());
7334 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7335 // Add MOVW patch.
7336 DCHECK(info.movw_label.IsBound());
7337 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7338 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7339 // Add MOVT patch.
7340 DCHECK(info.movt_label.IsBound());
7341 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7342 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7343 }
7344}
7345
Vladimir Marko58155012015-08-19 12:49:41 +00007346void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7347 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007348 size_t size =
7349 method_patches_.size() +
7350 call_patches_.size() +
7351 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007352 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007353 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007354 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007355 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007356 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007357 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007358 linker_patches->reserve(size);
7359 for (const auto& entry : method_patches_) {
7360 const MethodReference& target_method = entry.first;
7361 Literal* literal = entry.second;
7362 DCHECK(literal->GetLabel()->IsBound());
7363 uint32_t literal_offset = literal->GetLabel()->Position();
7364 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7365 target_method.dex_file,
7366 target_method.dex_method_index));
7367 }
7368 for (const auto& entry : call_patches_) {
7369 const MethodReference& target_method = entry.first;
7370 Literal* literal = entry.second;
7371 DCHECK(literal->GetLabel()->IsBound());
7372 uint32_t literal_offset = literal->GetLabel()->Position();
7373 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7374 target_method.dex_file,
7375 target_method.dex_method_index));
7376 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007377 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007378 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007379 linker_patches->push_back(
7380 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007381 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007382 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7383 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007384 for (const auto& entry : boot_image_string_patches_) {
7385 const StringReference& target_string = entry.first;
7386 Literal* literal = entry.second;
7387 DCHECK(literal->GetLabel()->IsBound());
7388 uint32_t literal_offset = literal->GetLabel()->Position();
7389 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7390 target_string.dex_file,
7391 target_string.string_index));
7392 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007393 if (!GetCompilerOptions().IsBootImage()) {
7394 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7395 linker_patches);
7396 } else {
7397 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7398 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007399 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007400 for (const auto& entry : boot_image_type_patches_) {
7401 const TypeReference& target_type = entry.first;
7402 Literal* literal = entry.second;
7403 DCHECK(literal->GetLabel()->IsBound());
7404 uint32_t literal_offset = literal->GetLabel()->Position();
7405 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7406 target_type.dex_file,
7407 target_type.type_index));
7408 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007409 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7410 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007411 for (const auto& entry : boot_image_address_patches_) {
7412 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7413 Literal* literal = entry.second;
7414 DCHECK(literal->GetLabel()->IsBound());
7415 uint32_t literal_offset = literal->GetLabel()->Position();
7416 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7417 }
7418}
7419
7420Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7421 return map->GetOrCreate(
7422 value,
7423 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007424}
7425
7426Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7427 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007428 return map->GetOrCreate(
7429 target_method,
7430 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007431}
7432
7433Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7434 return DeduplicateMethodLiteral(target_method, &method_patches_);
7435}
7436
7437Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7438 return DeduplicateMethodLiteral(target_method, &call_patches_);
7439}
7440
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007441void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7442 LocationSummary* locations =
7443 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7444 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7445 Location::RequiresRegister());
7446 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7447 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7449}
7450
7451void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7452 LocationSummary* locations = instr->GetLocations();
7453 Register res = locations->Out().AsRegister<Register>();
7454 Register accumulator =
7455 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7456 Register mul_left =
7457 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7458 Register mul_right =
7459 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7460
7461 if (instr->GetOpKind() == HInstruction::kAdd) {
7462 __ mla(res, mul_left, mul_right, accumulator);
7463 } else {
7464 __ mls(res, mul_left, mul_right, accumulator);
7465 }
7466}
7467
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007468void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007469 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007470 LOG(FATAL) << "Unreachable";
7471}
7472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007473void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007474 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007475 LOG(FATAL) << "Unreachable";
7476}
7477
Mark Mendellfe57faa2015-09-18 09:26:15 -04007478// Simple implementation of packed switch - generate cascaded compare/jumps.
7479void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7480 LocationSummary* locations =
7481 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7482 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007483 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007484 codegen_->GetAssembler()->IsThumb()) {
7485 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7486 if (switch_instr->GetStartValue() != 0) {
7487 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7488 }
7489 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007490}
7491
7492void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7493 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007494 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007495 LocationSummary* locations = switch_instr->GetLocations();
7496 Register value_reg = locations->InAt(0).AsRegister<Register>();
7497 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7498
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007499 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007500 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007501 Register temp_reg = IP;
7502 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7503 // the immediate, because IP is used as the destination register. For the other
7504 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7505 // and they can be encoded in the instruction without making use of IP register.
7506 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7507
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007508 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007509 // Jump to successors[0] if value == lower_bound.
7510 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7511 int32_t last_index = 0;
7512 for (; num_entries - last_index > 2; last_index += 2) {
7513 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7514 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7515 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7516 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7517 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7518 }
7519 if (num_entries - last_index == 2) {
7520 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007521 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007522 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007523 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007524
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007525 // And the default for any other value.
7526 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7527 __ b(codegen_->GetLabelOf(default_block));
7528 }
7529 } else {
7530 // Create a table lookup.
7531 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7532
7533 // Materialize a pointer to the switch table
7534 std::vector<Label*> labels(num_entries);
7535 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7536 for (uint32_t i = 0; i < num_entries; i++) {
7537 labels[i] = codegen_->GetLabelOf(successors[i]);
7538 }
7539 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7540
7541 // Remove the bias.
7542 Register key_reg;
7543 if (lower_bound != 0) {
7544 key_reg = locations->GetTemp(1).AsRegister<Register>();
7545 __ AddConstant(key_reg, value_reg, -lower_bound);
7546 } else {
7547 key_reg = value_reg;
7548 }
7549
7550 // Check whether the value is in the table, jump to default block if not.
7551 __ CmpConstant(key_reg, num_entries - 1);
7552 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7553
7554 // Load the displacement from the table.
7555 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7556
7557 // Dispatch is a direct add to the PC (for Thumb2).
7558 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007559 }
7560}
7561
Vladimir Markob4536b72015-11-24 13:45:23 +00007562void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7564 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007565}
7566
7567void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7568 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007569 CodeGeneratorARM::PcRelativePatchInfo* labels =
7570 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007571 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007572 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007573 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007574 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007575 __ BindTrackedLabel(&labels->add_pc_label);
7576 __ add(base_reg, base_reg, ShifterOperand(PC));
7577}
7578
Andreas Gampe85b62f22015-09-09 13:15:38 -07007579void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7580 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007581 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007582 return;
7583 }
7584
7585 DCHECK_NE(type, Primitive::kPrimVoid);
7586
7587 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7588 if (return_loc.Equals(trg)) {
7589 return;
7590 }
7591
7592 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7593 // with the last branch.
7594 if (type == Primitive::kPrimLong) {
7595 HParallelMove parallel_move(GetGraph()->GetArena());
7596 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7597 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7598 GetMoveResolver()->EmitNativeCode(&parallel_move);
7599 } else if (type == Primitive::kPrimDouble) {
7600 HParallelMove parallel_move(GetGraph()->GetArena());
7601 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7602 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7603 GetMoveResolver()->EmitNativeCode(&parallel_move);
7604 } else {
7605 // Let the parallel move resolver take care of all of this.
7606 HParallelMove parallel_move(GetGraph()->GetArena());
7607 parallel_move.AddMove(return_loc, trg, type, nullptr);
7608 GetMoveResolver()->EmitNativeCode(&parallel_move);
7609 }
7610}
7611
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007612void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7613 LocationSummary* locations =
7614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7615 locations->SetInAt(0, Location::RequiresRegister());
7616 locations->SetOut(Location::RequiresRegister());
7617}
7618
7619void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7620 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007621 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007622 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007623 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007624 __ LoadFromOffset(kLoadWord,
7625 locations->Out().AsRegister<Register>(),
7626 locations->InAt(0).AsRegister<Register>(),
7627 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007628 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007629 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007630 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007631 __ LoadFromOffset(kLoadWord,
7632 locations->Out().AsRegister<Register>(),
7633 locations->InAt(0).AsRegister<Register>(),
7634 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7635 __ LoadFromOffset(kLoadWord,
7636 locations->Out().AsRegister<Register>(),
7637 locations->Out().AsRegister<Register>(),
7638 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007639 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007640}
7641
Roland Levillain4d027112015-07-01 15:41:14 +01007642#undef __
7643#undef QUICK_ENTRY_POINT
7644
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007645} // namespace arm
7646} // namespace art