blob: a052873afdeac503ab604a0feaad6933d51b1613 [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
Artem Serovf4d6aee2016-07-11 10:41:45 +0100425class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000426 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000427 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100428 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000429
Alexandre Rames67555f72014-11-18 10:55:16 +0000430 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000431 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100432 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
433 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000434 DCHECK(instruction_->IsCheckCast()
435 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000436
437 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
438 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000440 if (!is_fatal_) {
441 SaveLiveRegisters(codegen, locations);
442 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000443
444 // We're moving two locations to locations that could overlap, so we need a parallel
445 // move resolver.
446 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000447 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000449 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100450 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100452 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
453 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000454
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000455 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100456 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100457 instruction_,
458 instruction_->GetDexPc(),
459 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000460 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700461 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000462 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
463 } else {
464 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100465 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000466 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000467 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 if (!is_fatal_) {
470 RestoreLiveRegisters(codegen, locations);
471 __ b(GetExitLabel());
472 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
476
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 bool IsFatal() const OVERRIDE { return is_fatal_; }
478
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000479 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000481
482 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
483};
484
Artem Serovf4d6aee2016-07-11 10:41:45 +0100485class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700486 public:
Aart Bik42249c32016-01-07 15:33:50 -0800487 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100488 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800491 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100493 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000494 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495 }
496
Alexandre Rames9931f312015-06-19 14:47:01 +0100497 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
498
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
501};
502
Artem Serovf4d6aee2016-07-11 10:41:45 +0100503class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100504 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100505 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506
507 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
508 LocationSummary* locations = instruction_->GetLocations();
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 InvokeRuntimeCallingConvention calling_convention;
513 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
514 parallel_move.AddMove(
515 locations->InAt(0),
516 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
517 Primitive::kPrimNot,
518 nullptr);
519 parallel_move.AddMove(
520 locations->InAt(1),
521 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
522 Primitive::kPrimInt,
523 nullptr);
524 parallel_move.AddMove(
525 locations->InAt(2),
526 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
527 Primitive::kPrimNot,
528 nullptr);
529 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
530
531 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100532 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000533 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100534 RestoreLiveRegisters(codegen, locations);
535 __ b(GetExitLabel());
536 }
537
538 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
539
540 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100541 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
542};
543
Roland Levillainc9285912015-12-18 10:38:42 +0000544// Slow path marking an object during a read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100545class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000546 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100547 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100548 : SlowPathCodeARM(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000549 DCHECK(kEmitCompilerReadBarrier);
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
553
554 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
555 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100556 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000557 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100558 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000559 DCHECK(instruction_->IsInstanceFieldGet() ||
560 instruction_->IsStaticFieldGet() ||
561 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100562 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000563 instruction_->IsLoadClass() ||
564 instruction_->IsLoadString() ||
565 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100566 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100567 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
568 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000569 << "Unexpected instruction in read barrier marking slow path: "
570 << instruction_->DebugName();
571
572 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100573 // No need to save live registers; it's taken care of by the
574 // entrypoint. Also, there is no need to update the stack mask,
575 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000576 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100577 DCHECK_NE(reg, SP);
578 DCHECK_NE(reg, LR);
579 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100580 // IP is used internally by the ReadBarrierMarkRegX entry point
581 // as a temporary, it cannot be the entry point's input/output.
582 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100583 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
584 // "Compact" slow path, saving two moves.
585 //
586 // Instead of using the standard runtime calling convention (input
587 // and output in R0):
588 //
589 // R0 <- obj
590 // R0 <- ReadBarrierMark(R0)
591 // obj <- R0
592 //
593 // we just use rX (the register holding `obj`) as input and output
594 // of a dedicated entrypoint:
595 //
596 // rX <- ReadBarrierMarkRegX(rX)
597 //
598 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700599 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100600 // This runtime call does not require a stack map.
601 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000602 __ b(GetExitLabel());
603 }
604
605 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000606 const Location obj_;
607
608 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
609};
610
Roland Levillain3b359c72015-11-17 19:35:12 +0000611// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100612class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000613 public:
614 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
615 Location out,
616 Location ref,
617 Location obj,
618 uint32_t offset,
619 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100620 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000621 out_(out),
622 ref_(ref),
623 obj_(obj),
624 offset_(offset),
625 index_(index) {
626 DCHECK(kEmitCompilerReadBarrier);
627 // If `obj` is equal to `out` or `ref`, it means the initial object
628 // has been overwritten by (or after) the heap object reference load
629 // to be instrumented, e.g.:
630 //
631 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000632 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000633 //
634 // In that case, we have lost the information about the original
635 // object, and the emitted read barrier cannot work properly.
636 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
637 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
638 }
639
640 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
641 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
642 LocationSummary* locations = instruction_->GetLocations();
643 Register reg_out = out_.AsRegister<Register>();
644 DCHECK(locations->CanCall());
645 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100646 DCHECK(instruction_->IsInstanceFieldGet() ||
647 instruction_->IsStaticFieldGet() ||
648 instruction_->IsArrayGet() ||
649 instruction_->IsInstanceOf() ||
650 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100651 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000652 << "Unexpected instruction in read barrier for heap reference slow path: "
653 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000654
655 __ Bind(GetEntryLabel());
656 SaveLiveRegisters(codegen, locations);
657
658 // We may have to change the index's value, but as `index_` is a
659 // constant member (like other "inputs" of this slow path),
660 // introduce a copy of it, `index`.
661 Location index = index_;
662 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100663 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000664 if (instruction_->IsArrayGet()) {
665 // Compute the actual memory offset and store it in `index`.
666 Register index_reg = index_.AsRegister<Register>();
667 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
668 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
669 // We are about to change the value of `index_reg` (see the
670 // calls to art::arm::Thumb2Assembler::Lsl and
671 // art::arm::Thumb2Assembler::AddConstant below), but it has
672 // not been saved by the previous call to
673 // art::SlowPathCode::SaveLiveRegisters, as it is a
674 // callee-save register --
675 // art::SlowPathCode::SaveLiveRegisters does not consider
676 // callee-save registers, as it has been designed with the
677 // assumption that callee-save registers are supposed to be
678 // handled by the called function. So, as a callee-save
679 // register, `index_reg` _would_ eventually be saved onto
680 // the stack, but it would be too late: we would have
681 // changed its value earlier. Therefore, we manually save
682 // it here into another freely available register,
683 // `free_reg`, chosen of course among the caller-save
684 // registers (as a callee-save `free_reg` register would
685 // exhibit the same problem).
686 //
687 // Note we could have requested a temporary register from
688 // the register allocator instead; but we prefer not to, as
689 // this is a slow path, and we know we can find a
690 // caller-save register that is available.
691 Register free_reg = FindAvailableCallerSaveRegister(codegen);
692 __ Mov(free_reg, index_reg);
693 index_reg = free_reg;
694 index = Location::RegisterLocation(index_reg);
695 } else {
696 // The initial register stored in `index_` has already been
697 // saved in the call to art::SlowPathCode::SaveLiveRegisters
698 // (as it is not a callee-save register), so we can freely
699 // use it.
700 }
701 // Shifting the index value contained in `index_reg` by the scale
702 // factor (2) cannot overflow in practice, as the runtime is
703 // unable to allocate object arrays with a size larger than
704 // 2^26 - 1 (that is, 2^28 - 4 bytes).
705 __ Lsl(index_reg, index_reg, TIMES_4);
706 static_assert(
707 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
708 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
709 __ AddConstant(index_reg, index_reg, offset_);
710 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100711 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
712 // intrinsics, `index_` is not shifted by a scale factor of 2
713 // (as in the case of ArrayGet), as it is actually an offset
714 // to an object field within an object.
715 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000716 DCHECK(instruction_->GetLocations()->Intrinsified());
717 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
718 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
719 << instruction_->AsInvoke()->GetIntrinsic();
720 DCHECK_EQ(offset_, 0U);
721 DCHECK(index_.IsRegisterPair());
722 // UnsafeGet's offset location is a register pair, the low
723 // part contains the correct offset.
724 index = index_.ToLow();
725 }
726 }
727
728 // We're moving two or three locations to locations that could
729 // overlap, so we need a parallel move resolver.
730 InvokeRuntimeCallingConvention calling_convention;
731 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
732 parallel_move.AddMove(ref_,
733 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
734 Primitive::kPrimNot,
735 nullptr);
736 parallel_move.AddMove(obj_,
737 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
738 Primitive::kPrimNot,
739 nullptr);
740 if (index.IsValid()) {
741 parallel_move.AddMove(index,
742 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
743 Primitive::kPrimInt,
744 nullptr);
745 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
746 } else {
747 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
748 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
749 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100750 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000751 CheckEntrypointTypes<
752 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
753 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
754
755 RestoreLiveRegisters(codegen, locations);
756 __ b(GetExitLabel());
757 }
758
759 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
760
761 private:
762 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
763 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
764 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
765 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
766 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
767 return static_cast<Register>(i);
768 }
769 }
770 // We shall never fail to find a free caller-save register, as
771 // there are more than two core caller-save registers on ARM
772 // (meaning it is possible to find one which is different from
773 // `ref` and `obj`).
774 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
775 LOG(FATAL) << "Could not find a free caller-save register";
776 UNREACHABLE();
777 }
778
Roland Levillain3b359c72015-11-17 19:35:12 +0000779 const Location out_;
780 const Location ref_;
781 const Location obj_;
782 const uint32_t offset_;
783 // An additional location containing an index to an array.
784 // Only used for HArrayGet and the UnsafeGetObject &
785 // UnsafeGetObjectVolatile intrinsics.
786 const Location index_;
787
788 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
789};
790
791// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100792class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000793 public:
794 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100795 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000796 DCHECK(kEmitCompilerReadBarrier);
797 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000798
799 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
800 LocationSummary* locations = instruction_->GetLocations();
801 Register reg_out = out_.AsRegister<Register>();
802 DCHECK(locations->CanCall());
803 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000804 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
805 << "Unexpected instruction in read barrier for GC root slow path: "
806 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000807
808 __ Bind(GetEntryLabel());
809 SaveLiveRegisters(codegen, locations);
810
811 InvokeRuntimeCallingConvention calling_convention;
812 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
813 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100814 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000815 instruction_,
816 instruction_->GetDexPc(),
817 this);
818 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
819 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
820
821 RestoreLiveRegisters(codegen, locations);
822 __ b(GetExitLabel());
823 }
824
825 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
826
827 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000828 const Location out_;
829 const Location root_;
830
831 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
832};
833
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000834#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100835// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
836#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700837
Aart Bike9f37602015-10-09 11:15:55 -0700838inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700839 switch (cond) {
840 case kCondEQ: return EQ;
841 case kCondNE: return NE;
842 case kCondLT: return LT;
843 case kCondLE: return LE;
844 case kCondGT: return GT;
845 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700846 case kCondB: return LO;
847 case kCondBE: return LS;
848 case kCondA: return HI;
849 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700850 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100851 LOG(FATAL) << "Unreachable";
852 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700853}
854
Aart Bike9f37602015-10-09 11:15:55 -0700855// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100856inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700857 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100858 case kCondEQ: return EQ;
859 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700860 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100861 case kCondLT: return LO;
862 case kCondLE: return LS;
863 case kCondGT: return HI;
864 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700865 // Unsigned remain unchanged.
866 case kCondB: return LO;
867 case kCondBE: return LS;
868 case kCondA: return HI;
869 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700870 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100871 LOG(FATAL) << "Unreachable";
872 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700873}
874
Vladimir Markod6e069b2016-01-18 11:11:01 +0000875inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
876 // The ARM condition codes can express all the necessary branches, see the
877 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
878 // There is no dex instruction or HIR that would need the missing conditions
879 // "equal or unordered" or "not equal".
880 switch (cond) {
881 case kCondEQ: return EQ;
882 case kCondNE: return NE /* unordered */;
883 case kCondLT: return gt_bias ? CC : LT /* unordered */;
884 case kCondLE: return gt_bias ? LS : LE /* unordered */;
885 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
886 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
887 default:
888 LOG(FATAL) << "UNREACHABLE";
889 UNREACHABLE();
890 }
891}
892
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100893void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100894 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100895}
896
897void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100898 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100899}
900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100901size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
902 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
903 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100904}
905
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100906size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
907 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
908 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100909}
910
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000911size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
912 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
913 return kArmWordSize;
914}
915
916size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
917 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
918 return kArmWordSize;
919}
920
Calin Juravle34166012014-12-19 17:22:29 +0000921CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000922 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100923 const CompilerOptions& compiler_options,
924 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000925 : CodeGenerator(graph,
926 kNumberOfCoreRegisters,
927 kNumberOfSRegisters,
928 kNumberOfRegisterPairs,
929 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
930 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000931 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
932 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100933 compiler_options,
934 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100935 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100936 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100937 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100938 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100939 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000940 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000941 uint32_literals_(std::less<uint32_t>(),
942 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100943 method_patches_(MethodReferenceComparator(),
944 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 call_patches_(MethodReferenceComparator(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000947 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000948 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
949 boot_image_string_patches_(StringReferenceValueComparator(),
950 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
951 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100952 boot_image_type_patches_(TypeReferenceValueComparator(),
953 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
954 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000955 boot_image_address_patches_(std::less<uint32_t>(),
956 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700957 // Always save the LR register to mimic Quick.
958 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100959}
960
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000961void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
962 // Ensure that we fix up branches and literal loads and emit the literal pool.
963 __ FinalizeCode();
964
965 // Adjust native pc offsets in stack maps.
966 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
967 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
968 uint32_t new_position = __ GetAdjustedPosition(old_position);
969 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
970 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100971 // Adjust pc offsets for the disassembly information.
972 if (disasm_info_ != nullptr) {
973 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
974 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
975 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
976 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
977 it.second.start = __ GetAdjustedPosition(it.second.start);
978 it.second.end = __ GetAdjustedPosition(it.second.end);
979 }
980 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
981 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
982 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
983 }
984 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000985
986 CodeGenerator::Finalize(allocator);
987}
988
David Brazdil58282f42016-01-14 12:45:10 +0000989void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100990 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100991 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100992
993 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100994 blocked_core_registers_[SP] = true;
995 blocked_core_registers_[LR] = true;
996 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100997
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100998 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100999 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001000
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001001 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001002 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001003
David Brazdil58282f42016-01-14 12:45:10 +00001004 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001005 // Stubs do not save callee-save floating point registers. If the graph
1006 // is debuggable, we need to deal with these registers differently. For
1007 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001008 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1009 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1010 }
1011 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001012
1013 UpdateBlockedPairRegisters();
1014}
1015
1016void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
1017 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1018 ArmManagedRegister current =
1019 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1020 if (blocked_core_registers_[current.AsRegisterPairLow()]
1021 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1022 blocked_register_pairs_[i] = true;
1023 }
1024 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025}
1026
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001027InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001028 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001029 assembler_(codegen->GetAssembler()),
1030 codegen_(codegen) {}
1031
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001032void CodeGeneratorARM::ComputeSpillMask() {
1033 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1034 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001035 // There is no easy instruction to restore just the PC on thumb2. We spill and
1036 // restore another arbitrary register.
1037 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001038 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1039 // We use vpush and vpop for saving and restoring floating point registers, which take
1040 // a SRegister and the number of registers to save/restore after that SRegister. We
1041 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1042 // but in the range.
1043 if (fpu_spill_mask_ != 0) {
1044 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1045 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1046 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1047 fpu_spill_mask_ |= (1 << i);
1048 }
1049 }
1050}
1051
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001052static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001053 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054}
1055
1056static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001057 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058}
1059
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001061 bool skip_overflow_check =
1062 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001063 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 __ Bind(&frame_entry_label_);
1065
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001066 if (HasEmptyFrame()) {
1067 return;
1068 }
1069
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001070 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1072 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1073 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001074 }
1075
Andreas Gampe501fd632015-09-10 16:11:06 -07001076 __ PushList(core_spill_mask_);
1077 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1078 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001079 if (fpu_spill_mask_ != 0) {
1080 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1081 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001083 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001084 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001085 int adjust = GetFrameSize() - FrameEntrySpillSize();
1086 __ AddConstant(SP, -adjust);
1087 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001088 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001089}
1090
1091void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001092 if (HasEmptyFrame()) {
1093 __ bx(LR);
1094 return;
1095 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001096 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001097 int adjust = GetFrameSize() - FrameEntrySpillSize();
1098 __ AddConstant(SP, adjust);
1099 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001100 if (fpu_spill_mask_ != 0) {
1101 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1102 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001103 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001104 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001105 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001106 // Pop LR into PC to return.
1107 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1108 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1109 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001110 __ cfi().RestoreState();
1111 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001112}
1113
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001114void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001115 Label* label = GetLabelOf(block);
1116 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001117}
1118
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001119Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001120 switch (type) {
1121 case Primitive::kPrimBoolean:
1122 case Primitive::kPrimByte:
1123 case Primitive::kPrimChar:
1124 case Primitive::kPrimShort:
1125 case Primitive::kPrimInt:
1126 case Primitive::kPrimNot: {
1127 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001128 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001129 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001131 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001132 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001133 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001134 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001135
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001136 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001137 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001138 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001139 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001140 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001141 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001142 if (calling_convention.GetRegisterAt(index) == R1) {
1143 // Skip R1, and use R2_R3 instead.
1144 gp_index_++;
1145 index++;
1146 }
1147 }
1148 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1149 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001150 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001151
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001152 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001153 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001154 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001155 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1156 }
1157 }
1158
1159 case Primitive::kPrimFloat: {
1160 uint32_t stack_index = stack_index_++;
1161 if (float_index_ % 2 == 0) {
1162 float_index_ = std::max(double_index_, float_index_);
1163 }
1164 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1165 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1166 } else {
1167 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1168 }
1169 }
1170
1171 case Primitive::kPrimDouble: {
1172 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1173 uint32_t stack_index = stack_index_;
1174 stack_index_ += 2;
1175 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1176 uint32_t index = double_index_;
1177 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001178 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001179 calling_convention.GetFpuRegisterAt(index),
1180 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001181 DCHECK(ExpectedPairLayout(result));
1182 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001183 } else {
1184 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 }
1186 }
1187
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001188 case Primitive::kPrimVoid:
1189 LOG(FATAL) << "Unexpected parameter type " << type;
1190 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001191 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001192 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001193}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001194
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001195Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001196 switch (type) {
1197 case Primitive::kPrimBoolean:
1198 case Primitive::kPrimByte:
1199 case Primitive::kPrimChar:
1200 case Primitive::kPrimShort:
1201 case Primitive::kPrimInt:
1202 case Primitive::kPrimNot: {
1203 return Location::RegisterLocation(R0);
1204 }
1205
1206 case Primitive::kPrimFloat: {
1207 return Location::FpuRegisterLocation(S0);
1208 }
1209
1210 case Primitive::kPrimLong: {
1211 return Location::RegisterPairLocation(R0, R1);
1212 }
1213
1214 case Primitive::kPrimDouble: {
1215 return Location::FpuRegisterPairLocation(S0, S1);
1216 }
1217
1218 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001219 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001220 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001221
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001222 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001223}
1224
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1226 return Location::RegisterLocation(kMethodRegisterArgument);
1227}
1228
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229void CodeGeneratorARM::Move32(Location destination, Location source) {
1230 if (source.Equals(destination)) {
1231 return;
1232 }
1233 if (destination.IsRegister()) {
1234 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else if (destination.IsFpuRegister()) {
1242 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001250 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001252 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001256 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1258 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 }
1260 }
1261}
1262
1263void CodeGeneratorARM::Move64(Location destination, Location source) {
1264 if (source.Equals(destination)) {
1265 return;
1266 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 if (destination.IsRegisterPair()) {
1268 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001269 EmitParallelMoves(
1270 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1271 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001272 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001274 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1275 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001277 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 } else if (source.IsFpuRegisterPair()) {
1279 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1280 destination.AsRegisterPairHigh<Register>(),
1281 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001282 } else {
1283 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001284 DCHECK(ExpectedPairLayout(destination));
1285 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1286 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001287 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001288 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001289 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001290 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1291 SP,
1292 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 } else if (source.IsRegisterPair()) {
1294 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1295 source.AsRegisterPairLow<Register>(),
1296 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001297 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001298 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001299 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 } else {
1301 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001303 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001304 if (source.AsRegisterPairLow<Register>() == R1) {
1305 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1307 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001310 SP, destination.GetStackIndex());
1311 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001312 } else if (source.IsFpuRegisterPair()) {
1313 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1314 SP,
1315 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001316 } else {
1317 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001318 EmitParallelMoves(
1319 Location::StackSlot(source.GetStackIndex()),
1320 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001321 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001322 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001323 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1324 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 }
1326 }
1327}
1328
Calin Juravle175dc732015-08-25 15:42:32 +01001329void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1330 DCHECK(location.IsRegister());
1331 __ LoadImmediate(location.AsRegister<Register>(), value);
1332}
1333
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001335 HParallelMove move(GetGraph()->GetArena());
1336 move.AddMove(src, dst, dst_type, nullptr);
1337 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338}
1339
1340void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1341 if (location.IsRegister()) {
1342 locations->AddTemp(location);
1343 } else if (location.IsRegisterPair()) {
1344 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1345 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1346 } else {
1347 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1348 }
1349}
1350
Calin Juravle175dc732015-08-25 15:42:32 +01001351void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1352 HInstruction* instruction,
1353 uint32_t dex_pc,
1354 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001355 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001356 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001357 if (EntrypointRequiresStackMap(entrypoint)) {
1358 RecordPcInfo(instruction, dex_pc, slow_path);
1359 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360}
1361
Roland Levillaindec8f632016-07-22 17:10:06 +01001362void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1363 HInstruction* instruction,
1364 SlowPathCode* slow_path) {
1365 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001366 GenerateInvokeRuntime(entry_point_offset);
1367}
1368
1369void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001370 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1371 __ blx(LR);
1372}
1373
David Brazdilfc6a86a2015-06-26 10:33:45 +00001374void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001375 DCHECK(!successor->IsExitBlock());
1376
1377 HBasicBlock* block = got->GetBlock();
1378 HInstruction* previous = got->GetPrevious();
1379
1380 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001381 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001382 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1383 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1384 return;
1385 }
1386
1387 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1388 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1389 }
1390 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001392 }
1393}
1394
David Brazdilfc6a86a2015-06-26 10:33:45 +00001395void LocationsBuilderARM::VisitGoto(HGoto* got) {
1396 got->SetLocations(nullptr);
1397}
1398
1399void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1400 HandleGoto(got, got->GetSuccessor());
1401}
1402
1403void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1404 try_boundary->SetLocations(nullptr);
1405}
1406
1407void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1408 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1409 if (!successor->IsExitBlock()) {
1410 HandleGoto(try_boundary, successor);
1411 }
1412}
1413
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001414void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001415 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001416}
1417
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001418void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001419}
1420
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001421void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1422 Primitive::Type type = instruction->InputAt(0)->GetType();
1423 Location lhs_loc = instruction->GetLocations()->InAt(0);
1424 Location rhs_loc = instruction->GetLocations()->InAt(1);
1425 if (rhs_loc.IsConstant()) {
1426 // 0.0 is the only immediate that can be encoded directly in
1427 // a VCMP instruction.
1428 //
1429 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1430 // specify that in a floating-point comparison, positive zero
1431 // and negative zero are considered equal, so we can use the
1432 // literal 0.0 for both cases here.
1433 //
1434 // Note however that some methods (Float.equal, Float.compare,
1435 // Float.compareTo, Double.equal, Double.compare,
1436 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1437 // StrictMath.min) consider 0.0 to be (strictly) greater than
1438 // -0.0. So if we ever translate calls to these methods into a
1439 // HCompare instruction, we must handle the -0.0 case with
1440 // care here.
1441 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1442 if (type == Primitive::kPrimFloat) {
1443 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1444 } else {
1445 DCHECK_EQ(type, Primitive::kPrimDouble);
1446 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1447 }
1448 } else {
1449 if (type == Primitive::kPrimFloat) {
1450 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1451 } else {
1452 DCHECK_EQ(type, Primitive::kPrimDouble);
1453 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1454 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1455 }
1456 }
1457}
1458
Roland Levillain4fa13f62015-07-06 18:11:54 +01001459void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1460 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001461 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001462 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001463 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001464}
1465
1466void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1467 Label* true_label,
1468 Label* false_label) {
1469 LocationSummary* locations = cond->GetLocations();
1470 Location left = locations->InAt(0);
1471 Location right = locations->InAt(1);
1472 IfCondition if_cond = cond->GetCondition();
1473
1474 Register left_high = left.AsRegisterPairHigh<Register>();
1475 Register left_low = left.AsRegisterPairLow<Register>();
1476 IfCondition true_high_cond = if_cond;
1477 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001478 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001479
1480 // Set the conditions for the test, remembering that == needs to be
1481 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001482 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483 switch (if_cond) {
1484 case kCondEQ:
1485 case kCondNE:
1486 // Nothing to do.
1487 break;
1488 case kCondLT:
1489 false_high_cond = kCondGT;
1490 break;
1491 case kCondLE:
1492 true_high_cond = kCondLT;
1493 break;
1494 case kCondGT:
1495 false_high_cond = kCondLT;
1496 break;
1497 case kCondGE:
1498 true_high_cond = kCondGT;
1499 break;
Aart Bike9f37602015-10-09 11:15:55 -07001500 case kCondB:
1501 false_high_cond = kCondA;
1502 break;
1503 case kCondBE:
1504 true_high_cond = kCondB;
1505 break;
1506 case kCondA:
1507 false_high_cond = kCondB;
1508 break;
1509 case kCondAE:
1510 true_high_cond = kCondA;
1511 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001512 }
1513 if (right.IsConstant()) {
1514 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1515 int32_t val_low = Low32Bits(value);
1516 int32_t val_high = High32Bits(value);
1517
Vladimir Markoac6ac102015-12-17 12:14:00 +00001518 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001520 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001521 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001522 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001524 __ b(true_label, ARMCondition(true_high_cond));
1525 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001526 }
1527 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001528 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001529 } else {
1530 Register right_high = right.AsRegisterPairHigh<Register>();
1531 Register right_low = right.AsRegisterPairLow<Register>();
1532
1533 __ cmp(left_high, ShifterOperand(right_high));
1534 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001535 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001536 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001537 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001538 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001539 __ b(true_label, ARMCondition(true_high_cond));
1540 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001541 }
1542 // Must be equal high, so compare the lows.
1543 __ cmp(left_low, ShifterOperand(right_low));
1544 }
1545 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001546 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001547 __ b(true_label, final_condition);
1548}
1549
David Brazdil0debae72015-11-12 18:37:00 +00001550void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1551 Label* true_target_in,
1552 Label* false_target_in) {
1553 // Generated branching requires both targets to be explicit. If either of the
1554 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1555 Label fallthrough_target;
1556 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1557 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1558
Roland Levillain4fa13f62015-07-06 18:11:54 +01001559 Primitive::Type type = condition->InputAt(0)->GetType();
1560 switch (type) {
1561 case Primitive::kPrimLong:
1562 GenerateLongComparesAndJumps(condition, true_target, false_target);
1563 break;
1564 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001565 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001566 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001567 GenerateFPJumps(condition, true_target, false_target);
1568 break;
1569 default:
1570 LOG(FATAL) << "Unexpected compare type " << type;
1571 }
1572
David Brazdil0debae72015-11-12 18:37:00 +00001573 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001574 __ b(false_target);
1575 }
David Brazdil0debae72015-11-12 18:37:00 +00001576
1577 if (fallthrough_target.IsLinked()) {
1578 __ Bind(&fallthrough_target);
1579 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001580}
1581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001582void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001583 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001584 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001585 Label* false_target) {
1586 HInstruction* cond = instruction->InputAt(condition_input_index);
1587
1588 if (true_target == nullptr && false_target == nullptr) {
1589 // Nothing to do. The code always falls through.
1590 return;
1591 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001592 // Constant condition, statically compared against "true" (integer value 1).
1593 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001594 if (true_target != nullptr) {
1595 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001596 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001597 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001598 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001599 if (false_target != nullptr) {
1600 __ b(false_target);
1601 }
1602 }
1603 return;
1604 }
1605
1606 // The following code generates these patterns:
1607 // (1) true_target == nullptr && false_target != nullptr
1608 // - opposite condition true => branch to false_target
1609 // (2) true_target != nullptr && false_target == nullptr
1610 // - condition true => branch to true_target
1611 // (3) true_target != nullptr && false_target != nullptr
1612 // - condition true => branch to true_target
1613 // - branch to false_target
1614 if (IsBooleanValueOrMaterializedCondition(cond)) {
1615 // Condition has been materialized, compare the output to 0.
1616 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1617 DCHECK(cond_val.IsRegister());
1618 if (true_target == nullptr) {
1619 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1620 } else {
1621 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001622 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001623 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001624 // Condition has not been materialized. Use its inputs as the comparison and
1625 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001626 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001627
1628 // If this is a long or FP comparison that has been folded into
1629 // the HCondition, generate the comparison directly.
1630 Primitive::Type type = condition->InputAt(0)->GetType();
1631 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1632 GenerateCompareTestAndBranch(condition, true_target, false_target);
1633 return;
1634 }
1635
1636 LocationSummary* locations = cond->GetLocations();
1637 DCHECK(locations->InAt(0).IsRegister());
1638 Register left = locations->InAt(0).AsRegister<Register>();
1639 Location right = locations->InAt(1);
1640 if (right.IsRegister()) {
1641 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001642 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001643 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001644 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001645 }
1646 if (true_target == nullptr) {
1647 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1648 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001649 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001650 }
Dave Allison20dfc792014-06-16 20:44:29 -07001651 }
David Brazdil0debae72015-11-12 18:37:00 +00001652
1653 // If neither branch falls through (case 3), the conditional branch to `true_target`
1654 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1655 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001656 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657 }
1658}
1659
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001660void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1662 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001663 locations->SetInAt(0, Location::RequiresRegister());
1664 }
1665}
1666
1667void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001668 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1669 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1670 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1671 nullptr : codegen_->GetLabelOf(true_successor);
1672 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1673 nullptr : codegen_->GetLabelOf(false_successor);
1674 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675}
1676
1677void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1678 LocationSummary* locations = new (GetGraph()->GetArena())
1679 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001680 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001681 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682 locations->SetInAt(0, Location::RequiresRegister());
1683 }
1684}
1685
1686void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001687 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001688 GenerateTestAndBranch(deoptimize,
1689 /* condition_input_index */ 0,
1690 slow_path->GetEntryLabel(),
1691 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692}
Dave Allison20dfc792014-06-16 20:44:29 -07001693
David Brazdil74eb1b22015-12-14 11:44:01 +00001694void LocationsBuilderARM::VisitSelect(HSelect* select) {
1695 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1696 if (Primitive::IsFloatingPointType(select->GetType())) {
1697 locations->SetInAt(0, Location::RequiresFpuRegister());
1698 locations->SetInAt(1, Location::RequiresFpuRegister());
1699 } else {
1700 locations->SetInAt(0, Location::RequiresRegister());
1701 locations->SetInAt(1, Location::RequiresRegister());
1702 }
1703 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1704 locations->SetInAt(2, Location::RequiresRegister());
1705 }
1706 locations->SetOut(Location::SameAsFirstInput());
1707}
1708
1709void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1710 LocationSummary* locations = select->GetLocations();
1711 Label false_target;
1712 GenerateTestAndBranch(select,
1713 /* condition_input_index */ 2,
1714 /* true_target */ nullptr,
1715 &false_target);
1716 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1717 __ Bind(&false_target);
1718}
1719
David Srbecky0cf44932015-12-09 14:09:59 +00001720void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1721 new (GetGraph()->GetArena()) LocationSummary(info);
1722}
1723
David Srbeckyd28f4a02016-03-14 17:14:24 +00001724void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1725 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001726}
1727
1728void CodeGeneratorARM::GenerateNop() {
1729 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001730}
1731
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001733 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001734 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735 // Handle the long/FP comparisons made in instruction simplification.
1736 switch (cond->InputAt(0)->GetType()) {
1737 case Primitive::kPrimLong:
1738 locations->SetInAt(0, Location::RequiresRegister());
1739 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001740 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001741 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1742 }
1743 break;
1744
1745 case Primitive::kPrimFloat:
1746 case Primitive::kPrimDouble:
1747 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001748 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001749 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1751 }
1752 break;
1753
1754 default:
1755 locations->SetInAt(0, Location::RequiresRegister());
1756 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001757 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1759 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001760 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001761}
1762
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001764 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001765 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001766 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001767
1768 LocationSummary* locations = cond->GetLocations();
1769 Location left = locations->InAt(0);
1770 Location right = locations->InAt(1);
1771 Register out = locations->Out().AsRegister<Register>();
1772 Label true_label, false_label;
1773
1774 switch (cond->InputAt(0)->GetType()) {
1775 default: {
1776 // Integer case.
1777 if (right.IsRegister()) {
1778 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1779 } else {
1780 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001781 __ CmpConstant(left.AsRegister<Register>(),
1782 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001783 }
Aart Bike9f37602015-10-09 11:15:55 -07001784 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001786 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001787 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001788 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001789 return;
1790 }
1791 case Primitive::kPrimLong:
1792 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1793 break;
1794 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001795 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001796 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001797 GenerateFPJumps(cond, &true_label, &false_label);
1798 break;
1799 }
1800
1801 // Convert the jumps into the result.
1802 Label done_label;
1803
1804 // False case: result = 0.
1805 __ Bind(&false_label);
1806 __ LoadImmediate(out, 0);
1807 __ b(&done_label);
1808
1809 // True case: result = 1.
1810 __ Bind(&true_label);
1811 __ LoadImmediate(out, 1);
1812 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001813}
1814
1815void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001817}
1818
1819void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001821}
1822
1823void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001825}
1826
1827void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001829}
1830
1831void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001833}
1834
1835void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001837}
1838
1839void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001841}
1842
1843void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001845}
1846
1847void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001849}
1850
1851void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001853}
1854
1855void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001857}
1858
1859void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001861}
1862
Aart Bike9f37602015-10-09 11:15:55 -07001863void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001864 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001865}
1866
1867void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001869}
1870
1871void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001873}
1874
1875void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001877}
1878
1879void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001881}
1882
1883void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001885}
1886
1887void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001888 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001889}
1890
1891void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001893}
1894
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001898 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899}
1900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001901void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001902 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001903}
1904
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001905void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1906 LocationSummary* locations =
1907 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1908 locations->SetOut(Location::ConstantLocation(constant));
1909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001912 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001913}
1914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001918 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919}
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 // Will be generated at use site.
1923}
1924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1926 LocationSummary* locations =
1927 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1928 locations->SetOut(Location::ConstantLocation(constant));
1929}
1930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001932 // Will be generated at use site.
1933}
1934
1935void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1938 locations->SetOut(Location::ConstantLocation(constant));
1939}
1940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001941void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001942 // Will be generated at use site.
1943}
1944
Calin Juravle27df7582015-04-17 19:12:31 +01001945void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1946 memory_barrier->SetLocations(nullptr);
1947}
1948
1949void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001950 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001951}
1952
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001953void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001954 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955}
1956
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001957void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001959}
1960
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001962 LocationSummary* locations =
1963 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001964 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965}
1966
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001967void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001968 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Calin Juravle175dc732015-08-25 15:42:32 +01001971void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1972 // The trampoline uses the same calling convention as dex calling conventions,
1973 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1974 // the method_idx.
1975 HandleInvoke(invoke);
1976}
1977
1978void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1979 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1980}
1981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001982void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001983 // Explicit clinit checks triggered by static invokes must have been pruned by
1984 // art::PrepareForRegisterAllocation.
1985 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001986
Vladimir Marko68c981f2016-08-26 13:13:33 +01001987 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001988 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001989 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1990 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1991 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001992 return;
1993 }
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001996
1997 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1998 if (invoke->HasPcRelativeDexCache()) {
1999 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2000 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001}
2002
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002003static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2004 if (invoke->GetLocations()->Intrinsified()) {
2005 IntrinsicCodeGeneratorARM intrinsic(codegen);
2006 intrinsic.Dispatch(invoke);
2007 return true;
2008 }
2009 return false;
2010}
2011
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002012void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002013 // Explicit clinit checks triggered by static invokes must have been pruned by
2014 // art::PrepareForRegisterAllocation.
2015 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002016
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002017 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2018 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002019 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002020
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002021 LocationSummary* locations = invoke->GetLocations();
2022 codegen_->GenerateStaticOrDirectCall(
2023 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002024 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002025}
2026
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002027void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002028 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002029 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002030}
2031
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002032void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002033 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002034 if (intrinsic.TryDispatch(invoke)) {
2035 return;
2036 }
2037
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 HandleInvoke(invoke);
2039}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002040
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002041void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002042 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2043 return;
2044 }
2045
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002046 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002047 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002048 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002049}
2050
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2052 HandleInvoke(invoke);
2053 // Add the hidden argument.
2054 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2055}
2056
2057void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2058 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002059 LocationSummary* locations = invoke->GetLocations();
2060 Register temp = locations->GetTemp(0).AsRegister<Register>();
2061 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 Location receiver = locations->InAt(0);
2063 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2064
Roland Levillain3b359c72015-11-17 19:35:12 +00002065 // Set the hidden argument. This is safe to do this here, as R12
2066 // won't be modified thereafter, before the `blx` (call) instruction.
2067 DCHECK_EQ(R12, hidden_reg);
2068 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002069
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070 if (receiver.IsStackSlot()) {
2071 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002072 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2074 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002075 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002078 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002079 // Instead of simply (possibly) unpoisoning `temp` here, we should
2080 // emit a read barrier for the previous class reference load.
2081 // However this is not required in practice, as this is an
2082 // intermediate/temporary reference and because the current
2083 // concurrent copying collector keeps the from-space memory
2084 // intact/accessible until the end of the marking phase (the
2085 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002086 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002087 __ LoadFromOffset(kLoadWord, temp, temp,
2088 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2089 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002090 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002092 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002093 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002094 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 // LR = temp->GetEntryPoint();
2096 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2097 // LR();
2098 __ blx(LR);
2099 DCHECK(!codegen_->IsLeafMethod());
2100 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2101}
2102
Roland Levillain88cb1752014-10-20 16:36:47 +01002103void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2104 LocationSummary* locations =
2105 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2106 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002107 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002108 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2110 break;
2111 }
2112 case Primitive::kPrimLong: {
2113 locations->SetInAt(0, Location::RequiresRegister());
2114 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002115 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002116 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002117
Roland Levillain88cb1752014-10-20 16:36:47 +01002118 case Primitive::kPrimFloat:
2119 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002120 locations->SetInAt(0, Location::RequiresFpuRegister());
2121 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002122 break;
2123
2124 default:
2125 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2126 }
2127}
2128
2129void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2130 LocationSummary* locations = neg->GetLocations();
2131 Location out = locations->Out();
2132 Location in = locations->InAt(0);
2133 switch (neg->GetResultType()) {
2134 case Primitive::kPrimInt:
2135 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002137 break;
2138
2139 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002140 DCHECK(in.IsRegisterPair());
2141 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2142 __ rsbs(out.AsRegisterPairLow<Register>(),
2143 in.AsRegisterPairLow<Register>(),
2144 ShifterOperand(0));
2145 // We cannot emit an RSC (Reverse Subtract with Carry)
2146 // instruction here, as it does not exist in the Thumb-2
2147 // instruction set. We use the following approach
2148 // using SBC and SUB instead.
2149 //
2150 // out.hi = -C
2151 __ sbc(out.AsRegisterPairHigh<Register>(),
2152 out.AsRegisterPairHigh<Register>(),
2153 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2154 // out.hi = out.hi - in.hi
2155 __ sub(out.AsRegisterPairHigh<Register>(),
2156 out.AsRegisterPairHigh<Register>(),
2157 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2158 break;
2159
Roland Levillain88cb1752014-10-20 16:36:47 +01002160 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002161 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002163 break;
2164
Roland Levillain88cb1752014-10-20 16:36:47 +01002165 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002166 DCHECK(in.IsFpuRegisterPair());
2167 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2168 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002169 break;
2170
2171 default:
2172 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2173 }
2174}
2175
Roland Levillaindff1f282014-11-05 14:15:05 +00002176void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002177 Primitive::Type result_type = conversion->GetResultType();
2178 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002179 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002180
Roland Levillain5b3ee562015-04-14 16:02:41 +01002181 // The float-to-long, double-to-long and long-to-float type conversions
2182 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002183 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002184 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2185 && result_type == Primitive::kPrimLong)
2186 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002187 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002188 : LocationSummary::kNoCall;
2189 LocationSummary* locations =
2190 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2191
David Brazdilb2bd1c52015-03-25 11:17:37 +00002192 // The Java language does not allow treating boolean as an integral type but
2193 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002194
Roland Levillaindff1f282014-11-05 14:15:05 +00002195 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002196 case Primitive::kPrimByte:
2197 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002198 case Primitive::kPrimLong:
2199 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
2204 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002205 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 locations->SetInAt(0, Location::RequiresRegister());
2207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2208 break;
2209
2210 default:
2211 LOG(FATAL) << "Unexpected type conversion from " << input_type
2212 << " to " << result_type;
2213 }
2214 break;
2215
Roland Levillain01a8d712014-11-14 16:27:39 +00002216 case Primitive::kPrimShort:
2217 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002218 case Primitive::kPrimLong:
2219 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002222 case Primitive::kPrimByte:
2223 case Primitive::kPrimInt:
2224 case Primitive::kPrimChar:
2225 // Processing a Dex `int-to-short' instruction.
2226 locations->SetInAt(0, Location::RequiresRegister());
2227 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2228 break;
2229
2230 default:
2231 LOG(FATAL) << "Unexpected type conversion from " << input_type
2232 << " to " << result_type;
2233 }
2234 break;
2235
Roland Levillain946e1432014-11-11 17:35:19 +00002236 case Primitive::kPrimInt:
2237 switch (input_type) {
2238 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002239 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002240 locations->SetInAt(0, Location::Any());
2241 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2242 break;
2243
2244 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002245 // Processing a Dex `float-to-int' instruction.
2246 locations->SetInAt(0, Location::RequiresFpuRegister());
2247 locations->SetOut(Location::RequiresRegister());
2248 locations->AddTemp(Location::RequiresFpuRegister());
2249 break;
2250
Roland Levillain946e1432014-11-11 17:35:19 +00002251 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002252 // Processing a Dex `double-to-int' instruction.
2253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetOut(Location::RequiresRegister());
2255 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002256 break;
2257
2258 default:
2259 LOG(FATAL) << "Unexpected type conversion from " << input_type
2260 << " to " << result_type;
2261 }
2262 break;
2263
Roland Levillaindff1f282014-11-05 14:15:05 +00002264 case Primitive::kPrimLong:
2265 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002266 case Primitive::kPrimBoolean:
2267 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002268 case Primitive::kPrimByte:
2269 case Primitive::kPrimShort:
2270 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002271 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002272 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2275 break;
2276
Roland Levillain624279f2014-12-04 11:54:28 +00002277 case Primitive::kPrimFloat: {
2278 // Processing a Dex `float-to-long' instruction.
2279 InvokeRuntimeCallingConvention calling_convention;
2280 locations->SetInAt(0, Location::FpuRegisterLocation(
2281 calling_convention.GetFpuRegisterAt(0)));
2282 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2283 break;
2284 }
2285
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002286 case Primitive::kPrimDouble: {
2287 // Processing a Dex `double-to-long' instruction.
2288 InvokeRuntimeCallingConvention calling_convention;
2289 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2290 calling_convention.GetFpuRegisterAt(0),
2291 calling_convention.GetFpuRegisterAt(1)));
2292 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002293 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002294 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002295
2296 default:
2297 LOG(FATAL) << "Unexpected type conversion from " << input_type
2298 << " to " << result_type;
2299 }
2300 break;
2301
Roland Levillain981e4542014-11-14 11:47:14 +00002302 case Primitive::kPrimChar:
2303 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002304 case Primitive::kPrimLong:
2305 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002311 // Processing a Dex `int-to-char' instruction.
2312 locations->SetInAt(0, Location::RequiresRegister());
2313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 }
2320 break;
2321
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002323 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002324 case Primitive::kPrimBoolean:
2325 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002326 case Primitive::kPrimByte:
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimChar:
2330 // Processing a Dex `int-to-float' instruction.
2331 locations->SetInAt(0, Location::RequiresRegister());
2332 locations->SetOut(Location::RequiresFpuRegister());
2333 break;
2334
Roland Levillain5b3ee562015-04-14 16:02:41 +01002335 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002336 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002337 InvokeRuntimeCallingConvention calling_convention;
2338 locations->SetInAt(0, Location::RegisterPairLocation(
2339 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2340 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002341 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002342 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002343
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002345 // Processing a Dex `double-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 };
2354 break;
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimChar:
2364 // Processing a Dex `int-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 locations->SetOut(Location::RequiresFpuRegister());
2367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 // Processing a Dex `long-to-double' instruction.
2371 locations->SetInAt(0, Location::RequiresRegister());
2372 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002373 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002374 locations->AddTemp(Location::RequiresFpuRegister());
2375 break;
2376
Roland Levillaincff13742014-11-17 14:32:17 +00002377 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002378 // Processing a Dex `float-to-double' instruction.
2379 locations->SetInAt(0, Location::RequiresFpuRegister());
2380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393}
2394
2395void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2396 LocationSummary* locations = conversion->GetLocations();
2397 Location out = locations->Out();
2398 Location in = locations->InAt(0);
2399 Primitive::Type result_type = conversion->GetResultType();
2400 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002401 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002402 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002403 case Primitive::kPrimByte:
2404 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002405 case Primitive::kPrimLong:
2406 // Type conversion from long to byte is a result of code transformations.
2407 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2408 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002409 case Primitive::kPrimBoolean:
2410 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002411 case Primitive::kPrimShort:
2412 case Primitive::kPrimInt:
2413 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002414 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002415 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected type conversion from " << input_type
2420 << " to " << result_type;
2421 }
2422 break;
2423
Roland Levillain01a8d712014-11-14 16:27:39 +00002424 case Primitive::kPrimShort:
2425 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002426 case Primitive::kPrimLong:
2427 // Type conversion from long to short is a result of code transformations.
2428 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2429 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002430 case Primitive::kPrimBoolean:
2431 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002432 case Primitive::kPrimByte:
2433 case Primitive::kPrimInt:
2434 case Primitive::kPrimChar:
2435 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002436 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002437 break;
2438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 }
2443 break;
2444
Roland Levillain946e1432014-11-11 17:35:19 +00002445 case Primitive::kPrimInt:
2446 switch (input_type) {
2447 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002448 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002449 DCHECK(out.IsRegister());
2450 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002451 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002452 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002454 } else {
2455 DCHECK(in.IsConstant());
2456 DCHECK(in.GetConstant()->IsLongConstant());
2457 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002459 }
2460 break;
2461
Roland Levillain3f8f9362014-12-02 17:45:01 +00002462 case Primitive::kPrimFloat: {
2463 // Processing a Dex `float-to-int' instruction.
2464 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002465 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002466 __ vmovrs(out.AsRegister<Register>(), temp);
2467 break;
2468 }
2469
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002470 case Primitive::kPrimDouble: {
2471 // Processing a Dex `double-to-int' instruction.
2472 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002473 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002474 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002475 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002476 }
Roland Levillain946e1432014-11-11 17:35:19 +00002477
2478 default:
2479 LOG(FATAL) << "Unexpected type conversion from " << input_type
2480 << " to " << result_type;
2481 }
2482 break;
2483
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 case Primitive::kPrimLong:
2485 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002486 case Primitive::kPrimBoolean:
2487 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 case Primitive::kPrimByte:
2489 case Primitive::kPrimShort:
2490 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002491 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002492 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 DCHECK(out.IsRegisterPair());
2494 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 // Sign extension.
2497 __ Asr(out.AsRegisterPairHigh<Register>(),
2498 out.AsRegisterPairLow<Register>(),
2499 31);
2500 break;
2501
2502 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002503 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002504 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002505 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002506 break;
2507
Roland Levillaindff1f282014-11-05 14:15:05 +00002508 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002509 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002510 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002511 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002512 break;
2513
2514 default:
2515 LOG(FATAL) << "Unexpected type conversion from " << input_type
2516 << " to " << result_type;
2517 }
2518 break;
2519
Roland Levillain981e4542014-11-14 11:47:14 +00002520 case Primitive::kPrimChar:
2521 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002522 case Primitive::kPrimLong:
2523 // Type conversion from long to char is a result of code transformations.
2524 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2525 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002526 case Primitive::kPrimBoolean:
2527 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002528 case Primitive::kPrimByte:
2529 case Primitive::kPrimShort:
2530 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002531 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002532 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002533 break;
2534
2535 default:
2536 LOG(FATAL) << "Unexpected type conversion from " << input_type
2537 << " to " << result_type;
2538 }
2539 break;
2540
Roland Levillaindff1f282014-11-05 14:15:05 +00002541 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002542 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002543 case Primitive::kPrimBoolean:
2544 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002545 case Primitive::kPrimByte:
2546 case Primitive::kPrimShort:
2547 case Primitive::kPrimInt:
2548 case Primitive::kPrimChar: {
2549 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002550 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2551 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002552 break;
2553 }
2554
Roland Levillain5b3ee562015-04-14 16:02:41 +01002555 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002556 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002557 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002558 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002559 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560
Roland Levillaincff13742014-11-17 14:32:17 +00002561 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002562 // Processing a Dex `double-to-float' instruction.
2563 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2564 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
2570 };
2571 break;
2572
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002574 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002575 case Primitive::kPrimBoolean:
2576 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimByte:
2578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt:
2580 case Primitive::kPrimChar: {
2581 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002583 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2584 out.AsFpuRegisterPairLow<SRegister>());
2585 break;
2586 }
2587
Roland Levillain647b9ed2014-11-27 12:06:00 +00002588 case Primitive::kPrimLong: {
2589 // Processing a Dex `long-to-double' instruction.
2590 Register low = in.AsRegisterPairLow<Register>();
2591 Register high = in.AsRegisterPairHigh<Register>();
2592 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2593 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002594 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002595 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002596 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2597 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002598
Roland Levillain682393c2015-04-14 15:57:52 +01002599 // temp_d = int-to-double(high)
2600 __ vmovsr(temp_s, high);
2601 __ vcvtdi(temp_d, temp_s);
2602 // constant_d = k2Pow32EncodingForDouble
2603 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2604 // out_d = unsigned-to-double(low)
2605 __ vmovsr(out_s, low);
2606 __ vcvtdu(out_d, out_s);
2607 // out_d += temp_d * constant_d
2608 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002609 break;
2610 }
2611
Roland Levillaincff13742014-11-17 14:32:17 +00002612 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002613 // Processing a Dex `float-to-double' instruction.
2614 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2615 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unexpected type conversion from " << input_type
2626 << " to " << result_type;
2627 }
2628}
2629
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002634 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002635 locations->SetInAt(0, Location::RequiresRegister());
2636 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2638 break;
2639 }
2640
2641 case Primitive::kPrimLong: {
2642 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002643 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002645 break;
2646 }
2647
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002648 case Primitive::kPrimFloat:
2649 case Primitive::kPrimDouble: {
2650 locations->SetInAt(0, Location::RequiresFpuRegister());
2651 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002653 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002654 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002658 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659}
2660
2661void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2662 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002663 Location out = locations->Out();
2664 Location first = locations->InAt(0);
2665 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666 switch (add->GetResultType()) {
2667 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002668 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002669 __ add(out.AsRegister<Register>(),
2670 first.AsRegister<Register>(),
2671 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002672 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002673 __ AddConstant(out.AsRegister<Register>(),
2674 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002675 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002676 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002677 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002678
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002679 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002680 if (second.IsConstant()) {
2681 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2682 GenerateAddLongConst(out, first, value);
2683 } else {
2684 DCHECK(second.IsRegisterPair());
2685 __ adds(out.AsRegisterPairLow<Register>(),
2686 first.AsRegisterPairLow<Register>(),
2687 ShifterOperand(second.AsRegisterPairLow<Register>()));
2688 __ adc(out.AsRegisterPairHigh<Register>(),
2689 first.AsRegisterPairHigh<Register>(),
2690 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2691 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002692 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002694
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002695 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002696 __ vadds(out.AsFpuRegister<SRegister>(),
2697 first.AsFpuRegister<SRegister>(),
2698 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002699 break;
2700
2701 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002702 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2703 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2704 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002705 break;
2706
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002707 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002708 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002709 }
2710}
2711
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002712void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002715 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002716 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002717 locations->SetInAt(0, Location::RequiresRegister());
2718 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2720 break;
2721 }
2722
2723 case Primitive::kPrimLong: {
2724 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002725 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002726 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727 break;
2728 }
Calin Juravle11351682014-10-23 15:38:15 +01002729 case Primitive::kPrimFloat:
2730 case Primitive::kPrimDouble: {
2731 locations->SetInAt(0, Location::RequiresFpuRegister());
2732 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002733 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002734 break;
Calin Juravle11351682014-10-23 15:38:15 +01002735 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002736 default:
Calin Juravle11351682014-10-23 15:38:15 +01002737 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002738 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002739}
2740
2741void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2742 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002743 Location out = locations->Out();
2744 Location first = locations->InAt(0);
2745 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002746 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002747 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002748 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002749 __ sub(out.AsRegister<Register>(),
2750 first.AsRegister<Register>(),
2751 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002752 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002753 __ AddConstant(out.AsRegister<Register>(),
2754 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002755 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002756 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002757 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002758 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002759
Calin Juravle11351682014-10-23 15:38:15 +01002760 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002761 if (second.IsConstant()) {
2762 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2763 GenerateAddLongConst(out, first, -value);
2764 } else {
2765 DCHECK(second.IsRegisterPair());
2766 __ subs(out.AsRegisterPairLow<Register>(),
2767 first.AsRegisterPairLow<Register>(),
2768 ShifterOperand(second.AsRegisterPairLow<Register>()));
2769 __ sbc(out.AsRegisterPairHigh<Register>(),
2770 first.AsRegisterPairHigh<Register>(),
2771 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2772 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002773 break;
Calin Juravle11351682014-10-23 15:38:15 +01002774 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002775
Calin Juravle11351682014-10-23 15:38:15 +01002776 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002777 __ vsubs(out.AsFpuRegister<SRegister>(),
2778 first.AsFpuRegister<SRegister>(),
2779 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780 break;
Calin Juravle11351682014-10-23 15:38:15 +01002781 }
2782
2783 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002784 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2785 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2786 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002787 break;
2788 }
2789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 default:
Calin Juravle11351682014-10-23 15:38:15 +01002792 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002793 }
2794}
2795
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796void LocationsBuilderARM::VisitMul(HMul* mul) {
2797 LocationSummary* locations =
2798 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2799 switch (mul->GetResultType()) {
2800 case Primitive::kPrimInt:
2801 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002802 locations->SetInAt(0, Location::RequiresRegister());
2803 locations->SetInAt(1, Location::RequiresRegister());
2804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002805 break;
2806 }
2807
Calin Juravleb5bfa962014-10-21 18:02:24 +01002808 case Primitive::kPrimFloat:
2809 case Primitive::kPrimDouble: {
2810 locations->SetInAt(0, Location::RequiresFpuRegister());
2811 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002812 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002813 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002814 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002815
2816 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002817 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002818 }
2819}
2820
2821void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2822 LocationSummary* locations = mul->GetLocations();
2823 Location out = locations->Out();
2824 Location first = locations->InAt(0);
2825 Location second = locations->InAt(1);
2826 switch (mul->GetResultType()) {
2827 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002828 __ mul(out.AsRegister<Register>(),
2829 first.AsRegister<Register>(),
2830 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002831 break;
2832 }
2833 case Primitive::kPrimLong: {
2834 Register out_hi = out.AsRegisterPairHigh<Register>();
2835 Register out_lo = out.AsRegisterPairLow<Register>();
2836 Register in1_hi = first.AsRegisterPairHigh<Register>();
2837 Register in1_lo = first.AsRegisterPairLow<Register>();
2838 Register in2_hi = second.AsRegisterPairHigh<Register>();
2839 Register in2_lo = second.AsRegisterPairLow<Register>();
2840
2841 // Extra checks to protect caused by the existence of R1_R2.
2842 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2843 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2844 DCHECK_NE(out_hi, in1_lo);
2845 DCHECK_NE(out_hi, in2_lo);
2846
2847 // input: in1 - 64 bits, in2 - 64 bits
2848 // output: out
2849 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2850 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2851 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2852
2853 // IP <- in1.lo * in2.hi
2854 __ mul(IP, in1_lo, in2_hi);
2855 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2856 __ mla(out_hi, in1_hi, in2_lo, IP);
2857 // out.lo <- (in1.lo * in2.lo)[31:0];
2858 __ umull(out_lo, IP, in1_lo, in2_lo);
2859 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2860 __ add(out_hi, out_hi, ShifterOperand(IP));
2861 break;
2862 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002863
2864 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002865 __ vmuls(out.AsFpuRegister<SRegister>(),
2866 first.AsFpuRegister<SRegister>(),
2867 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002868 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002869 }
2870
2871 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002872 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2873 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2874 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002875 break;
2876 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002877
2878 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002880 }
2881}
2882
Zheng Xuc6667102015-05-15 16:08:45 +08002883void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2884 DCHECK(instruction->IsDiv() || instruction->IsRem());
2885 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2886
2887 LocationSummary* locations = instruction->GetLocations();
2888 Location second = locations->InAt(1);
2889 DCHECK(second.IsConstant());
2890
2891 Register out = locations->Out().AsRegister<Register>();
2892 Register dividend = locations->InAt(0).AsRegister<Register>();
2893 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2894 DCHECK(imm == 1 || imm == -1);
2895
2896 if (instruction->IsRem()) {
2897 __ LoadImmediate(out, 0);
2898 } else {
2899 if (imm == 1) {
2900 __ Mov(out, dividend);
2901 } else {
2902 __ rsb(out, dividend, ShifterOperand(0));
2903 }
2904 }
2905}
2906
2907void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2908 DCHECK(instruction->IsDiv() || instruction->IsRem());
2909 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2910
2911 LocationSummary* locations = instruction->GetLocations();
2912 Location second = locations->InAt(1);
2913 DCHECK(second.IsConstant());
2914
2915 Register out = locations->Out().AsRegister<Register>();
2916 Register dividend = locations->InAt(0).AsRegister<Register>();
2917 Register temp = locations->GetTemp(0).AsRegister<Register>();
2918 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002919 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002920 int ctz_imm = CTZ(abs_imm);
2921
2922 if (ctz_imm == 1) {
2923 __ Lsr(temp, dividend, 32 - ctz_imm);
2924 } else {
2925 __ Asr(temp, dividend, 31);
2926 __ Lsr(temp, temp, 32 - ctz_imm);
2927 }
2928 __ add(out, temp, ShifterOperand(dividend));
2929
2930 if (instruction->IsDiv()) {
2931 __ Asr(out, out, ctz_imm);
2932 if (imm < 0) {
2933 __ rsb(out, out, ShifterOperand(0));
2934 }
2935 } else {
2936 __ ubfx(out, out, 0, ctz_imm);
2937 __ sub(out, out, ShifterOperand(temp));
2938 }
2939}
2940
2941void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2942 DCHECK(instruction->IsDiv() || instruction->IsRem());
2943 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2944
2945 LocationSummary* locations = instruction->GetLocations();
2946 Location second = locations->InAt(1);
2947 DCHECK(second.IsConstant());
2948
2949 Register out = locations->Out().AsRegister<Register>();
2950 Register dividend = locations->InAt(0).AsRegister<Register>();
2951 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2952 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2953 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2954
2955 int64_t magic;
2956 int shift;
2957 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2958
2959 __ LoadImmediate(temp1, magic);
2960 __ smull(temp2, temp1, dividend, temp1);
2961
2962 if (imm > 0 && magic < 0) {
2963 __ add(temp1, temp1, ShifterOperand(dividend));
2964 } else if (imm < 0 && magic > 0) {
2965 __ sub(temp1, temp1, ShifterOperand(dividend));
2966 }
2967
2968 if (shift != 0) {
2969 __ Asr(temp1, temp1, shift);
2970 }
2971
2972 if (instruction->IsDiv()) {
2973 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2974 } else {
2975 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2976 // TODO: Strength reduction for mls.
2977 __ LoadImmediate(temp2, imm);
2978 __ mls(out, temp1, temp2, dividend);
2979 }
2980}
2981
2982void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2983 DCHECK(instruction->IsDiv() || instruction->IsRem());
2984 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2985
2986 LocationSummary* locations = instruction->GetLocations();
2987 Location second = locations->InAt(1);
2988 DCHECK(second.IsConstant());
2989
2990 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2991 if (imm == 0) {
2992 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2993 } else if (imm == 1 || imm == -1) {
2994 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002995 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002996 DivRemByPowerOfTwo(instruction);
2997 } else {
2998 DCHECK(imm <= -2 || imm >= 2);
2999 GenerateDivRemWithAnyConstant(instruction);
3000 }
3001}
3002
Calin Juravle7c4954d2014-10-28 16:57:40 +00003003void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003004 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3005 if (div->GetResultType() == Primitive::kPrimLong) {
3006 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003007 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003008 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3009 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003010 } else if (div->GetResultType() == Primitive::kPrimInt &&
3011 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3012 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003013 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003014 }
3015
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3017
Calin Juravle7c4954d2014-10-28 16:57:40 +00003018 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003019 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003020 if (div->InputAt(1)->IsConstant()) {
3021 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003022 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003024 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3025 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003026 // No temp register required.
3027 } else {
3028 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003029 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003030 locations->AddTemp(Location::RequiresRegister());
3031 }
3032 }
3033 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003034 locations->SetInAt(0, Location::RequiresRegister());
3035 locations->SetInAt(1, Location::RequiresRegister());
3036 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3037 } else {
3038 InvokeRuntimeCallingConvention calling_convention;
3039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3040 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3041 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3042 // we only need the former.
3043 locations->SetOut(Location::RegisterLocation(R0));
3044 }
Calin Juravled0d48522014-11-04 16:40:20 +00003045 break;
3046 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003047 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003048 InvokeRuntimeCallingConvention calling_convention;
3049 locations->SetInAt(0, Location::RegisterPairLocation(
3050 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3051 locations->SetInAt(1, Location::RegisterPairLocation(
3052 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003053 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003054 break;
3055 }
3056 case Primitive::kPrimFloat:
3057 case Primitive::kPrimDouble: {
3058 locations->SetInAt(0, Location::RequiresFpuRegister());
3059 locations->SetInAt(1, Location::RequiresFpuRegister());
3060 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3061 break;
3062 }
3063
3064 default:
3065 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3066 }
3067}
3068
3069void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3070 LocationSummary* locations = div->GetLocations();
3071 Location out = locations->Out();
3072 Location first = locations->InAt(0);
3073 Location second = locations->InAt(1);
3074
3075 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003076 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003077 if (second.IsConstant()) {
3078 GenerateDivRemConstantIntegral(div);
3079 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003080 __ sdiv(out.AsRegister<Register>(),
3081 first.AsRegister<Register>(),
3082 second.AsRegister<Register>());
3083 } else {
3084 InvokeRuntimeCallingConvention calling_convention;
3085 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3086 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3087 DCHECK_EQ(R0, out.AsRegister<Register>());
3088
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003089 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003090 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003091 }
Calin Juravled0d48522014-11-04 16:40:20 +00003092 break;
3093 }
3094
Calin Juravle7c4954d2014-10-28 16:57:40 +00003095 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003096 InvokeRuntimeCallingConvention calling_convention;
3097 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3098 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3099 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3100 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3101 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003102 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003103
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003104 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003105 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003106 break;
3107 }
3108
3109 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003110 __ vdivs(out.AsFpuRegister<SRegister>(),
3111 first.AsFpuRegister<SRegister>(),
3112 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003113 break;
3114 }
3115
3116 case Primitive::kPrimDouble: {
3117 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3118 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3119 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3120 break;
3121 }
3122
3123 default:
3124 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3125 }
3126}
3127
Calin Juravlebacfec32014-11-14 15:54:36 +00003128void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003129 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003130
3131 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003132 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003133 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3134 // sdiv will be replaced by other instruction sequence.
3135 call_kind = LocationSummary::kNoCall;
3136 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3137 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003138 // Have hardware divide instruction for int, do it with three instructions.
3139 call_kind = LocationSummary::kNoCall;
3140 }
3141
Calin Juravlebacfec32014-11-14 15:54:36 +00003142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3143
Calin Juravled2ec87d2014-12-08 14:24:46 +00003144 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003145 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003146 if (rem->InputAt(1)->IsConstant()) {
3147 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003148 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003150 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3151 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003152 // No temp register required.
3153 } else {
3154 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003155 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003156 locations->AddTemp(Location::RequiresRegister());
3157 }
3158 }
3159 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003160 locations->SetInAt(0, Location::RequiresRegister());
3161 locations->SetInAt(1, Location::RequiresRegister());
3162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3163 locations->AddTemp(Location::RequiresRegister());
3164 } else {
3165 InvokeRuntimeCallingConvention calling_convention;
3166 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3167 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3168 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3169 // we only need the latter.
3170 locations->SetOut(Location::RegisterLocation(R1));
3171 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003172 break;
3173 }
3174 case Primitive::kPrimLong: {
3175 InvokeRuntimeCallingConvention calling_convention;
3176 locations->SetInAt(0, Location::RegisterPairLocation(
3177 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3178 locations->SetInAt(1, Location::RegisterPairLocation(
3179 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3180 // The runtime helper puts the output in R2,R3.
3181 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3182 break;
3183 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003184 case Primitive::kPrimFloat: {
3185 InvokeRuntimeCallingConvention calling_convention;
3186 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3187 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3188 locations->SetOut(Location::FpuRegisterLocation(S0));
3189 break;
3190 }
3191
Calin Juravlebacfec32014-11-14 15:54:36 +00003192 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003193 InvokeRuntimeCallingConvention calling_convention;
3194 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3195 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3196 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3197 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3198 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003199 break;
3200 }
3201
3202 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003203 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003204 }
3205}
3206
3207void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3208 LocationSummary* locations = rem->GetLocations();
3209 Location out = locations->Out();
3210 Location first = locations->InAt(0);
3211 Location second = locations->InAt(1);
3212
Calin Juravled2ec87d2014-12-08 14:24:46 +00003213 Primitive::Type type = rem->GetResultType();
3214 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003215 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003216 if (second.IsConstant()) {
3217 GenerateDivRemConstantIntegral(rem);
3218 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003219 Register reg1 = first.AsRegister<Register>();
3220 Register reg2 = second.AsRegister<Register>();
3221 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003222
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003223 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003224 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003225 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003226 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003227 } else {
3228 InvokeRuntimeCallingConvention calling_convention;
3229 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3230 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3231 DCHECK_EQ(R1, out.AsRegister<Register>());
3232
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003233 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003234 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003235 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003236 break;
3237 }
3238
3239 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003240 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003241 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003242 break;
3243 }
3244
Calin Juravled2ec87d2014-12-08 14:24:46 +00003245 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003246 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003247 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003248 break;
3249 }
3250
Calin Juravlebacfec32014-11-14 15:54:36 +00003251 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003252 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003253 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003254 break;
3255 }
3256
3257 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003258 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003259 }
3260}
3261
Calin Juravled0d48522014-11-04 16:40:20 +00003262void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003263 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003264 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003265}
3266
3267void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003268 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003269 codegen_->AddSlowPath(slow_path);
3270
3271 LocationSummary* locations = instruction->GetLocations();
3272 Location value = locations->InAt(0);
3273
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003275 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003276 case Primitive::kPrimByte:
3277 case Primitive::kPrimChar:
3278 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003279 case Primitive::kPrimInt: {
3280 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003281 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003282 } else {
3283 DCHECK(value.IsConstant()) << value;
3284 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3285 __ b(slow_path->GetEntryLabel());
3286 }
3287 }
3288 break;
3289 }
3290 case Primitive::kPrimLong: {
3291 if (value.IsRegisterPair()) {
3292 __ orrs(IP,
3293 value.AsRegisterPairLow<Register>(),
3294 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3295 __ b(slow_path->GetEntryLabel(), EQ);
3296 } else {
3297 DCHECK(value.IsConstant()) << value;
3298 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3299 __ b(slow_path->GetEntryLabel());
3300 }
3301 }
3302 break;
3303 default:
3304 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3305 }
3306 }
Calin Juravled0d48522014-11-04 16:40:20 +00003307}
3308
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003309void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3310 Register in = locations->InAt(0).AsRegister<Register>();
3311 Location rhs = locations->InAt(1);
3312 Register out = locations->Out().AsRegister<Register>();
3313
3314 if (rhs.IsConstant()) {
3315 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3316 // so map all rotations to a +ve. equivalent in that range.
3317 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3318 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3319 if (rot) {
3320 // Rotate, mapping left rotations to right equivalents if necessary.
3321 // (e.g. left by 2 bits == right by 30.)
3322 __ Ror(out, in, rot);
3323 } else if (out != in) {
3324 __ Mov(out, in);
3325 }
3326 } else {
3327 __ Ror(out, in, rhs.AsRegister<Register>());
3328 }
3329}
3330
3331// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3332// rotates by swapping input regs (effectively rotating by the first 32-bits of
3333// a larger rotation) or flipping direction (thus treating larger right/left
3334// rotations as sub-word sized rotations in the other direction) as appropriate.
3335void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3336 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3337 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3338 Location rhs = locations->InAt(1);
3339 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3340 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3341
3342 if (rhs.IsConstant()) {
3343 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3344 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003345 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003346 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3347 // logic below to a simple pair of binary orr.
3348 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3349 if (rot >= kArmBitsPerWord) {
3350 rot -= kArmBitsPerWord;
3351 std::swap(in_reg_hi, in_reg_lo);
3352 }
3353 // Rotate, or mov to out for zero or word size rotations.
3354 if (rot != 0u) {
3355 __ Lsr(out_reg_hi, in_reg_hi, rot);
3356 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3357 __ Lsr(out_reg_lo, in_reg_lo, rot);
3358 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3359 } else {
3360 __ Mov(out_reg_lo, in_reg_lo);
3361 __ Mov(out_reg_hi, in_reg_hi);
3362 }
3363 } else {
3364 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3365 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3366 Label end;
3367 Label shift_by_32_plus_shift_right;
3368
3369 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3370 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3371 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3372 __ b(&shift_by_32_plus_shift_right, CC);
3373
3374 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3375 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3376 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3377 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3378 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3379 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3380 __ Lsr(shift_left, in_reg_hi, shift_right);
3381 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3382 __ b(&end);
3383
3384 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3385 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3386 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3387 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3388 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3389 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3390 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3391 __ Lsl(shift_right, in_reg_hi, shift_left);
3392 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3393
3394 __ Bind(&end);
3395 }
3396}
Roland Levillain22c49222016-03-18 14:04:28 +00003397
3398void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003399 LocationSummary* locations =
3400 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3401 switch (ror->GetResultType()) {
3402 case Primitive::kPrimInt: {
3403 locations->SetInAt(0, Location::RequiresRegister());
3404 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3406 break;
3407 }
3408 case Primitive::kPrimLong: {
3409 locations->SetInAt(0, Location::RequiresRegister());
3410 if (ror->InputAt(1)->IsConstant()) {
3411 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3412 } else {
3413 locations->SetInAt(1, Location::RequiresRegister());
3414 locations->AddTemp(Location::RequiresRegister());
3415 locations->AddTemp(Location::RequiresRegister());
3416 }
3417 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3418 break;
3419 }
3420 default:
3421 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3422 }
3423}
3424
Roland Levillain22c49222016-03-18 14:04:28 +00003425void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003426 LocationSummary* locations = ror->GetLocations();
3427 Primitive::Type type = ror->GetResultType();
3428 switch (type) {
3429 case Primitive::kPrimInt: {
3430 HandleIntegerRotate(locations);
3431 break;
3432 }
3433 case Primitive::kPrimLong: {
3434 HandleLongRotate(locations);
3435 break;
3436 }
3437 default:
3438 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003439 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003440 }
3441}
3442
Calin Juravle9aec02f2014-11-18 23:06:35 +00003443void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3444 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3445
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003446 LocationSummary* locations =
3447 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003448
3449 switch (op->GetResultType()) {
3450 case Primitive::kPrimInt: {
3451 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003452 if (op->InputAt(1)->IsConstant()) {
3453 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3455 } else {
3456 locations->SetInAt(1, Location::RequiresRegister());
3457 // Make the output overlap, as it will be used to hold the masked
3458 // second input.
3459 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3460 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003461 break;
3462 }
3463 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003464 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003465 if (op->InputAt(1)->IsConstant()) {
3466 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3467 // For simplicity, use kOutputOverlap even though we only require that low registers
3468 // don't clash with high registers which the register allocator currently guarantees.
3469 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3470 } else {
3471 locations->SetInAt(1, Location::RequiresRegister());
3472 locations->AddTemp(Location::RequiresRegister());
3473 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3474 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003475 break;
3476 }
3477 default:
3478 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3479 }
3480}
3481
3482void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3483 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3484
3485 LocationSummary* locations = op->GetLocations();
3486 Location out = locations->Out();
3487 Location first = locations->InAt(0);
3488 Location second = locations->InAt(1);
3489
3490 Primitive::Type type = op->GetResultType();
3491 switch (type) {
3492 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003493 Register out_reg = out.AsRegister<Register>();
3494 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003495 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003497 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003498 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003499 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003500 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003501 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003502 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003503 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003504 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003505 }
3506 } else {
3507 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003508 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003509 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003510 __ Mov(out_reg, first_reg);
3511 } else if (op->IsShl()) {
3512 __ Lsl(out_reg, first_reg, shift_value);
3513 } else if (op->IsShr()) {
3514 __ Asr(out_reg, first_reg, shift_value);
3515 } else {
3516 __ Lsr(out_reg, first_reg, shift_value);
3517 }
3518 }
3519 break;
3520 }
3521 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003522 Register o_h = out.AsRegisterPairHigh<Register>();
3523 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003524
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003525 Register high = first.AsRegisterPairHigh<Register>();
3526 Register low = first.AsRegisterPairLow<Register>();
3527
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003528 if (second.IsRegister()) {
3529 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003530
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003531 Register second_reg = second.AsRegister<Register>();
3532
3533 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003534 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003535 // Shift the high part
3536 __ Lsl(o_h, high, o_l);
3537 // Shift the low part and `or` what overflew on the high part
3538 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3539 __ Lsr(temp, low, temp);
3540 __ orr(o_h, o_h, ShifterOperand(temp));
3541 // If the shift is > 32 bits, override the high part
3542 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3543 __ it(PL);
3544 __ Lsl(o_h, low, temp, PL);
3545 // Shift the low part
3546 __ Lsl(o_l, low, o_l);
3547 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003548 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003549 // Shift the low part
3550 __ Lsr(o_l, low, o_h);
3551 // Shift the high part and `or` what underflew on the low part
3552 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3553 __ Lsl(temp, high, temp);
3554 __ orr(o_l, o_l, ShifterOperand(temp));
3555 // If the shift is > 32 bits, override the low part
3556 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3557 __ it(PL);
3558 __ Asr(o_l, high, temp, PL);
3559 // Shift the high part
3560 __ Asr(o_h, high, o_h);
3561 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003562 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003563 // same as Shr except we use `Lsr`s and not `Asr`s
3564 __ Lsr(o_l, low, o_h);
3565 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3566 __ Lsl(temp, high, temp);
3567 __ orr(o_l, o_l, ShifterOperand(temp));
3568 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3569 __ it(PL);
3570 __ Lsr(o_l, high, temp, PL);
3571 __ Lsr(o_h, high, o_h);
3572 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003573 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003574 // Register allocator doesn't create partial overlap.
3575 DCHECK_NE(o_l, high);
3576 DCHECK_NE(o_h, low);
3577 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003578 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003579 if (shift_value > 32) {
3580 if (op->IsShl()) {
3581 __ Lsl(o_h, low, shift_value - 32);
3582 __ LoadImmediate(o_l, 0);
3583 } else if (op->IsShr()) {
3584 __ Asr(o_l, high, shift_value - 32);
3585 __ Asr(o_h, high, 31);
3586 } else {
3587 __ Lsr(o_l, high, shift_value - 32);
3588 __ LoadImmediate(o_h, 0);
3589 }
3590 } else if (shift_value == 32) {
3591 if (op->IsShl()) {
3592 __ mov(o_h, ShifterOperand(low));
3593 __ LoadImmediate(o_l, 0);
3594 } else if (op->IsShr()) {
3595 __ mov(o_l, ShifterOperand(high));
3596 __ Asr(o_h, high, 31);
3597 } else {
3598 __ mov(o_l, ShifterOperand(high));
3599 __ LoadImmediate(o_h, 0);
3600 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003601 } else if (shift_value == 1) {
3602 if (op->IsShl()) {
3603 __ Lsls(o_l, low, 1);
3604 __ adc(o_h, high, ShifterOperand(high));
3605 } else if (op->IsShr()) {
3606 __ Asrs(o_h, high, 1);
3607 __ Rrx(o_l, low);
3608 } else {
3609 __ Lsrs(o_h, high, 1);
3610 __ Rrx(o_l, low);
3611 }
3612 } else {
3613 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003614 if (op->IsShl()) {
3615 __ Lsl(o_h, high, shift_value);
3616 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3617 __ Lsl(o_l, low, shift_value);
3618 } else if (op->IsShr()) {
3619 __ Lsr(o_l, low, shift_value);
3620 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3621 __ Asr(o_h, high, shift_value);
3622 } else {
3623 __ Lsr(o_l, low, shift_value);
3624 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3625 __ Lsr(o_h, high, shift_value);
3626 }
3627 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003628 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003629 break;
3630 }
3631 default:
3632 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003633 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003634 }
3635}
3636
3637void LocationsBuilderARM::VisitShl(HShl* shl) {
3638 HandleShift(shl);
3639}
3640
3641void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3642 HandleShift(shl);
3643}
3644
3645void LocationsBuilderARM::VisitShr(HShr* shr) {
3646 HandleShift(shr);
3647}
3648
3649void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3650 HandleShift(shr);
3651}
3652
3653void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3654 HandleShift(ushr);
3655}
3656
3657void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3658 HandleShift(ushr);
3659}
3660
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003661void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003662 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003664 if (instruction->IsStringAlloc()) {
3665 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3666 } else {
3667 InvokeRuntimeCallingConvention calling_convention;
3668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3669 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3670 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003671 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003672}
3673
3674void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003675 // Note: if heap poisoning is enabled, the entry point takes cares
3676 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003677 if (instruction->IsStringAlloc()) {
3678 // String is allocated through StringFactory. Call NewEmptyString entry point.
3679 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003680 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003681 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3682 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3683 __ blx(LR);
3684 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3685 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003686 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003687 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3688 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003689}
3690
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003691void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3692 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003694 InvokeRuntimeCallingConvention calling_convention;
3695 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003696 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003697 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003698 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003699}
3700
3701void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3702 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003703 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003704 // Note: if heap poisoning is enabled, the entry point takes cares
3705 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003706 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003707 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003708}
3709
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003710void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003711 LocationSummary* locations =
3712 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003713 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3714 if (location.IsStackSlot()) {
3715 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3716 } else if (location.IsDoubleStackSlot()) {
3717 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003718 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003719 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003720}
3721
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003722void InstructionCodeGeneratorARM::VisitParameterValue(
3723 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003724 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003725}
3726
3727void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3728 LocationSummary* locations =
3729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3730 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3731}
3732
3733void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3734 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003735}
3736
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003737void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003738 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003739 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003740 locations->SetInAt(0, Location::RequiresRegister());
3741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003742}
3743
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003744void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3745 LocationSummary* locations = not_->GetLocations();
3746 Location out = locations->Out();
3747 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003748 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003749 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003751 break;
3752
3753 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003754 __ mvn(out.AsRegisterPairLow<Register>(),
3755 ShifterOperand(in.AsRegisterPairLow<Register>()));
3756 __ mvn(out.AsRegisterPairHigh<Register>(),
3757 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003758 break;
3759
3760 default:
3761 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3762 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003763}
3764
David Brazdil66d126e2015-04-03 16:02:44 +01003765void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3768 locations->SetInAt(0, Location::RequiresRegister());
3769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3770}
3771
3772void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003773 LocationSummary* locations = bool_not->GetLocations();
3774 Location out = locations->Out();
3775 Location in = locations->InAt(0);
3776 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3777}
3778
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003779void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003780 LocationSummary* locations =
3781 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003782 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003783 case Primitive::kPrimBoolean:
3784 case Primitive::kPrimByte:
3785 case Primitive::kPrimShort:
3786 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003787 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003788 case Primitive::kPrimLong: {
3789 locations->SetInAt(0, Location::RequiresRegister());
3790 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003791 // Output overlaps because it is written before doing the low comparison.
3792 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003793 break;
3794 }
3795 case Primitive::kPrimFloat:
3796 case Primitive::kPrimDouble: {
3797 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003798 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003799 locations->SetOut(Location::RequiresRegister());
3800 break;
3801 }
3802 default:
3803 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3804 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003805}
3806
3807void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003808 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003810 Location left = locations->InAt(0);
3811 Location right = locations->InAt(1);
3812
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003813 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003814 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003815 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003816 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003817 case Primitive::kPrimBoolean:
3818 case Primitive::kPrimByte:
3819 case Primitive::kPrimShort:
3820 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003821 case Primitive::kPrimInt: {
3822 __ LoadImmediate(out, 0);
3823 __ cmp(left.AsRegister<Register>(),
3824 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3825 less_cond = LT;
3826 break;
3827 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003828 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003829 __ cmp(left.AsRegisterPairHigh<Register>(),
3830 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003831 __ b(&less, LT);
3832 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003833 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003834 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003835 __ cmp(left.AsRegisterPairLow<Register>(),
3836 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003837 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003838 break;
3839 }
3840 case Primitive::kPrimFloat:
3841 case Primitive::kPrimDouble: {
3842 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003843 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003844 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003845 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003846 break;
3847 }
3848 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003849 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003850 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003851 }
Aart Bika19616e2016-02-01 18:57:58 -08003852
Calin Juravleddb7df22014-11-25 20:56:51 +00003853 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003854 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003855
3856 __ Bind(&greater);
3857 __ LoadImmediate(out, 1);
3858 __ b(&done);
3859
3860 __ Bind(&less);
3861 __ LoadImmediate(out, -1);
3862
3863 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003864}
3865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003866void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003867 LocationSummary* locations =
3868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003869 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003870 locations->SetInAt(i, Location::Any());
3871 }
3872 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003873}
3874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003875void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003876 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003877}
3878
Roland Levillainc9285912015-12-18 10:38:42 +00003879void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3880 // TODO (ported from quick): revisit ARM barrier kinds.
3881 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003882 switch (kind) {
3883 case MemBarrierKind::kAnyStore:
3884 case MemBarrierKind::kLoadAny:
3885 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003886 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003887 break;
3888 }
3889 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003890 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003891 break;
3892 }
3893 default:
3894 LOG(FATAL) << "Unexpected memory barrier " << kind;
3895 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003896 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003897}
3898
3899void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3900 uint32_t offset,
3901 Register out_lo,
3902 Register out_hi) {
3903 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003904 // Ensure `out_lo` is different from `addr`, so that loading
3905 // `offset` into `out_lo` does not clutter `addr`.
3906 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003907 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003908 __ add(IP, addr, ShifterOperand(out_lo));
3909 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003910 }
3911 __ ldrexd(out_lo, out_hi, addr);
3912}
3913
3914void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3915 uint32_t offset,
3916 Register value_lo,
3917 Register value_hi,
3918 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003919 Register temp2,
3920 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003921 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003922 if (offset != 0) {
3923 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003924 __ add(IP, addr, ShifterOperand(temp1));
3925 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003926 }
3927 __ Bind(&fail);
3928 // We need a load followed by store. (The address used in a STREX instruction must
3929 // be the same as the address in the most recently executed LDREX instruction.)
3930 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003932 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003933 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003934}
3935
3936void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3937 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3938
Nicolas Geoffray39468442014-09-02 15:17:15 +01003939 LocationSummary* locations =
3940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003941 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003942
Calin Juravle52c48962014-12-16 17:02:57 +00003943 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003944 if (Primitive::IsFloatingPointType(field_type)) {
3945 locations->SetInAt(1, Location::RequiresFpuRegister());
3946 } else {
3947 locations->SetInAt(1, Location::RequiresRegister());
3948 }
3949
Calin Juravle52c48962014-12-16 17:02:57 +00003950 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003951 bool generate_volatile = field_info.IsVolatile()
3952 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003953 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003954 bool needs_write_barrier =
3955 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003956 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003957 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003958 if (needs_write_barrier) {
3959 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003960 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003961 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003962 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003963 // - registers need to be consecutive
3964 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003965 // We don't test for ARM yet, and the assertion makes sure that we
3966 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003967 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3968
3969 locations->AddTemp(Location::RequiresRegister());
3970 locations->AddTemp(Location::RequiresRegister());
3971 if (field_type == Primitive::kPrimDouble) {
3972 // For doubles we need two more registers to copy the value.
3973 locations->AddTemp(Location::RegisterLocation(R2));
3974 locations->AddTemp(Location::RegisterLocation(R3));
3975 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003976 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003977}
3978
Calin Juravle52c48962014-12-16 17:02:57 +00003979void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003980 const FieldInfo& field_info,
3981 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003982 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003985 Register base = locations->InAt(0).AsRegister<Register>();
3986 Location value = locations->InAt(1);
3987
3988 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003989 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003990 Primitive::Type field_type = field_info.GetFieldType();
3991 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003992 bool needs_write_barrier =
3993 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003994
3995 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003996 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003997 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003998
3999 switch (field_type) {
4000 case Primitive::kPrimBoolean:
4001 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004002 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004003 break;
4004 }
4005
4006 case Primitive::kPrimShort:
4007 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004008 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004009 break;
4010 }
4011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004013 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004014 if (kPoisonHeapReferences && needs_write_barrier) {
4015 // Note that in the case where `value` is a null reference,
4016 // we do not enter this block, as a null reference does not
4017 // need poisoning.
4018 DCHECK_EQ(field_type, Primitive::kPrimNot);
4019 Register temp = locations->GetTemp(0).AsRegister<Register>();
4020 __ Mov(temp, value.AsRegister<Register>());
4021 __ PoisonHeapReference(temp);
4022 __ StoreToOffset(kStoreWord, temp, base, offset);
4023 } else {
4024 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4025 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004026 break;
4027 }
4028
4029 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004030 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004031 GenerateWideAtomicStore(base, offset,
4032 value.AsRegisterPairLow<Register>(),
4033 value.AsRegisterPairHigh<Register>(),
4034 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004035 locations->GetTemp(1).AsRegister<Register>(),
4036 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004037 } else {
4038 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004039 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004040 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004041 break;
4042 }
4043
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004044 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004045 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004046 break;
4047 }
4048
4049 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004050 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004051 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004052 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4053 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4054
4055 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4056
4057 GenerateWideAtomicStore(base, offset,
4058 value_reg_lo,
4059 value_reg_hi,
4060 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004061 locations->GetTemp(3).AsRegister<Register>(),
4062 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004063 } else {
4064 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004065 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004066 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004067 break;
4068 }
4069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004070 case Primitive::kPrimVoid:
4071 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004072 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004073 }
Calin Juravle52c48962014-12-16 17:02:57 +00004074
Calin Juravle77520bc2015-01-12 18:45:46 +00004075 // Longs and doubles are handled in the switch.
4076 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4077 codegen_->MaybeRecordImplicitNullCheck(instruction);
4078 }
4079
4080 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4081 Register temp = locations->GetTemp(0).AsRegister<Register>();
4082 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004083 codegen_->MarkGCCard(
4084 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004085 }
4086
Calin Juravle52c48962014-12-16 17:02:57 +00004087 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004088 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004089 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004090}
4091
Calin Juravle52c48962014-12-16 17:02:57 +00004092void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4093 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004094
4095 bool object_field_get_with_read_barrier =
4096 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004097 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004098 new (GetGraph()->GetArena()) LocationSummary(instruction,
4099 object_field_get_with_read_barrier ?
4100 LocationSummary::kCallOnSlowPath :
4101 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004102 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004103 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004104 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004105 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004106
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004107 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004108 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004109 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004110 // The output overlaps in case of volatile long: we don't want the
4111 // code generated by GenerateWideAtomicLoad to overwrite the
4112 // object's location. Likewise, in the case of an object field get
4113 // with read barriers enabled, we do not want the load to overwrite
4114 // the object's location, as we need it to emit the read barrier.
4115 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4116 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004117
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004118 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4119 locations->SetOut(Location::RequiresFpuRegister());
4120 } else {
4121 locations->SetOut(Location::RequiresRegister(),
4122 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4123 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004124 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004125 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004126 // - registers need to be consecutive
4127 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004128 // We don't test for ARM yet, and the assertion makes sure that we
4129 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004130 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4131 locations->AddTemp(Location::RequiresRegister());
4132 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004133 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4134 // We need a temporary register for the read barrier marking slow
4135 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4136 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004137 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004138}
4139
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004140Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4141 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4142 << input->GetType();
4143 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4144 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4145 return Location::ConstantLocation(input->AsConstant());
4146 } else {
4147 return Location::RequiresFpuRegister();
4148 }
4149}
4150
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004151Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4152 Opcode opcode) {
4153 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4154 if (constant->IsConstant() &&
4155 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4156 return Location::ConstantLocation(constant->AsConstant());
4157 }
4158 return Location::RequiresRegister();
4159}
4160
4161bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4162 Opcode opcode) {
4163 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4164 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004165 Opcode high_opcode = opcode;
4166 SetCc low_set_cc = kCcDontCare;
4167 switch (opcode) {
4168 case SUB:
4169 // Flip the operation to an ADD.
4170 value = -value;
4171 opcode = ADD;
4172 FALLTHROUGH_INTENDED;
4173 case ADD:
4174 if (Low32Bits(value) == 0u) {
4175 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4176 }
4177 high_opcode = ADC;
4178 low_set_cc = kCcSet;
4179 break;
4180 default:
4181 break;
4182 }
4183 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4184 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004185 } else {
4186 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4187 }
4188}
4189
Vladimir Marko59751a72016-08-05 14:37:27 +01004190bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4191 Opcode opcode,
4192 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004193 ShifterOperand so;
4194 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004195 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004196 return true;
4197 }
4198 Opcode neg_opcode = kNoOperand;
4199 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004200 case AND: neg_opcode = BIC; value = ~value; break;
4201 case ORR: neg_opcode = ORN; value = ~value; break;
4202 case ADD: neg_opcode = SUB; value = -value; break;
4203 case ADC: neg_opcode = SBC; value = ~value; break;
4204 case SUB: neg_opcode = ADD; value = -value; break;
4205 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004206 default:
4207 return false;
4208 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004209 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004210}
4211
Calin Juravle52c48962014-12-16 17:02:57 +00004212void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4213 const FieldInfo& field_info) {
4214 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004215
Calin Juravle52c48962014-12-16 17:02:57 +00004216 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004217 Location base_loc = locations->InAt(0);
4218 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004219 Location out = locations->Out();
4220 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004221 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004222 Primitive::Type field_type = field_info.GetFieldType();
4223 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4224
4225 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004226 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004227 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004229
Roland Levillainc9285912015-12-18 10:38:42 +00004230 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004231 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004232 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233
Roland Levillainc9285912015-12-18 10:38:42 +00004234 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004235 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004237
Roland Levillainc9285912015-12-18 10:38:42 +00004238 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004239 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004240 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241
4242 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004243 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004245
4246 case Primitive::kPrimNot: {
4247 // /* HeapReference<Object> */ out = *(base + offset)
4248 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4249 Location temp_loc = locations->GetTemp(0);
4250 // Note that a potential implicit null check is handled in this
4251 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4252 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4253 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4254 if (is_volatile) {
4255 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4256 }
4257 } else {
4258 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4259 codegen_->MaybeRecordImplicitNullCheck(instruction);
4260 if (is_volatile) {
4261 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4262 }
4263 // If read barriers are enabled, emit read barriers other than
4264 // Baker's using a slow path (and also unpoison the loaded
4265 // reference, if heap poisoning is enabled).
4266 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4267 }
4268 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 }
4270
Roland Levillainc9285912015-12-18 10:38:42 +00004271 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004272 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004273 GenerateWideAtomicLoad(base, offset,
4274 out.AsRegisterPairLow<Register>(),
4275 out.AsRegisterPairHigh<Register>());
4276 } else {
4277 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4278 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280
Roland Levillainc9285912015-12-18 10:38:42 +00004281 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004282 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004283 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004284
4285 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004286 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004287 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004288 Register lo = locations->GetTemp(0).AsRegister<Register>();
4289 Register hi = locations->GetTemp(1).AsRegister<Register>();
4290 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004291 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004292 __ vmovdrr(out_reg, lo, hi);
4293 } else {
4294 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004296 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004297 break;
4298 }
4299
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004301 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004302 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 }
Calin Juravle52c48962014-12-16 17:02:57 +00004304
Roland Levillainc9285912015-12-18 10:38:42 +00004305 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4306 // Potential implicit null checks, in the case of reference or
4307 // double fields, are handled in the previous switch statement.
4308 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004309 codegen_->MaybeRecordImplicitNullCheck(instruction);
4310 }
4311
Calin Juravle52c48962014-12-16 17:02:57 +00004312 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004313 if (field_type == Primitive::kPrimNot) {
4314 // Memory barriers, in the case of references, are also handled
4315 // in the previous switch statement.
4316 } else {
4317 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4318 }
Roland Levillain4d027112015-07-01 15:41:14 +01004319 }
Calin Juravle52c48962014-12-16 17:02:57 +00004320}
4321
4322void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4323 HandleFieldSet(instruction, instruction->GetFieldInfo());
4324}
4325
4326void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004327 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004328}
4329
4330void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4331 HandleFieldGet(instruction, instruction->GetFieldInfo());
4332}
4333
4334void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4335 HandleFieldGet(instruction, instruction->GetFieldInfo());
4336}
4337
4338void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4339 HandleFieldGet(instruction, instruction->GetFieldInfo());
4340}
4341
4342void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4343 HandleFieldGet(instruction, instruction->GetFieldInfo());
4344}
4345
4346void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4347 HandleFieldSet(instruction, instruction->GetFieldInfo());
4348}
4349
4350void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004351 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352}
4353
Calin Juravlee460d1d2015-09-29 04:52:17 +01004354void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4355 HUnresolvedInstanceFieldGet* instruction) {
4356 FieldAccessCallingConventionARM calling_convention;
4357 codegen_->CreateUnresolvedFieldLocationSummary(
4358 instruction, instruction->GetFieldType(), calling_convention);
4359}
4360
4361void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4362 HUnresolvedInstanceFieldGet* instruction) {
4363 FieldAccessCallingConventionARM calling_convention;
4364 codegen_->GenerateUnresolvedFieldAccess(instruction,
4365 instruction->GetFieldType(),
4366 instruction->GetFieldIndex(),
4367 instruction->GetDexPc(),
4368 calling_convention);
4369}
4370
4371void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4372 HUnresolvedInstanceFieldSet* instruction) {
4373 FieldAccessCallingConventionARM calling_convention;
4374 codegen_->CreateUnresolvedFieldLocationSummary(
4375 instruction, instruction->GetFieldType(), calling_convention);
4376}
4377
4378void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4379 HUnresolvedInstanceFieldSet* instruction) {
4380 FieldAccessCallingConventionARM calling_convention;
4381 codegen_->GenerateUnresolvedFieldAccess(instruction,
4382 instruction->GetFieldType(),
4383 instruction->GetFieldIndex(),
4384 instruction->GetDexPc(),
4385 calling_convention);
4386}
4387
4388void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4389 HUnresolvedStaticFieldGet* instruction) {
4390 FieldAccessCallingConventionARM calling_convention;
4391 codegen_->CreateUnresolvedFieldLocationSummary(
4392 instruction, instruction->GetFieldType(), calling_convention);
4393}
4394
4395void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4396 HUnresolvedStaticFieldGet* instruction) {
4397 FieldAccessCallingConventionARM calling_convention;
4398 codegen_->GenerateUnresolvedFieldAccess(instruction,
4399 instruction->GetFieldType(),
4400 instruction->GetFieldIndex(),
4401 instruction->GetDexPc(),
4402 calling_convention);
4403}
4404
4405void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4406 HUnresolvedStaticFieldSet* instruction) {
4407 FieldAccessCallingConventionARM calling_convention;
4408 codegen_->CreateUnresolvedFieldLocationSummary(
4409 instruction, instruction->GetFieldType(), calling_convention);
4410}
4411
4412void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4413 HUnresolvedStaticFieldSet* instruction) {
4414 FieldAccessCallingConventionARM calling_convention;
4415 codegen_->GenerateUnresolvedFieldAccess(instruction,
4416 instruction->GetFieldType(),
4417 instruction->GetFieldIndex(),
4418 instruction->GetDexPc(),
4419 calling_convention);
4420}
4421
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004423 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4424 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425}
4426
Calin Juravle2ae48182016-03-16 14:05:09 +00004427void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4428 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004429 return;
4430 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004431 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004432
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004433 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004434 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004435}
4436
Calin Juravle2ae48182016-03-16 14:05:09 +00004437void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004438 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004439 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440
4441 LocationSummary* locations = instruction->GetLocations();
4442 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004444 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445}
4446
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004447void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004448 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004449}
4450
Artem Serov6c916792016-07-11 14:02:34 +01004451static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4452 switch (type) {
4453 case Primitive::kPrimNot:
4454 return kLoadWord;
4455 case Primitive::kPrimBoolean:
4456 return kLoadUnsignedByte;
4457 case Primitive::kPrimByte:
4458 return kLoadSignedByte;
4459 case Primitive::kPrimChar:
4460 return kLoadUnsignedHalfword;
4461 case Primitive::kPrimShort:
4462 return kLoadSignedHalfword;
4463 case Primitive::kPrimInt:
4464 return kLoadWord;
4465 case Primitive::kPrimLong:
4466 return kLoadWordPair;
4467 case Primitive::kPrimFloat:
4468 return kLoadSWord;
4469 case Primitive::kPrimDouble:
4470 return kLoadDWord;
4471 default:
4472 LOG(FATAL) << "Unreachable type " << type;
4473 UNREACHABLE();
4474 }
4475}
4476
4477static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4478 switch (type) {
4479 case Primitive::kPrimNot:
4480 return kStoreWord;
4481 case Primitive::kPrimBoolean:
4482 case Primitive::kPrimByte:
4483 return kStoreByte;
4484 case Primitive::kPrimChar:
4485 case Primitive::kPrimShort:
4486 return kStoreHalfword;
4487 case Primitive::kPrimInt:
4488 return kStoreWord;
4489 case Primitive::kPrimLong:
4490 return kStoreWordPair;
4491 case Primitive::kPrimFloat:
4492 return kStoreSWord;
4493 case Primitive::kPrimDouble:
4494 return kStoreDWord;
4495 default:
4496 LOG(FATAL) << "Unreachable type " << type;
4497 UNREACHABLE();
4498 }
4499}
4500
4501void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4502 Location out_loc,
4503 Register base,
4504 Register reg_offset,
4505 Condition cond) {
4506 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4507 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4508
4509 switch (type) {
4510 case Primitive::kPrimByte:
4511 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4512 break;
4513 case Primitive::kPrimBoolean:
4514 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4515 break;
4516 case Primitive::kPrimShort:
4517 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4518 break;
4519 case Primitive::kPrimChar:
4520 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4521 break;
4522 case Primitive::kPrimNot:
4523 case Primitive::kPrimInt:
4524 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4525 break;
4526 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4527 case Primitive::kPrimLong:
4528 case Primitive::kPrimFloat:
4529 case Primitive::kPrimDouble:
4530 default:
4531 LOG(FATAL) << "Unreachable type " << type;
4532 UNREACHABLE();
4533 }
4534}
4535
4536void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4537 Location loc,
4538 Register base,
4539 Register reg_offset,
4540 Condition cond) {
4541 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4542 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4543
4544 switch (type) {
4545 case Primitive::kPrimByte:
4546 case Primitive::kPrimBoolean:
4547 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4548 break;
4549 case Primitive::kPrimShort:
4550 case Primitive::kPrimChar:
4551 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4552 break;
4553 case Primitive::kPrimNot:
4554 case Primitive::kPrimInt:
4555 __ str(loc.AsRegister<Register>(), mem_address, cond);
4556 break;
4557 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4558 case Primitive::kPrimLong:
4559 case Primitive::kPrimFloat:
4560 case Primitive::kPrimDouble:
4561 default:
4562 LOG(FATAL) << "Unreachable type " << type;
4563 UNREACHABLE();
4564 }
4565}
4566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004568 bool object_array_get_with_read_barrier =
4569 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004570 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004571 new (GetGraph()->GetArena()) LocationSummary(instruction,
4572 object_array_get_with_read_barrier ?
4573 LocationSummary::kCallOnSlowPath :
4574 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004575 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004576 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004577 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004578 locations->SetInAt(0, Location::RequiresRegister());
4579 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004580 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4582 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004583 // The output overlaps in the case of an object array get with
4584 // read barriers enabled: we do not want the move to overwrite the
4585 // array's location, as we need it to emit the read barrier.
4586 locations->SetOut(
4587 Location::RequiresRegister(),
4588 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004589 }
Roland Levillainc9285912015-12-18 10:38:42 +00004590 // We need a temporary register for the read barrier marking slow
4591 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4592 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4593 locations->AddTemp(Location::RequiresRegister());
4594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595}
4596
4597void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4598 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004599 Location obj_loc = locations->InAt(0);
4600 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004602 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004603 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004604 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004605 HInstruction* array_instr = instruction->GetArray();
4606 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004607 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4608 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004609
Roland Levillain4d027112015-07-01 15:41:14 +01004610 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004611 case Primitive::kPrimBoolean:
4612 case Primitive::kPrimByte:
4613 case Primitive::kPrimShort:
4614 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004615 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004617 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4618 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4619
4620 LoadOperandType load_type = GetLoadOperandType(type);
4621 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004623 Register temp = IP;
4624
4625 if (has_intermediate_address) {
4626 // We do not need to compute the intermediate address from the array: the
4627 // input instruction has done it already. See the comment in
4628 // `TryExtractArrayAccessAddress()`.
4629 if (kIsDebugBuild) {
4630 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4631 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4632 }
4633 temp = obj;
4634 } else {
4635 __ add(temp, obj, ShifterOperand(data_offset));
4636 }
4637 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 }
4639 break;
4640 }
4641
Roland Levillainc9285912015-12-18 10:38:42 +00004642 case Primitive::kPrimNot: {
4643 static_assert(
4644 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4645 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004646 // /* HeapReference<Object> */ out =
4647 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4648 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4649 Location temp = locations->GetTemp(0);
4650 // Note that a potential implicit null check is handled in this
4651 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4652 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4653 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4654 } else {
4655 Register out = out_loc.AsRegister<Register>();
4656 if (index.IsConstant()) {
4657 size_t offset =
4658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4659 __ LoadFromOffset(kLoadWord, out, obj, offset);
4660 codegen_->MaybeRecordImplicitNullCheck(instruction);
4661 // If read barriers are enabled, emit read barriers other than
4662 // Baker's using a slow path (and also unpoison the loaded
4663 // reference, if heap poisoning is enabled).
4664 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4665 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004666 Register temp = IP;
4667
4668 if (has_intermediate_address) {
4669 // We do not need to compute the intermediate address from the array: the
4670 // input instruction has done it already. See the comment in
4671 // `TryExtractArrayAccessAddress()`.
4672 if (kIsDebugBuild) {
4673 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4674 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4675 }
4676 temp = obj;
4677 } else {
4678 __ add(temp, obj, ShifterOperand(data_offset));
4679 }
4680 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004681
Roland Levillainc9285912015-12-18 10:38:42 +00004682 codegen_->MaybeRecordImplicitNullCheck(instruction);
4683 // If read barriers are enabled, emit read barriers other than
4684 // Baker's using a slow path (and also unpoison the loaded
4685 // reference, if heap poisoning is enabled).
4686 codegen_->MaybeGenerateReadBarrierSlow(
4687 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4688 }
4689 }
4690 break;
4691 }
4692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004693 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004695 size_t offset =
4696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004697 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004699 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004700 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701 }
4702 break;
4703 }
4704
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004705 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004706 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004707 if (index.IsConstant()) {
4708 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004709 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004710 } else {
4711 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004712 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004713 }
4714 break;
4715 }
4716
4717 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004718 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004719 if (index.IsConstant()) {
4720 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004721 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004722 } else {
4723 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004724 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004725 }
4726 break;
4727 }
4728
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004730 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004731 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 }
Roland Levillain4d027112015-07-01 15:41:14 +01004733
4734 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004735 // Potential implicit null checks, in the case of reference
4736 // arrays, are handled in the previous switch statement.
4737 } else {
4738 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740}
4741
4742void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004743 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004744
4745 bool needs_write_barrier =
4746 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004747 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004748
Nicolas Geoffray39468442014-09-02 15:17:15 +01004749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004750 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004751 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004752 LocationSummary::kCallOnSlowPath :
4753 LocationSummary::kNoCall);
4754
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004755 locations->SetInAt(0, Location::RequiresRegister());
4756 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4757 if (Primitive::IsFloatingPointType(value_type)) {
4758 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 locations->SetInAt(2, Location::RequiresRegister());
4761 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004762 if (needs_write_barrier) {
4763 // Temporary registers for the write barrier.
4764 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004765 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767}
4768
4769void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4770 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004771 Location array_loc = locations->InAt(0);
4772 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004774 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004775 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004776 bool needs_write_barrier =
4777 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004778 uint32_t data_offset =
4779 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4780 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004781 HInstruction* array_instr = instruction->GetArray();
4782 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004783 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4784 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785
4786 switch (value_type) {
4787 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004788 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004789 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004790 case Primitive::kPrimChar:
4791 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004793 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4794 uint32_t full_offset =
4795 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4796 StoreOperandType store_type = GetStoreOperandType(value_type);
4797 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004799 Register temp = IP;
4800
4801 if (has_intermediate_address) {
4802 // We do not need to compute the intermediate address from the array: the
4803 // input instruction has done it already. See the comment in
4804 // `TryExtractArrayAccessAddress()`.
4805 if (kIsDebugBuild) {
4806 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4807 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4808 }
4809 temp = array;
4810 } else {
4811 __ add(temp, array, ShifterOperand(data_offset));
4812 }
Artem Serov6c916792016-07-11 14:02:34 +01004813 codegen_->StoreToShiftedRegOffset(value_type,
4814 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004815 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004816 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817 }
4818 break;
4819 }
4820
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004822 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004823 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4824 // See the comment in instruction_simplifier_shared.cc.
4825 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004826
4827 if (instruction->InputAt(2)->IsNullConstant()) {
4828 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004829 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004830 size_t offset =
4831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004832 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004833 } else {
4834 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004835 __ add(IP, array, ShifterOperand(data_offset));
4836 codegen_->StoreToShiftedRegOffset(value_type,
4837 value_loc,
4838 IP,
4839 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004840 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004842 DCHECK(!needs_write_barrier);
4843 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004845 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846
4847 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004848 Location temp1_loc = locations->GetTemp(0);
4849 Register temp1 = temp1_loc.AsRegister<Register>();
4850 Location temp2_loc = locations->GetTemp(1);
4851 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004852 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4853 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4854 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4855 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01004856 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857
Roland Levillain3b359c72015-11-17 19:35:12 +00004858 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004859 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4860 codegen_->AddSlowPath(slow_path);
4861 if (instruction->GetValueCanBeNull()) {
4862 Label non_zero;
4863 __ CompareAndBranchIfNonZero(value, &non_zero);
4864 if (index.IsConstant()) {
4865 size_t offset =
4866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4867 __ StoreToOffset(kStoreWord, value, array, offset);
4868 } else {
4869 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004870 __ add(IP, array, ShifterOperand(data_offset));
4871 codegen_->StoreToShiftedRegOffset(value_type,
4872 value_loc,
4873 IP,
4874 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004875 }
4876 codegen_->MaybeRecordImplicitNullCheck(instruction);
4877 __ b(&done);
4878 __ Bind(&non_zero);
4879 }
4880
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004881 // Note that when read barriers are enabled, the type checks
4882 // are performed without read barriers. This is fine, even in
4883 // the case where a class object is in the from-space after
4884 // the flip, as a comparison involving such a type would not
4885 // produce a false positive; it may of course produce a false
4886 // negative, in which case we would take the ArraySet slow
4887 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004888
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004889 // /* HeapReference<Class> */ temp1 = array->klass_
4890 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4891 codegen_->MaybeRecordImplicitNullCheck(instruction);
4892 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004893
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004894 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4895 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4896 // /* HeapReference<Class> */ temp2 = value->klass_
4897 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4898 // If heap poisoning is enabled, no need to unpoison `temp1`
4899 // nor `temp2`, as we are comparing two poisoned references.
4900 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004901
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004902 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4903 Label do_put;
4904 __ b(&do_put, EQ);
4905 // If heap poisoning is enabled, the `temp1` reference has
4906 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004907 __ MaybeUnpoisonHeapReference(temp1);
4908
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004909 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4910 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4911 // If heap poisoning is enabled, no need to unpoison
4912 // `temp1`, as we are comparing against null below.
4913 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4914 __ Bind(&do_put);
4915 } else {
4916 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004917 }
4918 }
4919
Artem Serov6c916792016-07-11 14:02:34 +01004920 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004921 if (kPoisonHeapReferences) {
4922 // Note that in the case where `value` is a null reference,
4923 // we do not enter this block, as a null reference does not
4924 // need poisoning.
4925 DCHECK_EQ(value_type, Primitive::kPrimNot);
4926 __ Mov(temp1, value);
4927 __ PoisonHeapReference(temp1);
4928 source = temp1;
4929 }
4930
4931 if (index.IsConstant()) {
4932 size_t offset =
4933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4934 __ StoreToOffset(kStoreWord, source, array, offset);
4935 } else {
4936 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004937
4938 __ add(IP, array, ShifterOperand(data_offset));
4939 codegen_->StoreToShiftedRegOffset(value_type,
4940 Location::RegisterLocation(source),
4941 IP,
4942 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004943 }
4944
Roland Levillain3b359c72015-11-17 19:35:12 +00004945 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004946 codegen_->MaybeRecordImplicitNullCheck(instruction);
4947 }
4948
4949 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4950
4951 if (done.IsLinked()) {
4952 __ Bind(&done);
4953 }
4954
4955 if (slow_path != nullptr) {
4956 __ Bind(slow_path->GetExitLabel());
4957 }
4958
4959 break;
4960 }
4961
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004962 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004963 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004964 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004965 size_t offset =
4966 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004967 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004969 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004970 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971 }
4972 break;
4973 }
4974
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004975 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004976 Location value = locations->InAt(2);
4977 DCHECK(value.IsFpuRegister());
4978 if (index.IsConstant()) {
4979 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004980 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004981 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004982 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004983 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4984 }
4985 break;
4986 }
4987
4988 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004989 Location value = locations->InAt(2);
4990 DCHECK(value.IsFpuRegisterPair());
4991 if (index.IsConstant()) {
4992 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004993 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004994 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004995 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004996 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4997 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004998
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004999 break;
5000 }
5001
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005003 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005004 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005005 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005006
Roland Levillain80e67092016-01-08 16:04:55 +00005007 // Objects are handled in the switch.
5008 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 codegen_->MaybeRecordImplicitNullCheck(instruction);
5010 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011}
5012
5013void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005014 LocationSummary* locations =
5015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005016 locations->SetInAt(0, Location::RequiresRegister());
5017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018}
5019
5020void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5021 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005022 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005023 Register obj = locations->InAt(0).AsRegister<Register>();
5024 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005025 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027}
5028
Artem Serov328429f2016-07-06 16:23:04 +01005029void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005030 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5031 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005032 LocationSummary* locations =
5033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5034
5035 locations->SetInAt(0, Location::RequiresRegister());
5036 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5038}
5039
5040void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5041 LocationSummary* locations = instruction->GetLocations();
5042 Location out = locations->Out();
5043 Location first = locations->InAt(0);
5044 Location second = locations->InAt(1);
5045
Roland Levillain4a3aa572016-08-15 13:17:06 +00005046 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5047 DCHECK(!kEmitCompilerReadBarrier);
5048
Artem Serov328429f2016-07-06 16:23:04 +01005049 if (second.IsRegister()) {
5050 __ add(out.AsRegister<Register>(),
5051 first.AsRegister<Register>(),
5052 ShifterOperand(second.AsRegister<Register>()));
5053 } else {
5054 __ AddConstant(out.AsRegister<Register>(),
5055 first.AsRegister<Register>(),
5056 second.GetConstant()->AsIntConstant()->GetValue());
5057 }
5058}
5059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005061 RegisterSet caller_saves = RegisterSet::Empty();
5062 InvokeRuntimeCallingConvention calling_convention;
5063 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5064 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5065 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 locations->SetInAt(0, Location::RequiresRegister());
5067 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068}
5069
5070void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5071 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005072 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005073 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 codegen_->AddSlowPath(slow_path);
5075
Roland Levillain271ab9c2014-11-27 15:23:57 +00005076 Register index = locations->InAt(0).AsRegister<Register>();
5077 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078
5079 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005080 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081}
5082
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005083void CodeGeneratorARM::MarkGCCard(Register temp,
5084 Register card,
5085 Register object,
5086 Register value,
5087 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005088 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005089 if (can_be_null) {
5090 __ CompareAndBranchIfZero(value, &is_null);
5091 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005092 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5094 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005095 if (can_be_null) {
5096 __ Bind(&is_null);
5097 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098}
5099
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005100void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005101 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005102}
5103
5104void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005105 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5106}
5107
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005108void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005109 LocationSummary* locations =
5110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005111 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005112}
5113
5114void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005115 HBasicBlock* block = instruction->GetBlock();
5116 if (block->GetLoopInformation() != nullptr) {
5117 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5118 // The back edge will generate the suspend check.
5119 return;
5120 }
5121 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5122 // The goto will generate the suspend check.
5123 return;
5124 }
5125 GenerateSuspendCheck(instruction, nullptr);
5126}
5127
5128void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5129 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005130 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005131 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5132 if (slow_path == nullptr) {
5133 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5134 instruction->SetSlowPath(slow_path);
5135 codegen_->AddSlowPath(slow_path);
5136 if (successor != nullptr) {
5137 DCHECK(successor->IsLoopHeader());
5138 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5139 }
5140 } else {
5141 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5142 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005143
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005144 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005145 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005146 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005147 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005148 __ Bind(slow_path->GetReturnLabel());
5149 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005150 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005151 __ b(slow_path->GetEntryLabel());
5152 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005153}
5154
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005155ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5156 return codegen_->GetAssembler();
5157}
5158
5159void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005160 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005161 Location source = move->GetSource();
5162 Location destination = move->GetDestination();
5163
5164 if (source.IsRegister()) {
5165 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005166 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005167 } else if (destination.IsFpuRegister()) {
5168 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005169 } else {
5170 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005171 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005172 SP, destination.GetStackIndex());
5173 }
5174 } else if (source.IsStackSlot()) {
5175 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005176 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005177 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005178 } else if (destination.IsFpuRegister()) {
5179 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005180 } else {
5181 DCHECK(destination.IsStackSlot());
5182 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5183 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5184 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005185 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005186 if (destination.IsRegister()) {
5187 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5188 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005189 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005190 } else {
5191 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005192 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5193 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005194 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005195 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005196 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5197 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 } else if (destination.IsRegisterPair()) {
5199 DCHECK(ExpectedPairLayout(destination));
5200 __ LoadFromOffset(
5201 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5202 } else {
5203 DCHECK(destination.IsFpuRegisterPair()) << destination;
5204 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5205 SP,
5206 source.GetStackIndex());
5207 }
5208 } else if (source.IsRegisterPair()) {
5209 if (destination.IsRegisterPair()) {
5210 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5211 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005212 } else if (destination.IsFpuRegisterPair()) {
5213 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5214 source.AsRegisterPairLow<Register>(),
5215 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005216 } else {
5217 DCHECK(destination.IsDoubleStackSlot()) << destination;
5218 DCHECK(ExpectedPairLayout(source));
5219 __ StoreToOffset(
5220 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5221 }
5222 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005223 if (destination.IsRegisterPair()) {
5224 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5225 destination.AsRegisterPairHigh<Register>(),
5226 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5227 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005228 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5229 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5230 } else {
5231 DCHECK(destination.IsDoubleStackSlot()) << destination;
5232 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5233 SP,
5234 destination.GetStackIndex());
5235 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005236 } else {
5237 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005238 HConstant* constant = source.GetConstant();
5239 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5240 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005241 if (destination.IsRegister()) {
5242 __ LoadImmediate(destination.AsRegister<Register>(), value);
5243 } else {
5244 DCHECK(destination.IsStackSlot());
5245 __ LoadImmediate(IP, value);
5246 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5247 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005248 } else if (constant->IsLongConstant()) {
5249 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005250 if (destination.IsRegisterPair()) {
5251 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5252 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005253 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005254 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005255 __ LoadImmediate(IP, Low32Bits(value));
5256 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5257 __ LoadImmediate(IP, High32Bits(value));
5258 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5259 }
5260 } else if (constant->IsDoubleConstant()) {
5261 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005262 if (destination.IsFpuRegisterPair()) {
5263 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005264 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005265 DCHECK(destination.IsDoubleStackSlot()) << destination;
5266 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005267 __ LoadImmediate(IP, Low32Bits(int_value));
5268 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5269 __ LoadImmediate(IP, High32Bits(int_value));
5270 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5271 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005272 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005273 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005274 float value = constant->AsFloatConstant()->GetValue();
5275 if (destination.IsFpuRegister()) {
5276 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5277 } else {
5278 DCHECK(destination.IsStackSlot());
5279 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5280 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5281 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005282 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005283 }
5284}
5285
5286void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5287 __ Mov(IP, reg);
5288 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5289 __ StoreToOffset(kStoreWord, IP, SP, mem);
5290}
5291
5292void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5293 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5294 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5295 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5296 SP, mem1 + stack_offset);
5297 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5298 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5299 SP, mem2 + stack_offset);
5300 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5301}
5302
5303void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005304 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005305 Location source = move->GetSource();
5306 Location destination = move->GetDestination();
5307
5308 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005309 DCHECK_NE(source.AsRegister<Register>(), IP);
5310 DCHECK_NE(destination.AsRegister<Register>(), IP);
5311 __ Mov(IP, source.AsRegister<Register>());
5312 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5313 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005314 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005315 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005316 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005317 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005318 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5319 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005320 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005321 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005322 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005323 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005324 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005325 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005326 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005327 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005328 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5329 destination.AsRegisterPairHigh<Register>(),
5330 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005331 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005332 Register low_reg = source.IsRegisterPair()
5333 ? source.AsRegisterPairLow<Register>()
5334 : destination.AsRegisterPairLow<Register>();
5335 int mem = source.IsRegisterPair()
5336 ? destination.GetStackIndex()
5337 : source.GetStackIndex();
5338 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005339 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005340 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005341 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005342 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005343 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5344 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005345 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005346 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005347 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005348 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5349 DRegister reg = source.IsFpuRegisterPair()
5350 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5351 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5352 int mem = source.IsFpuRegisterPair()
5353 ? destination.GetStackIndex()
5354 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005355 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005356 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005357 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005358 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5359 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5360 : destination.AsFpuRegister<SRegister>();
5361 int mem = source.IsFpuRegister()
5362 ? destination.GetStackIndex()
5363 : source.GetStackIndex();
5364
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005365 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005366 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005367 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005368 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005369 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5370 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005371 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005372 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005373 }
5374}
5375
5376void ParallelMoveResolverARM::SpillScratch(int reg) {
5377 __ Push(static_cast<Register>(reg));
5378}
5379
5380void ParallelMoveResolverARM::RestoreScratch(int reg) {
5381 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005382}
5383
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005384HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5385 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005386 switch (desired_class_load_kind) {
5387 case HLoadClass::LoadKind::kReferrersClass:
5388 break;
5389 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5390 DCHECK(!GetCompilerOptions().GetCompilePic());
5391 break;
5392 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5393 DCHECK(GetCompilerOptions().GetCompilePic());
5394 break;
5395 case HLoadClass::LoadKind::kBootImageAddress:
5396 break;
5397 case HLoadClass::LoadKind::kDexCacheAddress:
5398 DCHECK(Runtime::Current()->UseJitCompilation());
5399 break;
5400 case HLoadClass::LoadKind::kDexCachePcRelative:
5401 DCHECK(!Runtime::Current()->UseJitCompilation());
5402 // We disable pc-relative load when there is an irreducible loop, as the optimization
5403 // is incompatible with it.
5404 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5405 // with irreducible loops.
5406 if (GetGraph()->HasIrreducibleLoops()) {
5407 return HLoadClass::LoadKind::kDexCacheViaMethod;
5408 }
5409 break;
5410 case HLoadClass::LoadKind::kDexCacheViaMethod:
5411 break;
5412 }
5413 return desired_class_load_kind;
5414}
5415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005417 if (cls->NeedsAccessCheck()) {
5418 InvokeRuntimeCallingConvention calling_convention;
5419 CodeGenerator::CreateLoadClassLocationSummary(
5420 cls,
5421 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5422 Location::RegisterLocation(R0),
5423 /* code_generator_supports_read_barrier */ true);
5424 return;
5425 }
5426
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005427 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5428 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005429 ? LocationSummary::kCallOnSlowPath
5430 : LocationSummary::kNoCall;
5431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005432 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005433 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005434 }
5435
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5437 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5438 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5439 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5440 locations->SetInAt(0, Location::RequiresRegister());
5441 }
5442 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005443}
5444
5445void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005446 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005447 if (cls->NeedsAccessCheck()) {
5448 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005449 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005450 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005451 return;
5452 }
5453
Roland Levillain3b359c72015-11-17 19:35:12 +00005454 Location out_loc = locations->Out();
5455 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005456
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005457 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005458 bool generate_null_check = false;
5459 switch (cls->GetLoadKind()) {
5460 case HLoadClass::LoadKind::kReferrersClass: {
5461 DCHECK(!cls->CanCallRuntime());
5462 DCHECK(!cls->MustGenerateClinitCheck());
5463 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5464 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005465 GenerateGcRootFieldLoad(cls,
5466 out_loc,
5467 current_method,
5468 ArtMethod::DeclaringClassOffset().Int32Value(),
5469 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005470 break;
5471 }
5472 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005473 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005474 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5475 cls->GetTypeIndex()));
5476 break;
5477 }
5478 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005479 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 CodeGeneratorARM::PcRelativePatchInfo* labels =
5481 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5482 __ BindTrackedLabel(&labels->movw_label);
5483 __ movw(out, /* placeholder */ 0u);
5484 __ BindTrackedLabel(&labels->movt_label);
5485 __ movt(out, /* placeholder */ 0u);
5486 __ BindTrackedLabel(&labels->add_pc_label);
5487 __ add(out, out, ShifterOperand(PC));
5488 break;
5489 }
5490 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005491 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 DCHECK_NE(cls->GetAddress(), 0u);
5493 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5494 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5495 break;
5496 }
5497 case HLoadClass::LoadKind::kDexCacheAddress: {
5498 DCHECK_NE(cls->GetAddress(), 0u);
5499 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5500 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5501 // a 128B range. To try and reduce the number of literals if we load multiple types,
5502 // simply split the dex cache address to a 128B aligned base loaded from a literal
5503 // and the remaining offset embedded in the load.
5504 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5505 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5506 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5507 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5508 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5509 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5510 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005511 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005512 generate_null_check = !cls->IsInDexCache();
5513 break;
5514 }
5515 case HLoadClass::LoadKind::kDexCachePcRelative: {
5516 Register base_reg = locations->InAt(0).AsRegister<Register>();
5517 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5518 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5519 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005520 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005521 generate_null_check = !cls->IsInDexCache();
5522 break;
5523 }
5524 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5525 // /* GcRoot<mirror::Class>[] */ out =
5526 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5527 Register current_method = locations->InAt(0).AsRegister<Register>();
5528 __ LoadFromOffset(kLoadWord,
5529 out,
5530 current_method,
5531 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5532 // /* GcRoot<mirror::Class> */ out = out[type_index]
5533 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005534 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005535 generate_null_check = !cls->IsInDexCache();
5536 }
5537 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005538
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5540 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005541 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005542 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5543 codegen_->AddSlowPath(slow_path);
5544 if (generate_null_check) {
5545 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5546 }
5547 if (cls->MustGenerateClinitCheck()) {
5548 GenerateClassInitializationCheck(slow_path, out);
5549 } else {
5550 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005551 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005552 }
5553}
5554
5555void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5556 LocationSummary* locations =
5557 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5558 locations->SetInAt(0, Location::RequiresRegister());
5559 if (check->HasUses()) {
5560 locations->SetOut(Location::SameAsFirstInput());
5561 }
5562}
5563
5564void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005565 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005566 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005567 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005568 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005569 GenerateClassInitializationCheck(slow_path,
5570 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005571}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005573void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005574 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005575 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5576 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5577 __ b(slow_path->GetEntryLabel(), LT);
5578 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5579 // properly. Therefore, we do a memory fence.
5580 __ dmb(ISH);
5581 __ Bind(slow_path->GetExitLabel());
5582}
5583
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005584HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5585 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005586 switch (desired_string_load_kind) {
5587 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5588 DCHECK(!GetCompilerOptions().GetCompilePic());
5589 break;
5590 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5591 DCHECK(GetCompilerOptions().GetCompilePic());
5592 break;
5593 case HLoadString::LoadKind::kBootImageAddress:
5594 break;
5595 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005596 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005597 break;
5598 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005599 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 // We disable pc-relative load when there is an irreducible loop, as the optimization
5601 // is incompatible with it.
5602 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5603 // with irreducible loops.
5604 if (GetGraph()->HasIrreducibleLoops()) {
5605 return HLoadString::LoadKind::kDexCacheViaMethod;
5606 }
5607 break;
5608 case HLoadString::LoadKind::kDexCacheViaMethod:
5609 break;
5610 }
5611 return desired_string_load_kind;
5612}
5613
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005614void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005615 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5616 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005617 : LocationSummary::kNoCall;
5618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005619
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005621 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5622 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005623 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005624 locations->SetOut(Location::RegisterLocation(R0));
5625 } else {
5626 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005628}
5629
5630void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005631 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005632 Location out_loc = locations->Out();
5633 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005634 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005635
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005636 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005638 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5639 load->GetStringIndex()));
5640 return; // No dex cache slow path.
5641 }
5642 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005643 CodeGeneratorARM::PcRelativePatchInfo* labels =
5644 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5645 __ BindTrackedLabel(&labels->movw_label);
5646 __ movw(out, /* placeholder */ 0u);
5647 __ BindTrackedLabel(&labels->movt_label);
5648 __ movt(out, /* placeholder */ 0u);
5649 __ BindTrackedLabel(&labels->add_pc_label);
5650 __ add(out, out, ShifterOperand(PC));
5651 return; // No dex cache slow path.
5652 }
5653 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005654 DCHECK_NE(load->GetAddress(), 0u);
5655 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5656 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5657 return; // No dex cache slow path.
5658 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005659 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005660 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005662
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005663 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5664 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5665 InvokeRuntimeCallingConvention calling_convention;
5666 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5667 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5668 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005669}
5670
David Brazdilcb1c0552015-08-04 16:22:25 +01005671static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005672 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005673}
5674
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005675void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5676 LocationSummary* locations =
5677 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5678 locations->SetOut(Location::RequiresRegister());
5679}
5680
5681void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005682 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005683 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5684}
5685
5686void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5687 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5688}
5689
5690void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005691 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005692 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005693}
5694
5695void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5696 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005697 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005698 InvokeRuntimeCallingConvention calling_convention;
5699 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5700}
5701
5702void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005703 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005704 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005705}
5706
Roland Levillainc9285912015-12-18 10:38:42 +00005707static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5708 return kEmitCompilerReadBarrier &&
5709 (kUseBakerReadBarrier ||
5710 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5711 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5712 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5713}
5714
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005715void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005717 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005718 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005719 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 case TypeCheckKind::kExactCheck:
5721 case TypeCheckKind::kAbstractClassCheck:
5722 case TypeCheckKind::kClassHierarchyCheck:
5723 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005724 call_kind =
5725 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005726 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 break;
5728 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005729 case TypeCheckKind::kUnresolvedCheck:
5730 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 call_kind = LocationSummary::kCallOnSlowPath;
5732 break;
5733 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005734
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005735 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005736 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005737 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005738 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005739 locations->SetInAt(0, Location::RequiresRegister());
5740 locations->SetInAt(1, Location::RequiresRegister());
5741 // The "out" register is used as a temporary, so it overlaps with the inputs.
5742 // Note that TypeCheckSlowPathARM uses this register too.
5743 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5744 // When read barriers are enabled, we need a temporary register for
5745 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005746 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005747 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005748 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005749}
5750
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005751void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005752 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005753 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005754 Location obj_loc = locations->InAt(0);
5755 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005756 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005757 Location out_loc = locations->Out();
5758 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005759 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005760 locations->GetTemp(0) :
5761 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005762 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005763 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5764 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5765 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005766 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005767 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005768
5769 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005770 // avoid null check if we know obj is not null.
5771 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005772 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005773 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005774
Roland Levillain3b359c72015-11-17 19:35:12 +00005775 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005776 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777
Roland Levillainc9285912015-12-18 10:38:42 +00005778 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 case TypeCheckKind::kExactCheck: {
5780 __ cmp(out, ShifterOperand(cls));
5781 // Classes must be equal for the instanceof to succeed.
5782 __ b(&zero, NE);
5783 __ LoadImmediate(out, 1);
5784 __ b(&done);
5785 break;
5786 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005787
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 case TypeCheckKind::kAbstractClassCheck: {
5789 // If the class is abstract, we eagerly fetch the super class of the
5790 // object to avoid doing a comparison we know will fail.
5791 Label loop;
5792 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005793 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005794 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 // If `out` is null, we use it for the result, and jump to `done`.
5796 __ CompareAndBranchIfZero(out, &done);
5797 __ cmp(out, ShifterOperand(cls));
5798 __ b(&loop, NE);
5799 __ LoadImmediate(out, 1);
5800 if (zero.IsLinked()) {
5801 __ b(&done);
5802 }
5803 break;
5804 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005805
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 case TypeCheckKind::kClassHierarchyCheck: {
5807 // Walk over the class hierarchy to find a match.
5808 Label loop, success;
5809 __ Bind(&loop);
5810 __ cmp(out, ShifterOperand(cls));
5811 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005812 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005813 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 __ CompareAndBranchIfNonZero(out, &loop);
5815 // If `out` is null, we use it for the result, and jump to `done`.
5816 __ b(&done);
5817 __ Bind(&success);
5818 __ LoadImmediate(out, 1);
5819 if (zero.IsLinked()) {
5820 __ b(&done);
5821 }
5822 break;
5823 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005824
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005826 // Do an exact check.
5827 Label exact_check;
5828 __ cmp(out, ShifterOperand(cls));
5829 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005830 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005832 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 // If `out` is null, we use it for the result, and jump to `done`.
5834 __ CompareAndBranchIfZero(out, &done);
5835 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5836 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5837 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005838 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839 __ LoadImmediate(out, 1);
5840 __ b(&done);
5841 break;
5842 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 case TypeCheckKind::kArrayCheck: {
5845 __ cmp(out, ShifterOperand(cls));
5846 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005847 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5848 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 codegen_->AddSlowPath(slow_path);
5850 __ b(slow_path->GetEntryLabel(), NE);
5851 __ LoadImmediate(out, 1);
5852 if (zero.IsLinked()) {
5853 __ b(&done);
5854 }
5855 break;
5856 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005857
Calin Juravle98893e12015-10-02 21:05:03 +01005858 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005859 case TypeCheckKind::kInterfaceCheck: {
5860 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005861 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005862 // cases.
5863 //
5864 // We cannot directly call the InstanceofNonTrivial runtime
5865 // entry point without resorting to a type checking slow path
5866 // here (i.e. by calling InvokeRuntime directly), as it would
5867 // require to assign fixed registers for the inputs of this
5868 // HInstanceOf instruction (following the runtime calling
5869 // convention), which might be cluttered by the potential first
5870 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005871 //
5872 // TODO: Introduce a new runtime entry point taking the object
5873 // to test (instead of its class) as argument, and let it deal
5874 // with the read barrier issues. This will let us refactor this
5875 // case of the `switch` code as it was previously (with a direct
5876 // call to the runtime not using a type checking slow path).
5877 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005878 DCHECK(locations->OnlyCallsOnSlowPath());
5879 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5880 /* is_fatal */ false);
5881 codegen_->AddSlowPath(slow_path);
5882 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 if (zero.IsLinked()) {
5884 __ b(&done);
5885 }
5886 break;
5887 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005888 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005889
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005891 __ Bind(&zero);
5892 __ LoadImmediate(out, 0);
5893 }
5894
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005895 if (done.IsLinked()) {
5896 __ Bind(&done);
5897 }
5898
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005899 if (slow_path != nullptr) {
5900 __ Bind(slow_path->GetExitLabel());
5901 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005902}
5903
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005904void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5906 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5907
Roland Levillain3b359c72015-11-17 19:35:12 +00005908 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5909 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 case TypeCheckKind::kExactCheck:
5911 case TypeCheckKind::kAbstractClassCheck:
5912 case TypeCheckKind::kClassHierarchyCheck:
5913 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005914 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5915 LocationSummary::kCallOnSlowPath :
5916 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 break;
5918 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005919 case TypeCheckKind::kUnresolvedCheck:
5920 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921 call_kind = LocationSummary::kCallOnSlowPath;
5922 break;
5923 }
5924
Roland Levillain3b359c72015-11-17 19:35:12 +00005925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5926 locations->SetInAt(0, Location::RequiresRegister());
5927 locations->SetInAt(1, Location::RequiresRegister());
5928 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5929 locations->AddTemp(Location::RequiresRegister());
5930 // When read barriers are enabled, we need an additional temporary
5931 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005932 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005935}
5936
5937void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005938 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005939 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005940 Location obj_loc = locations->InAt(0);
5941 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005942 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005943 Location temp_loc = locations->GetTemp(0);
5944 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005945 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005946 locations->GetTemp(1) :
5947 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5950 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5951 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005952
Roland Levillain3b359c72015-11-17 19:35:12 +00005953 bool is_type_check_slow_path_fatal =
5954 (type_check_kind == TypeCheckKind::kExactCheck ||
5955 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5956 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5957 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5958 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005959 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00005960 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5961 is_type_check_slow_path_fatal);
5962 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005963
5964 Label done;
5965 // Avoid null check if we know obj is not null.
5966 if (instruction->MustDoNullCheck()) {
5967 __ CompareAndBranchIfZero(obj, &done);
5968 }
5969
Roland Levillain3b359c72015-11-17 19:35:12 +00005970 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005971 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972
Roland Levillain3b359c72015-11-17 19:35:12 +00005973 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974 case TypeCheckKind::kExactCheck:
5975 case TypeCheckKind::kArrayCheck: {
5976 __ cmp(temp, ShifterOperand(cls));
5977 // Jump to slow path for throwing the exception or doing a
5978 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005979 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 break;
5981 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005982
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 case TypeCheckKind::kAbstractClassCheck: {
5984 // If the class is abstract, we eagerly fetch the super class of the
5985 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005986 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005988 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005989 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005990
5991 // If the class reference currently in `temp` is not null, jump
5992 // to the `compare_classes` label to compare it with the checked
5993 // class.
5994 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5995 // Otherwise, jump to the slow path to throw the exception.
5996 //
5997 // But before, move back the object's class into `temp` before
5998 // going into the slow path, as it has been overwritten in the
5999 // meantime.
6000 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006001 GenerateReferenceLoadTwoRegisters(
6002 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006003 __ b(type_check_slow_path->GetEntryLabel());
6004
6005 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 __ cmp(temp, ShifterOperand(cls));
6007 __ b(&loop, NE);
6008 break;
6009 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 case TypeCheckKind::kClassHierarchyCheck: {
6012 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006013 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 __ Bind(&loop);
6015 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006016 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006017
Roland Levillain3b359c72015-11-17 19:35:12 +00006018 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006019 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006020
6021 // If the class reference currently in `temp` is not null, jump
6022 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006024 // Otherwise, jump to the slow path to throw the exception.
6025 //
6026 // But before, move back the object's class into `temp` before
6027 // going into the slow path, as it has been overwritten in the
6028 // meantime.
6029 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006030 GenerateReferenceLoadTwoRegisters(
6031 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006032 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 break;
6034 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006037 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006038 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006039 __ cmp(temp, ShifterOperand(cls));
6040 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006041
6042 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006043 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006044 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006045
6046 // If the component type is not null (i.e. the object is indeed
6047 // an array), jump to label `check_non_primitive_component_type`
6048 // to further check that this component type is not a primitive
6049 // type.
6050 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6051 // Otherwise, jump to the slow path to throw the exception.
6052 //
6053 // But before, move back the object's class into `temp` before
6054 // going into the slow path, as it has been overwritten in the
6055 // meantime.
6056 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006057 GenerateReferenceLoadTwoRegisters(
6058 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006059 __ b(type_check_slow_path->GetEntryLabel());
6060
6061 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006063 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6064 __ CompareAndBranchIfZero(temp, &done);
6065 // Same comment as above regarding `temp` and the slow path.
6066 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006067 GenerateReferenceLoadTwoRegisters(
6068 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006069 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 break;
6071 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006072
Calin Juravle98893e12015-10-02 21:05:03 +01006073 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006075 // We always go into the type check slow path for the unresolved
6076 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006077 //
6078 // We cannot directly call the CheckCast runtime entry point
6079 // without resorting to a type checking slow path here (i.e. by
6080 // calling InvokeRuntime directly), as it would require to
6081 // assign fixed registers for the inputs of this HInstanceOf
6082 // instruction (following the runtime calling convention), which
6083 // might be cluttered by the potential first read barrier
6084 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006085 //
6086 // TODO: Introduce a new runtime entry point taking the object
6087 // to test (instead of its class) as argument, and let it deal
6088 // with the read barrier issues. This will let us refactor this
6089 // case of the `switch` code as it was previously (with a direct
6090 // call to the runtime not using a type checking slow path).
6091 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006092 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 break;
6094 }
6095 __ Bind(&done);
6096
Roland Levillain3b359c72015-11-17 19:35:12 +00006097 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006098}
6099
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006100void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6101 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006103 InvokeRuntimeCallingConvention calling_convention;
6104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6105}
6106
6107void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006108 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6109 instruction,
6110 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006111 if (instruction->IsEnter()) {
6112 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6113 } else {
6114 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6115 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006116}
6117
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006118void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6119void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6120void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006121
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006122void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123 LocationSummary* locations =
6124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6125 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6126 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006127 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006128 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006129 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006131}
6132
6133void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6134 HandleBitwiseOperation(instruction);
6135}
6136
6137void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6138 HandleBitwiseOperation(instruction);
6139}
6140
6141void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6142 HandleBitwiseOperation(instruction);
6143}
6144
Artem Serov7fc63502016-02-09 17:15:29 +00006145
6146void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6147 LocationSummary* locations =
6148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6149 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6150 || instruction->GetResultType() == Primitive::kPrimLong);
6151
6152 locations->SetInAt(0, Location::RequiresRegister());
6153 locations->SetInAt(1, Location::RequiresRegister());
6154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6155}
6156
6157void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6158 LocationSummary* locations = instruction->GetLocations();
6159 Location first = locations->InAt(0);
6160 Location second = locations->InAt(1);
6161 Location out = locations->Out();
6162
6163 if (instruction->GetResultType() == Primitive::kPrimInt) {
6164 Register first_reg = first.AsRegister<Register>();
6165 ShifterOperand second_reg(second.AsRegister<Register>());
6166 Register out_reg = out.AsRegister<Register>();
6167
6168 switch (instruction->GetOpKind()) {
6169 case HInstruction::kAnd:
6170 __ bic(out_reg, first_reg, second_reg);
6171 break;
6172 case HInstruction::kOr:
6173 __ orn(out_reg, first_reg, second_reg);
6174 break;
6175 // There is no EON on arm.
6176 case HInstruction::kXor:
6177 default:
6178 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6179 UNREACHABLE();
6180 }
6181 return;
6182
6183 } else {
6184 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6185 Register first_low = first.AsRegisterPairLow<Register>();
6186 Register first_high = first.AsRegisterPairHigh<Register>();
6187 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6188 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6189 Register out_low = out.AsRegisterPairLow<Register>();
6190 Register out_high = out.AsRegisterPairHigh<Register>();
6191
6192 switch (instruction->GetOpKind()) {
6193 case HInstruction::kAnd:
6194 __ bic(out_low, first_low, second_low);
6195 __ bic(out_high, first_high, second_high);
6196 break;
6197 case HInstruction::kOr:
6198 __ orn(out_low, first_low, second_low);
6199 __ orn(out_high, first_high, second_high);
6200 break;
6201 // There is no EON on arm.
6202 case HInstruction::kXor:
6203 default:
6204 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6205 UNREACHABLE();
6206 }
6207 }
6208}
6209
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006210void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6211 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6212 if (value == 0xffffffffu) {
6213 if (out != first) {
6214 __ mov(out, ShifterOperand(first));
6215 }
6216 return;
6217 }
6218 if (value == 0u) {
6219 __ mov(out, ShifterOperand(0));
6220 return;
6221 }
6222 ShifterOperand so;
6223 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6224 __ and_(out, first, so);
6225 } else {
6226 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6227 __ bic(out, first, ShifterOperand(~value));
6228 }
6229}
6230
6231void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6232 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6233 if (value == 0u) {
6234 if (out != first) {
6235 __ mov(out, ShifterOperand(first));
6236 }
6237 return;
6238 }
6239 if (value == 0xffffffffu) {
6240 __ mvn(out, ShifterOperand(0));
6241 return;
6242 }
6243 ShifterOperand so;
6244 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6245 __ orr(out, first, so);
6246 } else {
6247 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6248 __ orn(out, first, ShifterOperand(~value));
6249 }
6250}
6251
6252void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6253 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6254 if (value == 0u) {
6255 if (out != first) {
6256 __ mov(out, ShifterOperand(first));
6257 }
6258 return;
6259 }
6260 __ eor(out, first, ShifterOperand(value));
6261}
6262
Vladimir Marko59751a72016-08-05 14:37:27 +01006263void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6264 Location first,
6265 uint64_t value) {
6266 Register out_low = out.AsRegisterPairLow<Register>();
6267 Register out_high = out.AsRegisterPairHigh<Register>();
6268 Register first_low = first.AsRegisterPairLow<Register>();
6269 Register first_high = first.AsRegisterPairHigh<Register>();
6270 uint32_t value_low = Low32Bits(value);
6271 uint32_t value_high = High32Bits(value);
6272 if (value_low == 0u) {
6273 if (out_low != first_low) {
6274 __ mov(out_low, ShifterOperand(first_low));
6275 }
6276 __ AddConstant(out_high, first_high, value_high);
6277 return;
6278 }
6279 __ AddConstantSetFlags(out_low, first_low, value_low);
6280 ShifterOperand so;
6281 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6282 __ adc(out_high, first_high, so);
6283 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6284 __ sbc(out_high, first_high, so);
6285 } else {
6286 LOG(FATAL) << "Unexpected constant " << value_high;
6287 UNREACHABLE();
6288 }
6289}
6290
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006291void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6292 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006293 Location first = locations->InAt(0);
6294 Location second = locations->InAt(1);
6295 Location out = locations->Out();
6296
6297 if (second.IsConstant()) {
6298 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6299 uint32_t value_low = Low32Bits(value);
6300 if (instruction->GetResultType() == Primitive::kPrimInt) {
6301 Register first_reg = first.AsRegister<Register>();
6302 Register out_reg = out.AsRegister<Register>();
6303 if (instruction->IsAnd()) {
6304 GenerateAndConst(out_reg, first_reg, value_low);
6305 } else if (instruction->IsOr()) {
6306 GenerateOrrConst(out_reg, first_reg, value_low);
6307 } else {
6308 DCHECK(instruction->IsXor());
6309 GenerateEorConst(out_reg, first_reg, value_low);
6310 }
6311 } else {
6312 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6313 uint32_t value_high = High32Bits(value);
6314 Register first_low = first.AsRegisterPairLow<Register>();
6315 Register first_high = first.AsRegisterPairHigh<Register>();
6316 Register out_low = out.AsRegisterPairLow<Register>();
6317 Register out_high = out.AsRegisterPairHigh<Register>();
6318 if (instruction->IsAnd()) {
6319 GenerateAndConst(out_low, first_low, value_low);
6320 GenerateAndConst(out_high, first_high, value_high);
6321 } else if (instruction->IsOr()) {
6322 GenerateOrrConst(out_low, first_low, value_low);
6323 GenerateOrrConst(out_high, first_high, value_high);
6324 } else {
6325 DCHECK(instruction->IsXor());
6326 GenerateEorConst(out_low, first_low, value_low);
6327 GenerateEorConst(out_high, first_high, value_high);
6328 }
6329 }
6330 return;
6331 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006332
6333 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006334 Register first_reg = first.AsRegister<Register>();
6335 ShifterOperand second_reg(second.AsRegister<Register>());
6336 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006337 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006338 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006339 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006340 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006341 } else {
6342 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006343 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006344 }
6345 } else {
6346 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006347 Register first_low = first.AsRegisterPairLow<Register>();
6348 Register first_high = first.AsRegisterPairHigh<Register>();
6349 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6350 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6351 Register out_low = out.AsRegisterPairLow<Register>();
6352 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006353 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006354 __ and_(out_low, first_low, second_low);
6355 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006356 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006357 __ orr(out_low, first_low, second_low);
6358 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006359 } else {
6360 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006361 __ eor(out_low, first_low, second_low);
6362 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006363 }
6364 }
6365}
6366
Roland Levillainc9285912015-12-18 10:38:42 +00006367void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6368 Location out,
6369 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006370 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006371 Register out_reg = out.AsRegister<Register>();
6372 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006373 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006374 if (kUseBakerReadBarrier) {
6375 // Load with fast path based Baker's read barrier.
6376 // /* HeapReference<Object> */ out = *(out + offset)
6377 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006378 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006379 } else {
6380 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006381 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006382 // in the following move operation, as we will need it for the
6383 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006384 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006385 // /* HeapReference<Object> */ out = *(out + offset)
6386 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006387 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006388 }
6389 } else {
6390 // Plain load with no read barrier.
6391 // /* HeapReference<Object> */ out = *(out + offset)
6392 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6393 __ MaybeUnpoisonHeapReference(out_reg);
6394 }
6395}
6396
6397void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6398 Location out,
6399 Location obj,
6400 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006401 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006402 Register out_reg = out.AsRegister<Register>();
6403 Register obj_reg = obj.AsRegister<Register>();
6404 if (kEmitCompilerReadBarrier) {
6405 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006406 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006407 // Load with fast path based Baker's read barrier.
6408 // /* HeapReference<Object> */ out = *(obj + offset)
6409 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006410 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006411 } else {
6412 // Load with slow path based read barrier.
6413 // /* HeapReference<Object> */ out = *(obj + offset)
6414 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6415 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6416 }
6417 } else {
6418 // Plain load with no read barrier.
6419 // /* HeapReference<Object> */ out = *(obj + offset)
6420 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6421 __ MaybeUnpoisonHeapReference(out_reg);
6422 }
6423}
6424
6425void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6426 Location root,
6427 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006428 uint32_t offset,
6429 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006430 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006431 if (requires_read_barrier) {
6432 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006433 if (kUseBakerReadBarrier) {
6434 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6435 // Baker's read barrier are used:
6436 //
6437 // root = obj.field;
6438 // if (Thread::Current()->GetIsGcMarking()) {
6439 // root = ReadBarrier::Mark(root)
6440 // }
6441
6442 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6443 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6444 static_assert(
6445 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6446 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6447 "have different sizes.");
6448 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6449 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6450 "have different sizes.");
6451
Vladimir Marko953437b2016-08-24 08:30:46 +00006452 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006453 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006454 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006455 codegen_->AddSlowPath(slow_path);
6456
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006457 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006458 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006459 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006460 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6461 __ Bind(slow_path->GetExitLabel());
6462 } else {
6463 // GC root loaded through a slow path for read barriers other
6464 // than Baker's.
6465 // /* GcRoot<mirror::Object>* */ root = obj + offset
6466 __ AddConstant(root_reg, obj, offset);
6467 // /* mirror::Object* */ root = root->Read()
6468 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6469 }
6470 } else {
6471 // Plain GC root load with no read barrier.
6472 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6473 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6474 // Note that GC roots are not affected by heap poisoning, thus we
6475 // do not have to unpoison `root_reg` here.
6476 }
6477}
6478
6479void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6480 Location ref,
6481 Register obj,
6482 uint32_t offset,
6483 Location temp,
6484 bool needs_null_check) {
6485 DCHECK(kEmitCompilerReadBarrier);
6486 DCHECK(kUseBakerReadBarrier);
6487
6488 // /* HeapReference<Object> */ ref = *(obj + offset)
6489 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006490 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006491 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006492 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006493}
6494
6495void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6496 Location ref,
6497 Register obj,
6498 uint32_t data_offset,
6499 Location index,
6500 Location temp,
6501 bool needs_null_check) {
6502 DCHECK(kEmitCompilerReadBarrier);
6503 DCHECK(kUseBakerReadBarrier);
6504
Roland Levillainbfea3352016-06-23 13:48:47 +01006505 static_assert(
6506 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6507 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006508 // /* HeapReference<Object> */ ref =
6509 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006510 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006511 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006512 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006513}
6514
6515void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6516 Location ref,
6517 Register obj,
6518 uint32_t offset,
6519 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006520 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006521 Location temp,
6522 bool needs_null_check) {
6523 DCHECK(kEmitCompilerReadBarrier);
6524 DCHECK(kUseBakerReadBarrier);
6525
6526 // In slow path based read barriers, the read barrier call is
6527 // inserted after the original load. However, in fast path based
6528 // Baker's read barriers, we need to perform the load of
6529 // mirror::Object::monitor_ *before* the original reference load.
6530 // This load-load ordering is required by the read barrier.
6531 // The fast path/slow path (for Baker's algorithm) should look like:
6532 //
6533 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6534 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6535 // HeapReference<Object> ref = *src; // Original reference load.
6536 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6537 // if (is_gray) {
6538 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6539 // }
6540 //
6541 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006542 // slightly more complex as it performs additional checks that we do
6543 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006544
6545 Register ref_reg = ref.AsRegister<Register>();
6546 Register temp_reg = temp.AsRegister<Register>();
6547 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6548
6549 // /* int32_t */ monitor = obj->monitor_
6550 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6551 if (needs_null_check) {
6552 MaybeRecordImplicitNullCheck(instruction);
6553 }
6554 // /* LockWord */ lock_word = LockWord(monitor)
6555 static_assert(sizeof(LockWord) == sizeof(int32_t),
6556 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006557
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006558 // Introduce a dependency on the lock_word including the rb_state,
6559 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006560 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006561 // `obj` is unchanged by this operation, but its value now depends
6562 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006563 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006564
6565 // The actual reference load.
6566 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006567 // Load types involving an "index": ArrayGet and
6568 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6569 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006570 if (index.IsConstant()) {
6571 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006572 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006573 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6574 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006575 // Handle the special case of the
6576 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6577 // a register pair as index ("long offset"), of which only the low
6578 // part contains data.
6579 Register index_reg = index.IsRegisterPair()
6580 ? index.AsRegisterPairLow<Register>()
6581 : index.AsRegister<Register>();
6582 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006583 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6584 }
6585 } else {
6586 // /* HeapReference<Object> */ ref = *(obj + offset)
6587 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6588 }
6589
6590 // Object* ref = ref_addr->AsMirrorPtr()
6591 __ MaybeUnpoisonHeapReference(ref_reg);
6592
Vladimir Marko953437b2016-08-24 08:30:46 +00006593 // Slow path marking the object `ref` when it is gray.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006594 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006595 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006596 AddSlowPath(slow_path);
6597
6598 // if (rb_state == ReadBarrier::gray_ptr_)
6599 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006600 // Given the numeric representation, it's enough to check the low bit of the
6601 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6602 // which can be a 16-bit instruction unlike the TST immediate.
6603 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6604 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6605 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6606 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6607 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006608 __ Bind(slow_path->GetExitLabel());
6609}
6610
6611void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6612 Location out,
6613 Location ref,
6614 Location obj,
6615 uint32_t offset,
6616 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006617 DCHECK(kEmitCompilerReadBarrier);
6618
Roland Levillainc9285912015-12-18 10:38:42 +00006619 // Insert a slow path based read barrier *after* the reference load.
6620 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006621 // If heap poisoning is enabled, the unpoisoning of the loaded
6622 // reference will be carried out by the runtime within the slow
6623 // path.
6624 //
6625 // Note that `ref` currently does not get unpoisoned (when heap
6626 // poisoning is enabled), which is alright as the `ref` argument is
6627 // not used by the artReadBarrierSlow entry point.
6628 //
6629 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006630 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006631 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6632 AddSlowPath(slow_path);
6633
Roland Levillain3b359c72015-11-17 19:35:12 +00006634 __ b(slow_path->GetEntryLabel());
6635 __ Bind(slow_path->GetExitLabel());
6636}
6637
Roland Levillainc9285912015-12-18 10:38:42 +00006638void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6639 Location out,
6640 Location ref,
6641 Location obj,
6642 uint32_t offset,
6643 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006644 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006645 // Baker's read barriers shall be handled by the fast path
6646 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6647 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006648 // If heap poisoning is enabled, unpoisoning will be taken care of
6649 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006650 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006651 } else if (kPoisonHeapReferences) {
6652 __ UnpoisonHeapReference(out.AsRegister<Register>());
6653 }
6654}
6655
Roland Levillainc9285912015-12-18 10:38:42 +00006656void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6657 Location out,
6658 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006659 DCHECK(kEmitCompilerReadBarrier);
6660
Roland Levillainc9285912015-12-18 10:38:42 +00006661 // Insert a slow path based read barrier *after* the GC root load.
6662 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006663 // Note that GC roots are not affected by heap poisoning, so we do
6664 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006665 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006666 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6667 AddSlowPath(slow_path);
6668
Roland Levillain3b359c72015-11-17 19:35:12 +00006669 __ b(slow_path->GetEntryLabel());
6670 __ Bind(slow_path->GetExitLabel());
6671}
6672
Vladimir Markodc151b22015-10-15 18:02:30 +01006673HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6674 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6675 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006676 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6677 // We disable pc-relative load when there is an irreducible loop, as the optimization
6678 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006679 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6680 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006681 if (GetGraph()->HasIrreducibleLoops() &&
6682 (dispatch_info.method_load_kind ==
6683 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6684 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6685 }
6686
6687 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006688 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6689 if (&outer_dex_file != target_method.dex_file) {
6690 // Calls across dex files are more likely to exceed the available BL range,
6691 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6692 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6693 (desired_dispatch_info.method_load_kind ==
6694 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6695 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6696 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6697 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006698 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006699 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006700 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006701 0u
6702 };
6703 }
6704 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006705 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006706}
6707
Vladimir Markob4536b72015-11-24 13:45:23 +00006708Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6709 Register temp) {
6710 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6711 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6712 if (!invoke->GetLocations()->Intrinsified()) {
6713 return location.AsRegister<Register>();
6714 }
6715 // For intrinsics we allow any location, so it may be on the stack.
6716 if (!location.IsRegister()) {
6717 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6718 return temp;
6719 }
6720 // For register locations, check if the register was saved. If so, get it from the stack.
6721 // Note: There is a chance that the register was saved but not overwritten, so we could
6722 // save one load. However, since this is just an intrinsic slow path we prefer this
6723 // simple and more robust approach rather that trying to determine if that's the case.
6724 SlowPathCode* slow_path = GetCurrentSlowPath();
6725 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6726 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6727 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6728 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6729 return temp;
6730 }
6731 return location.AsRegister<Register>();
6732}
6733
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006734void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006735 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006736 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006737 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6738 // LR = code address from literal pool with link-time patch.
6739 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006740 break;
6741 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6742 // LR = invoke->GetDirectCodePtr();
6743 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006744 break;
6745 default:
6746 break;
6747 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006748
Vladimir Marko58155012015-08-19 12:49:41 +00006749 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6750 switch (invoke->GetMethodLoadKind()) {
6751 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6752 // temp = thread->string_init_entrypoint
6753 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6754 break;
6755 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006756 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006757 break;
6758 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6759 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6760 break;
6761 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6762 __ LoadLiteral(temp.AsRegister<Register>(),
6763 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6764 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006765 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6766 HArmDexCacheArraysBase* base =
6767 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6768 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6769 temp.AsRegister<Register>());
6770 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6771 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6772 break;
6773 }
Vladimir Marko58155012015-08-19 12:49:41 +00006774 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006775 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006776 Register method_reg;
6777 Register reg = temp.AsRegister<Register>();
6778 if (current_method.IsRegister()) {
6779 method_reg = current_method.AsRegister<Register>();
6780 } else {
6781 DCHECK(invoke->GetLocations()->Intrinsified());
6782 DCHECK(!current_method.IsValid());
6783 method_reg = reg;
6784 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6785 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006786 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6787 __ LoadFromOffset(kLoadWord,
6788 reg,
6789 method_reg,
6790 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006791 // temp = temp[index_in_cache];
6792 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6793 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006794 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6795 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006796 }
Vladimir Marko58155012015-08-19 12:49:41 +00006797 }
6798
6799 switch (invoke->GetCodePtrLocation()) {
6800 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6801 __ bl(GetFrameEntryLabel());
6802 break;
6803 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006804 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006805 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006806 // Arbitrarily branch to the BL itself, override at link time.
6807 __ bl(&relative_call_patches_.back().label);
6808 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006809 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6810 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6811 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006812 // LR()
6813 __ blx(LR);
6814 break;
6815 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6816 // LR = callee_method->entry_point_from_quick_compiled_code_
6817 __ LoadFromOffset(
6818 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006819 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006820 // LR()
6821 __ blx(LR);
6822 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006823 }
6824
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006825 DCHECK(!IsLeafMethod());
6826}
6827
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006828void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6829 Register temp = temp_location.AsRegister<Register>();
6830 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6831 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006832
6833 // Use the calling convention instead of the location of the receiver, as
6834 // intrinsics may have put the receiver in a different register. In the intrinsics
6835 // slow path, the arguments have been moved to the right place, so here we are
6836 // guaranteed that the receiver is the first register of the calling convention.
6837 InvokeDexCallingConvention calling_convention;
6838 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006839 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006840 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006841 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006842 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006843 // Instead of simply (possibly) unpoisoning `temp` here, we should
6844 // emit a read barrier for the previous class reference load.
6845 // However this is not required in practice, as this is an
6846 // intermediate/temporary reference and because the current
6847 // concurrent copying collector keeps the from-space memory
6848 // intact/accessible until the end of the marking phase (the
6849 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006850 __ MaybeUnpoisonHeapReference(temp);
6851 // temp = temp->GetMethodAt(method_offset);
6852 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006853 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006854 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6855 // LR = temp->GetEntryPoint();
6856 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6857 // LR();
6858 __ blx(LR);
6859}
6860
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006861CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6862 const DexFile& dex_file, uint32_t string_index) {
6863 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6864}
6865
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006866CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6867 const DexFile& dex_file, uint32_t type_index) {
6868 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6869}
6870
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006871CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6872 const DexFile& dex_file, uint32_t element_offset) {
6873 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6874}
6875
6876CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6877 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6878 patches->emplace_back(dex_file, offset_or_index);
6879 return &patches->back();
6880}
6881
6882Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6883 uint32_t string_index) {
6884 return boot_image_string_patches_.GetOrCreate(
6885 StringReference(&dex_file, string_index),
6886 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6887}
6888
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006889Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6890 uint32_t type_index) {
6891 return boot_image_type_patches_.GetOrCreate(
6892 TypeReference(&dex_file, type_index),
6893 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6894}
6895
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006896Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6897 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6898 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6899 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6900}
6901
6902Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6903 return DeduplicateUint32Literal(address, &uint32_literals_);
6904}
6905
Vladimir Marko58155012015-08-19 12:49:41 +00006906void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6907 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006908 size_t size =
6909 method_patches_.size() +
6910 call_patches_.size() +
6911 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006912 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6913 boot_image_string_patches_.size() +
6914 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006915 boot_image_type_patches_.size() +
6916 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006917 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006918 linker_patches->reserve(size);
6919 for (const auto& entry : method_patches_) {
6920 const MethodReference& target_method = entry.first;
6921 Literal* literal = entry.second;
6922 DCHECK(literal->GetLabel()->IsBound());
6923 uint32_t literal_offset = literal->GetLabel()->Position();
6924 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6925 target_method.dex_file,
6926 target_method.dex_method_index));
6927 }
6928 for (const auto& entry : call_patches_) {
6929 const MethodReference& target_method = entry.first;
6930 Literal* literal = entry.second;
6931 DCHECK(literal->GetLabel()->IsBound());
6932 uint32_t literal_offset = literal->GetLabel()->Position();
6933 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6934 target_method.dex_file,
6935 target_method.dex_method_index));
6936 }
6937 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6938 uint32_t literal_offset = info.label.Position();
6939 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6940 info.target_method.dex_file,
6941 info.target_method.dex_method_index));
6942 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006943 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6944 const DexFile& dex_file = info.target_dex_file;
6945 size_t base_element_offset = info.offset_or_index;
6946 DCHECK(info.add_pc_label.IsBound());
6947 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006948 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006949 DCHECK(info.movw_label.IsBound());
6950 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006951 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6952 &dex_file,
6953 add_pc_offset,
6954 base_element_offset));
6955 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006956 DCHECK(info.movt_label.IsBound());
6957 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006958 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6959 &dex_file,
6960 add_pc_offset,
6961 base_element_offset));
6962 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006963 for (const auto& entry : boot_image_string_patches_) {
6964 const StringReference& target_string = entry.first;
6965 Literal* literal = entry.second;
6966 DCHECK(literal->GetLabel()->IsBound());
6967 uint32_t literal_offset = literal->GetLabel()->Position();
6968 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6969 target_string.dex_file,
6970 target_string.string_index));
6971 }
6972 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6973 const DexFile& dex_file = info.target_dex_file;
6974 uint32_t string_index = info.offset_or_index;
6975 DCHECK(info.add_pc_label.IsBound());
6976 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6977 // Add MOVW patch.
6978 DCHECK(info.movw_label.IsBound());
6979 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6980 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6981 &dex_file,
6982 add_pc_offset,
6983 string_index));
6984 // Add MOVT patch.
6985 DCHECK(info.movt_label.IsBound());
6986 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6987 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6988 &dex_file,
6989 add_pc_offset,
6990 string_index));
6991 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006992 for (const auto& entry : boot_image_type_patches_) {
6993 const TypeReference& target_type = entry.first;
6994 Literal* literal = entry.second;
6995 DCHECK(literal->GetLabel()->IsBound());
6996 uint32_t literal_offset = literal->GetLabel()->Position();
6997 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6998 target_type.dex_file,
6999 target_type.type_index));
7000 }
7001 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
7002 const DexFile& dex_file = info.target_dex_file;
7003 uint32_t type_index = info.offset_or_index;
7004 DCHECK(info.add_pc_label.IsBound());
7005 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7006 // Add MOVW patch.
7007 DCHECK(info.movw_label.IsBound());
7008 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7009 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
7010 &dex_file,
7011 add_pc_offset,
7012 type_index));
7013 // Add MOVT patch.
7014 DCHECK(info.movt_label.IsBound());
7015 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7016 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
7017 &dex_file,
7018 add_pc_offset,
7019 type_index));
7020 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007021 for (const auto& entry : boot_image_address_patches_) {
7022 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7023 Literal* literal = entry.second;
7024 DCHECK(literal->GetLabel()->IsBound());
7025 uint32_t literal_offset = literal->GetLabel()->Position();
7026 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7027 }
7028}
7029
7030Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7031 return map->GetOrCreate(
7032 value,
7033 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007034}
7035
7036Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7037 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007038 return map->GetOrCreate(
7039 target_method,
7040 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007041}
7042
7043Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7044 return DeduplicateMethodLiteral(target_method, &method_patches_);
7045}
7046
7047Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7048 return DeduplicateMethodLiteral(target_method, &call_patches_);
7049}
7050
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007051void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7052 LocationSummary* locations =
7053 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7054 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7055 Location::RequiresRegister());
7056 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7057 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7059}
7060
7061void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7062 LocationSummary* locations = instr->GetLocations();
7063 Register res = locations->Out().AsRegister<Register>();
7064 Register accumulator =
7065 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7066 Register mul_left =
7067 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7068 Register mul_right =
7069 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7070
7071 if (instr->GetOpKind() == HInstruction::kAdd) {
7072 __ mla(res, mul_left, mul_right, accumulator);
7073 } else {
7074 __ mls(res, mul_left, mul_right, accumulator);
7075 }
7076}
7077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007078void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007079 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007080 LOG(FATAL) << "Unreachable";
7081}
7082
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007083void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007084 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007085 LOG(FATAL) << "Unreachable";
7086}
7087
Mark Mendellfe57faa2015-09-18 09:26:15 -04007088// Simple implementation of packed switch - generate cascaded compare/jumps.
7089void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7090 LocationSummary* locations =
7091 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7092 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007093 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007094 codegen_->GetAssembler()->IsThumb()) {
7095 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7096 if (switch_instr->GetStartValue() != 0) {
7097 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7098 }
7099 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007100}
7101
7102void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7103 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007104 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007105 LocationSummary* locations = switch_instr->GetLocations();
7106 Register value_reg = locations->InAt(0).AsRegister<Register>();
7107 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7108
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007109 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007110 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007111 Register temp_reg = IP;
7112 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7113 // the immediate, because IP is used as the destination register. For the other
7114 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7115 // and they can be encoded in the instruction without making use of IP register.
7116 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7117
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007118 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007119 // Jump to successors[0] if value == lower_bound.
7120 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7121 int32_t last_index = 0;
7122 for (; num_entries - last_index > 2; last_index += 2) {
7123 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7124 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7125 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7126 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7127 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7128 }
7129 if (num_entries - last_index == 2) {
7130 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007131 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007132 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007133 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007134
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007135 // And the default for any other value.
7136 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7137 __ b(codegen_->GetLabelOf(default_block));
7138 }
7139 } else {
7140 // Create a table lookup.
7141 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7142
7143 // Materialize a pointer to the switch table
7144 std::vector<Label*> labels(num_entries);
7145 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7146 for (uint32_t i = 0; i < num_entries; i++) {
7147 labels[i] = codegen_->GetLabelOf(successors[i]);
7148 }
7149 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7150
7151 // Remove the bias.
7152 Register key_reg;
7153 if (lower_bound != 0) {
7154 key_reg = locations->GetTemp(1).AsRegister<Register>();
7155 __ AddConstant(key_reg, value_reg, -lower_bound);
7156 } else {
7157 key_reg = value_reg;
7158 }
7159
7160 // Check whether the value is in the table, jump to default block if not.
7161 __ CmpConstant(key_reg, num_entries - 1);
7162 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7163
7164 // Load the displacement from the table.
7165 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7166
7167 // Dispatch is a direct add to the PC (for Thumb2).
7168 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007169 }
7170}
7171
Vladimir Markob4536b72015-11-24 13:45:23 +00007172void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7173 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7174 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007175}
7176
7177void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7178 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007179 CodeGeneratorARM::PcRelativePatchInfo* labels =
7180 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007181 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007182 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007183 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007184 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007185 __ BindTrackedLabel(&labels->add_pc_label);
7186 __ add(base_reg, base_reg, ShifterOperand(PC));
7187}
7188
Andreas Gampe85b62f22015-09-09 13:15:38 -07007189void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7190 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007191 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007192 return;
7193 }
7194
7195 DCHECK_NE(type, Primitive::kPrimVoid);
7196
7197 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7198 if (return_loc.Equals(trg)) {
7199 return;
7200 }
7201
7202 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7203 // with the last branch.
7204 if (type == Primitive::kPrimLong) {
7205 HParallelMove parallel_move(GetGraph()->GetArena());
7206 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7207 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7208 GetMoveResolver()->EmitNativeCode(&parallel_move);
7209 } else if (type == Primitive::kPrimDouble) {
7210 HParallelMove parallel_move(GetGraph()->GetArena());
7211 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7212 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7213 GetMoveResolver()->EmitNativeCode(&parallel_move);
7214 } else {
7215 // Let the parallel move resolver take care of all of this.
7216 HParallelMove parallel_move(GetGraph()->GetArena());
7217 parallel_move.AddMove(return_loc, trg, type, nullptr);
7218 GetMoveResolver()->EmitNativeCode(&parallel_move);
7219 }
7220}
7221
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007222void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7223 LocationSummary* locations =
7224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7225 locations->SetInAt(0, Location::RequiresRegister());
7226 locations->SetOut(Location::RequiresRegister());
7227}
7228
7229void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7230 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007231 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007232 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007233 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007234 __ LoadFromOffset(kLoadWord,
7235 locations->Out().AsRegister<Register>(),
7236 locations->InAt(0).AsRegister<Register>(),
7237 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007238 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007239 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007240 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007241 __ LoadFromOffset(kLoadWord,
7242 locations->Out().AsRegister<Register>(),
7243 locations->InAt(0).AsRegister<Register>(),
7244 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7245 __ LoadFromOffset(kLoadWord,
7246 locations->Out().AsRegister<Register>(),
7247 locations->Out().AsRegister<Register>(),
7248 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007249 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007250}
7251
Roland Levillain4d027112015-07-01 15:41:14 +01007252#undef __
7253#undef QUICK_ENTRY_POINT
7254
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007255} // namespace arm
7256} // namespace art