blob: 80b49078ed570a58e09bf7c41a5b655135f2a01b [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) {
110 __ StoreDToOffset(d_reg, SP, stack_offset);
111 } 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();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100492 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
493 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000494 DCHECK(instruction_->IsCheckCast()
495 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000496
497 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
498 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000500 if (!is_fatal_) {
501 SaveLiveRegisters(codegen, locations);
502 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000503
504 // We're moving two locations to locations that could overlap, so we need a parallel
505 // move resolver.
506 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000507 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100508 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000509 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100510 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100511 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100512 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
513 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000514
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000515 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100516 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100517 instruction_,
518 instruction_->GetDexPc(),
519 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000520 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700521 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000522 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
523 } else {
524 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100525 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000526 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000527 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 if (!is_fatal_) {
530 RestoreLiveRegisters(codegen, locations);
531 __ b(GetExitLabel());
532 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000533 }
534
Alexandre Rames9931f312015-06-19 14:47:01 +0100535 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
536
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000537 bool IsFatal() const OVERRIDE { return is_fatal_; }
538
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000540 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000541
542 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
543};
544
Artem Serovf4d6aee2016-07-11 10:41:45 +0100545class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700546 public:
Aart Bik42249c32016-01-07 15:33:50 -0800547 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100548 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800551 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100553 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000554 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555 }
556
Alexandre Rames9931f312015-06-19 14:47:01 +0100557 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700559 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700560 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
561};
562
Artem Serovf4d6aee2016-07-11 10:41:45 +0100563class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100565 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566
567 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
568 LocationSummary* locations = instruction_->GetLocations();
569 __ Bind(GetEntryLabel());
570 SaveLiveRegisters(codegen, locations);
571
572 InvokeRuntimeCallingConvention calling_convention;
573 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
574 parallel_move.AddMove(
575 locations->InAt(0),
576 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
577 Primitive::kPrimNot,
578 nullptr);
579 parallel_move.AddMove(
580 locations->InAt(1),
581 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
582 Primitive::kPrimInt,
583 nullptr);
584 parallel_move.AddMove(
585 locations->InAt(2),
586 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
587 Primitive::kPrimNot,
588 nullptr);
589 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
590
591 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100592 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000593 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100594 RestoreLiveRegisters(codegen, locations);
595 __ b(GetExitLabel());
596 }
597
598 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
599
600 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
602};
603
Roland Levillainc9285912015-12-18 10:38:42 +0000604// Slow path marking an object during a read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100605class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000606 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100607 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100608 : SlowPathCodeARM(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000609 DCHECK(kEmitCompilerReadBarrier);
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
615 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100616 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000617 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100618 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000619 DCHECK(instruction_->IsInstanceFieldGet() ||
620 instruction_->IsStaticFieldGet() ||
621 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100622 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000623 instruction_->IsLoadClass() ||
624 instruction_->IsLoadString() ||
625 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100626 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100627 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
628 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000629 << "Unexpected instruction in read barrier marking slow path: "
630 << instruction_->DebugName();
631
632 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100633 // No need to save live registers; it's taken care of by the
634 // entrypoint. Also, there is no need to update the stack mask,
635 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000636 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100637 DCHECK_NE(reg, SP);
638 DCHECK_NE(reg, LR);
639 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100640 // IP is used internally by the ReadBarrierMarkRegX entry point
641 // as a temporary, it cannot be the entry point's input/output.
642 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100643 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
644 // "Compact" slow path, saving two moves.
645 //
646 // Instead of using the standard runtime calling convention (input
647 // and output in R0):
648 //
649 // R0 <- obj
650 // R0 <- ReadBarrierMark(R0)
651 // obj <- R0
652 //
653 // we just use rX (the register holding `obj`) as input and output
654 // of a dedicated entrypoint:
655 //
656 // rX <- ReadBarrierMarkRegX(rX)
657 //
658 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700659 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100660 // This runtime call does not require a stack map.
661 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000662 __ b(GetExitLabel());
663 }
664
665 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000666 const Location obj_;
667
668 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
669};
670
Roland Levillain3b359c72015-11-17 19:35:12 +0000671// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100672class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000673 public:
674 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
675 Location out,
676 Location ref,
677 Location obj,
678 uint32_t offset,
679 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100680 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000681 out_(out),
682 ref_(ref),
683 obj_(obj),
684 offset_(offset),
685 index_(index) {
686 DCHECK(kEmitCompilerReadBarrier);
687 // If `obj` is equal to `out` or `ref`, it means the initial object
688 // has been overwritten by (or after) the heap object reference load
689 // to be instrumented, e.g.:
690 //
691 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000692 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000693 //
694 // In that case, we have lost the information about the original
695 // object, and the emitted read barrier cannot work properly.
696 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
697 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
698 }
699
700 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
701 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
702 LocationSummary* locations = instruction_->GetLocations();
703 Register reg_out = out_.AsRegister<Register>();
704 DCHECK(locations->CanCall());
705 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100706 DCHECK(instruction_->IsInstanceFieldGet() ||
707 instruction_->IsStaticFieldGet() ||
708 instruction_->IsArrayGet() ||
709 instruction_->IsInstanceOf() ||
710 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100711 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000712 << "Unexpected instruction in read barrier for heap reference slow path: "
713 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000714
715 __ Bind(GetEntryLabel());
716 SaveLiveRegisters(codegen, locations);
717
718 // We may have to change the index's value, but as `index_` is a
719 // constant member (like other "inputs" of this slow path),
720 // introduce a copy of it, `index`.
721 Location index = index_;
722 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100723 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000724 if (instruction_->IsArrayGet()) {
725 // Compute the actual memory offset and store it in `index`.
726 Register index_reg = index_.AsRegister<Register>();
727 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
728 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
729 // We are about to change the value of `index_reg` (see the
730 // calls to art::arm::Thumb2Assembler::Lsl and
731 // art::arm::Thumb2Assembler::AddConstant below), but it has
732 // not been saved by the previous call to
733 // art::SlowPathCode::SaveLiveRegisters, as it is a
734 // callee-save register --
735 // art::SlowPathCode::SaveLiveRegisters does not consider
736 // callee-save registers, as it has been designed with the
737 // assumption that callee-save registers are supposed to be
738 // handled by the called function. So, as a callee-save
739 // register, `index_reg` _would_ eventually be saved onto
740 // the stack, but it would be too late: we would have
741 // changed its value earlier. Therefore, we manually save
742 // it here into another freely available register,
743 // `free_reg`, chosen of course among the caller-save
744 // registers (as a callee-save `free_reg` register would
745 // exhibit the same problem).
746 //
747 // Note we could have requested a temporary register from
748 // the register allocator instead; but we prefer not to, as
749 // this is a slow path, and we know we can find a
750 // caller-save register that is available.
751 Register free_reg = FindAvailableCallerSaveRegister(codegen);
752 __ Mov(free_reg, index_reg);
753 index_reg = free_reg;
754 index = Location::RegisterLocation(index_reg);
755 } else {
756 // The initial register stored in `index_` has already been
757 // saved in the call to art::SlowPathCode::SaveLiveRegisters
758 // (as it is not a callee-save register), so we can freely
759 // use it.
760 }
761 // Shifting the index value contained in `index_reg` by the scale
762 // factor (2) cannot overflow in practice, as the runtime is
763 // unable to allocate object arrays with a size larger than
764 // 2^26 - 1 (that is, 2^28 - 4 bytes).
765 __ Lsl(index_reg, index_reg, TIMES_4);
766 static_assert(
767 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
768 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
769 __ AddConstant(index_reg, index_reg, offset_);
770 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100771 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
772 // intrinsics, `index_` is not shifted by a scale factor of 2
773 // (as in the case of ArrayGet), as it is actually an offset
774 // to an object field within an object.
775 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000776 DCHECK(instruction_->GetLocations()->Intrinsified());
777 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
778 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
779 << instruction_->AsInvoke()->GetIntrinsic();
780 DCHECK_EQ(offset_, 0U);
781 DCHECK(index_.IsRegisterPair());
782 // UnsafeGet's offset location is a register pair, the low
783 // part contains the correct offset.
784 index = index_.ToLow();
785 }
786 }
787
788 // We're moving two or three locations to locations that could
789 // overlap, so we need a parallel move resolver.
790 InvokeRuntimeCallingConvention calling_convention;
791 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
792 parallel_move.AddMove(ref_,
793 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
794 Primitive::kPrimNot,
795 nullptr);
796 parallel_move.AddMove(obj_,
797 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
798 Primitive::kPrimNot,
799 nullptr);
800 if (index.IsValid()) {
801 parallel_move.AddMove(index,
802 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
803 Primitive::kPrimInt,
804 nullptr);
805 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
806 } else {
807 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
808 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
809 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100810 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000811 CheckEntrypointTypes<
812 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
813 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
814
815 RestoreLiveRegisters(codegen, locations);
816 __ b(GetExitLabel());
817 }
818
819 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
820
821 private:
822 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
823 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
824 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
825 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
826 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
827 return static_cast<Register>(i);
828 }
829 }
830 // We shall never fail to find a free caller-save register, as
831 // there are more than two core caller-save registers on ARM
832 // (meaning it is possible to find one which is different from
833 // `ref` and `obj`).
834 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
835 LOG(FATAL) << "Could not find a free caller-save register";
836 UNREACHABLE();
837 }
838
Roland Levillain3b359c72015-11-17 19:35:12 +0000839 const Location out_;
840 const Location ref_;
841 const Location obj_;
842 const uint32_t offset_;
843 // An additional location containing an index to an array.
844 // Only used for HArrayGet and the UnsafeGetObject &
845 // UnsafeGetObjectVolatile intrinsics.
846 const Location index_;
847
848 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
849};
850
851// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100852class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000853 public:
854 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100855 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000856 DCHECK(kEmitCompilerReadBarrier);
857 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000858
859 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
860 LocationSummary* locations = instruction_->GetLocations();
861 Register reg_out = out_.AsRegister<Register>();
862 DCHECK(locations->CanCall());
863 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000864 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
865 << "Unexpected instruction in read barrier for GC root slow path: "
866 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000867
868 __ Bind(GetEntryLabel());
869 SaveLiveRegisters(codegen, locations);
870
871 InvokeRuntimeCallingConvention calling_convention;
872 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
873 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100874 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000875 instruction_,
876 instruction_->GetDexPc(),
877 this);
878 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
879 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
880
881 RestoreLiveRegisters(codegen, locations);
882 __ b(GetExitLabel());
883 }
884
885 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
886
887 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000888 const Location out_;
889 const Location root_;
890
891 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
892};
893
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000894#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100895// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
896#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700897
Aart Bike9f37602015-10-09 11:15:55 -0700898inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700899 switch (cond) {
900 case kCondEQ: return EQ;
901 case kCondNE: return NE;
902 case kCondLT: return LT;
903 case kCondLE: return LE;
904 case kCondGT: return GT;
905 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700906 case kCondB: return LO;
907 case kCondBE: return LS;
908 case kCondA: return HI;
909 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700910 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100911 LOG(FATAL) << "Unreachable";
912 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700913}
914
Aart Bike9f37602015-10-09 11:15:55 -0700915// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100916inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700917 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100918 case kCondEQ: return EQ;
919 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700920 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100921 case kCondLT: return LO;
922 case kCondLE: return LS;
923 case kCondGT: return HI;
924 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700925 // Unsigned remain unchanged.
926 case kCondB: return LO;
927 case kCondBE: return LS;
928 case kCondA: return HI;
929 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700930 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100931 LOG(FATAL) << "Unreachable";
932 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700933}
934
Vladimir Markod6e069b2016-01-18 11:11:01 +0000935inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
936 // The ARM condition codes can express all the necessary branches, see the
937 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
938 // There is no dex instruction or HIR that would need the missing conditions
939 // "equal or unordered" or "not equal".
940 switch (cond) {
941 case kCondEQ: return EQ;
942 case kCondNE: return NE /* unordered */;
943 case kCondLT: return gt_bias ? CC : LT /* unordered */;
944 case kCondLE: return gt_bias ? LS : LE /* unordered */;
945 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
946 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
947 default:
948 LOG(FATAL) << "UNREACHABLE";
949 UNREACHABLE();
950 }
951}
952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100955}
956
957void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959}
960
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100961size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
962 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
963 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100964}
965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100966size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
967 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
968 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100969}
970
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000971size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
972 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
973 return kArmWordSize;
974}
975
976size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
977 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
978 return kArmWordSize;
979}
980
Calin Juravle34166012014-12-19 17:22:29 +0000981CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000982 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100983 const CompilerOptions& compiler_options,
984 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000985 : CodeGenerator(graph,
986 kNumberOfCoreRegisters,
987 kNumberOfSRegisters,
988 kNumberOfRegisterPairs,
989 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
990 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000991 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
992 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100993 compiler_options,
994 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100995 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100996 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100997 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100998 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100999 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001000 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001001 uint32_literals_(std::less<uint32_t>(),
1002 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001003 method_patches_(MethodReferenceComparator(),
1004 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1005 call_patches_(MethodReferenceComparator(),
1006 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001007 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001008 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1009 boot_image_string_patches_(StringReferenceValueComparator(),
1010 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1011 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001012 boot_image_type_patches_(TypeReferenceValueComparator(),
1013 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1014 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001015 boot_image_address_patches_(std::less<uint32_t>(),
1016 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001017 // Always save the LR register to mimic Quick.
1018 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001019}
1020
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001021void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1022 // Ensure that we fix up branches and literal loads and emit the literal pool.
1023 __ FinalizeCode();
1024
1025 // Adjust native pc offsets in stack maps.
1026 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1027 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1028 uint32_t new_position = __ GetAdjustedPosition(old_position);
1029 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1030 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001031 // Adjust pc offsets for the disassembly information.
1032 if (disasm_info_ != nullptr) {
1033 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1034 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1035 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1036 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1037 it.second.start = __ GetAdjustedPosition(it.second.start);
1038 it.second.end = __ GetAdjustedPosition(it.second.end);
1039 }
1040 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1041 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1042 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1043 }
1044 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001045
1046 CodeGenerator::Finalize(allocator);
1047}
1048
David Brazdil58282f42016-01-14 12:45:10 +00001049void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001051 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052
1053 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001054 blocked_core_registers_[SP] = true;
1055 blocked_core_registers_[LR] = true;
1056 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001057
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001058 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001059 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001061 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001062 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001063
David Brazdil58282f42016-01-14 12:45:10 +00001064 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001065 // Stubs do not save callee-save floating point registers. If the graph
1066 // is debuggable, we need to deal with these registers differently. For
1067 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001068 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1069 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1070 }
1071 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001072
1073 UpdateBlockedPairRegisters();
1074}
1075
1076void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
1077 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1078 ArmManagedRegister current =
1079 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1080 if (blocked_core_registers_[current.AsRegisterPairLow()]
1081 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1082 blocked_register_pairs_[i] = true;
1083 }
1084 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001085}
1086
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001087InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001088 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001089 assembler_(codegen->GetAssembler()),
1090 codegen_(codegen) {}
1091
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001092void CodeGeneratorARM::ComputeSpillMask() {
1093 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1094 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001095 // There is no easy instruction to restore just the PC on thumb2. We spill and
1096 // restore another arbitrary register.
1097 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001098 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1099 // We use vpush and vpop for saving and restoring floating point registers, which take
1100 // a SRegister and the number of registers to save/restore after that SRegister. We
1101 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1102 // but in the range.
1103 if (fpu_spill_mask_ != 0) {
1104 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1105 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1106 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1107 fpu_spill_mask_ |= (1 << i);
1108 }
1109 }
1110}
1111
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001112static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001113 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001114}
1115
1116static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001117 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001118}
1119
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001120void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001121 bool skip_overflow_check =
1122 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001123 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001124 __ Bind(&frame_entry_label_);
1125
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001126 if (HasEmptyFrame()) {
1127 return;
1128 }
1129
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001130 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001131 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1132 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1133 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001134 }
1135
Andreas Gampe501fd632015-09-10 16:11:06 -07001136 __ PushList(core_spill_mask_);
1137 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1138 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001139 if (fpu_spill_mask_ != 0) {
1140 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1141 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001142 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001143 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001144 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001145 int adjust = GetFrameSize() - FrameEntrySpillSize();
1146 __ AddConstant(SP, -adjust);
1147 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001148 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001149}
1150
1151void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001152 if (HasEmptyFrame()) {
1153 __ bx(LR);
1154 return;
1155 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001156 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001157 int adjust = GetFrameSize() - FrameEntrySpillSize();
1158 __ AddConstant(SP, adjust);
1159 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001160 if (fpu_spill_mask_ != 0) {
1161 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1162 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001163 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001164 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001165 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001166 // Pop LR into PC to return.
1167 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1168 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1169 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001170 __ cfi().RestoreState();
1171 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001172}
1173
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001174void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001175 Label* label = GetLabelOf(block);
1176 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001177}
1178
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001179Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 switch (type) {
1181 case Primitive::kPrimBoolean:
1182 case Primitive::kPrimByte:
1183 case Primitive::kPrimChar:
1184 case Primitive::kPrimShort:
1185 case Primitive::kPrimInt:
1186 case Primitive::kPrimNot: {
1187 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001188 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001191 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001192 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001193 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001194 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001195
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001196 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001197 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001198 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001199 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001200 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001201 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001202 if (calling_convention.GetRegisterAt(index) == R1) {
1203 // Skip R1, and use R2_R3 instead.
1204 gp_index_++;
1205 index++;
1206 }
1207 }
1208 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1209 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001210 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001211
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001212 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001213 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001215 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1216 }
1217 }
1218
1219 case Primitive::kPrimFloat: {
1220 uint32_t stack_index = stack_index_++;
1221 if (float_index_ % 2 == 0) {
1222 float_index_ = std::max(double_index_, float_index_);
1223 }
1224 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1225 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1226 } else {
1227 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1228 }
1229 }
1230
1231 case Primitive::kPrimDouble: {
1232 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1233 uint32_t stack_index = stack_index_;
1234 stack_index_ += 2;
1235 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1236 uint32_t index = double_index_;
1237 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001238 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001239 calling_convention.GetFpuRegisterAt(index),
1240 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001241 DCHECK(ExpectedPairLayout(result));
1242 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001243 } else {
1244 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001245 }
1246 }
1247
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001248 case Primitive::kPrimVoid:
1249 LOG(FATAL) << "Unexpected parameter type " << type;
1250 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001251 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001252 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001254
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001255Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001256 switch (type) {
1257 case Primitive::kPrimBoolean:
1258 case Primitive::kPrimByte:
1259 case Primitive::kPrimChar:
1260 case Primitive::kPrimShort:
1261 case Primitive::kPrimInt:
1262 case Primitive::kPrimNot: {
1263 return Location::RegisterLocation(R0);
1264 }
1265
1266 case Primitive::kPrimFloat: {
1267 return Location::FpuRegisterLocation(S0);
1268 }
1269
1270 case Primitive::kPrimLong: {
1271 return Location::RegisterPairLocation(R0, R1);
1272 }
1273
1274 case Primitive::kPrimDouble: {
1275 return Location::FpuRegisterPairLocation(S0, S1);
1276 }
1277
1278 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001279 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001280 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001281
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001282 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001283}
1284
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001285Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1286 return Location::RegisterLocation(kMethodRegisterArgument);
1287}
1288
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289void CodeGeneratorARM::Move32(Location destination, Location source) {
1290 if (source.Equals(destination)) {
1291 return;
1292 }
1293 if (destination.IsRegister()) {
1294 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001295 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001296 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001297 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001298 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 } else if (destination.IsFpuRegister()) {
1302 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001303 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001304 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001309 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001310 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001313 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001315 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001316 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1318 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001319 }
1320 }
1321}
1322
1323void CodeGeneratorARM::Move64(Location destination, Location source) {
1324 if (source.Equals(destination)) {
1325 return;
1326 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001327 if (destination.IsRegisterPair()) {
1328 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001329 EmitParallelMoves(
1330 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1331 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001332 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001333 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001334 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1335 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001336 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001337 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338 } else if (source.IsFpuRegisterPair()) {
1339 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1340 destination.AsRegisterPairHigh<Register>(),
1341 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 } else {
1343 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001344 DCHECK(ExpectedPairLayout(destination));
1345 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1346 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001347 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001348 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001349 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001350 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1351 SP,
1352 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001353 } else if (source.IsRegisterPair()) {
1354 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1355 source.AsRegisterPairLow<Register>(),
1356 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001357 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001358 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001359 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001360 } else {
1361 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001362 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001363 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001364 if (source.AsRegisterPairLow<Register>() == R1) {
1365 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1367 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001368 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001369 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001370 SP, destination.GetStackIndex());
1371 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001372 } else if (source.IsFpuRegisterPair()) {
1373 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1374 SP,
1375 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001376 } else {
1377 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001378 EmitParallelMoves(
1379 Location::StackSlot(source.GetStackIndex()),
1380 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001381 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001382 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001383 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1384 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001385 }
1386 }
1387}
1388
Calin Juravle175dc732015-08-25 15:42:32 +01001389void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1390 DCHECK(location.IsRegister());
1391 __ LoadImmediate(location.AsRegister<Register>(), value);
1392}
1393
Calin Juravlee460d1d2015-09-29 04:52:17 +01001394void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001395 HParallelMove move(GetGraph()->GetArena());
1396 move.AddMove(src, dst, dst_type, nullptr);
1397 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001398}
1399
1400void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1401 if (location.IsRegister()) {
1402 locations->AddTemp(location);
1403 } else if (location.IsRegisterPair()) {
1404 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1405 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1406 } else {
1407 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1408 }
1409}
1410
Calin Juravle175dc732015-08-25 15:42:32 +01001411void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1412 HInstruction* instruction,
1413 uint32_t dex_pc,
1414 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001415 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001416 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001417 if (EntrypointRequiresStackMap(entrypoint)) {
1418 RecordPcInfo(instruction, dex_pc, slow_path);
1419 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001420}
1421
Roland Levillaindec8f632016-07-22 17:10:06 +01001422void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1423 HInstruction* instruction,
1424 SlowPathCode* slow_path) {
1425 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001426 GenerateInvokeRuntime(entry_point_offset);
1427}
1428
1429void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001430 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1431 __ blx(LR);
1432}
1433
David Brazdilfc6a86a2015-06-26 10:33:45 +00001434void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001435 DCHECK(!successor->IsExitBlock());
1436
1437 HBasicBlock* block = got->GetBlock();
1438 HInstruction* previous = got->GetPrevious();
1439
1440 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001441 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001442 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1443 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1444 return;
1445 }
1446
1447 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1448 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1449 }
1450 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001452 }
1453}
1454
David Brazdilfc6a86a2015-06-26 10:33:45 +00001455void LocationsBuilderARM::VisitGoto(HGoto* got) {
1456 got->SetLocations(nullptr);
1457}
1458
1459void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1460 HandleGoto(got, got->GetSuccessor());
1461}
1462
1463void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1464 try_boundary->SetLocations(nullptr);
1465}
1466
1467void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1468 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1469 if (!successor->IsExitBlock()) {
1470 HandleGoto(try_boundary, successor);
1471 }
1472}
1473
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001475 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001476}
1477
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001478void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001479}
1480
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001481void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1482 Primitive::Type type = instruction->InputAt(0)->GetType();
1483 Location lhs_loc = instruction->GetLocations()->InAt(0);
1484 Location rhs_loc = instruction->GetLocations()->InAt(1);
1485 if (rhs_loc.IsConstant()) {
1486 // 0.0 is the only immediate that can be encoded directly in
1487 // a VCMP instruction.
1488 //
1489 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1490 // specify that in a floating-point comparison, positive zero
1491 // and negative zero are considered equal, so we can use the
1492 // literal 0.0 for both cases here.
1493 //
1494 // Note however that some methods (Float.equal, Float.compare,
1495 // Float.compareTo, Double.equal, Double.compare,
1496 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1497 // StrictMath.min) consider 0.0 to be (strictly) greater than
1498 // -0.0. So if we ever translate calls to these methods into a
1499 // HCompare instruction, we must handle the -0.0 case with
1500 // care here.
1501 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1502 if (type == Primitive::kPrimFloat) {
1503 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1504 } else {
1505 DCHECK_EQ(type, Primitive::kPrimDouble);
1506 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1507 }
1508 } else {
1509 if (type == Primitive::kPrimFloat) {
1510 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1511 } else {
1512 DCHECK_EQ(type, Primitive::kPrimDouble);
1513 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1514 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1515 }
1516 }
1517}
1518
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1520 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001521 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001522 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001523 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001524}
1525
1526void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1527 Label* true_label,
1528 Label* false_label) {
1529 LocationSummary* locations = cond->GetLocations();
1530 Location left = locations->InAt(0);
1531 Location right = locations->InAt(1);
1532 IfCondition if_cond = cond->GetCondition();
1533
1534 Register left_high = left.AsRegisterPairHigh<Register>();
1535 Register left_low = left.AsRegisterPairLow<Register>();
1536 IfCondition true_high_cond = if_cond;
1537 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001538 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001539
1540 // Set the conditions for the test, remembering that == needs to be
1541 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001542 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001543 switch (if_cond) {
1544 case kCondEQ:
1545 case kCondNE:
1546 // Nothing to do.
1547 break;
1548 case kCondLT:
1549 false_high_cond = kCondGT;
1550 break;
1551 case kCondLE:
1552 true_high_cond = kCondLT;
1553 break;
1554 case kCondGT:
1555 false_high_cond = kCondLT;
1556 break;
1557 case kCondGE:
1558 true_high_cond = kCondGT;
1559 break;
Aart Bike9f37602015-10-09 11:15:55 -07001560 case kCondB:
1561 false_high_cond = kCondA;
1562 break;
1563 case kCondBE:
1564 true_high_cond = kCondB;
1565 break;
1566 case kCondA:
1567 false_high_cond = kCondB;
1568 break;
1569 case kCondAE:
1570 true_high_cond = kCondA;
1571 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001572 }
1573 if (right.IsConstant()) {
1574 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1575 int32_t val_low = Low32Bits(value);
1576 int32_t val_high = High32Bits(value);
1577
Vladimir Markoac6ac102015-12-17 12:14:00 +00001578 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001579 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001580 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001581 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001582 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001583 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001584 __ b(true_label, ARMCondition(true_high_cond));
1585 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001586 }
1587 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001588 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001589 } else {
1590 Register right_high = right.AsRegisterPairHigh<Register>();
1591 Register right_low = right.AsRegisterPairLow<Register>();
1592
1593 __ cmp(left_high, ShifterOperand(right_high));
1594 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001595 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001596 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001597 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001598 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001599 __ b(true_label, ARMCondition(true_high_cond));
1600 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001601 }
1602 // Must be equal high, so compare the lows.
1603 __ cmp(left_low, ShifterOperand(right_low));
1604 }
1605 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001606 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001607 __ b(true_label, final_condition);
1608}
1609
David Brazdil0debae72015-11-12 18:37:00 +00001610void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1611 Label* true_target_in,
1612 Label* false_target_in) {
1613 // Generated branching requires both targets to be explicit. If either of the
1614 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1615 Label fallthrough_target;
1616 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1617 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1618
Roland Levillain4fa13f62015-07-06 18:11:54 +01001619 Primitive::Type type = condition->InputAt(0)->GetType();
1620 switch (type) {
1621 case Primitive::kPrimLong:
1622 GenerateLongComparesAndJumps(condition, true_target, false_target);
1623 break;
1624 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001625 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001626 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001627 GenerateFPJumps(condition, true_target, false_target);
1628 break;
1629 default:
1630 LOG(FATAL) << "Unexpected compare type " << type;
1631 }
1632
David Brazdil0debae72015-11-12 18:37:00 +00001633 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001634 __ b(false_target);
1635 }
David Brazdil0debae72015-11-12 18:37:00 +00001636
1637 if (fallthrough_target.IsLinked()) {
1638 __ Bind(&fallthrough_target);
1639 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001640}
1641
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001642void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001643 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001644 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001645 Label* false_target) {
1646 HInstruction* cond = instruction->InputAt(condition_input_index);
1647
1648 if (true_target == nullptr && false_target == nullptr) {
1649 // Nothing to do. The code always falls through.
1650 return;
1651 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001652 // Constant condition, statically compared against "true" (integer value 1).
1653 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001654 if (true_target != nullptr) {
1655 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001656 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001657 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001658 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001659 if (false_target != nullptr) {
1660 __ b(false_target);
1661 }
1662 }
1663 return;
1664 }
1665
1666 // The following code generates these patterns:
1667 // (1) true_target == nullptr && false_target != nullptr
1668 // - opposite condition true => branch to false_target
1669 // (2) true_target != nullptr && false_target == nullptr
1670 // - condition true => branch to true_target
1671 // (3) true_target != nullptr && false_target != nullptr
1672 // - condition true => branch to true_target
1673 // - branch to false_target
1674 if (IsBooleanValueOrMaterializedCondition(cond)) {
1675 // Condition has been materialized, compare the output to 0.
1676 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1677 DCHECK(cond_val.IsRegister());
1678 if (true_target == nullptr) {
1679 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1680 } else {
1681 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001682 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001683 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001684 // Condition has not been materialized. Use its inputs as the comparison and
1685 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001686 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001687
1688 // If this is a long or FP comparison that has been folded into
1689 // the HCondition, generate the comparison directly.
1690 Primitive::Type type = condition->InputAt(0)->GetType();
1691 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1692 GenerateCompareTestAndBranch(condition, true_target, false_target);
1693 return;
1694 }
1695
1696 LocationSummary* locations = cond->GetLocations();
1697 DCHECK(locations->InAt(0).IsRegister());
1698 Register left = locations->InAt(0).AsRegister<Register>();
1699 Location right = locations->InAt(1);
1700 if (right.IsRegister()) {
1701 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001702 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001703 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001704 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001705 }
1706 if (true_target == nullptr) {
1707 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1708 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001709 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001710 }
Dave Allison20dfc792014-06-16 20:44:29 -07001711 }
David Brazdil0debae72015-11-12 18:37:00 +00001712
1713 // If neither branch falls through (case 3), the conditional branch to `true_target`
1714 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1715 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001716 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001717 }
1718}
1719
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001720void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001721 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1722 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001723 locations->SetInAt(0, Location::RequiresRegister());
1724 }
1725}
1726
1727void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001728 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1729 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1730 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1731 nullptr : codegen_->GetLabelOf(true_successor);
1732 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1733 nullptr : codegen_->GetLabelOf(false_successor);
1734 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001735}
1736
1737void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1738 LocationSummary* locations = new (GetGraph()->GetArena())
1739 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001741 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001742 locations->SetInAt(0, Location::RequiresRegister());
1743 }
1744}
1745
1746void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001747 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001748 GenerateTestAndBranch(deoptimize,
1749 /* condition_input_index */ 0,
1750 slow_path->GetEntryLabel(),
1751 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001752}
Dave Allison20dfc792014-06-16 20:44:29 -07001753
David Brazdil74eb1b22015-12-14 11:44:01 +00001754void LocationsBuilderARM::VisitSelect(HSelect* select) {
1755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1756 if (Primitive::IsFloatingPointType(select->GetType())) {
1757 locations->SetInAt(0, Location::RequiresFpuRegister());
1758 locations->SetInAt(1, Location::RequiresFpuRegister());
1759 } else {
1760 locations->SetInAt(0, Location::RequiresRegister());
1761 locations->SetInAt(1, Location::RequiresRegister());
1762 }
1763 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1764 locations->SetInAt(2, Location::RequiresRegister());
1765 }
1766 locations->SetOut(Location::SameAsFirstInput());
1767}
1768
1769void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1770 LocationSummary* locations = select->GetLocations();
1771 Label false_target;
1772 GenerateTestAndBranch(select,
1773 /* condition_input_index */ 2,
1774 /* true_target */ nullptr,
1775 &false_target);
1776 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1777 __ Bind(&false_target);
1778}
1779
David Srbecky0cf44932015-12-09 14:09:59 +00001780void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1781 new (GetGraph()->GetArena()) LocationSummary(info);
1782}
1783
David Srbeckyd28f4a02016-03-14 17:14:24 +00001784void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1785 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001786}
1787
1788void CodeGeneratorARM::GenerateNop() {
1789 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001790}
1791
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001793 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001794 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001795 // Handle the long/FP comparisons made in instruction simplification.
1796 switch (cond->InputAt(0)->GetType()) {
1797 case Primitive::kPrimLong:
1798 locations->SetInAt(0, Location::RequiresRegister());
1799 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001800 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001801 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1802 }
1803 break;
1804
1805 case Primitive::kPrimFloat:
1806 case Primitive::kPrimDouble:
1807 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001808 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001809 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1811 }
1812 break;
1813
1814 default:
1815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001817 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001820 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001821}
1822
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001824 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001825 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001826 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001827
1828 LocationSummary* locations = cond->GetLocations();
1829 Location left = locations->InAt(0);
1830 Location right = locations->InAt(1);
1831 Register out = locations->Out().AsRegister<Register>();
1832 Label true_label, false_label;
1833
1834 switch (cond->InputAt(0)->GetType()) {
1835 default: {
1836 // Integer case.
1837 if (right.IsRegister()) {
1838 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1839 } else {
1840 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001841 __ CmpConstant(left.AsRegister<Register>(),
1842 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001843 }
Aart Bike9f37602015-10-09 11:15:55 -07001844 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001845 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001846 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001847 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001848 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001849 return;
1850 }
1851 case Primitive::kPrimLong:
1852 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1853 break;
1854 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001855 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001856 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001857 GenerateFPJumps(cond, &true_label, &false_label);
1858 break;
1859 }
1860
1861 // Convert the jumps into the result.
1862 Label done_label;
1863
1864 // False case: result = 0.
1865 __ Bind(&false_label);
1866 __ LoadImmediate(out, 0);
1867 __ b(&done_label);
1868
1869 // True case: result = 1.
1870 __ Bind(&true_label);
1871 __ LoadImmediate(out, 1);
1872 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001873}
1874
1875void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001877}
1878
1879void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001881}
1882
1883void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001885}
1886
1887void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001888 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001889}
1890
1891void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001893}
1894
1895void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001896 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001897}
1898
1899void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001900 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001901}
1902
1903void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001904 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001905}
1906
1907void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001908 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001909}
1910
1911void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001912 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001913}
1914
1915void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001916 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001917}
1918
1919void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001920 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001921}
1922
Aart Bike9f37602015-10-09 11:15:55 -07001923void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001924 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001925}
1926
1927void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001928 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001929}
1930
1931void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001932 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001933}
1934
1935void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001936 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001937}
1938
1939void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001940 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001941}
1942
1943void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001945}
1946
1947void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001949}
1950
1951void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001953}
1954
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001955void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001958 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959}
1960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001961void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001962 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963}
1964
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001965void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1966 LocationSummary* locations =
1967 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1968 locations->SetOut(Location::ConstantLocation(constant));
1969}
1970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001971void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001972 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001973}
1974
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001976 LocationSummary* locations =
1977 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001978 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001979}
1980
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001981void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001982 // Will be generated at use site.
1983}
1984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001985void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1986 LocationSummary* locations =
1987 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1988 locations->SetOut(Location::ConstantLocation(constant));
1989}
1990
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001991void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 // Will be generated at use site.
1993}
1994
1995void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1996 LocationSummary* locations =
1997 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1998 locations->SetOut(Location::ConstantLocation(constant));
1999}
2000
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002001void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002002 // Will be generated at use site.
2003}
2004
Calin Juravle27df7582015-04-17 19:12:31 +01002005void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2006 memory_barrier->SetLocations(nullptr);
2007}
2008
2009void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002010 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002011}
2012
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002013void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002014 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015}
2016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002017void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002019}
2020
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002024 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025}
2026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002027void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002028 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002029}
2030
Calin Juravle175dc732015-08-25 15:42:32 +01002031void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2032 // The trampoline uses the same calling convention as dex calling conventions,
2033 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2034 // the method_idx.
2035 HandleInvoke(invoke);
2036}
2037
2038void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2039 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2040}
2041
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002042void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002043 // Explicit clinit checks triggered by static invokes must have been pruned by
2044 // art::PrepareForRegisterAllocation.
2045 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002046
Vladimir Marko68c981f2016-08-26 13:13:33 +01002047 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002048 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002049 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2050 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2051 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002052 return;
2053 }
2054
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002055 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002056
2057 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2058 if (invoke->HasPcRelativeDexCache()) {
2059 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2060 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002061}
2062
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002063static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2064 if (invoke->GetLocations()->Intrinsified()) {
2065 IntrinsicCodeGeneratorARM intrinsic(codegen);
2066 intrinsic.Dispatch(invoke);
2067 return true;
2068 }
2069 return false;
2070}
2071
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002072void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002073 // Explicit clinit checks triggered by static invokes must have been pruned by
2074 // art::PrepareForRegisterAllocation.
2075 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002076
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002077 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2078 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002079 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002080
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002081 LocationSummary* locations = invoke->GetLocations();
2082 codegen_->GenerateStaticOrDirectCall(
2083 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002084 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002085}
2086
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002087void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002088 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002089 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002090}
2091
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002092void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002093 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002094 if (intrinsic.TryDispatch(invoke)) {
2095 return;
2096 }
2097
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 HandleInvoke(invoke);
2099}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002100
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002101void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002102 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2103 return;
2104 }
2105
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002106 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002107 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002108 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002109}
2110
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002111void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2112 HandleInvoke(invoke);
2113 // Add the hidden argument.
2114 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2115}
2116
2117void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2118 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002119 LocationSummary* locations = invoke->GetLocations();
2120 Register temp = locations->GetTemp(0).AsRegister<Register>();
2121 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002122 Location receiver = locations->InAt(0);
2123 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2124
Roland Levillain3b359c72015-11-17 19:35:12 +00002125 // Set the hidden argument. This is safe to do this here, as R12
2126 // won't be modified thereafter, before the `blx` (call) instruction.
2127 DCHECK_EQ(R12, hidden_reg);
2128 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002129
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002130 if (receiver.IsStackSlot()) {
2131 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002132 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002133 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2134 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002135 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002137 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002138 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002139 // Instead of simply (possibly) unpoisoning `temp` here, we should
2140 // emit a read barrier for the previous class reference load.
2141 // However this is not required in practice, as this is an
2142 // intermediate/temporary reference and because the current
2143 // concurrent copying collector keeps the from-space memory
2144 // intact/accessible until the end of the marking phase (the
2145 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002146 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002147 __ LoadFromOffset(kLoadWord, temp, temp,
2148 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2149 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002150 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002151 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002152 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002153 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002154 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002155 // LR = temp->GetEntryPoint();
2156 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2157 // LR();
2158 __ blx(LR);
2159 DCHECK(!codegen_->IsLeafMethod());
2160 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2161}
2162
Roland Levillain88cb1752014-10-20 16:36:47 +01002163void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2164 LocationSummary* locations =
2165 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2166 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002167 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002168 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2170 break;
2171 }
2172 case Primitive::kPrimLong: {
2173 locations->SetInAt(0, Location::RequiresRegister());
2174 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002175 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002176 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002177
Roland Levillain88cb1752014-10-20 16:36:47 +01002178 case Primitive::kPrimFloat:
2179 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002180 locations->SetInAt(0, Location::RequiresFpuRegister());
2181 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002182 break;
2183
2184 default:
2185 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2186 }
2187}
2188
2189void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2190 LocationSummary* locations = neg->GetLocations();
2191 Location out = locations->Out();
2192 Location in = locations->InAt(0);
2193 switch (neg->GetResultType()) {
2194 case Primitive::kPrimInt:
2195 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002196 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002197 break;
2198
2199 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002200 DCHECK(in.IsRegisterPair());
2201 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2202 __ rsbs(out.AsRegisterPairLow<Register>(),
2203 in.AsRegisterPairLow<Register>(),
2204 ShifterOperand(0));
2205 // We cannot emit an RSC (Reverse Subtract with Carry)
2206 // instruction here, as it does not exist in the Thumb-2
2207 // instruction set. We use the following approach
2208 // using SBC and SUB instead.
2209 //
2210 // out.hi = -C
2211 __ sbc(out.AsRegisterPairHigh<Register>(),
2212 out.AsRegisterPairHigh<Register>(),
2213 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2214 // out.hi = out.hi - in.hi
2215 __ sub(out.AsRegisterPairHigh<Register>(),
2216 out.AsRegisterPairHigh<Register>(),
2217 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2218 break;
2219
Roland Levillain88cb1752014-10-20 16:36:47 +01002220 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002221 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002223 break;
2224
Roland Levillain88cb1752014-10-20 16:36:47 +01002225 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002226 DCHECK(in.IsFpuRegisterPair());
2227 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2228 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002229 break;
2230
2231 default:
2232 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2233 }
2234}
2235
Roland Levillaindff1f282014-11-05 14:15:05 +00002236void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002237 Primitive::Type result_type = conversion->GetResultType();
2238 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002239 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002240
Roland Levillain5b3ee562015-04-14 16:02:41 +01002241 // The float-to-long, double-to-long and long-to-float type conversions
2242 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002243 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002244 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2245 && result_type == Primitive::kPrimLong)
2246 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002247 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002248 : LocationSummary::kNoCall;
2249 LocationSummary* locations =
2250 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2251
David Brazdilb2bd1c52015-03-25 11:17:37 +00002252 // The Java language does not allow treating boolean as an integral type but
2253 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002254
Roland Levillaindff1f282014-11-05 14:15:05 +00002255 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002256 case Primitive::kPrimByte:
2257 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002258 case Primitive::kPrimLong:
2259 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002260 case Primitive::kPrimBoolean:
2261 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002262 case Primitive::kPrimShort:
2263 case Primitive::kPrimInt:
2264 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002265 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002266 locations->SetInAt(0, Location::RequiresRegister());
2267 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2268 break;
2269
2270 default:
2271 LOG(FATAL) << "Unexpected type conversion from " << input_type
2272 << " to " << result_type;
2273 }
2274 break;
2275
Roland Levillain01a8d712014-11-14 16:27:39 +00002276 case Primitive::kPrimShort:
2277 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002278 case Primitive::kPrimLong:
2279 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002280 case Primitive::kPrimBoolean:
2281 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002282 case Primitive::kPrimByte:
2283 case Primitive::kPrimInt:
2284 case Primitive::kPrimChar:
2285 // Processing a Dex `int-to-short' instruction.
2286 locations->SetInAt(0, Location::RequiresRegister());
2287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2288 break;
2289
2290 default:
2291 LOG(FATAL) << "Unexpected type conversion from " << input_type
2292 << " to " << result_type;
2293 }
2294 break;
2295
Roland Levillain946e1432014-11-11 17:35:19 +00002296 case Primitive::kPrimInt:
2297 switch (input_type) {
2298 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002299 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002300 locations->SetInAt(0, Location::Any());
2301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2302 break;
2303
2304 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002305 // Processing a Dex `float-to-int' instruction.
2306 locations->SetInAt(0, Location::RequiresFpuRegister());
2307 locations->SetOut(Location::RequiresRegister());
2308 locations->AddTemp(Location::RequiresFpuRegister());
2309 break;
2310
Roland Levillain946e1432014-11-11 17:35:19 +00002311 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002312 // Processing a Dex `double-to-int' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresRegister());
2315 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected type conversion from " << input_type
2320 << " to " << result_type;
2321 }
2322 break;
2323
Roland Levillaindff1f282014-11-05 14:15:05 +00002324 case Primitive::kPrimLong:
2325 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002326 case Primitive::kPrimBoolean:
2327 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002328 case Primitive::kPrimByte:
2329 case Primitive::kPrimShort:
2330 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002331 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002332 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002333 locations->SetInAt(0, Location::RequiresRegister());
2334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2335 break;
2336
Roland Levillain624279f2014-12-04 11:54:28 +00002337 case Primitive::kPrimFloat: {
2338 // Processing a Dex `float-to-long' instruction.
2339 InvokeRuntimeCallingConvention calling_convention;
2340 locations->SetInAt(0, Location::FpuRegisterLocation(
2341 calling_convention.GetFpuRegisterAt(0)));
2342 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2343 break;
2344 }
2345
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002346 case Primitive::kPrimDouble: {
2347 // Processing a Dex `double-to-long' instruction.
2348 InvokeRuntimeCallingConvention calling_convention;
2349 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2350 calling_convention.GetFpuRegisterAt(0),
2351 calling_convention.GetFpuRegisterAt(1)));
2352 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002353 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002354 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002355
2356 default:
2357 LOG(FATAL) << "Unexpected type conversion from " << input_type
2358 << " to " << result_type;
2359 }
2360 break;
2361
Roland Levillain981e4542014-11-14 11:47:14 +00002362 case Primitive::kPrimChar:
2363 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002364 case Primitive::kPrimLong:
2365 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002371 // Processing a Dex `int-to-char' instruction.
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
2380 break;
2381
Roland Levillaindff1f282014-11-05 14:15:05 +00002382 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002383 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002384 case Primitive::kPrimBoolean:
2385 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002386 case Primitive::kPrimByte:
2387 case Primitive::kPrimShort:
2388 case Primitive::kPrimInt:
2389 case Primitive::kPrimChar:
2390 // Processing a Dex `int-to-float' instruction.
2391 locations->SetInAt(0, Location::RequiresRegister());
2392 locations->SetOut(Location::RequiresFpuRegister());
2393 break;
2394
Roland Levillain5b3ee562015-04-14 16:02:41 +01002395 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002396 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002397 InvokeRuntimeCallingConvention calling_convention;
2398 locations->SetInAt(0, Location::RegisterPairLocation(
2399 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2400 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002401 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002402 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002403
Roland Levillaincff13742014-11-17 14:32:17 +00002404 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002405 // Processing a Dex `double-to-float' instruction.
2406 locations->SetInAt(0, Location::RequiresFpuRegister());
2407 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected type conversion from " << input_type
2412 << " to " << result_type;
2413 };
2414 break;
2415
Roland Levillaindff1f282014-11-05 14:15:05 +00002416 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002417 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002418 case Primitive::kPrimBoolean:
2419 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002420 case Primitive::kPrimByte:
2421 case Primitive::kPrimShort:
2422 case Primitive::kPrimInt:
2423 case Primitive::kPrimChar:
2424 // Processing a Dex `int-to-double' instruction.
2425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetOut(Location::RequiresFpuRegister());
2427 break;
2428
2429 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002430 // Processing a Dex `long-to-double' instruction.
2431 locations->SetInAt(0, Location::RequiresRegister());
2432 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002433 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002434 locations->AddTemp(Location::RequiresFpuRegister());
2435 break;
2436
Roland Levillaincff13742014-11-17 14:32:17 +00002437 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002438 // Processing a Dex `float-to-double' instruction.
2439 locations->SetInAt(0, Location::RequiresFpuRegister());
2440 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453}
2454
2455void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2456 LocationSummary* locations = conversion->GetLocations();
2457 Location out = locations->Out();
2458 Location in = locations->InAt(0);
2459 Primitive::Type result_type = conversion->GetResultType();
2460 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002461 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002463 case Primitive::kPrimByte:
2464 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002465 case Primitive::kPrimLong:
2466 // Type conversion from long to byte is a result of code transformations.
2467 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2468 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002469 case Primitive::kPrimBoolean:
2470 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002471 case Primitive::kPrimShort:
2472 case Primitive::kPrimInt:
2473 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002474 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002476 break;
2477
2478 default:
2479 LOG(FATAL) << "Unexpected type conversion from " << input_type
2480 << " to " << result_type;
2481 }
2482 break;
2483
Roland Levillain01a8d712014-11-14 16:27:39 +00002484 case Primitive::kPrimShort:
2485 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002486 case Primitive::kPrimLong:
2487 // Type conversion from long to short is a result of code transformations.
2488 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2489 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimInt:
2494 case Primitive::kPrimChar:
2495 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Roland Levillain946e1432014-11-11 17:35:19 +00002505 case Primitive::kPrimInt:
2506 switch (input_type) {
2507 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002508 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002509 DCHECK(out.IsRegister());
2510 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002512 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002514 } else {
2515 DCHECK(in.IsConstant());
2516 DCHECK(in.GetConstant()->IsLongConstant());
2517 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002519 }
2520 break;
2521
Roland Levillain3f8f9362014-12-02 17:45:01 +00002522 case Primitive::kPrimFloat: {
2523 // Processing a Dex `float-to-int' instruction.
2524 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002525 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002526 __ vmovrs(out.AsRegister<Register>(), temp);
2527 break;
2528 }
2529
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002530 case Primitive::kPrimDouble: {
2531 // Processing a Dex `double-to-int' instruction.
2532 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002533 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002534 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002535 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002536 }
Roland Levillain946e1432014-11-11 17:35:19 +00002537
2538 default:
2539 LOG(FATAL) << "Unexpected type conversion from " << input_type
2540 << " to " << result_type;
2541 }
2542 break;
2543
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 case Primitive::kPrimLong:
2545 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002546 case Primitive::kPrimBoolean:
2547 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002548 case Primitive::kPrimByte:
2549 case Primitive::kPrimShort:
2550 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002551 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002552 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 DCHECK(out.IsRegisterPair());
2554 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 // Sign extension.
2557 __ Asr(out.AsRegisterPairHigh<Register>(),
2558 out.AsRegisterPairLow<Register>(),
2559 31);
2560 break;
2561
2562 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002563 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002564 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002565 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002566 break;
2567
Roland Levillaindff1f282014-11-05 14:15:05 +00002568 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002569 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002570 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002571 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 break;
2573
2574 default:
2575 LOG(FATAL) << "Unexpected type conversion from " << input_type
2576 << " to " << result_type;
2577 }
2578 break;
2579
Roland Levillain981e4542014-11-14 11:47:14 +00002580 case Primitive::kPrimChar:
2581 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002582 case Primitive::kPrimLong:
2583 // Type conversion from long to char is a result of code transformations.
2584 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2585 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002586 case Primitive::kPrimBoolean:
2587 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002588 case Primitive::kPrimByte:
2589 case Primitive::kPrimShort:
2590 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002591 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected type conversion from " << input_type
2597 << " to " << result_type;
2598 }
2599 break;
2600
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002602 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002603 case Primitive::kPrimBoolean:
2604 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002605 case Primitive::kPrimByte:
2606 case Primitive::kPrimShort:
2607 case Primitive::kPrimInt:
2608 case Primitive::kPrimChar: {
2609 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2611 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002612 break;
2613 }
2614
Roland Levillain5b3ee562015-04-14 16:02:41 +01002615 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002617 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002618 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002620
Roland Levillaincff13742014-11-17 14:32:17 +00002621 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002622 // Processing a Dex `double-to-float' instruction.
2623 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2624 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 };
2631 break;
2632
Roland Levillaindff1f282014-11-05 14:15:05 +00002633 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002634 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002635 case Primitive::kPrimBoolean:
2636 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002637 case Primitive::kPrimByte:
2638 case Primitive::kPrimShort:
2639 case Primitive::kPrimInt:
2640 case Primitive::kPrimChar: {
2641 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002643 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2644 out.AsFpuRegisterPairLow<SRegister>());
2645 break;
2646 }
2647
Roland Levillain647b9ed2014-11-27 12:06:00 +00002648 case Primitive::kPrimLong: {
2649 // Processing a Dex `long-to-double' instruction.
2650 Register low = in.AsRegisterPairLow<Register>();
2651 Register high = in.AsRegisterPairHigh<Register>();
2652 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2653 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002654 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002655 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002656 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2657 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002658
Roland Levillain682393c2015-04-14 15:57:52 +01002659 // temp_d = int-to-double(high)
2660 __ vmovsr(temp_s, high);
2661 __ vcvtdi(temp_d, temp_s);
2662 // constant_d = k2Pow32EncodingForDouble
2663 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2664 // out_d = unsigned-to-double(low)
2665 __ vmovsr(out_s, low);
2666 __ vcvtdu(out_d, out_s);
2667 // out_d += temp_d * constant_d
2668 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002669 break;
2670 }
2671
Roland Levillaincff13742014-11-17 14:32:17 +00002672 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002673 // Processing a Dex `float-to-double' instruction.
2674 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2675 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 }
2688}
2689
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002690void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002691 LocationSummary* locations =
2692 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002693 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002694 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002695 locations->SetInAt(0, Location::RequiresRegister());
2696 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2698 break;
2699 }
2700
2701 case Primitive::kPrimLong: {
2702 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002703 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002705 break;
2706 }
2707
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002708 case Primitive::kPrimFloat:
2709 case Primitive::kPrimDouble: {
2710 locations->SetInAt(0, Location::RequiresFpuRegister());
2711 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002715
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002717 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002718 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719}
2720
2721void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2722 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002723 Location out = locations->Out();
2724 Location first = locations->InAt(0);
2725 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002726 switch (add->GetResultType()) {
2727 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002728 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002729 __ add(out.AsRegister<Register>(),
2730 first.AsRegister<Register>(),
2731 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002732 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 __ AddConstant(out.AsRegister<Register>(),
2734 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002735 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002736 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002737 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002738
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002739 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002740 if (second.IsConstant()) {
2741 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2742 GenerateAddLongConst(out, first, value);
2743 } else {
2744 DCHECK(second.IsRegisterPair());
2745 __ adds(out.AsRegisterPairLow<Register>(),
2746 first.AsRegisterPairLow<Register>(),
2747 ShifterOperand(second.AsRegisterPairLow<Register>()));
2748 __ adc(out.AsRegisterPairHigh<Register>(),
2749 first.AsRegisterPairHigh<Register>(),
2750 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2751 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002753 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002754
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002755 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002756 __ vadds(out.AsFpuRegister<SRegister>(),
2757 first.AsFpuRegister<SRegister>(),
2758 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002759 break;
2760
2761 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002762 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2763 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2764 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 break;
2766
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002767 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002768 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 }
2770}
2771
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002772void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002773 LocationSummary* locations =
2774 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002775 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002776 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002777 locations->SetInAt(0, Location::RequiresRegister());
2778 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2780 break;
2781 }
2782
2783 case Primitive::kPrimLong: {
2784 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002785 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002787 break;
2788 }
Calin Juravle11351682014-10-23 15:38:15 +01002789 case Primitive::kPrimFloat:
2790 case Primitive::kPrimDouble: {
2791 locations->SetInAt(0, Location::RequiresFpuRegister());
2792 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002793 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794 break;
Calin Juravle11351682014-10-23 15:38:15 +01002795 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002796 default:
Calin Juravle11351682014-10-23 15:38:15 +01002797 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002798 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002799}
2800
2801void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2802 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002803 Location out = locations->Out();
2804 Location first = locations->InAt(0);
2805 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002806 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002807 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002808 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002809 __ sub(out.AsRegister<Register>(),
2810 first.AsRegister<Register>(),
2811 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002812 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ AddConstant(out.AsRegister<Register>(),
2814 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002815 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002816 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002817 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002818 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002819
Calin Juravle11351682014-10-23 15:38:15 +01002820 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002821 if (second.IsConstant()) {
2822 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2823 GenerateAddLongConst(out, first, -value);
2824 } else {
2825 DCHECK(second.IsRegisterPair());
2826 __ subs(out.AsRegisterPairLow<Register>(),
2827 first.AsRegisterPairLow<Register>(),
2828 ShifterOperand(second.AsRegisterPairLow<Register>()));
2829 __ sbc(out.AsRegisterPairHigh<Register>(),
2830 first.AsRegisterPairHigh<Register>(),
2831 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2832 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833 break;
Calin Juravle11351682014-10-23 15:38:15 +01002834 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002835
Calin Juravle11351682014-10-23 15:38:15 +01002836 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002837 __ vsubs(out.AsFpuRegister<SRegister>(),
2838 first.AsFpuRegister<SRegister>(),
2839 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002840 break;
Calin Juravle11351682014-10-23 15:38:15 +01002841 }
2842
2843 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002844 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2845 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2846 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002847 break;
2848 }
2849
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002851 default:
Calin Juravle11351682014-10-23 15:38:15 +01002852 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002853 }
2854}
2855
Calin Juravle34bacdf2014-10-07 20:23:36 +01002856void LocationsBuilderARM::VisitMul(HMul* mul) {
2857 LocationSummary* locations =
2858 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2859 switch (mul->GetResultType()) {
2860 case Primitive::kPrimInt:
2861 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002862 locations->SetInAt(0, Location::RequiresRegister());
2863 locations->SetInAt(1, Location::RequiresRegister());
2864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002865 break;
2866 }
2867
Calin Juravleb5bfa962014-10-21 18:02:24 +01002868 case Primitive::kPrimFloat:
2869 case Primitive::kPrimDouble: {
2870 locations->SetInAt(0, Location::RequiresFpuRegister());
2871 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002872 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002873 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002874 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002875
2876 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002877 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878 }
2879}
2880
2881void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2882 LocationSummary* locations = mul->GetLocations();
2883 Location out = locations->Out();
2884 Location first = locations->InAt(0);
2885 Location second = locations->InAt(1);
2886 switch (mul->GetResultType()) {
2887 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002888 __ mul(out.AsRegister<Register>(),
2889 first.AsRegister<Register>(),
2890 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002891 break;
2892 }
2893 case Primitive::kPrimLong: {
2894 Register out_hi = out.AsRegisterPairHigh<Register>();
2895 Register out_lo = out.AsRegisterPairLow<Register>();
2896 Register in1_hi = first.AsRegisterPairHigh<Register>();
2897 Register in1_lo = first.AsRegisterPairLow<Register>();
2898 Register in2_hi = second.AsRegisterPairHigh<Register>();
2899 Register in2_lo = second.AsRegisterPairLow<Register>();
2900
2901 // Extra checks to protect caused by the existence of R1_R2.
2902 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2903 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2904 DCHECK_NE(out_hi, in1_lo);
2905 DCHECK_NE(out_hi, in2_lo);
2906
2907 // input: in1 - 64 bits, in2 - 64 bits
2908 // output: out
2909 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2910 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2911 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2912
2913 // IP <- in1.lo * in2.hi
2914 __ mul(IP, in1_lo, in2_hi);
2915 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2916 __ mla(out_hi, in1_hi, in2_lo, IP);
2917 // out.lo <- (in1.lo * in2.lo)[31:0];
2918 __ umull(out_lo, IP, in1_lo, in2_lo);
2919 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2920 __ add(out_hi, out_hi, ShifterOperand(IP));
2921 break;
2922 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002923
2924 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002925 __ vmuls(out.AsFpuRegister<SRegister>(),
2926 first.AsFpuRegister<SRegister>(),
2927 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002928 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002929 }
2930
2931 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002932 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2933 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2934 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002935 break;
2936 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002937
2938 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002939 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002940 }
2941}
2942
Zheng Xuc6667102015-05-15 16:08:45 +08002943void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2944 DCHECK(instruction->IsDiv() || instruction->IsRem());
2945 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2946
2947 LocationSummary* locations = instruction->GetLocations();
2948 Location second = locations->InAt(1);
2949 DCHECK(second.IsConstant());
2950
2951 Register out = locations->Out().AsRegister<Register>();
2952 Register dividend = locations->InAt(0).AsRegister<Register>();
2953 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2954 DCHECK(imm == 1 || imm == -1);
2955
2956 if (instruction->IsRem()) {
2957 __ LoadImmediate(out, 0);
2958 } else {
2959 if (imm == 1) {
2960 __ Mov(out, dividend);
2961 } else {
2962 __ rsb(out, dividend, ShifterOperand(0));
2963 }
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2968 DCHECK(instruction->IsDiv() || instruction->IsRem());
2969 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2970
2971 LocationSummary* locations = instruction->GetLocations();
2972 Location second = locations->InAt(1);
2973 DCHECK(second.IsConstant());
2974
2975 Register out = locations->Out().AsRegister<Register>();
2976 Register dividend = locations->InAt(0).AsRegister<Register>();
2977 Register temp = locations->GetTemp(0).AsRegister<Register>();
2978 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002979 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002980 int ctz_imm = CTZ(abs_imm);
2981
2982 if (ctz_imm == 1) {
2983 __ Lsr(temp, dividend, 32 - ctz_imm);
2984 } else {
2985 __ Asr(temp, dividend, 31);
2986 __ Lsr(temp, temp, 32 - ctz_imm);
2987 }
2988 __ add(out, temp, ShifterOperand(dividend));
2989
2990 if (instruction->IsDiv()) {
2991 __ Asr(out, out, ctz_imm);
2992 if (imm < 0) {
2993 __ rsb(out, out, ShifterOperand(0));
2994 }
2995 } else {
2996 __ ubfx(out, out, 0, ctz_imm);
2997 __ sub(out, out, ShifterOperand(temp));
2998 }
2999}
3000
3001void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3002 DCHECK(instruction->IsDiv() || instruction->IsRem());
3003 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3004
3005 LocationSummary* locations = instruction->GetLocations();
3006 Location second = locations->InAt(1);
3007 DCHECK(second.IsConstant());
3008
3009 Register out = locations->Out().AsRegister<Register>();
3010 Register dividend = locations->InAt(0).AsRegister<Register>();
3011 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3012 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3013 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3014
3015 int64_t magic;
3016 int shift;
3017 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3018
3019 __ LoadImmediate(temp1, magic);
3020 __ smull(temp2, temp1, dividend, temp1);
3021
3022 if (imm > 0 && magic < 0) {
3023 __ add(temp1, temp1, ShifterOperand(dividend));
3024 } else if (imm < 0 && magic > 0) {
3025 __ sub(temp1, temp1, ShifterOperand(dividend));
3026 }
3027
3028 if (shift != 0) {
3029 __ Asr(temp1, temp1, shift);
3030 }
3031
3032 if (instruction->IsDiv()) {
3033 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3034 } else {
3035 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3036 // TODO: Strength reduction for mls.
3037 __ LoadImmediate(temp2, imm);
3038 __ mls(out, temp1, temp2, dividend);
3039 }
3040}
3041
3042void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3043 DCHECK(instruction->IsDiv() || instruction->IsRem());
3044 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3045
3046 LocationSummary* locations = instruction->GetLocations();
3047 Location second = locations->InAt(1);
3048 DCHECK(second.IsConstant());
3049
3050 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3051 if (imm == 0) {
3052 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3053 } else if (imm == 1 || imm == -1) {
3054 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003055 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003056 DivRemByPowerOfTwo(instruction);
3057 } else {
3058 DCHECK(imm <= -2 || imm >= 2);
3059 GenerateDivRemWithAnyConstant(instruction);
3060 }
3061}
3062
Calin Juravle7c4954d2014-10-28 16:57:40 +00003063void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003064 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3065 if (div->GetResultType() == Primitive::kPrimLong) {
3066 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003067 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003068 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3069 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003070 } else if (div->GetResultType() == Primitive::kPrimInt &&
3071 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3072 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003073 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003074 }
3075
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3077
Calin Juravle7c4954d2014-10-28 16:57:40 +00003078 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003079 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003080 if (div->InputAt(1)->IsConstant()) {
3081 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003082 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003084 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3085 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003086 // No temp register required.
3087 } else {
3088 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003089 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003090 locations->AddTemp(Location::RequiresRegister());
3091 }
3092 }
3093 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003094 locations->SetInAt(0, Location::RequiresRegister());
3095 locations->SetInAt(1, Location::RequiresRegister());
3096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3097 } else {
3098 InvokeRuntimeCallingConvention calling_convention;
3099 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3100 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3101 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3102 // we only need the former.
3103 locations->SetOut(Location::RegisterLocation(R0));
3104 }
Calin Juravled0d48522014-11-04 16:40:20 +00003105 break;
3106 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003107 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003108 InvokeRuntimeCallingConvention calling_convention;
3109 locations->SetInAt(0, Location::RegisterPairLocation(
3110 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3111 locations->SetInAt(1, Location::RegisterPairLocation(
3112 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003113 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003114 break;
3115 }
3116 case Primitive::kPrimFloat:
3117 case Primitive::kPrimDouble: {
3118 locations->SetInAt(0, Location::RequiresFpuRegister());
3119 locations->SetInAt(1, Location::RequiresFpuRegister());
3120 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3121 break;
3122 }
3123
3124 default:
3125 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3126 }
3127}
3128
3129void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3130 LocationSummary* locations = div->GetLocations();
3131 Location out = locations->Out();
3132 Location first = locations->InAt(0);
3133 Location second = locations->InAt(1);
3134
3135 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003136 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003137 if (second.IsConstant()) {
3138 GenerateDivRemConstantIntegral(div);
3139 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003140 __ sdiv(out.AsRegister<Register>(),
3141 first.AsRegister<Register>(),
3142 second.AsRegister<Register>());
3143 } else {
3144 InvokeRuntimeCallingConvention calling_convention;
3145 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3146 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3147 DCHECK_EQ(R0, out.AsRegister<Register>());
3148
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003149 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003150 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003151 }
Calin Juravled0d48522014-11-04 16:40:20 +00003152 break;
3153 }
3154
Calin Juravle7c4954d2014-10-28 16:57:40 +00003155 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003156 InvokeRuntimeCallingConvention calling_convention;
3157 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3158 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3159 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3160 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3161 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003162 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003163
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003164 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003165 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003166 break;
3167 }
3168
3169 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003170 __ vdivs(out.AsFpuRegister<SRegister>(),
3171 first.AsFpuRegister<SRegister>(),
3172 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003173 break;
3174 }
3175
3176 case Primitive::kPrimDouble: {
3177 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3178 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3179 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3180 break;
3181 }
3182
3183 default:
3184 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3185 }
3186}
3187
Calin Juravlebacfec32014-11-14 15:54:36 +00003188void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003189 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003190
3191 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003192 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003193 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3194 // sdiv will be replaced by other instruction sequence.
3195 call_kind = LocationSummary::kNoCall;
3196 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3197 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003198 // Have hardware divide instruction for int, do it with three instructions.
3199 call_kind = LocationSummary::kNoCall;
3200 }
3201
Calin Juravlebacfec32014-11-14 15:54:36 +00003202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3203
Calin Juravled2ec87d2014-12-08 14:24:46 +00003204 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003205 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003206 if (rem->InputAt(1)->IsConstant()) {
3207 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003208 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003210 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3211 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003212 // No temp register required.
3213 } else {
3214 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003215 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003216 locations->AddTemp(Location::RequiresRegister());
3217 }
3218 }
3219 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003220 locations->SetInAt(0, Location::RequiresRegister());
3221 locations->SetInAt(1, Location::RequiresRegister());
3222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3223 locations->AddTemp(Location::RequiresRegister());
3224 } else {
3225 InvokeRuntimeCallingConvention calling_convention;
3226 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3227 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3228 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3229 // we only need the latter.
3230 locations->SetOut(Location::RegisterLocation(R1));
3231 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003232 break;
3233 }
3234 case Primitive::kPrimLong: {
3235 InvokeRuntimeCallingConvention calling_convention;
3236 locations->SetInAt(0, Location::RegisterPairLocation(
3237 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3238 locations->SetInAt(1, Location::RegisterPairLocation(
3239 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3240 // The runtime helper puts the output in R2,R3.
3241 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3242 break;
3243 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003244 case Primitive::kPrimFloat: {
3245 InvokeRuntimeCallingConvention calling_convention;
3246 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3247 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3248 locations->SetOut(Location::FpuRegisterLocation(S0));
3249 break;
3250 }
3251
Calin Juravlebacfec32014-11-14 15:54:36 +00003252 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003253 InvokeRuntimeCallingConvention calling_convention;
3254 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3255 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3256 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3257 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3258 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003259 break;
3260 }
3261
3262 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003263 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003264 }
3265}
3266
3267void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3268 LocationSummary* locations = rem->GetLocations();
3269 Location out = locations->Out();
3270 Location first = locations->InAt(0);
3271 Location second = locations->InAt(1);
3272
Calin Juravled2ec87d2014-12-08 14:24:46 +00003273 Primitive::Type type = rem->GetResultType();
3274 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003275 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003276 if (second.IsConstant()) {
3277 GenerateDivRemConstantIntegral(rem);
3278 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003279 Register reg1 = first.AsRegister<Register>();
3280 Register reg2 = second.AsRegister<Register>();
3281 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003282
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003283 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003284 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003285 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003286 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003287 } else {
3288 InvokeRuntimeCallingConvention calling_convention;
3289 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3290 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3291 DCHECK_EQ(R1, out.AsRegister<Register>());
3292
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003293 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003294 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003295 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003296 break;
3297 }
3298
3299 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003300 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003301 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003302 break;
3303 }
3304
Calin Juravled2ec87d2014-12-08 14:24:46 +00003305 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003306 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003307 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003308 break;
3309 }
3310
Calin Juravlebacfec32014-11-14 15:54:36 +00003311 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003312 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003313 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003314 break;
3315 }
3316
3317 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003318 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003319 }
3320}
3321
Calin Juravled0d48522014-11-04 16:40:20 +00003322void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003323 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003324 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003325}
3326
3327void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003328 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003329 codegen_->AddSlowPath(slow_path);
3330
3331 LocationSummary* locations = instruction->GetLocations();
3332 Location value = locations->InAt(0);
3333
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003334 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003335 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003336 case Primitive::kPrimByte:
3337 case Primitive::kPrimChar:
3338 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003339 case Primitive::kPrimInt: {
3340 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003341 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003342 } else {
3343 DCHECK(value.IsConstant()) << value;
3344 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3345 __ b(slow_path->GetEntryLabel());
3346 }
3347 }
3348 break;
3349 }
3350 case Primitive::kPrimLong: {
3351 if (value.IsRegisterPair()) {
3352 __ orrs(IP,
3353 value.AsRegisterPairLow<Register>(),
3354 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3355 __ b(slow_path->GetEntryLabel(), EQ);
3356 } else {
3357 DCHECK(value.IsConstant()) << value;
3358 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3359 __ b(slow_path->GetEntryLabel());
3360 }
3361 }
3362 break;
3363 default:
3364 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3365 }
3366 }
Calin Juravled0d48522014-11-04 16:40:20 +00003367}
3368
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003369void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3370 Register in = locations->InAt(0).AsRegister<Register>();
3371 Location rhs = locations->InAt(1);
3372 Register out = locations->Out().AsRegister<Register>();
3373
3374 if (rhs.IsConstant()) {
3375 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3376 // so map all rotations to a +ve. equivalent in that range.
3377 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3378 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3379 if (rot) {
3380 // Rotate, mapping left rotations to right equivalents if necessary.
3381 // (e.g. left by 2 bits == right by 30.)
3382 __ Ror(out, in, rot);
3383 } else if (out != in) {
3384 __ Mov(out, in);
3385 }
3386 } else {
3387 __ Ror(out, in, rhs.AsRegister<Register>());
3388 }
3389}
3390
3391// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3392// rotates by swapping input regs (effectively rotating by the first 32-bits of
3393// a larger rotation) or flipping direction (thus treating larger right/left
3394// rotations as sub-word sized rotations in the other direction) as appropriate.
3395void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3396 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3397 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3398 Location rhs = locations->InAt(1);
3399 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3400 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3401
3402 if (rhs.IsConstant()) {
3403 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3404 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003405 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003406 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3407 // logic below to a simple pair of binary orr.
3408 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3409 if (rot >= kArmBitsPerWord) {
3410 rot -= kArmBitsPerWord;
3411 std::swap(in_reg_hi, in_reg_lo);
3412 }
3413 // Rotate, or mov to out for zero or word size rotations.
3414 if (rot != 0u) {
3415 __ Lsr(out_reg_hi, in_reg_hi, rot);
3416 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3417 __ Lsr(out_reg_lo, in_reg_lo, rot);
3418 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3419 } else {
3420 __ Mov(out_reg_lo, in_reg_lo);
3421 __ Mov(out_reg_hi, in_reg_hi);
3422 }
3423 } else {
3424 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3425 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3426 Label end;
3427 Label shift_by_32_plus_shift_right;
3428
3429 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3430 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3431 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3432 __ b(&shift_by_32_plus_shift_right, CC);
3433
3434 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3435 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3436 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3437 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3438 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3439 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3440 __ Lsr(shift_left, in_reg_hi, shift_right);
3441 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3442 __ b(&end);
3443
3444 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3445 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3446 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3447 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3448 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3449 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3450 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3451 __ Lsl(shift_right, in_reg_hi, shift_left);
3452 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3453
3454 __ Bind(&end);
3455 }
3456}
Roland Levillain22c49222016-03-18 14:04:28 +00003457
3458void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3461 switch (ror->GetResultType()) {
3462 case Primitive::kPrimInt: {
3463 locations->SetInAt(0, Location::RequiresRegister());
3464 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3466 break;
3467 }
3468 case Primitive::kPrimLong: {
3469 locations->SetInAt(0, Location::RequiresRegister());
3470 if (ror->InputAt(1)->IsConstant()) {
3471 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3472 } else {
3473 locations->SetInAt(1, Location::RequiresRegister());
3474 locations->AddTemp(Location::RequiresRegister());
3475 locations->AddTemp(Location::RequiresRegister());
3476 }
3477 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3478 break;
3479 }
3480 default:
3481 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3482 }
3483}
3484
Roland Levillain22c49222016-03-18 14:04:28 +00003485void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003486 LocationSummary* locations = ror->GetLocations();
3487 Primitive::Type type = ror->GetResultType();
3488 switch (type) {
3489 case Primitive::kPrimInt: {
3490 HandleIntegerRotate(locations);
3491 break;
3492 }
3493 case Primitive::kPrimLong: {
3494 HandleLongRotate(locations);
3495 break;
3496 }
3497 default:
3498 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003499 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003500 }
3501}
3502
Calin Juravle9aec02f2014-11-18 23:06:35 +00003503void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3504 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3505
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003508
3509 switch (op->GetResultType()) {
3510 case Primitive::kPrimInt: {
3511 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003512 if (op->InputAt(1)->IsConstant()) {
3513 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3515 } else {
3516 locations->SetInAt(1, Location::RequiresRegister());
3517 // Make the output overlap, as it will be used to hold the masked
3518 // second input.
3519 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3520 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003521 break;
3522 }
3523 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003524 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003525 if (op->InputAt(1)->IsConstant()) {
3526 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3527 // For simplicity, use kOutputOverlap even though we only require that low registers
3528 // don't clash with high registers which the register allocator currently guarantees.
3529 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3530 } else {
3531 locations->SetInAt(1, Location::RequiresRegister());
3532 locations->AddTemp(Location::RequiresRegister());
3533 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3534 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003535 break;
3536 }
3537 default:
3538 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3539 }
3540}
3541
3542void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3543 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3544
3545 LocationSummary* locations = op->GetLocations();
3546 Location out = locations->Out();
3547 Location first = locations->InAt(0);
3548 Location second = locations->InAt(1);
3549
3550 Primitive::Type type = op->GetResultType();
3551 switch (type) {
3552 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003553 Register out_reg = out.AsRegister<Register>();
3554 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003555 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003557 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003558 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003559 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003560 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003561 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003562 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003563 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003564 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003565 }
3566 } else {
3567 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003568 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003569 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003570 __ Mov(out_reg, first_reg);
3571 } else if (op->IsShl()) {
3572 __ Lsl(out_reg, first_reg, shift_value);
3573 } else if (op->IsShr()) {
3574 __ Asr(out_reg, first_reg, shift_value);
3575 } else {
3576 __ Lsr(out_reg, first_reg, shift_value);
3577 }
3578 }
3579 break;
3580 }
3581 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003582 Register o_h = out.AsRegisterPairHigh<Register>();
3583 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003584
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003585 Register high = first.AsRegisterPairHigh<Register>();
3586 Register low = first.AsRegisterPairLow<Register>();
3587
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003588 if (second.IsRegister()) {
3589 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003590
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003591 Register second_reg = second.AsRegister<Register>();
3592
3593 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003594 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003595 // Shift the high part
3596 __ Lsl(o_h, high, o_l);
3597 // Shift the low part and `or` what overflew on the high part
3598 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3599 __ Lsr(temp, low, temp);
3600 __ orr(o_h, o_h, ShifterOperand(temp));
3601 // If the shift is > 32 bits, override the high part
3602 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3603 __ it(PL);
3604 __ Lsl(o_h, low, temp, PL);
3605 // Shift the low part
3606 __ Lsl(o_l, low, o_l);
3607 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003608 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003609 // Shift the low part
3610 __ Lsr(o_l, low, o_h);
3611 // Shift the high part and `or` what underflew on the low part
3612 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3613 __ Lsl(temp, high, temp);
3614 __ orr(o_l, o_l, ShifterOperand(temp));
3615 // If the shift is > 32 bits, override the low part
3616 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3617 __ it(PL);
3618 __ Asr(o_l, high, temp, PL);
3619 // Shift the high part
3620 __ Asr(o_h, high, o_h);
3621 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003622 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003623 // same as Shr except we use `Lsr`s and not `Asr`s
3624 __ Lsr(o_l, low, o_h);
3625 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3626 __ Lsl(temp, high, temp);
3627 __ orr(o_l, o_l, ShifterOperand(temp));
3628 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3629 __ it(PL);
3630 __ Lsr(o_l, high, temp, PL);
3631 __ Lsr(o_h, high, o_h);
3632 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003634 // Register allocator doesn't create partial overlap.
3635 DCHECK_NE(o_l, high);
3636 DCHECK_NE(o_h, low);
3637 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003638 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003639 if (shift_value > 32) {
3640 if (op->IsShl()) {
3641 __ Lsl(o_h, low, shift_value - 32);
3642 __ LoadImmediate(o_l, 0);
3643 } else if (op->IsShr()) {
3644 __ Asr(o_l, high, shift_value - 32);
3645 __ Asr(o_h, high, 31);
3646 } else {
3647 __ Lsr(o_l, high, shift_value - 32);
3648 __ LoadImmediate(o_h, 0);
3649 }
3650 } else if (shift_value == 32) {
3651 if (op->IsShl()) {
3652 __ mov(o_h, ShifterOperand(low));
3653 __ LoadImmediate(o_l, 0);
3654 } else if (op->IsShr()) {
3655 __ mov(o_l, ShifterOperand(high));
3656 __ Asr(o_h, high, 31);
3657 } else {
3658 __ mov(o_l, ShifterOperand(high));
3659 __ LoadImmediate(o_h, 0);
3660 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003661 } else if (shift_value == 1) {
3662 if (op->IsShl()) {
3663 __ Lsls(o_l, low, 1);
3664 __ adc(o_h, high, ShifterOperand(high));
3665 } else if (op->IsShr()) {
3666 __ Asrs(o_h, high, 1);
3667 __ Rrx(o_l, low);
3668 } else {
3669 __ Lsrs(o_h, high, 1);
3670 __ Rrx(o_l, low);
3671 }
3672 } else {
3673 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003674 if (op->IsShl()) {
3675 __ Lsl(o_h, high, shift_value);
3676 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3677 __ Lsl(o_l, low, shift_value);
3678 } else if (op->IsShr()) {
3679 __ Lsr(o_l, low, shift_value);
3680 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3681 __ Asr(o_h, high, shift_value);
3682 } else {
3683 __ Lsr(o_l, low, shift_value);
3684 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3685 __ Lsr(o_h, high, shift_value);
3686 }
3687 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003689 break;
3690 }
3691 default:
3692 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003693 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694 }
3695}
3696
3697void LocationsBuilderARM::VisitShl(HShl* shl) {
3698 HandleShift(shl);
3699}
3700
3701void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3702 HandleShift(shl);
3703}
3704
3705void LocationsBuilderARM::VisitShr(HShr* shr) {
3706 HandleShift(shr);
3707}
3708
3709void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3710 HandleShift(shr);
3711}
3712
3713void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3714 HandleShift(ushr);
3715}
3716
3717void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3718 HandleShift(ushr);
3719}
3720
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003721void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003722 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003724 if (instruction->IsStringAlloc()) {
3725 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3726 } else {
3727 InvokeRuntimeCallingConvention calling_convention;
3728 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3729 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3730 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003731 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003732}
3733
3734void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003735 // Note: if heap poisoning is enabled, the entry point takes cares
3736 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003737 if (instruction->IsStringAlloc()) {
3738 // String is allocated through StringFactory. Call NewEmptyString entry point.
3739 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003740 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003741 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3742 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3743 __ blx(LR);
3744 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3745 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003746 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003747 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3748 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003749}
3750
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003751void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3752 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003754 InvokeRuntimeCallingConvention calling_convention;
3755 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003756 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003757 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003758 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003759}
3760
3761void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3762 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003763 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003764 // Note: if heap poisoning is enabled, the entry point takes cares
3765 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003766 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003767 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003768}
3769
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003770void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003771 LocationSummary* locations =
3772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003773 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3774 if (location.IsStackSlot()) {
3775 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3776 } else if (location.IsDoubleStackSlot()) {
3777 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003778 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003779 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003780}
3781
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003782void InstructionCodeGeneratorARM::VisitParameterValue(
3783 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003784 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003785}
3786
3787void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3788 LocationSummary* locations =
3789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3790 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3791}
3792
3793void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3794 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003795}
3796
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003797void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003798 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003799 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003800 locations->SetInAt(0, Location::RequiresRegister());
3801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003802}
3803
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003804void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3805 LocationSummary* locations = not_->GetLocations();
3806 Location out = locations->Out();
3807 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003808 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003809 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003810 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003811 break;
3812
3813 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003814 __ mvn(out.AsRegisterPairLow<Register>(),
3815 ShifterOperand(in.AsRegisterPairLow<Register>()));
3816 __ mvn(out.AsRegisterPairHigh<Register>(),
3817 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003818 break;
3819
3820 default:
3821 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3822 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003823}
3824
David Brazdil66d126e2015-04-03 16:02:44 +01003825void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3826 LocationSummary* locations =
3827 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3828 locations->SetInAt(0, Location::RequiresRegister());
3829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3830}
3831
3832void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003833 LocationSummary* locations = bool_not->GetLocations();
3834 Location out = locations->Out();
3835 Location in = locations->InAt(0);
3836 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3837}
3838
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003839void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003840 LocationSummary* locations =
3841 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003842 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003843 case Primitive::kPrimBoolean:
3844 case Primitive::kPrimByte:
3845 case Primitive::kPrimShort:
3846 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003847 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003848 case Primitive::kPrimLong: {
3849 locations->SetInAt(0, Location::RequiresRegister());
3850 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003851 // Output overlaps because it is written before doing the low comparison.
3852 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003853 break;
3854 }
3855 case Primitive::kPrimFloat:
3856 case Primitive::kPrimDouble: {
3857 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003858 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003859 locations->SetOut(Location::RequiresRegister());
3860 break;
3861 }
3862 default:
3863 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3864 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003865}
3866
3867void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003868 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003870 Location left = locations->InAt(0);
3871 Location right = locations->InAt(1);
3872
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003873 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003874 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003875 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003876 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003877 case Primitive::kPrimBoolean:
3878 case Primitive::kPrimByte:
3879 case Primitive::kPrimShort:
3880 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003881 case Primitive::kPrimInt: {
3882 __ LoadImmediate(out, 0);
3883 __ cmp(left.AsRegister<Register>(),
3884 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3885 less_cond = LT;
3886 break;
3887 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003888 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003889 __ cmp(left.AsRegisterPairHigh<Register>(),
3890 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003891 __ b(&less, LT);
3892 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003893 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003894 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003895 __ cmp(left.AsRegisterPairLow<Register>(),
3896 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003897 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003898 break;
3899 }
3900 case Primitive::kPrimFloat:
3901 case Primitive::kPrimDouble: {
3902 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003903 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003904 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003905 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003906 break;
3907 }
3908 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003909 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003910 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003911 }
Aart Bika19616e2016-02-01 18:57:58 -08003912
Calin Juravleddb7df22014-11-25 20:56:51 +00003913 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003914 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003915
3916 __ Bind(&greater);
3917 __ LoadImmediate(out, 1);
3918 __ b(&done);
3919
3920 __ Bind(&less);
3921 __ LoadImmediate(out, -1);
3922
3923 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003924}
3925
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003926void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003927 LocationSummary* locations =
3928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003929 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003930 locations->SetInAt(i, Location::Any());
3931 }
3932 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003933}
3934
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003935void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003936 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003937}
3938
Roland Levillainc9285912015-12-18 10:38:42 +00003939void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3940 // TODO (ported from quick): revisit ARM barrier kinds.
3941 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003942 switch (kind) {
3943 case MemBarrierKind::kAnyStore:
3944 case MemBarrierKind::kLoadAny:
3945 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003946 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003947 break;
3948 }
3949 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003950 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003951 break;
3952 }
3953 default:
3954 LOG(FATAL) << "Unexpected memory barrier " << kind;
3955 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003956 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003957}
3958
3959void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3960 uint32_t offset,
3961 Register out_lo,
3962 Register out_hi) {
3963 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003964 // Ensure `out_lo` is different from `addr`, so that loading
3965 // `offset` into `out_lo` does not clutter `addr`.
3966 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003967 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003968 __ add(IP, addr, ShifterOperand(out_lo));
3969 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003970 }
3971 __ ldrexd(out_lo, out_hi, addr);
3972}
3973
3974void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3975 uint32_t offset,
3976 Register value_lo,
3977 Register value_hi,
3978 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003979 Register temp2,
3980 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003981 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003982 if (offset != 0) {
3983 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003984 __ add(IP, addr, ShifterOperand(temp1));
3985 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003986 }
3987 __ Bind(&fail);
3988 // We need a load followed by store. (The address used in a STREX instruction must
3989 // be the same as the address in the most recently executed LDREX instruction.)
3990 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003992 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003993 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003994}
3995
3996void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3997 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3998
Nicolas Geoffray39468442014-09-02 15:17:15 +01003999 LocationSummary* locations =
4000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004001 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004002
Calin Juravle52c48962014-12-16 17:02:57 +00004003 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004004 if (Primitive::IsFloatingPointType(field_type)) {
4005 locations->SetInAt(1, Location::RequiresFpuRegister());
4006 } else {
4007 locations->SetInAt(1, Location::RequiresRegister());
4008 }
4009
Calin Juravle52c48962014-12-16 17:02:57 +00004010 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004011 bool generate_volatile = field_info.IsVolatile()
4012 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004013 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004014 bool needs_write_barrier =
4015 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004016 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004017 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004018 if (needs_write_barrier) {
4019 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004020 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004021 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004022 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004023 // - registers need to be consecutive
4024 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004025 // We don't test for ARM yet, and the assertion makes sure that we
4026 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004027 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4028
4029 locations->AddTemp(Location::RequiresRegister());
4030 locations->AddTemp(Location::RequiresRegister());
4031 if (field_type == Primitive::kPrimDouble) {
4032 // For doubles we need two more registers to copy the value.
4033 locations->AddTemp(Location::RegisterLocation(R2));
4034 locations->AddTemp(Location::RegisterLocation(R3));
4035 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004036 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004037}
4038
Calin Juravle52c48962014-12-16 17:02:57 +00004039void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004040 const FieldInfo& field_info,
4041 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004042 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4043
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004044 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004045 Register base = locations->InAt(0).AsRegister<Register>();
4046 Location value = locations->InAt(1);
4047
4048 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004049 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004050 Primitive::Type field_type = field_info.GetFieldType();
4051 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004052 bool needs_write_barrier =
4053 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004054
4055 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004056 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004057 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004058
4059 switch (field_type) {
4060 case Primitive::kPrimBoolean:
4061 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004062 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063 break;
4064 }
4065
4066 case Primitive::kPrimShort:
4067 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004068 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069 break;
4070 }
4071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004073 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004074 if (kPoisonHeapReferences && needs_write_barrier) {
4075 // Note that in the case where `value` is a null reference,
4076 // we do not enter this block, as a null reference does not
4077 // need poisoning.
4078 DCHECK_EQ(field_type, Primitive::kPrimNot);
4079 Register temp = locations->GetTemp(0).AsRegister<Register>();
4080 __ Mov(temp, value.AsRegister<Register>());
4081 __ PoisonHeapReference(temp);
4082 __ StoreToOffset(kStoreWord, temp, base, offset);
4083 } else {
4084 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4085 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004086 break;
4087 }
4088
4089 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004090 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004091 GenerateWideAtomicStore(base, offset,
4092 value.AsRegisterPairLow<Register>(),
4093 value.AsRegisterPairHigh<Register>(),
4094 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004095 locations->GetTemp(1).AsRegister<Register>(),
4096 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004097 } else {
4098 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004099 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004100 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004101 break;
4102 }
4103
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004104 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004105 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004106 break;
4107 }
4108
4109 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004110 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004111 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004112 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4113 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4114
4115 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4116
4117 GenerateWideAtomicStore(base, offset,
4118 value_reg_lo,
4119 value_reg_hi,
4120 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004121 locations->GetTemp(3).AsRegister<Register>(),
4122 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004123 } else {
4124 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004125 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004126 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004127 break;
4128 }
4129
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130 case Primitive::kPrimVoid:
4131 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004132 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133 }
Calin Juravle52c48962014-12-16 17:02:57 +00004134
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 // Longs and doubles are handled in the switch.
4136 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4137 codegen_->MaybeRecordImplicitNullCheck(instruction);
4138 }
4139
4140 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4141 Register temp = locations->GetTemp(0).AsRegister<Register>();
4142 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004143 codegen_->MarkGCCard(
4144 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004145 }
4146
Calin Juravle52c48962014-12-16 17:02:57 +00004147 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004149 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004150}
4151
Calin Juravle52c48962014-12-16 17:02:57 +00004152void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4153 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004154
4155 bool object_field_get_with_read_barrier =
4156 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004157 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004158 new (GetGraph()->GetArena()) LocationSummary(instruction,
4159 object_field_get_with_read_barrier ?
4160 LocationSummary::kCallOnSlowPath :
4161 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004162 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004163 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004164 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004165 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004166
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004167 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004168 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004169 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004170 // The output overlaps in case of volatile long: we don't want the
4171 // code generated by GenerateWideAtomicLoad to overwrite the
4172 // object's location. Likewise, in the case of an object field get
4173 // with read barriers enabled, we do not want the load to overwrite
4174 // the object's location, as we need it to emit the read barrier.
4175 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4176 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004177
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004178 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4179 locations->SetOut(Location::RequiresFpuRegister());
4180 } else {
4181 locations->SetOut(Location::RequiresRegister(),
4182 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4183 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004184 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004185 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004186 // - registers need to be consecutive
4187 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004188 // We don't test for ARM yet, and the assertion makes sure that we
4189 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004190 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4191 locations->AddTemp(Location::RequiresRegister());
4192 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004193 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4194 // We need a temporary register for the read barrier marking slow
4195 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4196 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004197 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004198}
4199
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004200Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4201 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4202 << input->GetType();
4203 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4204 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4205 return Location::ConstantLocation(input->AsConstant());
4206 } else {
4207 return Location::RequiresFpuRegister();
4208 }
4209}
4210
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004211Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4212 Opcode opcode) {
4213 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4214 if (constant->IsConstant() &&
4215 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4216 return Location::ConstantLocation(constant->AsConstant());
4217 }
4218 return Location::RequiresRegister();
4219}
4220
4221bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4222 Opcode opcode) {
4223 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4224 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004225 Opcode high_opcode = opcode;
4226 SetCc low_set_cc = kCcDontCare;
4227 switch (opcode) {
4228 case SUB:
4229 // Flip the operation to an ADD.
4230 value = -value;
4231 opcode = ADD;
4232 FALLTHROUGH_INTENDED;
4233 case ADD:
4234 if (Low32Bits(value) == 0u) {
4235 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4236 }
4237 high_opcode = ADC;
4238 low_set_cc = kCcSet;
4239 break;
4240 default:
4241 break;
4242 }
4243 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4244 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004245 } else {
4246 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4247 }
4248}
4249
Vladimir Marko59751a72016-08-05 14:37:27 +01004250bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4251 Opcode opcode,
4252 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004253 ShifterOperand so;
4254 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004255 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004256 return true;
4257 }
4258 Opcode neg_opcode = kNoOperand;
4259 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004260 case AND: neg_opcode = BIC; value = ~value; break;
4261 case ORR: neg_opcode = ORN; value = ~value; break;
4262 case ADD: neg_opcode = SUB; value = -value; break;
4263 case ADC: neg_opcode = SBC; value = ~value; break;
4264 case SUB: neg_opcode = ADD; value = -value; break;
4265 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004266 default:
4267 return false;
4268 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004269 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004270}
4271
Calin Juravle52c48962014-12-16 17:02:57 +00004272void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4273 const FieldInfo& field_info) {
4274 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275
Calin Juravle52c48962014-12-16 17:02:57 +00004276 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004277 Location base_loc = locations->InAt(0);
4278 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004279 Location out = locations->Out();
4280 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004281 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004282 Primitive::Type field_type = field_info.GetFieldType();
4283 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4284
4285 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004286 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004287 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289
Roland Levillainc9285912015-12-18 10:38:42 +00004290 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004291 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293
Roland Levillainc9285912015-12-18 10:38:42 +00004294 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004295 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297
Roland Levillainc9285912015-12-18 10:38:42 +00004298 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004299 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004301
4302 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004303 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004305
4306 case Primitive::kPrimNot: {
4307 // /* HeapReference<Object> */ out = *(base + offset)
4308 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4309 Location temp_loc = locations->GetTemp(0);
4310 // Note that a potential implicit null check is handled in this
4311 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4312 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4313 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4314 if (is_volatile) {
4315 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4316 }
4317 } else {
4318 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4319 codegen_->MaybeRecordImplicitNullCheck(instruction);
4320 if (is_volatile) {
4321 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4322 }
4323 // If read barriers are enabled, emit read barriers other than
4324 // Baker's using a slow path (and also unpoison the loaded
4325 // reference, if heap poisoning is enabled).
4326 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4327 }
4328 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004329 }
4330
Roland Levillainc9285912015-12-18 10:38:42 +00004331 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004332 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004333 GenerateWideAtomicLoad(base, offset,
4334 out.AsRegisterPairLow<Register>(),
4335 out.AsRegisterPairHigh<Register>());
4336 } else {
4337 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4338 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340
Roland Levillainc9285912015-12-18 10:38:42 +00004341 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004342 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004343 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004344
4345 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004346 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004347 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004348 Register lo = locations->GetTemp(0).AsRegister<Register>();
4349 Register hi = locations->GetTemp(1).AsRegister<Register>();
4350 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004352 __ vmovdrr(out_reg, lo, hi);
4353 } else {
4354 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004356 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004357 break;
4358 }
4359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004361 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004362 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 }
Calin Juravle52c48962014-12-16 17:02:57 +00004364
Roland Levillainc9285912015-12-18 10:38:42 +00004365 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4366 // Potential implicit null checks, in the case of reference or
4367 // double fields, are handled in the previous switch statement.
4368 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004369 codegen_->MaybeRecordImplicitNullCheck(instruction);
4370 }
4371
Calin Juravle52c48962014-12-16 17:02:57 +00004372 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004373 if (field_type == Primitive::kPrimNot) {
4374 // Memory barriers, in the case of references, are also handled
4375 // in the previous switch statement.
4376 } else {
4377 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4378 }
Roland Levillain4d027112015-07-01 15:41:14 +01004379 }
Calin Juravle52c48962014-12-16 17:02:57 +00004380}
4381
4382void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4383 HandleFieldSet(instruction, instruction->GetFieldInfo());
4384}
4385
4386void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004387 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004388}
4389
4390void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4391 HandleFieldGet(instruction, instruction->GetFieldInfo());
4392}
4393
4394void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4395 HandleFieldGet(instruction, instruction->GetFieldInfo());
4396}
4397
4398void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4399 HandleFieldGet(instruction, instruction->GetFieldInfo());
4400}
4401
4402void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4403 HandleFieldGet(instruction, instruction->GetFieldInfo());
4404}
4405
4406void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4407 HandleFieldSet(instruction, instruction->GetFieldInfo());
4408}
4409
4410void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004411 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412}
4413
Calin Juravlee460d1d2015-09-29 04:52:17 +01004414void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4415 HUnresolvedInstanceFieldGet* instruction) {
4416 FieldAccessCallingConventionARM calling_convention;
4417 codegen_->CreateUnresolvedFieldLocationSummary(
4418 instruction, instruction->GetFieldType(), calling_convention);
4419}
4420
4421void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4422 HUnresolvedInstanceFieldGet* instruction) {
4423 FieldAccessCallingConventionARM calling_convention;
4424 codegen_->GenerateUnresolvedFieldAccess(instruction,
4425 instruction->GetFieldType(),
4426 instruction->GetFieldIndex(),
4427 instruction->GetDexPc(),
4428 calling_convention);
4429}
4430
4431void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4432 HUnresolvedInstanceFieldSet* instruction) {
4433 FieldAccessCallingConventionARM calling_convention;
4434 codegen_->CreateUnresolvedFieldLocationSummary(
4435 instruction, instruction->GetFieldType(), calling_convention);
4436}
4437
4438void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4439 HUnresolvedInstanceFieldSet* instruction) {
4440 FieldAccessCallingConventionARM calling_convention;
4441 codegen_->GenerateUnresolvedFieldAccess(instruction,
4442 instruction->GetFieldType(),
4443 instruction->GetFieldIndex(),
4444 instruction->GetDexPc(),
4445 calling_convention);
4446}
4447
4448void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4449 HUnresolvedStaticFieldGet* instruction) {
4450 FieldAccessCallingConventionARM calling_convention;
4451 codegen_->CreateUnresolvedFieldLocationSummary(
4452 instruction, instruction->GetFieldType(), calling_convention);
4453}
4454
4455void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4456 HUnresolvedStaticFieldGet* instruction) {
4457 FieldAccessCallingConventionARM calling_convention;
4458 codegen_->GenerateUnresolvedFieldAccess(instruction,
4459 instruction->GetFieldType(),
4460 instruction->GetFieldIndex(),
4461 instruction->GetDexPc(),
4462 calling_convention);
4463}
4464
4465void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4466 HUnresolvedStaticFieldSet* instruction) {
4467 FieldAccessCallingConventionARM calling_convention;
4468 codegen_->CreateUnresolvedFieldLocationSummary(
4469 instruction, instruction->GetFieldType(), calling_convention);
4470}
4471
4472void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4473 HUnresolvedStaticFieldSet* instruction) {
4474 FieldAccessCallingConventionARM calling_convention;
4475 codegen_->GenerateUnresolvedFieldAccess(instruction,
4476 instruction->GetFieldType(),
4477 instruction->GetFieldIndex(),
4478 instruction->GetDexPc(),
4479 calling_convention);
4480}
4481
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004483 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4484 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485}
4486
Calin Juravle2ae48182016-03-16 14:05:09 +00004487void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4488 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004489 return;
4490 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004491 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004492
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004493 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004494 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004495}
4496
Calin Juravle2ae48182016-03-16 14:05:09 +00004497void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004498 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004499 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500
4501 LocationSummary* locations = instruction->GetLocations();
4502 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004503
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004504 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505}
4506
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004507void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004508 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004509}
4510
Artem Serov6c916792016-07-11 14:02:34 +01004511static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4512 switch (type) {
4513 case Primitive::kPrimNot:
4514 return kLoadWord;
4515 case Primitive::kPrimBoolean:
4516 return kLoadUnsignedByte;
4517 case Primitive::kPrimByte:
4518 return kLoadSignedByte;
4519 case Primitive::kPrimChar:
4520 return kLoadUnsignedHalfword;
4521 case Primitive::kPrimShort:
4522 return kLoadSignedHalfword;
4523 case Primitive::kPrimInt:
4524 return kLoadWord;
4525 case Primitive::kPrimLong:
4526 return kLoadWordPair;
4527 case Primitive::kPrimFloat:
4528 return kLoadSWord;
4529 case Primitive::kPrimDouble:
4530 return kLoadDWord;
4531 default:
4532 LOG(FATAL) << "Unreachable type " << type;
4533 UNREACHABLE();
4534 }
4535}
4536
4537static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4538 switch (type) {
4539 case Primitive::kPrimNot:
4540 return kStoreWord;
4541 case Primitive::kPrimBoolean:
4542 case Primitive::kPrimByte:
4543 return kStoreByte;
4544 case Primitive::kPrimChar:
4545 case Primitive::kPrimShort:
4546 return kStoreHalfword;
4547 case Primitive::kPrimInt:
4548 return kStoreWord;
4549 case Primitive::kPrimLong:
4550 return kStoreWordPair;
4551 case Primitive::kPrimFloat:
4552 return kStoreSWord;
4553 case Primitive::kPrimDouble:
4554 return kStoreDWord;
4555 default:
4556 LOG(FATAL) << "Unreachable type " << type;
4557 UNREACHABLE();
4558 }
4559}
4560
4561void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4562 Location out_loc,
4563 Register base,
4564 Register reg_offset,
4565 Condition cond) {
4566 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4567 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4568
4569 switch (type) {
4570 case Primitive::kPrimByte:
4571 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4572 break;
4573 case Primitive::kPrimBoolean:
4574 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4575 break;
4576 case Primitive::kPrimShort:
4577 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4578 break;
4579 case Primitive::kPrimChar:
4580 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4581 break;
4582 case Primitive::kPrimNot:
4583 case Primitive::kPrimInt:
4584 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4585 break;
4586 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4587 case Primitive::kPrimLong:
4588 case Primitive::kPrimFloat:
4589 case Primitive::kPrimDouble:
4590 default:
4591 LOG(FATAL) << "Unreachable type " << type;
4592 UNREACHABLE();
4593 }
4594}
4595
4596void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4597 Location loc,
4598 Register base,
4599 Register reg_offset,
4600 Condition cond) {
4601 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4602 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4603
4604 switch (type) {
4605 case Primitive::kPrimByte:
4606 case Primitive::kPrimBoolean:
4607 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4608 break;
4609 case Primitive::kPrimShort:
4610 case Primitive::kPrimChar:
4611 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4612 break;
4613 case Primitive::kPrimNot:
4614 case Primitive::kPrimInt:
4615 __ str(loc.AsRegister<Register>(), mem_address, cond);
4616 break;
4617 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4618 case Primitive::kPrimLong:
4619 case Primitive::kPrimFloat:
4620 case Primitive::kPrimDouble:
4621 default:
4622 LOG(FATAL) << "Unreachable type " << type;
4623 UNREACHABLE();
4624 }
4625}
4626
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004628 bool object_array_get_with_read_barrier =
4629 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004630 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004631 new (GetGraph()->GetArena()) LocationSummary(instruction,
4632 object_array_get_with_read_barrier ?
4633 LocationSummary::kCallOnSlowPath :
4634 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004635 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004636 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004637 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004638 locations->SetInAt(0, Location::RequiresRegister());
4639 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004640 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4642 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004643 // The output overlaps in the case of an object array get with
4644 // read barriers enabled: we do not want the move to overwrite the
4645 // array's location, as we need it to emit the read barrier.
4646 locations->SetOut(
4647 Location::RequiresRegister(),
4648 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004649 }
Roland Levillainc9285912015-12-18 10:38:42 +00004650 // We need a temporary register for the read barrier marking slow
4651 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004652 // Also need for String compression feature.
4653 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4654 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004655 locations->AddTemp(Location::RequiresRegister());
4656 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657}
4658
4659void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4660 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004661 Location obj_loc = locations->InAt(0);
4662 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004663 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004664 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004665 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004666 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004667 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4668 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004669 HInstruction* array_instr = instruction->GetArray();
4670 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004671 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4672 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004673
Roland Levillain4d027112015-07-01 15:41:14 +01004674 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004675 case Primitive::kPrimBoolean:
4676 case Primitive::kPrimByte:
4677 case Primitive::kPrimShort:
4678 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004679 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004680 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004681 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004682 if (maybe_compressed_char_at) {
4683 Register length = IP;
4684 Label uncompressed_load, done;
4685 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4686 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4687 codegen_->MaybeRecordImplicitNullCheck(instruction);
4688 __ cmp(length, ShifterOperand(0));
4689 __ b(&uncompressed_load, GE);
4690 __ LoadFromOffset(kLoadUnsignedByte,
4691 out_loc.AsRegister<Register>(),
4692 obj,
4693 data_offset + const_index);
4694 __ b(&done);
4695 __ Bind(&uncompressed_load);
4696 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4697 out_loc.AsRegister<Register>(),
4698 obj,
4699 data_offset + (const_index << 1));
4700 __ Bind(&done);
4701 } else {
4702 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004703
jessicahandojo05765752016-09-09 19:01:32 -07004704 LoadOperandType load_type = GetLoadOperandType(type);
4705 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4706 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004708 Register temp = IP;
4709
4710 if (has_intermediate_address) {
4711 // We do not need to compute the intermediate address from the array: the
4712 // input instruction has done it already. See the comment in
4713 // `TryExtractArrayAccessAddress()`.
4714 if (kIsDebugBuild) {
4715 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4716 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4717 }
4718 temp = obj;
4719 } else {
4720 __ add(temp, obj, ShifterOperand(data_offset));
4721 }
jessicahandojo05765752016-09-09 19:01:32 -07004722 if (maybe_compressed_char_at) {
4723 Label uncompressed_load, done;
4724 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4725 Register length = locations->GetTemp(0).AsRegister<Register>();
4726 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4727 codegen_->MaybeRecordImplicitNullCheck(instruction);
4728 __ cmp(length, ShifterOperand(0));
4729 __ b(&uncompressed_load, GE);
4730 __ ldrb(out_loc.AsRegister<Register>(),
4731 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4732 __ b(&done);
4733 __ Bind(&uncompressed_load);
4734 __ ldrh(out_loc.AsRegister<Register>(),
4735 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4736 __ Bind(&done);
4737 } else {
4738 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 }
4741 break;
4742 }
4743
Roland Levillainc9285912015-12-18 10:38:42 +00004744 case Primitive::kPrimNot: {
4745 static_assert(
4746 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4747 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004748 // /* HeapReference<Object> */ out =
4749 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4750 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4751 Location temp = locations->GetTemp(0);
4752 // Note that a potential implicit null check is handled in this
4753 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4754 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4755 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4756 } else {
4757 Register out = out_loc.AsRegister<Register>();
4758 if (index.IsConstant()) {
4759 size_t offset =
4760 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4761 __ LoadFromOffset(kLoadWord, out, obj, offset);
4762 codegen_->MaybeRecordImplicitNullCheck(instruction);
4763 // If read barriers are enabled, emit read barriers other than
4764 // Baker's using a slow path (and also unpoison the loaded
4765 // reference, if heap poisoning is enabled).
4766 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4767 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004768 Register temp = IP;
4769
4770 if (has_intermediate_address) {
4771 // We do not need to compute the intermediate address from the array: the
4772 // input instruction has done it already. See the comment in
4773 // `TryExtractArrayAccessAddress()`.
4774 if (kIsDebugBuild) {
4775 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4776 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4777 }
4778 temp = obj;
4779 } else {
4780 __ add(temp, obj, ShifterOperand(data_offset));
4781 }
4782 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004783
Roland Levillainc9285912015-12-18 10:38:42 +00004784 codegen_->MaybeRecordImplicitNullCheck(instruction);
4785 // If read barriers are enabled, emit read barriers other than
4786 // Baker's using a slow path (and also unpoison the loaded
4787 // reference, if heap poisoning is enabled).
4788 codegen_->MaybeGenerateReadBarrierSlow(
4789 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4790 }
4791 }
4792 break;
4793 }
4794
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004795 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004796 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004797 size_t offset =
4798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004799 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004801 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004802 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803 }
4804 break;
4805 }
4806
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004807 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004808 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004809 if (index.IsConstant()) {
4810 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004811 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004812 } else {
4813 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004814 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004815 }
4816 break;
4817 }
4818
4819 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004820 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004821 if (index.IsConstant()) {
4822 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004823 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004824 } else {
4825 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004826 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004827 }
4828 break;
4829 }
4830
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004832 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004833 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834 }
Roland Levillain4d027112015-07-01 15:41:14 +01004835
4836 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004837 // Potential implicit null checks, in the case of reference
4838 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07004839 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00004840 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842}
4843
4844void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004845 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004846
4847 bool needs_write_barrier =
4848 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004849 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004850
Nicolas Geoffray39468442014-09-02 15:17:15 +01004851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004852 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004853 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004854 LocationSummary::kCallOnSlowPath :
4855 LocationSummary::kNoCall);
4856
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857 locations->SetInAt(0, Location::RequiresRegister());
4858 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4859 if (Primitive::IsFloatingPointType(value_type)) {
4860 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 locations->SetInAt(2, Location::RequiresRegister());
4863 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004864 if (needs_write_barrier) {
4865 // Temporary registers for the write barrier.
4866 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004867 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869}
4870
4871void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4872 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004873 Location array_loc = locations->InAt(0);
4874 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004876 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004877 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004878 bool needs_write_barrier =
4879 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004880 uint32_t data_offset =
4881 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4882 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004883 HInstruction* array_instr = instruction->GetArray();
4884 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004885 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4886 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887
4888 switch (value_type) {
4889 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004890 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004892 case Primitive::kPrimChar:
4893 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004895 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4896 uint32_t full_offset =
4897 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4898 StoreOperandType store_type = GetStoreOperandType(value_type);
4899 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004901 Register temp = IP;
4902
4903 if (has_intermediate_address) {
4904 // We do not need to compute the intermediate address from the array: the
4905 // input instruction has done it already. See the comment in
4906 // `TryExtractArrayAccessAddress()`.
4907 if (kIsDebugBuild) {
4908 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4909 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4910 }
4911 temp = array;
4912 } else {
4913 __ add(temp, array, ShifterOperand(data_offset));
4914 }
Artem Serov6c916792016-07-11 14:02:34 +01004915 codegen_->StoreToShiftedRegOffset(value_type,
4916 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004917 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004918 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 }
4920 break;
4921 }
4922
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004924 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004925 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4926 // See the comment in instruction_simplifier_shared.cc.
4927 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004928
4929 if (instruction->InputAt(2)->IsNullConstant()) {
4930 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004931 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004932 size_t offset =
4933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004934 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004935 } else {
4936 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004937 __ add(IP, array, ShifterOperand(data_offset));
4938 codegen_->StoreToShiftedRegOffset(value_type,
4939 value_loc,
4940 IP,
4941 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004942 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004943 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004944 DCHECK(!needs_write_barrier);
4945 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004946 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004947 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004948
4949 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004950 Location temp1_loc = locations->GetTemp(0);
4951 Register temp1 = temp1_loc.AsRegister<Register>();
4952 Location temp2_loc = locations->GetTemp(1);
4953 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004954 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4955 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4956 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4957 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01004958 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004959
Roland Levillain3b359c72015-11-17 19:35:12 +00004960 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4962 codegen_->AddSlowPath(slow_path);
4963 if (instruction->GetValueCanBeNull()) {
4964 Label non_zero;
4965 __ CompareAndBranchIfNonZero(value, &non_zero);
4966 if (index.IsConstant()) {
4967 size_t offset =
4968 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4969 __ StoreToOffset(kStoreWord, value, array, offset);
4970 } else {
4971 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004972 __ add(IP, array, ShifterOperand(data_offset));
4973 codegen_->StoreToShiftedRegOffset(value_type,
4974 value_loc,
4975 IP,
4976 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004977 }
4978 codegen_->MaybeRecordImplicitNullCheck(instruction);
4979 __ b(&done);
4980 __ Bind(&non_zero);
4981 }
4982
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004983 // Note that when read barriers are enabled, the type checks
4984 // are performed without read barriers. This is fine, even in
4985 // the case where a class object is in the from-space after
4986 // the flip, as a comparison involving such a type would not
4987 // produce a false positive; it may of course produce a false
4988 // negative, in which case we would take the ArraySet slow
4989 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004990
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004991 // /* HeapReference<Class> */ temp1 = array->klass_
4992 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4993 codegen_->MaybeRecordImplicitNullCheck(instruction);
4994 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004995
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004996 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4997 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4998 // /* HeapReference<Class> */ temp2 = value->klass_
4999 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5000 // If heap poisoning is enabled, no need to unpoison `temp1`
5001 // nor `temp2`, as we are comparing two poisoned references.
5002 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005003
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005004 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5005 Label do_put;
5006 __ b(&do_put, EQ);
5007 // If heap poisoning is enabled, the `temp1` reference has
5008 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005009 __ MaybeUnpoisonHeapReference(temp1);
5010
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005011 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5012 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5013 // If heap poisoning is enabled, no need to unpoison
5014 // `temp1`, as we are comparing against null below.
5015 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5016 __ Bind(&do_put);
5017 } else {
5018 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005019 }
5020 }
5021
Artem Serov6c916792016-07-11 14:02:34 +01005022 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005023 if (kPoisonHeapReferences) {
5024 // Note that in the case where `value` is a null reference,
5025 // we do not enter this block, as a null reference does not
5026 // need poisoning.
5027 DCHECK_EQ(value_type, Primitive::kPrimNot);
5028 __ Mov(temp1, value);
5029 __ PoisonHeapReference(temp1);
5030 source = temp1;
5031 }
5032
5033 if (index.IsConstant()) {
5034 size_t offset =
5035 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5036 __ StoreToOffset(kStoreWord, source, array, offset);
5037 } else {
5038 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005039
5040 __ add(IP, array, ShifterOperand(data_offset));
5041 codegen_->StoreToShiftedRegOffset(value_type,
5042 Location::RegisterLocation(source),
5043 IP,
5044 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005045 }
5046
Roland Levillain3b359c72015-11-17 19:35:12 +00005047 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005048 codegen_->MaybeRecordImplicitNullCheck(instruction);
5049 }
5050
5051 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5052
5053 if (done.IsLinked()) {
5054 __ Bind(&done);
5055 }
5056
5057 if (slow_path != nullptr) {
5058 __ Bind(slow_path->GetExitLabel());
5059 }
5060
5061 break;
5062 }
5063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005065 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005067 size_t offset =
5068 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005069 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005071 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005072 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 }
5074 break;
5075 }
5076
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005077 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005078 Location value = locations->InAt(2);
5079 DCHECK(value.IsFpuRegister());
5080 if (index.IsConstant()) {
5081 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005082 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005083 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005084 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005085 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5086 }
5087 break;
5088 }
5089
5090 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005091 Location value = locations->InAt(2);
5092 DCHECK(value.IsFpuRegisterPair());
5093 if (index.IsConstant()) {
5094 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005095 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005096 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005097 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005098 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5099 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005100
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005101 break;
5102 }
5103
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005105 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005106 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005108
Roland Levillain80e67092016-01-08 16:04:55 +00005109 // Objects are handled in the switch.
5110 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005111 codegen_->MaybeRecordImplicitNullCheck(instruction);
5112 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113}
5114
5115void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005116 LocationSummary* locations =
5117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005118 locations->SetInAt(0, Location::RequiresRegister());
5119 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120}
5121
5122void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5123 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005124 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 Register obj = locations->InAt(0).AsRegister<Register>();
5126 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005128 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005129 // Mask out compression flag from String's array length.
5130 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5131 __ bic(out, out, ShifterOperand(1u << 31));
5132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133}
5134
Artem Serov328429f2016-07-06 16:23:04 +01005135void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005136 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5137 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005138 LocationSummary* locations =
5139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5140
5141 locations->SetInAt(0, Location::RequiresRegister());
5142 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5144}
5145
5146void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5147 LocationSummary* locations = instruction->GetLocations();
5148 Location out = locations->Out();
5149 Location first = locations->InAt(0);
5150 Location second = locations->InAt(1);
5151
Roland Levillain4a3aa572016-08-15 13:17:06 +00005152 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5153 DCHECK(!kEmitCompilerReadBarrier);
5154
Artem Serov328429f2016-07-06 16:23:04 +01005155 if (second.IsRegister()) {
5156 __ add(out.AsRegister<Register>(),
5157 first.AsRegister<Register>(),
5158 ShifterOperand(second.AsRegister<Register>()));
5159 } else {
5160 __ AddConstant(out.AsRegister<Register>(),
5161 first.AsRegister<Register>(),
5162 second.GetConstant()->AsIntConstant()->GetValue());
5163 }
5164}
5165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005167 RegisterSet caller_saves = RegisterSet::Empty();
5168 InvokeRuntimeCallingConvention calling_convention;
5169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5170 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5171 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 locations->SetInAt(0, Location::RequiresRegister());
5173 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174}
5175
5176void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5177 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005178 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005179 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180 codegen_->AddSlowPath(slow_path);
5181
Roland Levillain271ab9c2014-11-27 15:23:57 +00005182 Register index = locations->InAt(0).AsRegister<Register>();
5183 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184
5185 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005186 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187}
5188
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005189void CodeGeneratorARM::MarkGCCard(Register temp,
5190 Register card,
5191 Register object,
5192 Register value,
5193 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005194 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005195 if (can_be_null) {
5196 __ CompareAndBranchIfZero(value, &is_null);
5197 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005198 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5200 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005201 if (can_be_null) {
5202 __ Bind(&is_null);
5203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204}
5205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005206void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005207 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005208}
5209
5210void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005211 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5212}
5213
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005214void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005215 LocationSummary* locations =
5216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005217 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005218}
5219
5220void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005221 HBasicBlock* block = instruction->GetBlock();
5222 if (block->GetLoopInformation() != nullptr) {
5223 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5224 // The back edge will generate the suspend check.
5225 return;
5226 }
5227 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5228 // The goto will generate the suspend check.
5229 return;
5230 }
5231 GenerateSuspendCheck(instruction, nullptr);
5232}
5233
5234void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5235 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005236 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005237 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5238 if (slow_path == nullptr) {
5239 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5240 instruction->SetSlowPath(slow_path);
5241 codegen_->AddSlowPath(slow_path);
5242 if (successor != nullptr) {
5243 DCHECK(successor->IsLoopHeader());
5244 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5245 }
5246 } else {
5247 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5248 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005249
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005250 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005251 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005252 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005253 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005254 __ Bind(slow_path->GetReturnLabel());
5255 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005256 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005257 __ b(slow_path->GetEntryLabel());
5258 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005259}
5260
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005261ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5262 return codegen_->GetAssembler();
5263}
5264
5265void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005266 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005267 Location source = move->GetSource();
5268 Location destination = move->GetDestination();
5269
5270 if (source.IsRegister()) {
5271 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005272 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005273 } else if (destination.IsFpuRegister()) {
5274 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005275 } else {
5276 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005277 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005278 SP, destination.GetStackIndex());
5279 }
5280 } else if (source.IsStackSlot()) {
5281 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005283 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005284 } else if (destination.IsFpuRegister()) {
5285 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005286 } else {
5287 DCHECK(destination.IsStackSlot());
5288 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5289 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5290 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005291 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005292 if (destination.IsRegister()) {
5293 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5294 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005295 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005296 } else {
5297 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005298 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5299 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005300 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005301 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005302 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5303 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005304 } else if (destination.IsRegisterPair()) {
5305 DCHECK(ExpectedPairLayout(destination));
5306 __ LoadFromOffset(
5307 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5308 } else {
5309 DCHECK(destination.IsFpuRegisterPair()) << destination;
5310 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5311 SP,
5312 source.GetStackIndex());
5313 }
5314 } else if (source.IsRegisterPair()) {
5315 if (destination.IsRegisterPair()) {
5316 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5317 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005318 } else if (destination.IsFpuRegisterPair()) {
5319 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5320 source.AsRegisterPairLow<Register>(),
5321 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005322 } else {
5323 DCHECK(destination.IsDoubleStackSlot()) << destination;
5324 DCHECK(ExpectedPairLayout(source));
5325 __ StoreToOffset(
5326 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5327 }
5328 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005329 if (destination.IsRegisterPair()) {
5330 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5331 destination.AsRegisterPairHigh<Register>(),
5332 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5333 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005334 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5335 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5336 } else {
5337 DCHECK(destination.IsDoubleStackSlot()) << destination;
5338 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5339 SP,
5340 destination.GetStackIndex());
5341 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005342 } else {
5343 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005344 HConstant* constant = source.GetConstant();
5345 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5346 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005347 if (destination.IsRegister()) {
5348 __ LoadImmediate(destination.AsRegister<Register>(), value);
5349 } else {
5350 DCHECK(destination.IsStackSlot());
5351 __ LoadImmediate(IP, value);
5352 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5353 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005354 } else if (constant->IsLongConstant()) {
5355 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005356 if (destination.IsRegisterPair()) {
5357 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5358 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005359 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005360 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005361 __ LoadImmediate(IP, Low32Bits(value));
5362 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5363 __ LoadImmediate(IP, High32Bits(value));
5364 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5365 }
5366 } else if (constant->IsDoubleConstant()) {
5367 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005368 if (destination.IsFpuRegisterPair()) {
5369 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005370 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005371 DCHECK(destination.IsDoubleStackSlot()) << destination;
5372 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005373 __ LoadImmediate(IP, Low32Bits(int_value));
5374 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5375 __ LoadImmediate(IP, High32Bits(int_value));
5376 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5377 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005378 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005379 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005380 float value = constant->AsFloatConstant()->GetValue();
5381 if (destination.IsFpuRegister()) {
5382 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5383 } else {
5384 DCHECK(destination.IsStackSlot());
5385 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5386 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5387 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005388 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005389 }
5390}
5391
5392void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5393 __ Mov(IP, reg);
5394 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5395 __ StoreToOffset(kStoreWord, IP, SP, mem);
5396}
5397
5398void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5399 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5400 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5401 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5402 SP, mem1 + stack_offset);
5403 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5404 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5405 SP, mem2 + stack_offset);
5406 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5407}
5408
5409void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005410 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005411 Location source = move->GetSource();
5412 Location destination = move->GetDestination();
5413
5414 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005415 DCHECK_NE(source.AsRegister<Register>(), IP);
5416 DCHECK_NE(destination.AsRegister<Register>(), IP);
5417 __ Mov(IP, source.AsRegister<Register>());
5418 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5419 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005420 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005421 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005422 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005423 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005424 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5425 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005426 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005427 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005428 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005429 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005430 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005431 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005432 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005433 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005434 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5435 destination.AsRegisterPairHigh<Register>(),
5436 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005437 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005438 Register low_reg = source.IsRegisterPair()
5439 ? source.AsRegisterPairLow<Register>()
5440 : destination.AsRegisterPairLow<Register>();
5441 int mem = source.IsRegisterPair()
5442 ? destination.GetStackIndex()
5443 : source.GetStackIndex();
5444 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005445 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005446 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005447 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005448 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005449 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5450 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005451 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005452 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005453 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005454 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5455 DRegister reg = source.IsFpuRegisterPair()
5456 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5457 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5458 int mem = source.IsFpuRegisterPair()
5459 ? destination.GetStackIndex()
5460 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005461 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005462 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005463 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005464 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5465 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5466 : destination.AsFpuRegister<SRegister>();
5467 int mem = source.IsFpuRegister()
5468 ? destination.GetStackIndex()
5469 : source.GetStackIndex();
5470
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005471 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005472 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005473 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005474 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005475 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5476 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005477 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005478 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005479 }
5480}
5481
5482void ParallelMoveResolverARM::SpillScratch(int reg) {
5483 __ Push(static_cast<Register>(reg));
5484}
5485
5486void ParallelMoveResolverARM::RestoreScratch(int reg) {
5487 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005488}
5489
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005490HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5491 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 switch (desired_class_load_kind) {
5493 case HLoadClass::LoadKind::kReferrersClass:
5494 break;
5495 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5496 DCHECK(!GetCompilerOptions().GetCompilePic());
5497 break;
5498 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5499 DCHECK(GetCompilerOptions().GetCompilePic());
5500 break;
5501 case HLoadClass::LoadKind::kBootImageAddress:
5502 break;
5503 case HLoadClass::LoadKind::kDexCacheAddress:
5504 DCHECK(Runtime::Current()->UseJitCompilation());
5505 break;
5506 case HLoadClass::LoadKind::kDexCachePcRelative:
5507 DCHECK(!Runtime::Current()->UseJitCompilation());
5508 // We disable pc-relative load when there is an irreducible loop, as the optimization
5509 // is incompatible with it.
5510 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5511 // with irreducible loops.
5512 if (GetGraph()->HasIrreducibleLoops()) {
5513 return HLoadClass::LoadKind::kDexCacheViaMethod;
5514 }
5515 break;
5516 case HLoadClass::LoadKind::kDexCacheViaMethod:
5517 break;
5518 }
5519 return desired_class_load_kind;
5520}
5521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005522void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005523 if (cls->NeedsAccessCheck()) {
5524 InvokeRuntimeCallingConvention calling_convention;
5525 CodeGenerator::CreateLoadClassLocationSummary(
5526 cls,
5527 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5528 Location::RegisterLocation(R0),
5529 /* code_generator_supports_read_barrier */ true);
5530 return;
5531 }
5532
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005533 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5534 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005535 ? LocationSummary::kCallOnSlowPath
5536 : LocationSummary::kNoCall;
5537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005538 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005539 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005540 }
5541
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005542 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5543 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5544 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5545 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5546 locations->SetInAt(0, Location::RequiresRegister());
5547 }
5548 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005549}
5550
5551void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005552 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005553 if (cls->NeedsAccessCheck()) {
5554 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005555 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005556 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005557 return;
5558 }
5559
Roland Levillain3b359c72015-11-17 19:35:12 +00005560 Location out_loc = locations->Out();
5561 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005562
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005563 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005564 bool generate_null_check = false;
5565 switch (cls->GetLoadKind()) {
5566 case HLoadClass::LoadKind::kReferrersClass: {
5567 DCHECK(!cls->CanCallRuntime());
5568 DCHECK(!cls->MustGenerateClinitCheck());
5569 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5570 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005571 GenerateGcRootFieldLoad(cls,
5572 out_loc,
5573 current_method,
5574 ArtMethod::DeclaringClassOffset().Int32Value(),
5575 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005576 break;
5577 }
5578 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005579 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005580 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5581 cls->GetTypeIndex()));
5582 break;
5583 }
5584 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005585 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005586 CodeGeneratorARM::PcRelativePatchInfo* labels =
5587 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5588 __ BindTrackedLabel(&labels->movw_label);
5589 __ movw(out, /* placeholder */ 0u);
5590 __ BindTrackedLabel(&labels->movt_label);
5591 __ movt(out, /* placeholder */ 0u);
5592 __ BindTrackedLabel(&labels->add_pc_label);
5593 __ add(out, out, ShifterOperand(PC));
5594 break;
5595 }
5596 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005597 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005598 DCHECK_NE(cls->GetAddress(), 0u);
5599 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5600 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5601 break;
5602 }
5603 case HLoadClass::LoadKind::kDexCacheAddress: {
5604 DCHECK_NE(cls->GetAddress(), 0u);
5605 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5606 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5607 // a 128B range. To try and reduce the number of literals if we load multiple types,
5608 // simply split the dex cache address to a 128B aligned base loaded from a literal
5609 // and the remaining offset embedded in the load.
5610 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5611 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5612 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5613 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5614 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5615 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5616 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005617 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005618 generate_null_check = !cls->IsInDexCache();
5619 break;
5620 }
5621 case HLoadClass::LoadKind::kDexCachePcRelative: {
5622 Register base_reg = locations->InAt(0).AsRegister<Register>();
5623 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5624 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5625 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005626 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005627 generate_null_check = !cls->IsInDexCache();
5628 break;
5629 }
5630 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5631 // /* GcRoot<mirror::Class>[] */ out =
5632 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5633 Register current_method = locations->InAt(0).AsRegister<Register>();
5634 __ LoadFromOffset(kLoadWord,
5635 out,
5636 current_method,
5637 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5638 // /* GcRoot<mirror::Class> */ out = out[type_index]
5639 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005640 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005641 generate_null_check = !cls->IsInDexCache();
5642 }
5643 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005644
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005645 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5646 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005647 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005648 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5649 codegen_->AddSlowPath(slow_path);
5650 if (generate_null_check) {
5651 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5652 }
5653 if (cls->MustGenerateClinitCheck()) {
5654 GenerateClassInitializationCheck(slow_path, out);
5655 } else {
5656 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005657 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005658 }
5659}
5660
5661void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5662 LocationSummary* locations =
5663 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5664 locations->SetInAt(0, Location::RequiresRegister());
5665 if (check->HasUses()) {
5666 locations->SetOut(Location::SameAsFirstInput());
5667 }
5668}
5669
5670void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005671 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005672 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005673 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005674 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005675 GenerateClassInitializationCheck(slow_path,
5676 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005677}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005679void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005680 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005681 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5682 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5683 __ b(slow_path->GetEntryLabel(), LT);
5684 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5685 // properly. Therefore, we do a memory fence.
5686 __ dmb(ISH);
5687 __ Bind(slow_path->GetExitLabel());
5688}
5689
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005690HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5691 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005692 switch (desired_string_load_kind) {
5693 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5694 DCHECK(!GetCompilerOptions().GetCompilePic());
5695 break;
5696 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5697 DCHECK(GetCompilerOptions().GetCompilePic());
5698 break;
5699 case HLoadString::LoadKind::kBootImageAddress:
5700 break;
5701 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005702 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005704 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005705 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005706 break;
5707 case HLoadString::LoadKind::kDexCacheViaMethod:
5708 break;
5709 }
5710 return desired_string_load_kind;
5711}
5712
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005713void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005714 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005715 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5716 ? LocationSummary::kCallOnMainOnly
5717 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005718 : LocationSummary::kNoCall;
5719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005720
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005721 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005722 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005723 locations->SetOut(Location::RegisterLocation(R0));
5724 } else {
5725 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005726 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5727 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5728 // Rely on the pResolveString and/or marking to save everything, including temps.
5729 // Note that IP may theoretically be clobbered by saving/restoring the live register
5730 // (only one thanks to the custom calling convention), so we request a different temp.
5731 locations->AddTemp(Location::RequiresRegister());
5732 RegisterSet caller_saves = RegisterSet::Empty();
5733 InvokeRuntimeCallingConvention calling_convention;
5734 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5735 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5736 // that the the kPrimNot result register is the same as the first argument register.
5737 locations->SetCustomSlowPathCallerSaves(caller_saves);
5738 } else {
5739 // For non-Baker read barrier we have a temp-clobbering call.
5740 }
5741 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005742 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005743}
5744
5745void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005746 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005747 Location out_loc = locations->Out();
5748 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005749 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005750
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005751 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005752 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5754 load->GetStringIndex()));
5755 return; // No dex cache slow path.
5756 }
5757 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005758 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005759 CodeGeneratorARM::PcRelativePatchInfo* labels =
5760 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5761 __ BindTrackedLabel(&labels->movw_label);
5762 __ movw(out, /* placeholder */ 0u);
5763 __ BindTrackedLabel(&labels->movt_label);
5764 __ movt(out, /* placeholder */ 0u);
5765 __ BindTrackedLabel(&labels->add_pc_label);
5766 __ add(out, out, ShifterOperand(PC));
5767 return; // No dex cache slow path.
5768 }
5769 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770 DCHECK_NE(load->GetAddress(), 0u);
5771 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5772 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5773 return; // No dex cache slow path.
5774 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005775 case HLoadString::LoadKind::kBssEntry: {
5776 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005777 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005778 CodeGeneratorARM::PcRelativePatchInfo* labels =
5779 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5780 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005781 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005782 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005783 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005784 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005785 __ add(temp, temp, ShifterOperand(PC));
5786 GenerateGcRootFieldLoad(load, out_loc, temp, 0);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005787 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5788 codegen_->AddSlowPath(slow_path);
5789 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5790 __ Bind(slow_path->GetExitLabel());
5791 return;
5792 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005793 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005794 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005796
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005797 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5798 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5799 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005800 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005801 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5802 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5803 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005804}
5805
David Brazdilcb1c0552015-08-04 16:22:25 +01005806static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005807 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005808}
5809
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005810void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5811 LocationSummary* locations =
5812 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5813 locations->SetOut(Location::RequiresRegister());
5814}
5815
5816void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005817 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005818 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5819}
5820
5821void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5822 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5823}
5824
5825void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005826 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005827 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005828}
5829
5830void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5831 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005833 InvokeRuntimeCallingConvention calling_convention;
5834 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5835}
5836
5837void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005838 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005839 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005840}
5841
Roland Levillainc9285912015-12-18 10:38:42 +00005842static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5843 return kEmitCompilerReadBarrier &&
5844 (kUseBakerReadBarrier ||
5845 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5846 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5847 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5848}
5849
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005850void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005852 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005853 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005854 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 case TypeCheckKind::kExactCheck:
5856 case TypeCheckKind::kAbstractClassCheck:
5857 case TypeCheckKind::kClassHierarchyCheck:
5858 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005859 call_kind =
5860 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005861 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 break;
5863 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005864 case TypeCheckKind::kUnresolvedCheck:
5865 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866 call_kind = LocationSummary::kCallOnSlowPath;
5867 break;
5868 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005869
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005871 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005872 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005873 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005874 locations->SetInAt(0, Location::RequiresRegister());
5875 locations->SetInAt(1, Location::RequiresRegister());
5876 // The "out" register is used as a temporary, so it overlaps with the inputs.
5877 // Note that TypeCheckSlowPathARM uses this register too.
5878 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5879 // When read barriers are enabled, we need a temporary register for
5880 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005881 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005882 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005884}
5885
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005886void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005887 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005888 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005889 Location obj_loc = locations->InAt(0);
5890 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005891 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005892 Location out_loc = locations->Out();
5893 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005894 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005895 locations->GetTemp(0) :
5896 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005897 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005898 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5899 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5900 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005901 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005902 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005903
5904 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005905 // avoid null check if we know obj is not null.
5906 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005907 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005908 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005909
Roland Levillain3b359c72015-11-17 19:35:12 +00005910 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005911 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912
Roland Levillainc9285912015-12-18 10:38:42 +00005913 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 case TypeCheckKind::kExactCheck: {
5915 __ cmp(out, ShifterOperand(cls));
5916 // Classes must be equal for the instanceof to succeed.
5917 __ b(&zero, NE);
5918 __ LoadImmediate(out, 1);
5919 __ b(&done);
5920 break;
5921 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005922
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 case TypeCheckKind::kAbstractClassCheck: {
5924 // If the class is abstract, we eagerly fetch the super class of the
5925 // object to avoid doing a comparison we know will fail.
5926 Label loop;
5927 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005928 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005929 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 // If `out` is null, we use it for the result, and jump to `done`.
5931 __ CompareAndBranchIfZero(out, &done);
5932 __ cmp(out, ShifterOperand(cls));
5933 __ b(&loop, NE);
5934 __ LoadImmediate(out, 1);
5935 if (zero.IsLinked()) {
5936 __ b(&done);
5937 }
5938 break;
5939 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005940
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 case TypeCheckKind::kClassHierarchyCheck: {
5942 // Walk over the class hierarchy to find a match.
5943 Label loop, success;
5944 __ Bind(&loop);
5945 __ cmp(out, ShifterOperand(cls));
5946 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005947 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005948 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 __ CompareAndBranchIfNonZero(out, &loop);
5950 // If `out` is null, we use it for the result, and jump to `done`.
5951 __ b(&done);
5952 __ Bind(&success);
5953 __ LoadImmediate(out, 1);
5954 if (zero.IsLinked()) {
5955 __ b(&done);
5956 }
5957 break;
5958 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005959
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005961 // Do an exact check.
5962 Label exact_check;
5963 __ cmp(out, ShifterOperand(cls));
5964 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005965 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005966 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005967 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 // If `out` is null, we use it for the result, and jump to `done`.
5969 __ CompareAndBranchIfZero(out, &done);
5970 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5971 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5972 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005973 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974 __ LoadImmediate(out, 1);
5975 __ b(&done);
5976 break;
5977 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 case TypeCheckKind::kArrayCheck: {
5980 __ cmp(out, ShifterOperand(cls));
5981 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005982 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5983 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005984 codegen_->AddSlowPath(slow_path);
5985 __ b(slow_path->GetEntryLabel(), NE);
5986 __ LoadImmediate(out, 1);
5987 if (zero.IsLinked()) {
5988 __ b(&done);
5989 }
5990 break;
5991 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005992
Calin Juravle98893e12015-10-02 21:05:03 +01005993 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005994 case TypeCheckKind::kInterfaceCheck: {
5995 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005996 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005997 // cases.
5998 //
5999 // We cannot directly call the InstanceofNonTrivial runtime
6000 // entry point without resorting to a type checking slow path
6001 // here (i.e. by calling InvokeRuntime directly), as it would
6002 // require to assign fixed registers for the inputs of this
6003 // HInstanceOf instruction (following the runtime calling
6004 // convention), which might be cluttered by the potential first
6005 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006006 //
6007 // TODO: Introduce a new runtime entry point taking the object
6008 // to test (instead of its class) as argument, and let it deal
6009 // with the read barrier issues. This will let us refactor this
6010 // case of the `switch` code as it was previously (with a direct
6011 // call to the runtime not using a type checking slow path).
6012 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006013 DCHECK(locations->OnlyCallsOnSlowPath());
6014 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6015 /* is_fatal */ false);
6016 codegen_->AddSlowPath(slow_path);
6017 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 if (zero.IsLinked()) {
6019 __ b(&done);
6020 }
6021 break;
6022 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006023 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006024
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006026 __ Bind(&zero);
6027 __ LoadImmediate(out, 0);
6028 }
6029
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 if (done.IsLinked()) {
6031 __ Bind(&done);
6032 }
6033
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006034 if (slow_path != nullptr) {
6035 __ Bind(slow_path->GetExitLabel());
6036 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006037}
6038
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006039void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6041 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6042
Roland Levillain3b359c72015-11-17 19:35:12 +00006043 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6044 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006045 case TypeCheckKind::kExactCheck:
6046 case TypeCheckKind::kAbstractClassCheck:
6047 case TypeCheckKind::kClassHierarchyCheck:
6048 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006049 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6050 LocationSummary::kCallOnSlowPath :
6051 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 break;
6053 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006054 case TypeCheckKind::kUnresolvedCheck:
6055 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 call_kind = LocationSummary::kCallOnSlowPath;
6057 break;
6058 }
6059
Roland Levillain3b359c72015-11-17 19:35:12 +00006060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6061 locations->SetInAt(0, Location::RequiresRegister());
6062 locations->SetInAt(1, Location::RequiresRegister());
6063 // Note that TypeCheckSlowPathARM uses this "temp" register too.
6064 locations->AddTemp(Location::RequiresRegister());
6065 // When read barriers are enabled, we need an additional temporary
6066 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006067 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006068 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006070}
6071
6072void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006073 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006074 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006075 Location obj_loc = locations->InAt(0);
6076 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006077 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006078 Location temp_loc = locations->GetTemp(0);
6079 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006080 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006081 locations->GetTemp(1) :
6082 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006083 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006084 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6085 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6086 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006087
Roland Levillain3b359c72015-11-17 19:35:12 +00006088 bool is_type_check_slow_path_fatal =
6089 (type_check_kind == TypeCheckKind::kExactCheck ||
6090 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6091 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6092 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6093 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006094 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006095 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6096 is_type_check_slow_path_fatal);
6097 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098
6099 Label done;
6100 // Avoid null check if we know obj is not null.
6101 if (instruction->MustDoNullCheck()) {
6102 __ CompareAndBranchIfZero(obj, &done);
6103 }
6104
Roland Levillain3b359c72015-11-17 19:35:12 +00006105 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006106 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006107
Roland Levillain3b359c72015-11-17 19:35:12 +00006108 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 case TypeCheckKind::kExactCheck:
6110 case TypeCheckKind::kArrayCheck: {
6111 __ cmp(temp, ShifterOperand(cls));
6112 // Jump to slow path for throwing the exception or doing a
6113 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006114 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 break;
6116 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006117
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 case TypeCheckKind::kAbstractClassCheck: {
6119 // If the class is abstract, we eagerly fetch the super class of the
6120 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00006121 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006123 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006124 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006125
6126 // If the class reference currently in `temp` is not null, jump
6127 // to the `compare_classes` label to compare it with the checked
6128 // class.
6129 __ CompareAndBranchIfNonZero(temp, &compare_classes);
6130 // Otherwise, jump to the slow path to throw the exception.
6131 //
6132 // But before, move back the object's class into `temp` before
6133 // going into the slow path, as it has been overwritten in the
6134 // meantime.
6135 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006136 GenerateReferenceLoadTwoRegisters(
6137 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 __ b(type_check_slow_path->GetEntryLabel());
6139
6140 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 __ cmp(temp, ShifterOperand(cls));
6142 __ b(&loop, NE);
6143 break;
6144 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006145
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 case TypeCheckKind::kClassHierarchyCheck: {
6147 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006148 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 __ Bind(&loop);
6150 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006151 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006152
Roland Levillain3b359c72015-11-17 19:35:12 +00006153 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006154 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006155
6156 // If the class reference currently in `temp` is not null, jump
6157 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006159 // Otherwise, jump to the slow path to throw the exception.
6160 //
6161 // But before, move back the object's class into `temp` before
6162 // going into the slow path, as it has been overwritten in the
6163 // meantime.
6164 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006165 GenerateReferenceLoadTwoRegisters(
6166 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006167 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 break;
6169 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006172 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006173 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006174 __ cmp(temp, ShifterOperand(cls));
6175 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006176
6177 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006178 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006179 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006180
6181 // If the component type is not null (i.e. the object is indeed
6182 // an array), jump to label `check_non_primitive_component_type`
6183 // to further check that this component type is not a primitive
6184 // type.
6185 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6186 // Otherwise, jump to the slow path to throw the exception.
6187 //
6188 // But before, move back the object's class into `temp` before
6189 // going into the slow path, as it has been overwritten in the
6190 // meantime.
6191 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006192 GenerateReferenceLoadTwoRegisters(
6193 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006194 __ b(type_check_slow_path->GetEntryLabel());
6195
6196 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006198 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6199 __ CompareAndBranchIfZero(temp, &done);
6200 // Same comment as above regarding `temp` and the slow path.
6201 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006202 GenerateReferenceLoadTwoRegisters(
6203 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006204 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 break;
6206 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006207
Calin Juravle98893e12015-10-02 21:05:03 +01006208 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006210 // We always go into the type check slow path for the unresolved
6211 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006212 //
6213 // We cannot directly call the CheckCast runtime entry point
6214 // without resorting to a type checking slow path here (i.e. by
6215 // calling InvokeRuntime directly), as it would require to
6216 // assign fixed registers for the inputs of this HInstanceOf
6217 // instruction (following the runtime calling convention), which
6218 // might be cluttered by the potential first read barrier
6219 // 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 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 break;
6229 }
6230 __ Bind(&done);
6231
Roland Levillain3b359c72015-11-17 19:35:12 +00006232 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006233}
6234
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006235void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6236 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006238 InvokeRuntimeCallingConvention calling_convention;
6239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6240}
6241
6242void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006243 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6244 instruction,
6245 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006246 if (instruction->IsEnter()) {
6247 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6248 } else {
6249 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6250 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006251}
6252
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006253void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6254void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6255void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006256
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006257void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006258 LocationSummary* locations =
6259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6260 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6261 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006262 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006263 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006264 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006266}
6267
6268void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6269 HandleBitwiseOperation(instruction);
6270}
6271
6272void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6273 HandleBitwiseOperation(instruction);
6274}
6275
6276void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6277 HandleBitwiseOperation(instruction);
6278}
6279
Artem Serov7fc63502016-02-09 17:15:29 +00006280
6281void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6282 LocationSummary* locations =
6283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6284 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6285 || instruction->GetResultType() == Primitive::kPrimLong);
6286
6287 locations->SetInAt(0, Location::RequiresRegister());
6288 locations->SetInAt(1, Location::RequiresRegister());
6289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6290}
6291
6292void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6293 LocationSummary* locations = instruction->GetLocations();
6294 Location first = locations->InAt(0);
6295 Location second = locations->InAt(1);
6296 Location out = locations->Out();
6297
6298 if (instruction->GetResultType() == Primitive::kPrimInt) {
6299 Register first_reg = first.AsRegister<Register>();
6300 ShifterOperand second_reg(second.AsRegister<Register>());
6301 Register out_reg = out.AsRegister<Register>();
6302
6303 switch (instruction->GetOpKind()) {
6304 case HInstruction::kAnd:
6305 __ bic(out_reg, first_reg, second_reg);
6306 break;
6307 case HInstruction::kOr:
6308 __ orn(out_reg, first_reg, second_reg);
6309 break;
6310 // There is no EON on arm.
6311 case HInstruction::kXor:
6312 default:
6313 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6314 UNREACHABLE();
6315 }
6316 return;
6317
6318 } else {
6319 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6320 Register first_low = first.AsRegisterPairLow<Register>();
6321 Register first_high = first.AsRegisterPairHigh<Register>();
6322 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6323 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6324 Register out_low = out.AsRegisterPairLow<Register>();
6325 Register out_high = out.AsRegisterPairHigh<Register>();
6326
6327 switch (instruction->GetOpKind()) {
6328 case HInstruction::kAnd:
6329 __ bic(out_low, first_low, second_low);
6330 __ bic(out_high, first_high, second_high);
6331 break;
6332 case HInstruction::kOr:
6333 __ orn(out_low, first_low, second_low);
6334 __ orn(out_high, first_high, second_high);
6335 break;
6336 // There is no EON on arm.
6337 case HInstruction::kXor:
6338 default:
6339 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6340 UNREACHABLE();
6341 }
6342 }
6343}
6344
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006345void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6346 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6347 if (value == 0xffffffffu) {
6348 if (out != first) {
6349 __ mov(out, ShifterOperand(first));
6350 }
6351 return;
6352 }
6353 if (value == 0u) {
6354 __ mov(out, ShifterOperand(0));
6355 return;
6356 }
6357 ShifterOperand so;
6358 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6359 __ and_(out, first, so);
6360 } else {
6361 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6362 __ bic(out, first, ShifterOperand(~value));
6363 }
6364}
6365
6366void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6367 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6368 if (value == 0u) {
6369 if (out != first) {
6370 __ mov(out, ShifterOperand(first));
6371 }
6372 return;
6373 }
6374 if (value == 0xffffffffu) {
6375 __ mvn(out, ShifterOperand(0));
6376 return;
6377 }
6378 ShifterOperand so;
6379 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6380 __ orr(out, first, so);
6381 } else {
6382 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6383 __ orn(out, first, ShifterOperand(~value));
6384 }
6385}
6386
6387void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6388 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6389 if (value == 0u) {
6390 if (out != first) {
6391 __ mov(out, ShifterOperand(first));
6392 }
6393 return;
6394 }
6395 __ eor(out, first, ShifterOperand(value));
6396}
6397
Vladimir Marko59751a72016-08-05 14:37:27 +01006398void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6399 Location first,
6400 uint64_t value) {
6401 Register out_low = out.AsRegisterPairLow<Register>();
6402 Register out_high = out.AsRegisterPairHigh<Register>();
6403 Register first_low = first.AsRegisterPairLow<Register>();
6404 Register first_high = first.AsRegisterPairHigh<Register>();
6405 uint32_t value_low = Low32Bits(value);
6406 uint32_t value_high = High32Bits(value);
6407 if (value_low == 0u) {
6408 if (out_low != first_low) {
6409 __ mov(out_low, ShifterOperand(first_low));
6410 }
6411 __ AddConstant(out_high, first_high, value_high);
6412 return;
6413 }
6414 __ AddConstantSetFlags(out_low, first_low, value_low);
6415 ShifterOperand so;
6416 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6417 __ adc(out_high, first_high, so);
6418 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6419 __ sbc(out_high, first_high, so);
6420 } else {
6421 LOG(FATAL) << "Unexpected constant " << value_high;
6422 UNREACHABLE();
6423 }
6424}
6425
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006426void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6427 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006428 Location first = locations->InAt(0);
6429 Location second = locations->InAt(1);
6430 Location out = locations->Out();
6431
6432 if (second.IsConstant()) {
6433 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6434 uint32_t value_low = Low32Bits(value);
6435 if (instruction->GetResultType() == Primitive::kPrimInt) {
6436 Register first_reg = first.AsRegister<Register>();
6437 Register out_reg = out.AsRegister<Register>();
6438 if (instruction->IsAnd()) {
6439 GenerateAndConst(out_reg, first_reg, value_low);
6440 } else if (instruction->IsOr()) {
6441 GenerateOrrConst(out_reg, first_reg, value_low);
6442 } else {
6443 DCHECK(instruction->IsXor());
6444 GenerateEorConst(out_reg, first_reg, value_low);
6445 }
6446 } else {
6447 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6448 uint32_t value_high = High32Bits(value);
6449 Register first_low = first.AsRegisterPairLow<Register>();
6450 Register first_high = first.AsRegisterPairHigh<Register>();
6451 Register out_low = out.AsRegisterPairLow<Register>();
6452 Register out_high = out.AsRegisterPairHigh<Register>();
6453 if (instruction->IsAnd()) {
6454 GenerateAndConst(out_low, first_low, value_low);
6455 GenerateAndConst(out_high, first_high, value_high);
6456 } else if (instruction->IsOr()) {
6457 GenerateOrrConst(out_low, first_low, value_low);
6458 GenerateOrrConst(out_high, first_high, value_high);
6459 } else {
6460 DCHECK(instruction->IsXor());
6461 GenerateEorConst(out_low, first_low, value_low);
6462 GenerateEorConst(out_high, first_high, value_high);
6463 }
6464 }
6465 return;
6466 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006467
6468 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006469 Register first_reg = first.AsRegister<Register>();
6470 ShifterOperand second_reg(second.AsRegister<Register>());
6471 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006472 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006473 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006474 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006475 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006476 } else {
6477 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006478 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006479 }
6480 } else {
6481 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006482 Register first_low = first.AsRegisterPairLow<Register>();
6483 Register first_high = first.AsRegisterPairHigh<Register>();
6484 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6485 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6486 Register out_low = out.AsRegisterPairLow<Register>();
6487 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006488 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006489 __ and_(out_low, first_low, second_low);
6490 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006491 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006492 __ orr(out_low, first_low, second_low);
6493 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006494 } else {
6495 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006496 __ eor(out_low, first_low, second_low);
6497 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006498 }
6499 }
6500}
6501
Roland Levillainc9285912015-12-18 10:38:42 +00006502void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6503 Location out,
6504 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006505 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006506 Register out_reg = out.AsRegister<Register>();
6507 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006508 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006509 if (kUseBakerReadBarrier) {
6510 // Load with fast path based Baker's read barrier.
6511 // /* HeapReference<Object> */ out = *(out + offset)
6512 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006513 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006514 } else {
6515 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006516 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006517 // in the following move operation, as we will need it for the
6518 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006519 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006520 // /* HeapReference<Object> */ out = *(out + offset)
6521 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006522 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006523 }
6524 } else {
6525 // Plain load with no read barrier.
6526 // /* HeapReference<Object> */ out = *(out + offset)
6527 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6528 __ MaybeUnpoisonHeapReference(out_reg);
6529 }
6530}
6531
6532void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6533 Location out,
6534 Location obj,
6535 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006536 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006537 Register out_reg = out.AsRegister<Register>();
6538 Register obj_reg = obj.AsRegister<Register>();
6539 if (kEmitCompilerReadBarrier) {
6540 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006541 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006542 // Load with fast path based Baker's read barrier.
6543 // /* HeapReference<Object> */ out = *(obj + offset)
6544 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006545 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006546 } else {
6547 // Load with slow path based read barrier.
6548 // /* HeapReference<Object> */ out = *(obj + offset)
6549 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6550 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6551 }
6552 } else {
6553 // Plain load with no read barrier.
6554 // /* HeapReference<Object> */ out = *(obj + offset)
6555 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6556 __ MaybeUnpoisonHeapReference(out_reg);
6557 }
6558}
6559
6560void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6561 Location root,
6562 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006563 uint32_t offset,
6564 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006565 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006566 if (requires_read_barrier) {
6567 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006568 if (kUseBakerReadBarrier) {
6569 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6570 // Baker's read barrier are used:
6571 //
6572 // root = obj.field;
6573 // if (Thread::Current()->GetIsGcMarking()) {
6574 // root = ReadBarrier::Mark(root)
6575 // }
6576
6577 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6578 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6579 static_assert(
6580 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6581 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6582 "have different sizes.");
6583 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6584 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6585 "have different sizes.");
6586
Vladimir Marko953437b2016-08-24 08:30:46 +00006587 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006588 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006589 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006590 codegen_->AddSlowPath(slow_path);
6591
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006592 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006593 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006594 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006595 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6596 __ Bind(slow_path->GetExitLabel());
6597 } else {
6598 // GC root loaded through a slow path for read barriers other
6599 // than Baker's.
6600 // /* GcRoot<mirror::Object>* */ root = obj + offset
6601 __ AddConstant(root_reg, obj, offset);
6602 // /* mirror::Object* */ root = root->Read()
6603 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6604 }
6605 } else {
6606 // Plain GC root load with no read barrier.
6607 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6608 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6609 // Note that GC roots are not affected by heap poisoning, thus we
6610 // do not have to unpoison `root_reg` here.
6611 }
6612}
6613
6614void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6615 Location ref,
6616 Register obj,
6617 uint32_t offset,
6618 Location temp,
6619 bool needs_null_check) {
6620 DCHECK(kEmitCompilerReadBarrier);
6621 DCHECK(kUseBakerReadBarrier);
6622
6623 // /* HeapReference<Object> */ ref = *(obj + offset)
6624 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006625 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006626 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006627 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006628}
6629
6630void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6631 Location ref,
6632 Register obj,
6633 uint32_t data_offset,
6634 Location index,
6635 Location temp,
6636 bool needs_null_check) {
6637 DCHECK(kEmitCompilerReadBarrier);
6638 DCHECK(kUseBakerReadBarrier);
6639
Roland Levillainbfea3352016-06-23 13:48:47 +01006640 static_assert(
6641 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6642 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006643 // /* HeapReference<Object> */ ref =
6644 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006645 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006646 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006647 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006648}
6649
6650void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6651 Location ref,
6652 Register obj,
6653 uint32_t offset,
6654 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006655 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006656 Location temp,
6657 bool needs_null_check) {
6658 DCHECK(kEmitCompilerReadBarrier);
6659 DCHECK(kUseBakerReadBarrier);
6660
6661 // In slow path based read barriers, the read barrier call is
6662 // inserted after the original load. However, in fast path based
6663 // Baker's read barriers, we need to perform the load of
6664 // mirror::Object::monitor_ *before* the original reference load.
6665 // This load-load ordering is required by the read barrier.
6666 // The fast path/slow path (for Baker's algorithm) should look like:
6667 //
6668 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6669 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6670 // HeapReference<Object> ref = *src; // Original reference load.
6671 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6672 // if (is_gray) {
6673 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6674 // }
6675 //
6676 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006677 // slightly more complex as it performs additional checks that we do
6678 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006679
6680 Register ref_reg = ref.AsRegister<Register>();
6681 Register temp_reg = temp.AsRegister<Register>();
6682 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6683
6684 // /* int32_t */ monitor = obj->monitor_
6685 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6686 if (needs_null_check) {
6687 MaybeRecordImplicitNullCheck(instruction);
6688 }
6689 // /* LockWord */ lock_word = LockWord(monitor)
6690 static_assert(sizeof(LockWord) == sizeof(int32_t),
6691 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006692
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006693 // Introduce a dependency on the lock_word including the rb_state,
6694 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006695 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006696 // `obj` is unchanged by this operation, but its value now depends
6697 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006698 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006699
6700 // The actual reference load.
6701 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006702 // Load types involving an "index": ArrayGet and
6703 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6704 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006705 if (index.IsConstant()) {
6706 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006707 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006708 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6709 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006710 // Handle the special case of the
6711 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6712 // a register pair as index ("long offset"), of which only the low
6713 // part contains data.
6714 Register index_reg = index.IsRegisterPair()
6715 ? index.AsRegisterPairLow<Register>()
6716 : index.AsRegister<Register>();
6717 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006718 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6719 }
6720 } else {
6721 // /* HeapReference<Object> */ ref = *(obj + offset)
6722 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6723 }
6724
6725 // Object* ref = ref_addr->AsMirrorPtr()
6726 __ MaybeUnpoisonHeapReference(ref_reg);
6727
Vladimir Marko953437b2016-08-24 08:30:46 +00006728 // Slow path marking the object `ref` when it is gray.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006729 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006730 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006731 AddSlowPath(slow_path);
6732
6733 // if (rb_state == ReadBarrier::gray_ptr_)
6734 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006735 // Given the numeric representation, it's enough to check the low bit of the
6736 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6737 // which can be a 16-bit instruction unlike the TST immediate.
6738 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6739 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6740 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6741 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6742 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006743 __ Bind(slow_path->GetExitLabel());
6744}
6745
6746void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6747 Location out,
6748 Location ref,
6749 Location obj,
6750 uint32_t offset,
6751 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006752 DCHECK(kEmitCompilerReadBarrier);
6753
Roland Levillainc9285912015-12-18 10:38:42 +00006754 // Insert a slow path based read barrier *after* the reference load.
6755 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006756 // If heap poisoning is enabled, the unpoisoning of the loaded
6757 // reference will be carried out by the runtime within the slow
6758 // path.
6759 //
6760 // Note that `ref` currently does not get unpoisoned (when heap
6761 // poisoning is enabled), which is alright as the `ref` argument is
6762 // not used by the artReadBarrierSlow entry point.
6763 //
6764 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006765 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006766 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6767 AddSlowPath(slow_path);
6768
Roland Levillain3b359c72015-11-17 19:35:12 +00006769 __ b(slow_path->GetEntryLabel());
6770 __ Bind(slow_path->GetExitLabel());
6771}
6772
Roland Levillainc9285912015-12-18 10:38:42 +00006773void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6774 Location out,
6775 Location ref,
6776 Location obj,
6777 uint32_t offset,
6778 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006779 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006780 // Baker's read barriers shall be handled by the fast path
6781 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6782 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006783 // If heap poisoning is enabled, unpoisoning will be taken care of
6784 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006785 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006786 } else if (kPoisonHeapReferences) {
6787 __ UnpoisonHeapReference(out.AsRegister<Register>());
6788 }
6789}
6790
Roland Levillainc9285912015-12-18 10:38:42 +00006791void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6792 Location out,
6793 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006794 DCHECK(kEmitCompilerReadBarrier);
6795
Roland Levillainc9285912015-12-18 10:38:42 +00006796 // Insert a slow path based read barrier *after* the GC root load.
6797 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006798 // Note that GC roots are not affected by heap poisoning, so we do
6799 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006800 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006801 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6802 AddSlowPath(slow_path);
6803
Roland Levillain3b359c72015-11-17 19:35:12 +00006804 __ b(slow_path->GetEntryLabel());
6805 __ Bind(slow_path->GetExitLabel());
6806}
6807
Vladimir Markodc151b22015-10-15 18:02:30 +01006808HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6809 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006810 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006811 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6812 // We disable pc-relative load when there is an irreducible loop, as the optimization
6813 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6815 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006816 if (GetGraph()->HasIrreducibleLoops() &&
6817 (dispatch_info.method_load_kind ==
6818 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6819 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6820 }
6821
6822 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006823 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006824 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006825 // Calls across dex files are more likely to exceed the available BL range,
6826 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6827 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6828 (desired_dispatch_info.method_load_kind ==
6829 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6830 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6831 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6832 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006833 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006834 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006835 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006836 0u
6837 };
6838 }
6839 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006840 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006841}
6842
Vladimir Markob4536b72015-11-24 13:45:23 +00006843Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6844 Register temp) {
6845 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6846 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6847 if (!invoke->GetLocations()->Intrinsified()) {
6848 return location.AsRegister<Register>();
6849 }
6850 // For intrinsics we allow any location, so it may be on the stack.
6851 if (!location.IsRegister()) {
6852 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6853 return temp;
6854 }
6855 // For register locations, check if the register was saved. If so, get it from the stack.
6856 // Note: There is a chance that the register was saved but not overwritten, so we could
6857 // save one load. However, since this is just an intrinsic slow path we prefer this
6858 // simple and more robust approach rather that trying to determine if that's the case.
6859 SlowPathCode* slow_path = GetCurrentSlowPath();
6860 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6861 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6862 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6863 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6864 return temp;
6865 }
6866 return location.AsRegister<Register>();
6867}
6868
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006869void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006870 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006871 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006872 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6873 // LR = code address from literal pool with link-time patch.
6874 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006875 break;
6876 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6877 // LR = invoke->GetDirectCodePtr();
6878 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006879 break;
6880 default:
6881 break;
6882 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006883
Vladimir Marko58155012015-08-19 12:49:41 +00006884 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6885 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006886 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
6887 uint32_t offset =
6888 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00006889 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006890 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00006891 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006892 }
Vladimir Marko58155012015-08-19 12:49:41 +00006893 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006894 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006895 break;
6896 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6897 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6898 break;
6899 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6900 __ LoadLiteral(temp.AsRegister<Register>(),
6901 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6902 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006903 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6904 HArmDexCacheArraysBase* base =
6905 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6906 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6907 temp.AsRegister<Register>());
6908 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6909 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6910 break;
6911 }
Vladimir Marko58155012015-08-19 12:49:41 +00006912 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006913 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006914 Register method_reg;
6915 Register reg = temp.AsRegister<Register>();
6916 if (current_method.IsRegister()) {
6917 method_reg = current_method.AsRegister<Register>();
6918 } else {
6919 DCHECK(invoke->GetLocations()->Intrinsified());
6920 DCHECK(!current_method.IsValid());
6921 method_reg = reg;
6922 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6923 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006924 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6925 __ LoadFromOffset(kLoadWord,
6926 reg,
6927 method_reg,
6928 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006929 // temp = temp[index_in_cache];
6930 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6931 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006932 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6933 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006934 }
Vladimir Marko58155012015-08-19 12:49:41 +00006935 }
6936
6937 switch (invoke->GetCodePtrLocation()) {
6938 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6939 __ bl(GetFrameEntryLabel());
6940 break;
6941 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006942 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
6943 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006944 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006945 // Arbitrarily branch to the BL itself, override at link time.
6946 __ bl(&relative_call_patches_.back().label);
6947 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006948 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6949 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6950 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006951 // LR()
6952 __ blx(LR);
6953 break;
6954 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6955 // LR = callee_method->entry_point_from_quick_compiled_code_
6956 __ LoadFromOffset(
6957 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006958 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006959 // LR()
6960 __ blx(LR);
6961 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006962 }
6963
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006964 DCHECK(!IsLeafMethod());
6965}
6966
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006967void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6968 Register temp = temp_location.AsRegister<Register>();
6969 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6970 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006971
6972 // Use the calling convention instead of the location of the receiver, as
6973 // intrinsics may have put the receiver in a different register. In the intrinsics
6974 // slow path, the arguments have been moved to the right place, so here we are
6975 // guaranteed that the receiver is the first register of the calling convention.
6976 InvokeDexCallingConvention calling_convention;
6977 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006979 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006980 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006981 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006982 // Instead of simply (possibly) unpoisoning `temp` here, we should
6983 // emit a read barrier for the previous class reference load.
6984 // However this is not required in practice, as this is an
6985 // intermediate/temporary reference and because the current
6986 // concurrent copying collector keeps the from-space memory
6987 // intact/accessible until the end of the marking phase (the
6988 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006989 __ MaybeUnpoisonHeapReference(temp);
6990 // temp = temp->GetMethodAt(method_offset);
6991 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006992 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006993 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6994 // LR = temp->GetEntryPoint();
6995 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6996 // LR();
6997 __ blx(LR);
6998}
6999
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007000CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7001 const DexFile& dex_file, uint32_t string_index) {
7002 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7003}
7004
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007005CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
7006 const DexFile& dex_file, uint32_t type_index) {
7007 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7008}
7009
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007010CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7011 const DexFile& dex_file, uint32_t element_offset) {
7012 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7013}
7014
7015CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7016 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7017 patches->emplace_back(dex_file, offset_or_index);
7018 return &patches->back();
7019}
7020
7021Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
7022 uint32_t string_index) {
7023 return boot_image_string_patches_.GetOrCreate(
7024 StringReference(&dex_file, string_index),
7025 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7026}
7027
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007028Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
7029 uint32_t type_index) {
7030 return boot_image_type_patches_.GetOrCreate(
7031 TypeReference(&dex_file, type_index),
7032 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7033}
7034
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007035Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7036 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7037 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7038 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7039}
7040
7041Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7042 return DeduplicateUint32Literal(address, &uint32_literals_);
7043}
7044
Vladimir Markoaad75c62016-10-03 08:46:48 +00007045template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7046inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7047 const ArenaDeque<PcRelativePatchInfo>& infos,
7048 ArenaVector<LinkerPatch>* linker_patches) {
7049 for (const PcRelativePatchInfo& info : infos) {
7050 const DexFile& dex_file = info.target_dex_file;
7051 size_t offset_or_index = info.offset_or_index;
7052 DCHECK(info.add_pc_label.IsBound());
7053 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7054 // Add MOVW patch.
7055 DCHECK(info.movw_label.IsBound());
7056 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7057 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7058 // Add MOVT patch.
7059 DCHECK(info.movt_label.IsBound());
7060 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7061 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7062 }
7063}
7064
Vladimir Marko58155012015-08-19 12:49:41 +00007065void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7066 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007067 size_t size =
7068 method_patches_.size() +
7069 call_patches_.size() +
7070 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007071 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007072 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007073 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007074 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007075 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007076 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007077 linker_patches->reserve(size);
7078 for (const auto& entry : method_patches_) {
7079 const MethodReference& target_method = entry.first;
7080 Literal* literal = entry.second;
7081 DCHECK(literal->GetLabel()->IsBound());
7082 uint32_t literal_offset = literal->GetLabel()->Position();
7083 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7084 target_method.dex_file,
7085 target_method.dex_method_index));
7086 }
7087 for (const auto& entry : call_patches_) {
7088 const MethodReference& target_method = entry.first;
7089 Literal* literal = entry.second;
7090 DCHECK(literal->GetLabel()->IsBound());
7091 uint32_t literal_offset = literal->GetLabel()->Position();
7092 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7093 target_method.dex_file,
7094 target_method.dex_method_index));
7095 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007096 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007097 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007098 linker_patches->push_back(
7099 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007100 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007101 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7102 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007103 for (const auto& entry : boot_image_string_patches_) {
7104 const StringReference& target_string = entry.first;
7105 Literal* literal = entry.second;
7106 DCHECK(literal->GetLabel()->IsBound());
7107 uint32_t literal_offset = literal->GetLabel()->Position();
7108 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7109 target_string.dex_file,
7110 target_string.string_index));
7111 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007112 if (!GetCompilerOptions().IsBootImage()) {
7113 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7114 linker_patches);
7115 } else {
7116 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7117 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007118 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007119 for (const auto& entry : boot_image_type_patches_) {
7120 const TypeReference& target_type = entry.first;
7121 Literal* literal = entry.second;
7122 DCHECK(literal->GetLabel()->IsBound());
7123 uint32_t literal_offset = literal->GetLabel()->Position();
7124 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7125 target_type.dex_file,
7126 target_type.type_index));
7127 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007128 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7129 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007130 for (const auto& entry : boot_image_address_patches_) {
7131 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7132 Literal* literal = entry.second;
7133 DCHECK(literal->GetLabel()->IsBound());
7134 uint32_t literal_offset = literal->GetLabel()->Position();
7135 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7136 }
7137}
7138
7139Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7140 return map->GetOrCreate(
7141 value,
7142 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007143}
7144
7145Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7146 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007147 return map->GetOrCreate(
7148 target_method,
7149 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007150}
7151
7152Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7153 return DeduplicateMethodLiteral(target_method, &method_patches_);
7154}
7155
7156Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7157 return DeduplicateMethodLiteral(target_method, &call_patches_);
7158}
7159
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007160void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7161 LocationSummary* locations =
7162 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7163 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7164 Location::RequiresRegister());
7165 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7166 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7168}
7169
7170void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7171 LocationSummary* locations = instr->GetLocations();
7172 Register res = locations->Out().AsRegister<Register>();
7173 Register accumulator =
7174 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7175 Register mul_left =
7176 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7177 Register mul_right =
7178 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7179
7180 if (instr->GetOpKind() == HInstruction::kAdd) {
7181 __ mla(res, mul_left, mul_right, accumulator);
7182 } else {
7183 __ mls(res, mul_left, mul_right, accumulator);
7184 }
7185}
7186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007187void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007188 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007189 LOG(FATAL) << "Unreachable";
7190}
7191
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007192void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007193 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007194 LOG(FATAL) << "Unreachable";
7195}
7196
Mark Mendellfe57faa2015-09-18 09:26:15 -04007197// Simple implementation of packed switch - generate cascaded compare/jumps.
7198void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7199 LocationSummary* locations =
7200 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7201 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007202 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007203 codegen_->GetAssembler()->IsThumb()) {
7204 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7205 if (switch_instr->GetStartValue() != 0) {
7206 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7207 }
7208 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007209}
7210
7211void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7212 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007213 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007214 LocationSummary* locations = switch_instr->GetLocations();
7215 Register value_reg = locations->InAt(0).AsRegister<Register>();
7216 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7217
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007218 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007219 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007220 Register temp_reg = IP;
7221 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7222 // the immediate, because IP is used as the destination register. For the other
7223 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7224 // and they can be encoded in the instruction without making use of IP register.
7225 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7226
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007227 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007228 // Jump to successors[0] if value == lower_bound.
7229 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7230 int32_t last_index = 0;
7231 for (; num_entries - last_index > 2; last_index += 2) {
7232 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7233 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7234 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7235 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7236 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7237 }
7238 if (num_entries - last_index == 2) {
7239 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007240 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007241 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007242 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007243
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007244 // And the default for any other value.
7245 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7246 __ b(codegen_->GetLabelOf(default_block));
7247 }
7248 } else {
7249 // Create a table lookup.
7250 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7251
7252 // Materialize a pointer to the switch table
7253 std::vector<Label*> labels(num_entries);
7254 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7255 for (uint32_t i = 0; i < num_entries; i++) {
7256 labels[i] = codegen_->GetLabelOf(successors[i]);
7257 }
7258 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7259
7260 // Remove the bias.
7261 Register key_reg;
7262 if (lower_bound != 0) {
7263 key_reg = locations->GetTemp(1).AsRegister<Register>();
7264 __ AddConstant(key_reg, value_reg, -lower_bound);
7265 } else {
7266 key_reg = value_reg;
7267 }
7268
7269 // Check whether the value is in the table, jump to default block if not.
7270 __ CmpConstant(key_reg, num_entries - 1);
7271 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7272
7273 // Load the displacement from the table.
7274 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7275
7276 // Dispatch is a direct add to the PC (for Thumb2).
7277 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007278 }
7279}
7280
Vladimir Markob4536b72015-11-24 13:45:23 +00007281void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7282 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7283 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007284}
7285
7286void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7287 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007288 CodeGeneratorARM::PcRelativePatchInfo* labels =
7289 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007290 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007291 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007292 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007293 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007294 __ BindTrackedLabel(&labels->add_pc_label);
7295 __ add(base_reg, base_reg, ShifterOperand(PC));
7296}
7297
Andreas Gampe85b62f22015-09-09 13:15:38 -07007298void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7299 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007300 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007301 return;
7302 }
7303
7304 DCHECK_NE(type, Primitive::kPrimVoid);
7305
7306 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7307 if (return_loc.Equals(trg)) {
7308 return;
7309 }
7310
7311 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7312 // with the last branch.
7313 if (type == Primitive::kPrimLong) {
7314 HParallelMove parallel_move(GetGraph()->GetArena());
7315 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7316 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7317 GetMoveResolver()->EmitNativeCode(&parallel_move);
7318 } else if (type == Primitive::kPrimDouble) {
7319 HParallelMove parallel_move(GetGraph()->GetArena());
7320 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7321 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7322 GetMoveResolver()->EmitNativeCode(&parallel_move);
7323 } else {
7324 // Let the parallel move resolver take care of all of this.
7325 HParallelMove parallel_move(GetGraph()->GetArena());
7326 parallel_move.AddMove(return_loc, trg, type, nullptr);
7327 GetMoveResolver()->EmitNativeCode(&parallel_move);
7328 }
7329}
7330
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007331void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7332 LocationSummary* locations =
7333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7334 locations->SetInAt(0, Location::RequiresRegister());
7335 locations->SetOut(Location::RequiresRegister());
7336}
7337
7338void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7339 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007340 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007341 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007342 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007343 __ LoadFromOffset(kLoadWord,
7344 locations->Out().AsRegister<Register>(),
7345 locations->InAt(0).AsRegister<Register>(),
7346 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007347 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007348 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007349 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007350 __ LoadFromOffset(kLoadWord,
7351 locations->Out().AsRegister<Register>(),
7352 locations->InAt(0).AsRegister<Register>(),
7353 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7354 __ LoadFromOffset(kLoadWord,
7355 locations->Out().AsRegister<Register>(),
7356 locations->Out().AsRegister<Register>(),
7357 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007358 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007359}
7360
Roland Levillain4d027112015-07-01 15:41:14 +01007361#undef __
7362#undef QUICK_ENTRY_POINT
7363
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007364} // namespace arm
7365} // namespace art